【问题标题】:How to Predict value in Spark ML如何在 Spark ML 中预测价值
【发布时间】:2019-11-26 18:31:03
【问题描述】:

非常是 Spark 机器学习的新手(4 天大)我正在 Spark Shell 中执行以下代码,我正在尝试预测一些值

我的要求是我的数据包含以下内容

 Userid,Date,SwipeIntime
 1, 1-Jan-2017,9.30
 1, 2-Jan-2017,9.35
 1, 3-Jan-2017,9.45
 1, 4-Jan-2017,9.26
 2, 1-Jan-2017,9.37
 2, 2-Jan-2017,9.35
 2, 3-Jan-2017,9.45
 2, 4-Jan-2017,9.46     

我需要预测 SwipeIntime 将是什么 Userid = 1 将在 2017 年 1 月 5 日或任何日期出现

我尝试的是 Spark Shell 中的以下代码

代码:

 case class LabeledDocument(Userid: Double, Date: String, label: Double)
 val training = spark.read.option("inferSchema", true).csv("/root/Predictiondata2.csv").toDF
 ("Userid","Date","label").toDF().as[LabeledDocument]
 import scala.beans.BeanInfo
 import org.apache.spark.{SparkConf, SparkContext}
 import org.apache.spark.ml.Pipeline
 import org.apache.spark.ml.classification.LogisticRegression
 import org.apache.spark.ml.feature.{HashingTF, Tokenizer}
 import org.apache.spark.mllib.linalg.Vector
 import org.apache.spark.sql.{Row, SQLContext}
 val tokenizer = new Tokenizer().setInputCol("Date").setOutputCol("words")
 val hashingTF = new HashingTF().setNumFeatures(1000).setInputCol(tokenizer.getOutputCol).setOutputCol("features")
 import org.apache.spark.ml.regression.LinearRegression
 val lr = new LinearRegression().setMaxIter(10).setRegParam(0.3).setElasticNetParam(0.8)
 val pipeline = new Pipeline().setStages(Array(tokenizer, hashingTF, lr))
 val model = pipeline.fit(training.toDF())
 case class Document(Userid: Integer, Date: String)
 val test = sc.parallelize(Seq(Document(4, "04-Jan-18"),Document(5, "01-Jan-17"),Document(2, "03-Jan-17")))
 model.transform(test.toDF()).show()

得到不正确的输出(所有用户的 SwipeIntime 相同)

 scala> model.transform(test.toDF()).show() 
 +------+---------+-----------+------------------+-----------------+
 |Userid|     Date|      words|          features|       prediction|
 +------+---------+-----------+------------------+-----------------+
 |     4|04-Jan-18|[04-jan-18]|(1000,[455],[1.0])|9.726888888888887|
 |     5|01-Jan-17|[01-jan-17]|(1000,[595],[1.0])|9.726888888888887|
 |     2|03-Jan-17|[03-jan-17]|(1000,[987],[1.0])|9.726888888888887|
 +------+---------+-----------+------------------+-----------------+

如果有人对上述代码提供任何建议以使事情正常运行,我将不胜感激。

【问题讨论】:

  • 首先,你的数据集太小了……
  • 需要多少数据?让我知道,这样我就可以创建..
  • stackoverflow.com/questions/59032147/… 伙计们任何想法。如果可能,请给我解决方案

标签: scala apache-spark apache-spark-mllib prediction


【解决方案1】:

为什么你认为它不起作用?因为预测都是一样的?

我遇到了与描述 here 类似的问题,但在 PySpark 中。

我通过提高 MaxIter 并降低 RegParam 和 ElasticNetParam 解决了这个问题。

尝试这样设置:

val lr = new LinearRegression().setMaxIter(100).setRegParam(0.001).setElasticNetParam(0.0001)

希望它有效!

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2018-06-12
  • 2020-01-27
  • 1970-01-01
  • 2018-11-11
  • 1970-01-01
  • 2016-05-15
  • 1970-01-01
  • 2017-05-06
相关资源
最近更新 更多