0
這可能是天真的,但我剛開始使用PySpark和Spark。請幫助我瞭解Pyspark的一項熱門技術。我正在嘗試在其中一列上進行OneHotEncoding。在一次熱編碼之後,數據幀架構添加了一個向量。但是要應用機器學習算法,那應該是將單個列添加到現有數據框中,每列代表一個類別,而不是矢量類型列。如何驗證OneHotEncoding。PySpark- OneHotEncoding
我的代碼:
stringIndexer = StringIndexer(inputCol="business_type", outputCol="business_type_Index")
model = stringIndexer.fit(df)
indexed = model.transform(df)
encoder = OneHotEncoder(dropLast=False, inputCol="business_type_Index", outputCol="business_type_Vec")
encoded = encoder.transform(indexed)
encoded.select("business_type_Vec").show()
這顯示:
+-----------------+
|business_type_Vec|
+-----------------+
| (2,[0],[1.0])|
| (2,[0],[1.0])|
| (2,[0],[1.0])|
| (2,[0],[1.0])|
| (2,[0],[1.0])|
| (2,[0],[1.0])|
| (2,[0],[1.0])|
| (2,[0],[1.0])|
| (2,[0],[1.0])|
| (2,[0],[1.0])|
| (2,[0],[1.0])|
| (2,[0],[1.0])|
| (2,[0],[1.0])|
| (2,[0],[1.0])|
| (2,[0],[1.0])|
| (2,[0],[1.0])|
| (2,[0],[1.0])|
| (2,[0],[1.0])|
| (2,[0],[1.0])|
| (2,[0],[1.0])|
+-----------------+
only showing top 20 rows
新添加的列是向量類型的。我如何將它轉換爲每個類別的各個欄目
這是預期的行爲,您不需要轉換爲單個列,因爲spark ML可以處理特徵向量。 – mtoto