// 设置线性回归参数 val lr1 = new LinearRegression() .setLabelCol("lowprice") .setFeaturesCol("features") .setFitIntercept(true) // 是否有w0截距 // 设置正则化参数 val lr2 = lr1.setMaxIter(30) // 最大迭代次数 .setRegParam(0.3) .setElasticNetParam(0.8) val regression = lr2 val model = regression.fit(train) val result = model.transform(test) result.show()
时间: 2024-03-31 08:33:44 浏览: 22
这段代码中,首先定义了一个 LinearRegression 模型,并设置了模型的参数。其中,setLabelCol 函数指定了模型的标签列为 "lowprice",setFeaturesCol 函数指定了模型的特征列为 "features"。同时,setFitIntercept 函数设置了是否拟合截距项 w0。接着,使用 setMaxIter 函数设置了最大迭代次数,setRegParam 函数设置了正则化参数(Ridge 正则化),setElasticNetParam 函数设置了 ElasticNet 正则化参数。最后,将 LinearRegression 模型赋值给 regression 变量,并使用 fit 函数对训练集进行拟合,生成一个模型。使用 transform 函数将测试集作为输入数据,生成预测结果,并使用 show 函数展示预测结果。
相关问题
import spark.implicits._ // 对 DtaFrame 中的数据进行筛选与处理, 并最后转化为一个新的 DataFram val dataPre = file.select("lowprice", "minprice", "topprice") .map(row => (row.getAs[String](0).toDouble, row.getAs[String](1).toDouble, row.getAs[String](2).toDouble)) val data: DataFrame = dataPre.toDF("lowprice", "minprice", "topprice") data.show() // VectorAssembler 是一个转换器 val assembler = new VectorAssembler() .setInputCols(Array("lowprice", "minprice", "topprice")) .setOutputCol("features") val dataset = assembler.transform(data) dataset.show() //拆分成训练集和测试集 val Array(train, test) = dataset.randomSplit(Array(0.9,0.1),1234L) // 设置线性回归参数 val lr1 = new LinearRegression() .setLabelCol("lowprice") .setFeaturesCol("features") .setFitIntercept(true) // 是否有w0截距 // 设置正则化参数 val lr2 = lr1.setMaxIter(30) // 最大迭代次数 .setRegParam(0.3) .setElasticNetParam(0.8) val regression = lr2 val model = regression.fit(train) val result = model.transform(test) result.show()
这部分代码对 DataFrame 进行了一些筛选和处理,并使用 VectorAssembler 转换器将数据转换为特征向量。然后,它将数据集拆分成训练集和测试集,并使用 LinearRegression 类设置线性回归参数。接着,它使用正则化参数来进行模型训练,最终得到训练好的模型。最后,使用模型对测试集进行预测,并将预测结果打印出来。
error: value train is not a member of org.apache.spark.mllib.regression.IsotonicRegression val model = new IsotonicRegression().setIsotonic(true).train(train)
这个错误出现的原因是因为 `train` 方法是 `IsotonicRegressionModel` 类的方法,而不是 `IsotonicRegression` 类的方法。因此你不能在 `IsotonicRegression` 类的实例上调用 `train` 方法。
要训练 `IsotonicRegression` 模型,你需要之前建议的那样使用 `run` 方法。具体来说,你可以将 `train` RDD 转换为 `(Double, Double, Double)` 格式的元组,并将其传递给 `run` 方法。例如:
```scala
val data = train.map(lp => (lp.label, lp.features(0), 1.0))
val model = new IsotonicRegression().setIsotonic(true).run(data)
```
这里假设 `train` RDD 中的 `LabeledPoint` 对象的 `features` 属性只包含一个 Double 值。如果 `features` 属性包含多个值,你需要将它们转换为一个 Double 值,或者选择一个合适的值作为元组的第二个元素。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)