2016-09-29 139 views
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 

新添加的列是向量類型的。我如何將它轉換爲每個類別的各個欄目

+0

這是預期的行爲,您不需要轉換爲單個列,因爲spark ML可以處理特徵向量。 – mtoto

回答

0

您可能已經有了答案,但也許會對其他人有所幫助。對於矢量份額,你可以使用這個答案(我檢查了它的工作原理):

How to split dense Vector into columns - using pyspark

不過,我不認爲你需要向量轉換回的列(如mtoto已經說過),如火花中的所有模型實際上都要求您提供矢量格式的輸入功能(如果我錯了,請糾正我)。