我刚刚尝试使用Apache Spark ml库进行Logistic回归,但每次尝试时,都会出现错误信息,如
"ERROR OWLQN: Failure! 重置历史记录:breeze.optimze.NaNHistory。"
逻辑回归的数据集例子如下。
+-----+---------+---------+---------+--------+-------------+
|state|dayOfWeek|hourOfDay|minOfHour|secOfMin| features|
+-----+---------+---------+---------+--------+-------------+
| 1.0| 7.0| 0.0| 0.0| 0.0|(4,[0],[7.0])|
而逻辑回归的代码如下:
//Data Set
StructType schema = new StructType(
new StructField[]{
new StructField("state", DataTypes.DoubleType, false, Metadata.empty()),
new StructField("dayOfWeek", DataTypes.DoubleType, false, Metadata.empty()),
new StructField("hourOfDay", DataTypes.DoubleType, false, Metadata.empty()),
new StructField("minOfHour", DataTypes.DoubleType, false, Metadata.empty()),
new StructField("secOfMin", DataTypes.DoubleType, false, Metadata.empty())
});
List<Row> dataFromRDD = bucketsForMLs.map(p -> {
return RowFactory.create(p.label(), p.features().apply(0), p.features().apply(1), p.features().apply(2), p.features().apply(3));
}).collect();
Dataset<Row> stateDF = sparkSession.createDataFrame(dataFromRDD, schema);
String[] featureCols = new String[]{"dayOfWeek", "hourOfDay", "minOfHour", "secOfMin"};
VectorAssembler vectorAssembler = new VectorAssembler().setInputCols(featureCols).setOutputCol("features");
Dataset<Row> stateDFWithFeatures = vectorAssembler.transform(stateDF);
StringIndexer labelIndexer = new StringIndexer().setInputCol("state").setOutputCol("label");
Dataset<Row> stateDFWithLabelAndFeatures = labelIndexer.fit(stateDFWithFeatures).transform(stateDFWithFeatures);
MLRExecutionForDF mlrExe = new MLRExecutionForDF(javaSparkContext);
mlrExe.execute(stateDFWithLabelAndFeatures);
// Logistic Regression part
LogisticRegressionModel lrModel = new LogisticRegression().setMaxIter(maxItr).setRegParam(regParam).setElasticNetParam(elasticNetParam)
// This part would occur error
.fit(stateDFWithLabelAndFeatures);
我刚刚也遇到了同样的错误。它来自于 微风 ScalaNLP包,Spark只是导入了它。它说什么衍生函数无法产生。
我不知道这到底是什么意思,但在我的数据集中,我可以说,我使用的数据越少,抛出错误的频率就越高。意思是说,一个类要训练的特征缺失的比例越高,错误发生的频率就越高。我认为这和因为一个类的信息缺失而无法正确优化有关。
尽管如此,这个错误似乎并没有阻止代码的运行。