spark org.apache.spark.ml.linalg.DenseVector cannot be cast to org.apache.spark.ml.linalg.SparseVect

在使用 import org.apache.spark.ml.feature.VectorAssembler 轉換特徵後,想要放入 import org.apache.spark.mllib.classification.SVMWithSGD 去訓練的時候出現錯誤:java

Caused by: java.lang.ClassCastException: org.apache.spark.ml.linalg.DenseVector cannot be cast to org.apache.spark.ml.linalg.SparseVector

修改以下:apache

val trainDataFrame = dataframe.rdd.map(r => LabeledPoint(
  r.getAs[Double]("label"),
org.apache.spark.mllib.linalg.Vectors.fromML(r.getAs[org.apache.spark.ml.linalg.SparseVector]("features").toDense)

))
相關文章
相關標籤/搜索