【问题标题】:dataframe into dense vector spark数据帧成密集矢量火花
【发布时间】:2017-05-16 13:58:02
【问题描述】:

Acyally 正在开发 spark 2.0.2 我想知道,例如,基于 Spark ML 进行逻辑回归。我想将数据帧的每一行放入一个向量中,该向量将作为逻辑回归的输入,你能帮助获取导致数据帧的行吗?每一行都变成一个密集的向量。谢谢。这是我为获取数据帧所做的工作。

import org.apache.spark.ml.classification.LogisticRegression
import org.apache.spark.ml.linalg.{Vector, Vectors}
import org.apache.spark.ml.param.ParamMap
import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.Row
import org.apache.hadoop.fs.shell.Display

object Example extends App {
val sparkSession = SparkSession.builder.master("local").appName("my-spark-app").getOrCreate()
val data=sparkSession.read.option("header", "true").csv("C://sample_lda_data.csv").toDF()
val data2=data.select("col2","col3","col4","col5","col6","col7","col8","col9")

最后我想得到这样的东西作为逻辑回归的输入 在第一个位置,它将是数据框的第一列,请提供任何帮助

val data=sparkSession.read.option("header", "true").csv("C://sample_lda_data.csv").toDF()
val data2=data.select("col2","col3","col4","col5","col6","col7","col8","col9")
val assembler = new VectorAssembler().setInputCols(Array("col2", "col3", "col4")).setOutputCol("features")
val output = assembler.transform(data2)

main" java.lang.IllegalArgumentException: Data type StringType is not supported.

我会很感激的。谢谢你们

【问题讨论】:

  • 你可以使用VectorAssembler
  • @mtoto 我用了你所说的,我编辑了代码我得到了这个错误 main" java.lang.IllegalArgumentException: Data type StringType is not supported.Any help
  • 你所有的列都应该是数字

标签: scala apache-spark dataframe apache-spark-sql


【解决方案1】:
I have wrote code to convert dataframe's numeric columns into dense vector. Please find below code. Note: here col1 and col2 are numeric type columns.

import sparksession.implicits._;
   val result: Dataset[LabeledPoint] = df.map{ x => LabeledPoint(x.getAs[Integer]("Col1").toDouble, Vectors.dense(x.getAs[Double]("col2"))) }
   result.show();
result.printSchema();

+-------+----------+
|  label|  features|
+-------+----------+
|31825.0| [75000.0]|
|58784.0| [24044.0]|
|  121.0| [41000.0]|

root
 |-- label: double (nullable = true)
 |-- features: vector (nullable = true)

【讨论】:

    【解决方案2】:

    你可以使用array函数,然后映射到LabeledPoints:

    import org.apache.spark.mllib.linalg.Vectors
    import org.apache.spark.mllib.regression.LabeledPoint
    import org.apache.spark.sql._
    import org.apache.spark.sql.functions._
    import org.apache.spark.sql.types.DoubleType
    
    // create an array column from all but first one:
    val arrayCol: Column = array(df.columns.drop(1).map(col).map(_.cast(DoubleType)): _*)
    
    // select array column and first column, and map into LabeledPoints
    val result: Dataset[LabeledPoint] = df.select(col("col1").cast(DoubleType), arrayCol)
      .map(r => LabeledPoint(
        r.getAs[Double](0),
        Vectors.dense(r.getAs[mutable.WrappedArray[Double]](1).toArray)
      ))
    
    // You can use the Dataset or the RDD
    result.show()
    // +-----+---------------------+
    // |label|features             |
    // +-----+---------------------+
    // |1.0  |[2.0,3.0,4.0,0.5]    |
    // |11.0 |[12.0,13.0,14.0,15.0]|
    // |21.0 |[22.0,23.0,24.0,25.0]|
    // +-----+---------------------+
    
    result.rdd.foreach(println)
    // (1.0,[2.0,3.0,4.0,0.5])
    // (21.0,[22.0,23.0,24.0,25.0])
    

    【讨论】:

    • 请问您使用 thnx 的导入包是什么来帮助您,我正在尝试您的代码 Thnx 再次
    • 对不起,我的朋友是 scala 和 spark 的新手,我收到一个错误,告诉我 $ 不是 StringContext 的成员 thnx 提前
    • 哦,这是另一个缺少的导入 (import sparkSession.implicits._),添加它或将 $"col1" 替换为 col("col1")
    • 你好,我的朋友,我不知道如何感谢你。但是你能帮我一个忙吗,如何改变你的代码来将我的数据转换成双精度?非常感谢,
    • "我不知道如何感谢你" - 这就是接受按钮的用途;) 将一列转换为双列,您可以使用 cast(DoubleType) - 我再次更新了答案,虽然我确实建议您阅读 DataFrame 文档,它都在那里 - 我不会根据您的要求继续编辑答案...
    猜你喜欢
    • 2017-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-09
    • 2018-02-15
    • 2019-04-12
    • 2016-05-01
    • 1970-01-01
    相关资源
    最近更新 更多