前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Sparkml库标签和索引之间转化

Sparkml库标签和索引之间转化

作者头像
Spark学习技巧
发布2018-01-31 12:22:55
6820
发布2018-01-31 12:22:55
举报

StringIndexer

StringIndexer将一串字符串标签编码为一列标签索引。这些索引范围是[0, numLabels)按照标签频率排序,因此最频繁的标签获得索引0。如果用户选择保留它们,那么看不见的标签将被放在索引numLabels处。如果输入列是数字,我们将其转换为字符串值并将为其建索引。当下游管道组件(例如Estimator或 Transformer使用此字符串索引标签)时,必须将组件的输入列设置为此字符串索引列名称。在许多情况下,您可以使用设置输入列setInputCol。

例1, 假如我们有下面的DataFrame,带有id和category列:

Id

category

0

a

1

b

2

c

3

a

4

a

5

c

对着个Dataframe使用StringIndexer,输入列式category,categoryIndex作为输出列,得到如下值:

Id

Category

CategoryIndex

0

a

0.0

1

b

2.0

2

c

1.0

3

a

0.0

4

a

0.0

5

c

1.0

字符a,索引值是0,原因是a出现的频率最高,接着就是c:1,b:2。

另外,对于不可见的标签,StringIndexer有是三种处理策略:

1, 抛出异常,这是默认行为

2, 跳过不可见的标签

3, 把不可见的标签,标记为numLabels(这个是无用的)。

还用上面的例子,数据如下:

Id

Category

0

a

1

b

2

c

3

a

4

a

5

c

6

d

7

e

如果你没有设置StringIndexer如何处理这些不可见的词,或者设置为了error,他将会抛出一个异常。然而,你如果设置setHandleInvalid("skip"),将会得到如下结果:

Id

Category

CategoryIndex

0

a

0.0

1

b

2.0

2

c

1.0

注意,包含d,e的行并没有出现。

如果,调用setHandleInvalid("keep"),会得到下面的结果:

Id

Category

CategoryIndex

0

a

0.0

1

b

2.0

2

c

1.0

3

d

3.0

4

e

3.0

注意,d,e获得的索引值是3.0

代码用例如下:

import org.apache.spark.ml.feature.StringIndexer
val df = spark.createDataFrame(
 Seq((0, "a"), (1, "b"), (2, "c"), (3, "a"), (4, "a"), (5, "c"))
).toDF("id", "category")
val indexer = new StringIndexer()
 .setInputCol("category")
 .setOutputCol("categoryIndex")
val indexed = indexer.fit(df).transform(df)
indexed.show()

IndexToString

对称地StringIndexer,IndexToString将一列标签索引映射回包含作为字符串的原始标签的列。一个常见的用例是从标签生成索引StringIndexer,用这些索引对模型进行训练,并从预测索引列中检索原始标签IndexToString。但是,您可以自由提供自己的标签。

例如,假如我们有dataframe格式如下:

Id

CategoryIndex

0

0.0

1

2.0

2

1.0

3

0.0

4

0.0

5

1.0

使用IndexToString 并且使用categoryIndex作为输入列,originalCategory作为输出列,可以检索到原始标签如下:

Id

originalCategory

CategoryIndex

0

a

0.0

1

b

2.0

2

c

1.0

3

a

0.0

4

a

0.0

5

c

1.0

代码案例如下:

import org.apache.spark.ml.attribute.Attribute
import org.apache.spark.ml.feature.{IndexToString, StringIndexer}
val df = spark.createDataFrame(Seq(
 (0, "a"),
 (1, "b"),
 (2, "c"),
 (3, "a"),
 (4, "a"),
 (5, "c")
)).toDF("id", "category")
val indexer = new StringIndexer()
 .setInputCol("category")
 .setOutputCol("categoryIndex")
 .fit(df)
val indexed = indexer.transform(df)
println(s"Transformed string column '${indexer.getInputCol}' " +
 s"to indexed column '${indexer.getOutputCol}'")
indexed.show()
val inputColSchema = indexed.schema(indexer.getOutputCol)
println(s"StringIndexer will store labels in output column metadata: " +
 s"${Attribute.fromStructField(inputColSchema).toString} ")
val converter = new IndexToString()
 .setInputCol("categoryIndex")
 .setOutputCol("originalCategory")
val converted = converter.transform(indexed)
println(s"Transformed indexed column '${converter.getInputCol}' back to original string " +
 s"column '${converter.getOutputCol}' using labels in metadata")
converted.select("id", "categoryIndex", "originalCategory").show()
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-12-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 浪尖聊大数据 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档