【问题标题】:How to decompose feature vector in Java?如何在Java中分解特征向量?
【发布时间】:2020-01-20 14:33:51
【问题描述】:

我有一个如下的数据框:

+---------------+--------------------+
|IndexedArtistID|     recommendations|
+---------------+--------------------+
|           1580|[[919, 0.00249262...|
|           4900|[[41749, 7.143963...|
|           5300|[[0, 2.0147272E-4...|
|           6620|[[208780, 9.81092...|
+---------------+--------------------+

我想拆分推荐列,以便有如下数据框:

+---------------+--------------------+
|IndexedArtistID|     recommendations|
+---------------+--------------------+
|           1580|919                 |
|           1580|0.00249262          |
|           4900|41749               |
|           4900|7.143963            |
|           5300|0                   |
|           5300|2.0147272E-4        |
|           6620|208780              |
|           6620|9.81092             |
+---------------+--------------------+

所以基本上,我想将特征向量拆分为列,然后将这些列合并为一列。合并部分在:How to split single row into multiple rows in Spark DataFrame using Java 中描述。 现在,如何使用java进行拆分部分? 对于 scala,在此处进行了解释:Spark Scala: How to convert Dataframe[vector] to DataFrame[f1:Double, ..., fn: Double)],但我无法找到在 java 中按照链接中给出的相同方式进行操作的方法。

数据框的架构如下,IndexedUserID 的值将被纳入新创建的推荐列:

root
 |-- IndexedArtistID: integer (nullable = false)
 |-- recommendations: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- IndexedUserID: integer (nullable = true)
 |    |    |-- rating: float (nullable = true)

【问题讨论】:

  • 我提供了scala代码,没有使用explode函数。您可以用 Java 编写与我的示例类似的拆分部分。我没有太多使用 Java,所以认为这会有所帮助。请看看它是否适合你。

标签: java apache-spark apache-spark-sql


【解决方案1】:

我试图找到问题的解决方案,我必须说有很多内容可用于解决人们在 python 和 scala for spark 中面临的问题,但在 java 中可用的内容很少。 因此,解决方案如下:

List<ElementStruct> structElements = dataFrameWithFeatures.javaRDD().map(row -> {
        int artistId = row.getInt(0);
        List<Object> recommendations = row.getList(1);
        return new ElementStruct(artistId, recommendations);
    }).collect();

    List<Recommendation> recommendations = new ArrayList<>();
    for (ElementStruct element : structElements) {
        List<Object> features = element.getFeatures();
        int artistId = element.getArtistId();
        for (int i = 0; i < features.size(); i++) {
            Object o = ((GenericRowWithSchema) features.get(i)).get(0);
            recommendations.add(new Recommendation(artistId, (int) o));
        }
    }
    SparkSession sparkSession = SessionCreator.getOrCreateSparkSession();
    Dataset<Row> decomposedDataframe = sparkSession.createDataFrame(recommendations, Recommendation.class);

ElementStruct 类

import java.io.Serializable;
import java.util.List;

public class ElementStruct implements Serializable {
    private int artistId;
    private List<Object> features;

    public ElementStruct(int artistId, List<Object> features) {
        this.artistId = artistId;
        this.features = features;
    }

    public int getArtistId() {
        return artistId;
    }

    public void setArtistId(int artistId) {
        this.artistId = artistId;
    }

    public List<Object> getFeatures() {
        return features;
    }

    public void setFeatures(List<Object> features) {
        this.features = features;
    }
}

推荐等级

import java.io.Serializable;

public class Recommendation implements Serializable {
    private int artistId;
    private int userId;

    public Recommendation(int artistId, int userId){
        this.artistId = artistId;
        this.userId = userId;
    }

    public int getArtistId() {
        return artistId;
    }

    public void setArtistId(int artistId) {
        this.artistId = artistId;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }
}

说明: 1.对于dataframe中的每一行,将艺术家和特征作为一个列表,以便于进一步处理。将这些艺术家和特征列表存储为 java 对象(在本例中为 Element 结构)。

  1. 为特征列表中的每个艺术家和元素,创建一个新的对象列表(在本例中为推荐)并将每个对象存储在该列表中。

  2. 最后,从第二步获得的对象列表中创建一个数据框。

结果:

root
 |-- artistId: integer (nullable = false)
 |-- userId: integer (nullable = false)

+---------------+----------------+
|       artistId|          userId|
+---------------+----------------+
|           1580|919             |
|           1580|0.00249262      |
|           4900|41749           |
|           4900|7.143963        |
|           5300|0               |
|           5300|2.0147272E-4    |
|           6620|208780          |
|           6620|9.81092         |
+---------------+----------------+

【讨论】:

    猜你喜欢
    • 2011-03-18
    • 1970-01-01
    • 2015-03-20
    • 2015-06-30
    • 2015-12-08
    • 1970-01-01
    • 1970-01-01
    • 2018-01-15
    相关资源
    最近更新 更多