【问题标题】:Spark - create RDD of (label, features) pairs from CSV fileSpark - 从 CSV 文件创建(标签、特征)对的 RDD
【发布时间】:2015-05-18 22:13:49
【问题描述】:

我有一个 CSV 文件,想对数据执行简单的 LinearRegressionWithSGD。

示例数据如下(文件中的总行数为 99,包括标签),目标是预测 y_3 变量:

y_3,x_6,x_7,x_73_1,x_73_2,x_73_3,x_8
2995.3846153846152,17.0,1800.0,0.0,1.0,0.0,12.0
2236.304347826087,17.0,1432.0,1.0,0.0,0.0,12.0
2001.9512195121952,35.0,1432.0,0.0,1.0,0.0,5.0
992.4324324324324,17.0,1430.0,1.0,0.0,0.0,12.0
4386.666666666667,26.0,1430.0,0.0,0.0,1.0,25.0
1335.9036144578313,17.0,1432.0,0.0,1.0,0.0,5.0
1097.560975609756,17.0,1100.0,0.0,1.0,0.0,5.0
3526.6666666666665,26.0,1432.0,0.0,1.0,0.0,12.0
506.8421052631579,17.0,1430.0,1.0,0.0,0.0,5.0
2095.890410958904,35.0,1430.0,1.0,0.0,0.0,12.0
720.0,35.0,1430.0,1.0,0.0,0.0,5.0
2416.5,17.0,1432.0,0.0,0.0,1.0,12.0
3306.6666666666665,35.0,1800.0,0.0,0.0,1.0,12.0
6105.974025974026,35.0,1800.0,1.0,0.0,0.0,25.0
1400.4624277456646,35.0,1800.0,1.0,0.0,0.0,5.0
1414.5454545454545,26.0,1430.0,1.0,0.0,0.0,12.0
5204.68085106383,26.0,1800.0,0.0,0.0,1.0,25.0
1812.2222222222222,17.0,1800.0,1.0,0.0,0.0,12.0
2763.5928143712576,35.0,1100.0,1.0,0.0,0.0,12.0

我已经使用以下命令读取了数据:

val data = sc.textFile(datadir + "/data_2.csv");

当我想使用以下命令创建(标签、特征)对的 RDD 时:

val parsedData = data.map { line =>
    val parts = line.split(',')
    LabeledPoint(parts(0).toDouble, Vectors.dense(parts(1).split(' ').map(_.toDouble)))
    }.cache()

所以我不能继续训练模型,有什么帮助吗?

附:我在 Windows 7 x64 中使用 Scala IDE 运行 spark。

【问题讨论】:

  • 你需要过滤掉header。另见:stackoverflow.com/questions/24299427/…
  • 谢谢,我把标题去掉了,但现在当我使用时: val parsedData = data.map { line => val parts = line.split(',') LabeledPoint(parts(0).toDouble , Vectors.dense(parts(1).split(' ').map(_.toDouble))) }.cache(),出现错误“value split is not a member of Array[String]”。你能帮帮我吗?
  • 你在做parts(1).split(' ').map(_.toDouble),不知道你为什么用空格分割,因为输入没有空格。另外,您更改了代码,我没有看到您上面提到的错误。

标签: scala apache-spark linear-regression


【解决方案1】:

经过大量努力,我找到了解决方案。第一个问题与标题行有关,第二个问题与映射功能有关。这是完整的解决方案:

//To read the file
val csv = sc.textFile(datadir + "/data_2.csv");

//To find the headers
val header = csv.first;

//To remove the header
val data = csv.filter(_(0) != header(0));

//To create a RDD of (label, features) pairs
val parsedData = data.map { line =>
    val parts = line.split(',')
    LabeledPoint(parts(0).toDouble, Vectors.dense(parts(1).split(' ').map(_.toDouble)))
    }.cache()

我希望它可以节省您的时间。

【讨论】:

    【解决方案2】:

    当您读取文件的第一行时

    y_3,x_6,x_7,x_73_1,x_73_2,x_73_3,x_8
    

    也在您的map 函数中读取和转换,因此您尝试在y_3 上调用toDouble。您需要过滤掉第一行并使用剩余的行进行学习。

    【讨论】:

    • 谢谢,我把标题去掉了,但现在当我使用时: val parsedData = data.map { line => val parts = line.split(',') LabeledPoint(parts(0).toDouble , Vectors.dense(parts(1).split(' ').map(_.toDouble))) }.cache(),出现错误“value split is not a member of Array[String]”。你能帮帮我吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多