2016-09-21 29 views
1

我正在用Spark 1.6構建NB模型,並使用ChiSqSelector來確定頂級功能。我總共有7個功能,並尋找前3名。雖然流程運行良好,但我將如何確定被評爲頂級功能的實際功能。由於數據被分類,我無法將輸出映射到實際的輸入列。ChiSqSelector - 真實的功能 - 火花

val chidata = cat_recs.map(r => (r.getDouble(targetInd), Vectors.dense(featuresidx.map(r.getDouble(_)).toArray))).toDF("target","features") 
val sel = new ChiSqSelector().setNumTopFeatures(3).setFeaturesCol("features").setLabelCol("target").setOutputCol("selectedFeatuers") 
val chiresult = sel.fit(chidata).transform(chidata) 

輸出是

scala> chiresult.foreach(println) 
[1.0,[0.0,2.0,0.0,5.0,7.0,5.0,1.0],[0.0,5.0,7.0]] 
[1.0,[4.0,3.0,0.0,5.0,7.0,5.0,1.0],[0.0,5.0,7.0]] 
[0.0,[3.0,2.0,0.0,5.0,7.0,5.0,3.0],[0.0,5.0,7.0]] 
[1.0,[1.0,2.0,0.0,1.0,7.0,5.0,2.0],[0.0,1.0,7.0]] 
[1.0,[0.0,2.0,0.0,1.0,7.0,5.0,3.0],[0.0,1.0,7.0]] 

結構 - 目標:雙,特點:矢量,selectedFeatures:矢量 從上面,讓我們在第一行的示例

[1.0,[0.0,2.0,0.0,5.0,7.0,5.0,1.0],[0.0,5.0,7.0]] 

我如何確定它在selectedFeatures中引用的是0.0,同樣也在第5行。

請幫助..

感謝

巴拉

回答

2

在您的例子:

[1.0,[0.0,2.0,0.0,5.0,7.0,5.0,1.0],[0.0,5.0,7.0]] 

最後一列[0.0,5.0,7.0]代表所選要素的價值,在這種情況下設有2個, 3和4(從0開始計數)。爲了提取未來的指數,只需使用

val model = sel.fit(chidata) 
val importantFeatures = model.selectedFeatures 
+0

感謝您的回覆。我明白,我可以得到selectedFeatures,但我的問題是,我怎麼知道0.0在selectedFeatures是第0指數,而不是第2。 [1.0,[0.0,2.0,0.0,5.0,7.0,5.0,1.0],[0.0,5.0,7.0]] selectedFeatures(0)= 0.0 selectedFeatures(1)= 5.0 selectedFeatures(2)= 7.0 在數據中我們有兩個0.0,我們怎麼能說selectedFeatures(0)是第一個0.0而不是隨後的0.0 謝謝 –

+1

'selectedFeatures'返回'Array [Int]'帶有索引而不是值,所以在你的情況下,這應該返回(2,3,4),這意味着所選的功能是第3,第4和第5列。 – bear911

+0

感謝您的回覆。 **不幸的是,當我做以下 scala> chiresult.select(「selectedFeatures」)。show + ---------------- + | selectedFeatures | + ---------------- + | [2.0,1.0,7.0] | | [0.0,1.0,7.0] | | [0.0,1.0,7.0] | 我似乎得到的值,而不是一個數組[內部] ** –