如何在Scala for Dataframe中运行逻辑回归
我已阅读数据文件如下:如何在Scala for Dataframe中运行逻辑回归,scala,apache-spark,logistic-regression,Scala,Apache Spark,Logistic Regression,我已阅读数据文件如下: val df = sqlContext.read.format("com.databricks.spark.csv").option("header", "true").load("D:/ModelData.csv") +---------+---------+---+-----+-------+ |c1 | c2 |c3 | c4 | c5 | +---------+---------+---+-----+-------+ |
val df = sqlContext.read.format("com.databricks.spark.csv").option("header", "true").load("D:/ModelData.csv")
+---------+---------+---+-----+-------+
|c1 | c2 |c3 | c4 | c5 |
+---------+---------+---+-----+-------+
| 1| 1| 13| 100| 1|
| 1| 1| 13| 200| 0|
| 1| 1| 13| 300| 0|
+---------+---------+---+-----+-------+
那么我对模型的输入是c5和c4。(c1、c2、c3对所有行都是相同的)
但这似乎不起作用。它不会打印任何内容。我们非常感谢您的帮助。您的分类器需要知道要学习什么。因此,必须为分类器定义标签列和特征列 要素列的默认名称为“要素”,标签列的默认名称为“标签” 您可以将列重命名为“标签”和“功能”: 或者,您可以定义要用作要素/标签列的列:
val lr = new LogisticRegression()
.setMaxIter(10)
.setRegParam(0.3)
.setElasticNetParam(0.8)
.setLabelColumn("c5")
.setFeaturesColumn("c4")
您的分类器需要知道要学习什么。因此,必须为分类器定义标签列和特征列 要素列的默认名称为“要素”,标签列的默认名称为“标签” 您可以将列重命名为“标签”和“功能”: 或者,您可以定义要用作要素/标签列的列:
val lr = new LogisticRegression()
.setMaxIter(10)
.setRegParam(0.3)
.setElasticNetParam(0.8)
.setLabelColumn("c5")
.setFeaturesColumn("c4")
给定
数据帧
为
+---+---+---+---+---+
|c1 |2 |c3 |c4 |c5 |
+---+---+---+---+---+
|1 |1 |13 |100|1 |
|1 |1 |13 |200|0 |
|1 |1 |13 |300|0 |
+---+---+---+---+---+
val assembler = new VectorAssembler()
.setInputCols(Array("c4"))
.setOutputCol("features")
该问题建议将c4
和c5
用于LogisticRegression
(c4
和c5
as特性
和c5
as标签
)
双精度
的特征
向量列
可以使用向量汇编程序
作为
+---+---+---+---+---+
|c1 |2 |c3 |c4 |c5 |
+---+---+---+---+---+
|1 |1 |13 |100|1 |
|1 |1 |13 |200|0 |
|1 |1 |13 |300|0 |
+---+---+---+---+---+
val assembler = new VectorAssembler()
.setInputCols(Array("c4"))
.setOutputCol("features")
逻辑回归
需要标签
和功能
列
val df3 = assembler.transform(df).select($"c5".cast(DoubleType).as("label"), $"features")
那是
+-----+--------+
|label|features|
+-----+--------+
|1.0 |[100.0] |
|0.0 |[200.0] |
|0.0 |[300.0] |
+-----+--------+
现在LogisticRegression
可以作为
val lr = new LogisticRegression()
.setMaxIter(10)
.setRegParam(0.3)
.setElasticNetParam(0.8)
val lrModel = lr.fit(df3)
val trainingSummary = lrModel.summary
println(trainingSummary)
输出为
org.apache.spark.ml.classification.BinaryLogisticRegressionTrainingSummary@6e9f8160
给定
数据帧
为
+---+---+---+---+---+
|c1 |2 |c3 |c4 |c5 |
+---+---+---+---+---+
|1 |1 |13 |100|1 |
|1 |1 |13 |200|0 |
|1 |1 |13 |300|0 |
+---+---+---+---+---+
val assembler = new VectorAssembler()
.setInputCols(Array("c4"))
.setOutputCol("features")
该问题建议将c4
和c5
用于LogisticRegression
(c4
和c5
as特性
和c5
as标签
)
双精度
的特征
向量列
可以使用向量汇编程序
作为
+---+---+---+---+---+
|c1 |2 |c3 |c4 |c5 |
+---+---+---+---+---+
|1 |1 |13 |100|1 |
|1 |1 |13 |200|0 |
|1 |1 |13 |300|0 |
+---+---+---+---+---+
val assembler = new VectorAssembler()
.setInputCols(Array("c4"))
.setOutputCol("features")
逻辑回归
需要标签
和功能
列
val df3 = assembler.transform(df).select($"c5".cast(DoubleType).as("label"), $"features")
那是
+-----+--------+
|label|features|
+-----+--------+
|1.0 |[100.0] |
|0.0 |[200.0] |
|0.0 |[300.0] |
+-----+--------+
现在LogisticRegression
可以作为
val lr = new LogisticRegression()
.setMaxIter(10)
.setRegParam(0.3)
.setElasticNetParam(0.8)
val lrModel = lr.fit(df3)
val trainingSummary = lrModel.summary
println(trainingSummary)
输出为
org.apache.spark.ml.classification.BinaryLogisticRegressionTrainingSummary@6e9f8160
为什么要将标签用作功能?监督建模的目的是确定标签。将其用作功能对我来说毫无意义。这只是一个帮助您理解流程的示例:)您可以对功能使用除标签以外的其他值。@Rumo我已更新了答案。:)现在应该没事了。是吗?@Rumo如果答案是肯定的,那么请删除你的反对票:)谢谢你为什么使用标签作为功能?监督建模的目的是确定标签。将其用作功能对我来说毫无意义。这只是一个帮助您理解流程的示例:)您可以对功能使用除标签以外的其他值。@Rumo我已更新了答案。:)现在应该没事了。是吗?@Rumo如果答案是好的,那么请删除你的反对票:)谢谢