Без изменений при использовании pyspark.ml.feature VectorAssemblerPython

Программы на Python
Ответить
Anonymous
 Без изменений при использовании pyspark.ml.feature VectorAssembler

Сообщение Anonymous »

После примера из Databricks с моими собственными данными я не могу заставить работать преобразование VectorAssembler.
string_indexer = StringIndexer(inputCol='ptype', outputCol='index_ptype', handleInvalid="skip")
string_indexer_model = string_indexer.fit(sample_df)
indexed_df = string_indexer_model.transform(sample_df)

ohe = OneHotEncoder(inputCol='index_ptype', outputCol='ohe_ptype', handleInvalid="keep")
ohe_model = ohe.fit(indexed_df)
ohe_df = ohe_model.transform(indexed_df)
ohe_df.show()

+-----+-----------+-------------+
|ptype|index_ptype| ohe_ptype|
+-----+-----------+-------------+
| 5.0| 2.0|(6,[2],[1.0])|
| 7.0| 4.0|(6,[4],[1.0])|
| 3.0| 1.0|(6,[1],[1.0])|
| 1.0| 0.0|(6,[0],[1.0])|
| 6.0| 3.0|(6,[3],[1.0])|
| 8.0| 5.0|(6,[5],[1.0])|
+-----+-----------+-------------+

assembler = VectorAssembler(inputCols=['ohe_ptype'], outputCol="features")
result_df_dense = assembler.transform(ohe_df)

result_df_dense.show(truncate=False)

+-----+-----------+-------------+-------------+
|ptype|index_ptype|ohe_ptype |features |
+-----+-----------+-------------+-------------+
|5.0 |2.0 |(6,[2],[1.0])|(6,[2],[1.0])|
|7.0 |4.0 |(6,[4],[1.0])|(6,[4],[1.0])|
|3.0 |1.0 |(6,[1],[1.0])|(6,[1],[1.0])|
|1.0 |0.0 |(6,[0],[1.0])|(6,[0],[1.0])|
|6.0 |3.0 |(6,[3],[1.0])|(6,[3],[1.0])|
|8.0 |5.0 |(6,[5],[1.0])|(6,[5],[1.0])|
+-----+-----------+-------------+-------------+


Как мы видим, мои функции точно такие же, как у ohe_ptype!
Я ожидаю получить что-то вроде этого
+-----+-----------+-------------+-------------------------+
|ptype|index_ptype|ohe_ptype |features |
+-----+-----------+-------------+-------------------------+
|5.0 |2.0 |(6,[2],[1.0])|[0.0,0.0,1.0,0.0,0.0,0.0]|


Подробнее здесь: https://stackoverflow.com/questions/792 ... rassembler
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»