CountVectorizer提取功能



我有以下数据框架

+------------------------------------------------+
|filtered                                        |
+------------------------------------------------+
|[human, interface, computer]                    |
|[survey, user, computer, system, response, time]|
|[eps, user, interface, system]                  |
|[system, human, system, eps]                    |
|[user, response, time]                          |
|[trees]                                         |
|[graph, trees]                                  |
|[graph, minors, trees]                          |
|[graph, minors, survey]                         |
+------------------------------------------------+

在上面的列上运行CountVectorizer后,我将获得以下输出

+------------------------------------------------+-------------------
--------------------------+
|filtered                                        |features                                     |
+------------------------------------------------+---------------------------------------------+
|[human, interface, computer]                    |(12,[4,7,9],[1.0,1.0,1.0])                   |
|[survey, user, computer, system, response, time]|(12,[0,2,6,7,8,11],[1.0,1.0,1.0,1.0,1.0,1.0])|
|[eps, user, interface, system]                  |(12,[0,2,4,10],[1.0,1.0,1.0,1.0])            |
|[system, human, system, eps]                    |(12,[0,9,10],[2.0,1.0,1.0])                  |
|[user, response, time]                          |(12,[2,8,11],[1.0,1.0,1.0])                  |
|[trees]                                         |(12,[1],[1.0])                               |
|[graph, trees]                                  |(12,[1,3],[1.0,1.0])                         |
|[graph, minors, trees]                          |(12,[1,3,5],[1.0,1.0,1.0])                   |
|[graph, minors, survey]                         |(12,[3,5,6],[1.0,1.0,1.0])                   |
+------------------------------------------------+---------------------------------------------+

现在我想在功能列上运行地图功能,然后将其转换为类似的东西

+------------------------------------------------+--------------------------------------------------------+
|features                                        |transformed                                             |
+------------------------------------------------+--------------------------------------------------------+
|(12,[4,7,9],[1.0,1.0,1.0])                      |["1 4 1", "1 7 1", "1 9 1"]                             |
|(12,[0,2,6,7,8,11],[1.0,1.0,1.0,1.0,1.0,1.0])   |["2 0 1", "2 2 1", "2 6 1", "2 7 1", "2 8 1", "2 11 1"] |
|(12,[0,2,4,10],[1.0,1.0,1.0,1.0])               |["3 0 1", "3 2 1", "3 4 1", "3 10 1"]                   |
[TRUNCATED]

转换功能的方式是从功能中获取中间数组,然后从中创建子阵列。例如,在features列的第1行和Col 1中,我们有

(12,[4,7,9],[1.0,1.0,1.0])

现在以[4,7,9]为中间数组,并将其FREQ与第三列(即[1.0,1.0,1.0]预启动" 1")进行比较,以获取以下输出:

["1 4 1", "1 7 1", "1 9 1"]

通常看起来像这样:

["RowNumber MiddleFeatEl CorrespondingFreq", ....]

我无法从CountVectorizer通过应用地图功能生成的功能列分别提取中间 last freq list list P>

所以以下是地图代码:

def corpus_create(feats):
    return feats[1] # Here i want to get [4,7,9] instead of 1 single feat score.
corpus_udf = udf(lambda feats: corpus_create(feats), StringType())
df3 = df.withColumn("corpus", corpus_udf("features"))

行数本质上是毫无意义的,但是如果您不介意:

def f(x):
    row, i = x
    jvs = (
        # SparseVector
        zip(row.features.indices, row.features.values) if hasattr(row.features, "indices")
        # DenseVector
        else enumerate(row.features.toArray()))
    s = ["{} {} {}".format(i, j, v) 
        for j, v in jvs if v]
    return row + (s, )

df.rdd.zipWithIndex().map(f).toDF(df.columns + ["transformed"])

相关内容

  • 没有找到相关文章

最新更新