【问题标题】:Converting Dataframe to Vector.dense for k-mean将 Dataframe 转换为 Vector.dense 以获得 k-mean
【发布时间】:2017-10-01 20:28:35
【问题描述】:

按照这个问题的答案 How to convert type Row into Vector to feed to the KMeans

我已经为我的数据创建了特征表。(assembler 是一个向量汇编器)

val kmeanInput  = assembler.transform(table1).select("features")

当我使用 kmeanInput 运行 k-means 时

val clusters = KMeans.train(kmeanInput, numCluster, numIteration)

我得到了错误

:102: 错误:类型不匹配;找到:org.apache.spark.sql.DataFrame(扩展为)org.apache.spark.sql.Dataset[org.apache.spark.sql.Row] 需要:org.apache.spark.rdd.RDD[org.apache.spark.sql.Row] apache.spark.mllib.linalg.Vector] val clusters = KMeans.train(kmeanInput, numCluster, numIteration)

正如@Jed 在他的回答中提到的,发生这种情况是因为行不是Vectors.dense 格式。 为了解决这个问题,我尝试了

 val dat = kmeanInput.rdd.map(lambda row: Vectors.dense([x for x in 
 row["features"]]))

我得到了这个错误

:3: 错误:')' 预期但 '(' 找到。val dat = kmeanInput.rdd.map(lambda row: Vectors.dense([x for x in row["features"]]))

:3: 错误:';'预期但 ')' 找到。 val dat = kmeanInput.rdd.map(lambda row: Vectors.dense([x for x in row["features"]]))

【问题讨论】:

    标签: scala apache-spark k-means


    【解决方案1】:

    您导入了错误的库,您应该使用来自mlKMeans 而不是mllib。第一个使用DataFrame,第二个使用RDD

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-17
      • 2017-03-23
      • 2016-03-23
      • 2021-08-24
      • 2018-08-29
      • 2020-01-24
      相关资源
      最近更新 更多