java - 将特征重要性向量压缩到列名数组时,Scala java.io toArray 错误
问题描述
当试图将特征重要性向量从 lightGBM 压缩getfeatureImportances
到列名数组时,我遇到了以下错误:
import com.microsoft.ml.spark.LightGBMClassificationModel
import org.apache.spark.ml.classification.RandomForestClassificationModel
def getFeatureImportances(inputContainer: PipelineModelContainer): (String, String) = {
val transformer = inputContainer.pipelineModel.stages.last
val featureImportancesVector = inputContainer.params match {
case RandomForestParameters(numTrees, treeDepth, featureTransformer) =>
transformer.asInstanceOf[RandomForestClassificationModel].featureImportances
case LightGBMParameters(treeDepth, numLeaves, iterations, featureTransformer) =>
transformer.asInstanceOf[LightGBMClassificationModel].getFeatureImportances("split")
}
val colNames = inputContainer.featureColNames
val sortedFeatures = (colNames zip featureImportancesVector.toArray).sortWith(_._2 > _._2).zipWithIndex
}
参考我的代码的最后一行,我收到此错误:
value toArray is not a member of java.io.Serializable
似乎轻 GBM 特征重要性不能转换为数组。如果它只是 randomForestClassifier 功能的重要性,则此代码可以正常工作。我还能做什么?
解决方案
在该match
块的两个分支中,一个返回Array[Double]
,另一个返回Vector
。
这两种类型的共同超类型是java.io.Serializable
,因此 Scala 将变量的类型推断为该类型featureImportancesVector
。
toArray
尽管该方法在这两种情况下都存在,但在该类型中不可用。
要解决这个问题很容易,正如评论中所建议的那样,将 移动.toArray
到featureImportances
,以便两个分支的类型以及变量的类型变为Array[Double]
。
推荐阅读
- python - 如何在对象向量上使用条件进行while循环
- java - 由于“找不到 bom-all.jar”而无法构建 thorntail-examples/gradle-examples/jaxrs-cdi
- mysql - 使用自定义计算列改进复杂查询
- openid - OneLogin OIDC 身份验证代码流第 1 部分和第 2 部分之间的重定向是什么?
- c - 在不使用 STL 的情况下为下限和上限找到合适的 C 代码时遇到麻烦
- python - time.sleep 对线程访问的影响
- javascript - 视差滚动绑定动画,以不同的速度制作动画
- ios - 在 Swift 语言和 Realm 数据库中声明日期对象时,是否需要格式化日期?
- python - 使用 Kosaraju 算法的图中强连通分量 (SCC) 的大小(边数)
- sql - 当发生变化时如何跟踪数据库中的变化?