我正在使用 Spark 1.6 构建 NB 模型,并使用 ChiSqSelector 来识别最重要的特征。我总共有 7 个功能,正在寻找前 3 个功能。虽然流程运行良好,但我如何识别被评为顶级功能的实际功能。由于数据已分类,我无法将输出映射到实际输入列。
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)
Run Code Online (Sandbox Code Playgroud)
输出是
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]]
Run Code Online (Sandbox Code Playgroud)
结构 -- target: double, features: vector, selectedFeatures: vector 从上面来看,我们以第一行为例
[1.0,[0.0,2.0,0.0,5.0,7.0,5.0,1.0],[0.0,5.0,7.0]]
Run Code Online (Sandbox Code Playgroud)
我如何识别它在 selectedFeatures 中引用的是哪个 0.0,第五行也类似。
请帮忙..
谢谢
巴拉
我有一个返回数据帧的UDF.像下面这样的东西
scala> predict_churn(Vectors.dense(2.0,1.0,0.0,3.0,4.0,4.0,0.0,4.0,5.0,2.0))
res3: org.apache.spark.sql.DataFrame = [noprob: string, yesprob: string, pred: string]
scala> predict_churn(Vectors.dense(2.0,1.0,0.0,3.0,4.0,4.0,0.0,4.0,5.0,2.0)).show
+------------------+------------------+----+
| noprob| yesprob|pred|
+------------------+------------------+----+
|0.3619977592578127|0.6380022407421874| 1.0|
+------------------+------------------+----+
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用该命令将其注册为UDF时
hiveContext.udf.register("predict_churn", outerpredict _)
Run Code Online (Sandbox Code Playgroud)
我得到一个错误
java.lang.UnsupportedOperationException: Schema for type org.apache.spark.sql.DataFrame is not supported
at org.apache.spark.sql.catalyst.ScalaReflection$class.schemaFor(ScalaReflection.scala:715)
Run Code Online (Sandbox Code Playgroud)
返回的数据帧不受支持.我使用Spark 1.6.1和Scala 2.10.如果不支持,请如何将多列返回到外部程序.
谢谢
巴拉