我有數據集包含字符串列。我如何編碼基於字符串的列,如我們在scikit-learn中所做的那樣LabelEncoder如何在Apache Spark中執行LabelEncoding或分類值
4
A
回答
4
我們正在開發sparkit-learn,它旨在提供PySpark上的scikit-learn功能和API。您可以使用SparkLabelEncoder方式如下:
$ pip install sparkit-learn
>>> 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')
1
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()
相關問題
- 1. 如何在apache spark中執行詞幹?
- 2. Apache Spark:如何在執行器中處理分區
- 3. 如何在apache spark中執行排序JavaPairRDD
- 4. 如何在Apache Spark中執行sql查詢
- 5. 如何在Apache Spark中執行簡單的reduceByKey?
- 6. 在哪裏執行Apache Spark reductionByWindow函數?
- 7. Apache Spark如何計算分區以及如何在執行器中處理分區
- 8. 如何在spark中執行初始化?
- 9. 如何在Spark Scala中執行ANOVA?
- 10. Apache Spark數量的執行程序
- 11. 如何在Spark中按分區對鍵/值進行分組?
- 12. Apache Spark的分區
- 13. Apache Spark RDD拆分「|」
- 14. 在Apache Spark中使用分類和數字功能對數據進行聚類
- 15. 如何在Spark Spark中快速執行mysql查詢
- 16. 如何使用Apache Spark執行簡單的網格搜索
- 17. 如何使用Apache Spark Dataframes(Python)執行Switch語句
- 18. Jupyter筆記本執行器是否在Apache Spark中動態分發?
- 19. 如何執行分類
- 20. 在Apache Spark中設置動態分配?
- 21. 在Apache Spark中更改分隔符
- 22. 在Apache Spark中分割數據幀
- 23. 在Bash腳本中執行Apache Spark(Scala)代碼
- 24. 執行spark-shell時出現Apache Spark異常
- 25. 如何在Apache Spark Cluster模式下運行更多的執行程序
- 26. Java中Spark MLlib的分類
- 27. 使用Apache Spark進行實時分析
- 28. 使用Apache Spark進行重新分區
- 29. 如何在Apache Spark中加入兩個(或更多)流(JavaDStream)
- 30. 如何在Apache Spark中使用Stanford NER
你想LabelEncoder或OneHotEncoder什麼? – elyase
我會優先考慮LabelEncoder,但我不會拒絕HotEncoder,如果PySpark中可用的話 –