[
https://issues.apache.org/jira/browse/SPARK-18783?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15750057#comment-15750057
]
Joseph K. Bradley commented on SPARK-18783:
-------------------------------------------
I'd separate this into 2 issues:
1. nested fields (new feature)
2. silent failure during transform
For issue #1: I doubt we'll support nested fields soon, though it would be neat
to have in the future. One related issue is multi-column support: [SPARK-8418].
For issue #2:
This is because of a hack we did to allow PipelineModel.transform() to work
without a label column. During fitting, the StringIndexerModel would index the
label. But during prediction/transform, there would not be a label. It's
documented here:
[http://spark.apache.org/docs/latest/api/scala/index.html#org.apache.spark.ml.feature.StringIndexerModel]
We ran into this issue here: [SPARK-8051]. Long term, we should think about
adding a Param to PipelineStage to turn the stage on/off during fit/transform.
That's a pretty awkward API, though, so we'll have to discuss it.
I'm going to close this since I don't think we'll add nesting in the near
future, but we can continue the conversation as needed. Thanks!
> ML StringIndexer does not work with nested fields
> -------------------------------------------------
>
> Key: SPARK-18783
> URL: https://issues.apache.org/jira/browse/SPARK-18783
> Project: Spark
> Issue Type: Bug
> Components: ML
> Affects Versions: 2.0.0
> Reporter: manuel garrido
>
> Using StringIndexer.transform with a nested field (from parsing json data)
> results in the output dataframe not having the new column.
> {code}
> sample = [
> {'city': u'',
> 'device': {u'make': u'HTC',
> u'os': u'Android'}
> },
> {'city': u'Bangalore',
> 'device': {u'make': u'Xiaomi',
> u'os': u'Android'}
> },
> {'city': u'Overpelt',
> 'device': {u'make': u'Samsung',
> u'os': u'Android'}
> }
> ]
> sample_df = sc.parallelize(sample).toDF()
> # First we use a StringIndexer with a non nested field
> city_indexer = StringIndexer(inputCol="city", outputCol="cityIndex",
> handleInvalid="skip")
> city_indexed = city_indexer.fit(sample_df).transform(sample_df)
> print([i.asDict() for i in city_indexed.collect()])
> >>>[{'device': {u'make': u'HTC', u'os': u'Android'}, 'city': u'',
> >>>'cityIndex': 0.0}, {'device': {u'make': u'Xiaomi', u'os': u'Android'},
> >>>'city': u'Bangalore', 'cityIndex': 2.0}, {'device': {u'make': u'Samsung',
> >>>u'os': u'Android'}, 'city': u'Overpelt', 'cityIndex': 1.0}]
> # Now we try with a nested field
> os_indexer = StringIndexer(inputCol="device.os", outputCol="osIndex",
> handleInvalid="skip")
> os_indexed = os_indexer.fit(sample_df).transform(sample_df)
> print([i.asDict() for i in os_indexed.collect()])
> >>>[{'device': {u'make': u'HTC', u'os': u'Android'}, 'city': u''}, {'device':
> >>>{u'make': u'Xiaomi', u'os': u'Android'}, 'city': u'Bangalore'}, {'device':
> >>>{u'make': u'Samsung', u'os': u'Android'}, 'city': u'Overpelt'}] #===> we
> >>>see the field osIndex is not showing up
> #If we rename the same field device.os as a flat field it works as expected
> os_indexer = StringIndexer(inputCol="device_os", outputCol="osIndex",
> handleInvalid="skip")
> os_indexed = os_indexer.fit(
> sample_df.withColumn('device_os', col('device.os'))
> ).transform(
> sample_df.withColumn('device_os', col('device.os'))
> )
> print([i.asDict() for i in os_indexed.collect()])
> {code}
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]