apache-spark - 如何在 Apache Spark 中进行 LabelEncoding 或分类值

标签 apache-spark scikit-learn

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

最佳答案

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()

关于apache-spark - 如何在 Apache Spark 中进行 LabelEncoding 或分类值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30580410/

相关文章:

apache-spark - 如何修复来自 apache-spark 的对等消息重置连接?

apache-spark - 从 S3 Bucket 读取文件到 PySpark Dataframe Boto3

python - 使用日期数据进行 Sklearn 线性回归

numpy - 如何为 sklearn.svm.SVC 定义自定义内核函数?

scala - mvn 测试错误 : java. lang.IllegalStateException:无法在已停止的 SparkContext 上调用方法

java - Spark 和 Cassandra Java 应用程序 : Exception in thread "main" java. lang.NoClassDefFoundError: org/apache/spark/sql/Dataset

apache-spark - 如何将 JVM 选项 -Xss512m 添加到 spark-submit?

Python:CountVectorizer 忽略一个字母单词 "I"

multiple-regression - scikit_learn 回归总结

python - 如何从 scikit-learn KMeans 获取聚类中心的文本?