如何在Apache Spark中执行LabelEncoding或分类值

Abh*_*ary 4 scikit-learn apache-spark

我有数据集包含字符串列.如何编码基于字符串的列,就像我们在scikit-learn LabelEncoder中所做的那样

小智 7

您需要使用StringIndexer https://spark.apache.org/docs/1.5.1/ml-features.html#stringindexer

from pyspark.ml.feature import StringIndexer

df = sqlContext.createDataFrame(
            [(0, "a"), (1, "b"), (2, "c"), (3, "a"), (4, "a"), (5, "c")],
            ["id", "category"]) 
indexer = StringIndexer(inputCol="category", outputCol="categoryIndex") 
indexed = indexer.fit(df).transform(df) 
indexed.show()
Run Code Online (Sandbox Code Playgroud)


小智 5

我们正在开发sparkit-learn,旨在为PySpark提供scikit-learn功能和API.您可以通过以下方式使用SparkLabelEncoder:

$ pip install sparkit-learn
Run Code Online (Sandbox Code Playgroud)
>>> from splearn.preprocessing import SparkLabelEncoder
>>> from splearn import BlockRDD
>>>
>>> data = ["paris", "paris", "tokyo", "amsterdam"]
>>> y = BlockRDD(sc.parallelize(data))
>>>
>>> le = SparkLabelEncoder()
>>> le.fit(y)
>>> le.classes_
array(['amsterdam', 'paris', 'tokyo'],
      dtype='|S9')
>>>
>>> test = ["tokyo", "tokyo", "paris"]
>>> y_test = BlockRDD(sc.parallelize(test))
>>>
>>> le.transform(y_test).toarray()
array([2, 2, 1])
>>>
>>> test = [2, 2, 1]
>>> y_test = BlockRDD(sc.parallelize(test))
>>>
>>> le.inverse_transform(y_test).toarray()
array(['tokyo', 'tokyo', 'paris'],
      dtype='|S9')
Run Code Online (Sandbox Code Playgroud)