- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
从一个示例开始,我尝试进行线性回归。问题是我得到了错误的结果。作为拦截器我应该有:2.2。
我尝试添加在另一篇文章中找到的 .optimizer.setStepSize(0.1),但仍然得到一个奇怪的结果。建议?
这是一组数据
1,2
2,4
3,5
4,4
5,5
代码:
import org.apache.spark.SparkConf
import org.apache.spark.SparkContext
import org.apache.spark.mllib.regression.LabeledPoint
import org.apache.spark.mllib.regression.LinearRegressionModel
import org.apache.spark.mllib.regression.LinearRegressionWithSGD
import org.apache.spark.mllib.linalg.Vectors
object linearReg {
def main(args: Array[String]) {
StreamingExamples.setStreamingLogLevels()
val sparkConf = new SparkConf().setAppName("linearReg").setMaster("local")
val sc=new SparkContext(sparkConf)
// Load and parse the data
val data = sc.textFile("/home/daniele/dati.data")
val parsedData = data.map { line =>
val parts = line.split(',')
LabeledPoint(parts(0).toDouble, Vectors.dense(Array(1.0)++parts(1).split(' ').map(_.toDouble)))
}.cache()
parsedData.collect().foreach(println)
// Building the model
val numIterations = 1000
val model = LinearRegressionWithSGD.train(parsedData, numIterations)
println("Interceptor:"+model.intercept)
// Evaluate model on training examples and compute training error
val valuesAndPreds = parsedData.map { point =>
val prediction = model.predict(point.features)
(point.label, prediction)
}
valuesAndPreds.collect().foreach(println)
val MSE = valuesAndPreds.map { case (v, p) => math.pow((v - p), 2) }.mean()
println("training Mean Squared Error = " + MSE)
// Save and load model
model.save(sc, "myModelPath")
val sameModel = LinearRegressionModel.load(sc, "myModelPath")
}
}
结果:
weights: [-4.062601003207354E25], intercept: -9.484399253945647E24
更新-使用.train方法-添加了 1.0 作为拦截的附录。1.0 附录中数据以这种方式出现
最佳答案
您正在使用run
,这意味着您传入的数据被解释为“配置参数”,而不是要回归的功能。
docs包含训练然后运行模型的良好示例:
//note the "train" instead of "run"
val numIterations = 1000
val model = LinearRegressionWithSGD.train(parsedData, numIterations)
结果是更准确的重量:
scala> model.weights
res4: org.apache.spark.mllib.linalg.Vector = [0.7674418604651163]
如果您想添加截距,只需将 1.0 值作为密集向量中的特征即可。修改您的示例代码:
...
LabeledPoint(Parts(0).toDouble, Vectors.dense(Array(1.0) ++ parts(1).split(' ').map(_.toDouble)))
...
第一个特征是你的拦截。
关于scala - Spark mllib LinearRegression 奇怪的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33783187/
使用 Spark1.6.0 MLLib,我将构建一个模型(如 RandomForest)并保存到 hdfs,然后可以从 hdfs 加载随机森林模型以在没有 SparkContext 的情况下进行预测。
我在某处读到 MLlib 本地向量/矩阵目前正在包装 Breeze 实现,但是将 MLlib 转换为 Breeze 向量/矩阵的方法是 org.apache.spark.mllib 范围内的私有(pr
我正在尝试在 Web 项目中使用 spark mllib.jar。我下载了 spark-1.1.0-bin-hadoop2.4 并解压。找到如下jar: datanucleus-api-jdi-3.2
在尝试使用 Python 使用 Spark mllib 的 LinearRegressionWithSGD 进行线性回归时,我一直得到非常糟糕的结果。 我调查了类似的问题,如下所示: Spark -
我使用 mllib 创建了 Apache Spark 机器学习管道。评估器结果是一个带有“概率”列的 DataFrame,它是概率的 mllib 向量(类似于 scikit-learn 中的 pred
我正在尝试使用 Spark 的 MLlib 在 Java 上实现 KMeans,我偶然发现了一个问题,那就是,尽管我导入了正确的 jar,但我的编译器无法识别这一行 // Cluster the da
我正在使用 Scala 对其进行编程,但语言在这里并不重要。 在这种情况下,隐式反馈协作过滤器 (ALS.trainImplicit) 的输入是产品的 View : Rating("user1", "
假设我有一个Array[RDD]类型的对象data。我想学习此对象中每个 RDD 上的独立机器学习模型。例如,对于随机森林: data.map{ d => RandomForest.trainRegr
我想运行 SVM 回归,但输入格式有问题。现在,我为一位客户设置的训练和测试集如下所示: 1 '12262064 |f offer_quantity:1 has_bought_brand_compan
我在 Spark 控制台中尝试了以下代码 import org.apache.spark.mllib.linalg.{Matrix, Matrices, DenseMatrix} val dm: De
Spark 2.0.0 中是否有任何预构建的异常值检测算法/四分位距识别方法?我在这里找到了一些代码,但我认为这在spark2.0.0中尚不可用 谢谢 最佳答案 如果您没有找到预构建的方法,您可以执行
我正在使用 pySpark MLlib 和开箱即用的 ALS 方法进行协同过滤。只是想知道,Spark 是否提供了其他一些进行过滤(用于计算距离)的方法,例如 Pearson 或 Cosine 的?可
您好,我是 spark mllib 的新手。我已经有一个 r 模型。我正在尝试使用 spark mllib 的相同模型。这里是 R 模型代码。 R 代码。 delhi data = sc.textF
我正在学习如何将机器学习与 Spark MLLib 结合使用,目的是对推文进行情感分析。我从这里得到了一个情绪分析数据集: http://thinknook.com/wp-content/upload
我有一个具有这种结构的小文件“naivebayetest.txt” 10 1:1 20 1:2 20 1:2 根据这些数据,我试图对 vector (1) 进行分类。如果我正确理解贝叶斯 (1) 的标
“spark mllib”提供的机器学习算法,如 naive byes、random forest 能否在 spark 集群中以并行模式运行?或者我们需要更改代码?请提供一个并行运行的例子?不确定 M
我正在使用 Spark 1.5.0 MLlib 随机森林算法(Scala 代码)进行二分类。由于我使用的数据集高度不平衡,因此多数类以 10% 的采样率进行下采样。 是否可以在 Spark 随机森林训
我正在使用 Spark MLlib 1.4.1 创建决策树模型。现在我想从决策树中提取规则。 如何提取规则? 最佳答案 您可以通过调用 model.toDebugString() 以字符串形式获取完整
我正在尝试使用 MlLib 进行协作过滤。 我在 Apache Spark 1.0.0 中运行 Scala 程序时遇到以下错误。 14/07/15 16:16:31 WARN NativeCod
我正在尝试在 Spark 中实现的协作过滤算法,并遇到以下问题: 假设我用以下数据训练模型: u1|p1|3 u1|p2|3 u2|p1|2 u2|p2|3 现在,如果我用以下数据测试它: u1|p1
我是一名优秀的程序员,十分优秀!