【发布时间】:2023-03-17 07:06:01
【问题描述】:
我正在尝试将一些机器学习算法应用于 Spark (Java) 中的数据集。
在尝试 Logistic regression in spark 的示例时
CoefficientMatrixis 是这样的:
3 x 4 CSCMatrix
(1,2) -0.7889290490451877
(0,3) 0.2989598305580243
(1,3) -0.36583869680195286
Intercept: [0.07898530675801645,-0.14799468898820128,0.06900938223018485]
如果我没记错的话,(1,2) -0.7889290490451877
(0,3) 0.2989598305580243
(1,3) -0.36583869680195286 代表每个班级的“最佳拟合”模型。
现在当我尝试我的数据集时,它有 4 个不同的类和 8192 个特征,系数是
4 x 8192 CSCMatrix
Intercept: [1.3629726436521425,0.7373644161565249,-1.0762606057817274,-1.0240764540269398]
我不熟悉逻辑回归算法,所以我不明白为什么没有“最佳拟合”?
我的代码
HashingTF hashingTF = new HashingTF()
.setInputCol("listT")
.setOutputCol("rawFeatures")
.setNumFeatures(8192) ;
Dataset<Row> featurizedData = hashingTF.transform(ReviewRawData);
featurizedData.show();
IDF idf = new IDF().setInputCol("rawFeatures").setOutputCol("features");
IDFModel idfModel = idf.fit(featurizedData);
Dataset<Row> rescaledData = idfModel.transform(featurizedData);
//add the label col based on some conditions
Dataset<Row> lebeldata = rescaledData.withColumn("label",newCol );
lebeldata.groupBy("label").count().show();
Dataset<Row>[] splits = lebeldata.select("label","features").randomSplit(new double[]{0.7, 0.3});
Dataset<Row> train = splits[0];
Dataset<Row> test = splits[1];
LogisticRegression lr = new LogisticRegression()
.setMaxIter(10)
.setRegParam(0.3)
.setElasticNetParam(0.8)
.setLabelCol("label")
.setFeaturesCol("features")
.setFamily("multinomial");
LogisticRegressionModel lrModel = lr.fit(train);
System.out.println("Coefficients: \n"
+ lrModel.coefficientMatrix() + " \nIntercept: " +
lrModel.interceptVector());
我的数据集
+-----+-----+
|label|count|
+-----+-----+
| 0.0| 6455|
| 1.0| 3360|
| 3.0| 599|
| 2.0| 560|
+-----+-----+
并且在评估分类器时,只预测了第一类。
Class 0.000000 precision = 0.599511
Class 0.000000 recall = 1.000000
Class 0.000000 F1 score = 0.749618
Class 1.000000 precision = 0.000000
Class 1.000000 recall = 0.000000
Class 1.000000 F1 score = 0.000000
Class 2.000000 precision = 0.000000
Class 2.000000 recall = 0.000000
Class 2.000000 F1 score = 0.000000
Class 3.000000 precision = 0.000000
Class 3.000000 recall = 0.000000
Class 3.000000 F1 score = 0.000000
顺便说一句,我将具有相同上述步骤的相同数据集应用于 spark 上的另一个机器学习算法,并且效果很好!
【问题讨论】:
-
嘿,Mahmoud,您了解系数矩阵是什么吗?这是一个逻辑回归,所以我认为权重应该只是 n 个特征的 1xn 的形式。为什么每个类都有多个,因为一组权重给出了每个类的概率。还有什么最适合每个类什么意思?
-
在stackoverflow.com/a/70003293/12232260查看我对这个问题的回答
标签: apache-spark