- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在使用 Spark
和 pyspark
并且我有一个 pipeline
设置了一堆 StringIndexer
对象,我用它来将字符串列编码为索引列:
indexers = [StringIndexer(inputCol=column, outputCol=column + '_index').setHandleInvalid('skip')
for column in list(set(data_frame.columns) - ignore_columns)]
pipeline = Pipeline(stages=indexers)
new_data_frame = pipeline.fit(data_frame).transform(data_frame)
问题是,我需要为每个 StringIndexer
对象获取标签列表。对于没有管道的单个列和单个 StringIndexer
,这是一项简单的任务。在 DataFrame
上安装索引器后,我可以访问 labels
属性:
indexer = StringIndexer(inputCol="name", outputCol="name_index")
indexer_fitted = indexer.fit(data_frame)
labels = indexer_fitted.labels
new_data_frame = indexer_fitted.transform(data_frame)
但是当我使用管道时,这似乎是不可能的,或者至少我不知道该怎么做。
所以我想我的问题归结为:有没有办法访问在索引过程中为每个列使用的标签?
或者我是否必须在这个用例中放弃管道,例如循环遍历 StringIndexer
对象列表并手动执行? (我相信这是可能的。但是使用管道会更好)
最佳答案
示例数据和管道
:
from pyspark.ml.feature import StringIndexer, StringIndexerModel
df = spark.createDataFrame([("a", "foo"), ("b", "bar")], ("x1", "x2"))
pipeline = Pipeline(stages=[
StringIndexer(inputCol=c, outputCol='{}_index'.format(c))
for c in df.columns
])
model = pipeline.fit(df)
从阶段
中提取:
# Accessing _java_obj shouldn't be necessary in Spark 2.3+
{x._java_obj.getOutputCol(): x.labels
for x in model.stages if isinstance(x, StringIndexerModel)}
{'x1_index': ['a', 'b'], 'x2_index': ['foo', 'bar']}
来自转换后的 DataFrame
的元数据:
indexed = model.transform(df)
{c.name: c.metadata["ml_attr"]["vals"]
for c in indexed.schema.fields if c.name.endswith("_index")}
{'x1_index': ['a', 'b'], 'x2_index': ['foo', 'bar']}
关于python - 从 Spark (pyspark) 管道内的 StringIndexer 阶段获取标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45885044/
Apache Spark StringIndexerModel 在对某一特定列进行转换后返回空数据集。我正在使用成人数据集:http://mlr.cs.umass.edu/ml/datasets/Ad
我正在尝试对一列句子执行 StringIndexer 操作,即将单词列表转换为整数列表。 例如: 输入数据集: (1, ["I", "like", "Spark"]) (2, ["I", "h
我的 java 脚本有以下问题。 我有jQuery对象。在本地获取其值的第 i 个符号,我使用以下代码 $(this).val()[i]; 当我在服务器中部署此代码时,此行开始抛出异常,表示 $(th
我正在使用 PySpark 通过 ALS 进行协同过滤。我的原始用户和项目 ID 是字符串,所以我使用了 StringIndexer将它们转换为数字索引(PySpark 的 ALS 模型要求我们这样做
我的 PipelinedRDD 中有一列标称值,我希望将其转换为索引编码以用于分类目的。 我曾经在pyspark.ml中使用StringIndexer,它非常容易使用。不过,这次我正在学习如何处理 r
PySpark - v2.4.0 我尝试将 String 列 Country 转换为 Interger 列 Country_ID,结果看起来不错。但是当我尝试访问 Country_ID 列时,我得到了
Dataset dataFrame = ... ; StringIndexerModel labelIndexer = new StringIndexer() .se
我有格式化为以下示例的大数据记录: // +---+------+------+ // |cid|itemId|bought| // +---+------+------+ // |abc| 12
我收到了 StringIndex我正在处理的 10,000 个字符串中的一个特定字符串的错误。我真的不知道这个字符串有什么问题。我想这可能是一个特殊的性格问题。 如果我 println然后将该字符串分
我的目标是建立一个multicalss分类器。 我已经建立了用于特征提取的管道,并且第一步包括StringIndexer转换器,将每个类名称映射到标签,该标签将在分类器训练步骤中使用。 管道已安装培训
如何从经过训练的 Spark MLlib StringIndexerModel 中获取映射? val stringIndexer = new StringIndexer() .setInput
如何通过从 labelIndexer 获取标签,使用 IndexToString 进行转换? labelIndexer = StringIndexer(inputCol="shutdown_reaso
我有一个包含一些分类字符串列的数据集,我想用 double 类型表示它们。我使用 StringIndexer 进行此转换并且它有效,但是当我在另一个具有 NULL 值的数据集中尝试它时,它给出了 ja
当我使用 StringIndexer 和 OneHot Encoder 为我的矩阵准备数据时,我现在如何知道重要特征的名称/来源是什么? randomForest 分类器只会给我索引,我看不到原始数据
我正在使用 Scala 并使用 StringIndexer 为训练集中的每个类别分配索引。它根据每个类别的频率分配索引。 问题是在我的测试数据中,类别的频率不同,因此 StringIndexer 为类
我正在使用 Spark 和 pyspark 并且我有一个 pipeline 设置了一堆 StringIndexer 对象,我用它来将字符串列编码为索引列: indexers = [StringInde
我有一个 PySpark 数据框 +-------+--------------+----+----+ |address| date|name|food| +-------+----
我正在尝试在具有大约 15.000.000 个唯一字符串值的列上使用 Spark 的 StringIndexer 特征转换器。无论我投入多少资源,Spark 总是会因某种内存不足异常而死在我身上。 f
我是一名优秀的程序员,十分优秀!