spark中逻辑回归中的空系数

Mah*_*rad 4 apache-spark

我正在尝试将一些机器学习算法应用于 Spark (Java) 中的数据集。当在 spark 中尝试Logistic 回归的例子时, 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());
Run Code Online (Sandbox Code Playgroud)

我的数据集

+-----+-----+
|label|count|
+-----+-----+
|  0.0| 6455|
|  1.0| 3360|
|  3.0|  599|
|  2.0|  560|
+-----+-----+
Run Code Online (Sandbox Code Playgroud)

在评估分类器时,只预测了第一类。

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
Run Code Online (Sandbox Code Playgroud)

顺便说一句,我将具有上述相同步骤的相同数据集应用于 spark 上的另一个机器学习算法,并且效果很好!

Cha*_*ang 5

我有类似的问题LogisticRegression来自spark.ml于星火2.1.1和去除.setElasticNetParam(0.8)为我工作。

另一种可能性是您的数据集中有高杠杆点(特征范围内的异常值),这会扭曲预测。

  • 我的猜测是 `setElasticNetParam(0.8)` 将强制逻辑回归在 L1 和 L2 惩罚之间找到平衡,并且在大多数情况下,L1 惩罚会将回归系数推到 0 并破坏分类器。 (2认同)