Pyspark 多标签文本分类

Pyspark multilabel text classification

我正在尝试预测未知文本的标签。我的数据如下所示:

+-----------------+-----------+
|      label      |   text    |
+-----------------+-----------+
| [0, 1, 0, 1, 0] | blah blah |
| [1, 1, 0, 0, 0] | foo bar   |
+-----------------+-----------+

第一列使用多标签二值化方法编码。 我的管道:

tokenizer = Tokenizer(inputCol="text", outputCol="words")
hashingTF = HashingTF(inputCol=tokenizer.getOutputCol(), outputCol="features")
lsvc = LinearSVC(maxIter=10, regParam=0.1)
ovr = OneVsRest(classifier=lsvc)

pipeline = Pipeline(stages=[tokenizer, hashingTF, ovr])

model = pipeline.fit(result)

当我 运行 此代码时,出现此错误:

ValueError: invalid literal for int() with base 10: '[1, 0, 1, 0, 1, 1, 1, 0, 0]'

有什么问题吗?

查看错误

invalid literal for int()

我们看到问题是标签的预期类型不是数组,而是对应于样本class的单个值。换句话说,你需要将标签从多标签二值化编码转换为单个数字。

一种方法是先将数组转换为字符串,然后使用 StringIndexer:

to_string_udf = udf(lambda x: ''.join(str(e) for e in x), StringType())
df = df.withColumn("labelstring", to_string_udf(df.label))

indexer = StringIndexer(inputCol="labelstring", outputCol="label")
indexed = indexer.fit(df).transform(df)

这将为每个独特的数组创建一个单独的类别(class 标签)。