spark-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "manuel garrido (JIRA)" <j...@apache.org>
Subject [jira] [Updated] (SPARK-18783) ML StringIndexer does not work with nested fields
Date Thu, 08 Dec 2016 11:35:58 GMT

     [ https://issues.apache.org/jira/browse/SPARK-18783?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]

manuel garrido updated SPARK-18783:
-----------------------------------
    Description: 
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}

  was:
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

{code}


> 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: issues-unsubscribe@spark.apache.org
For additional commands, e-mail: issues-help@spark.apache.org


Mime
View raw message