0

我正在尝试将情绪分析程序添加到 Spark 管道。这样做时,我有扩展类org.apache.spark.ml.PredictionModel。扩展此类时PredictionModel,我必须重写predict()预测给定特征标签的方法。但是,当我执行此代码时,我总是得到 0 或 1。例如,如果有 10 条电影评论,其中 5 条是负面评论,另外 5 条是负面评论,它将所有评论归类为负面。我附上了下面的代码。

import org.apache.spark.ml.PredictionModel;
import org.apache.spark.ml.param.ParamMap;
import org.apache.spark.mllib.linalg.DenseVector;
import org.apache.spark.mllib.linalg.Vector;
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
import org.nd4j.linalg.api.buffer.DataBuffer;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.factory.Nd4j;
import java.io.*;

//Model produced by a ProbabilisticClassifier
public class MovieReviewClassifierModel extends PredictionModel<Object, MovieReviewClassifierModel> implements  Serializable{


    private static final long serialVersionUID = 1L;
    private MultiLayerNetwork net;

    MovieReviewClassifierModel (MultiLayerNetwork net) throws Exception {
        this.net=net;
 }

    @Override
    public MovieReviewClassifierModel copy(ParamMap args0) {
        return null;
    }

    @Override
    public String uid() {
        return "MovieReviewClassifierModel";
    }


    public double raw2prediction(Vector rawPrediction) {//Given a vector of raw predictions, select the predicted label
        return rawPrediction.toArray()[0];
    }

    @Override
    public double predict(Object o) {

        int prediction=0;
        DenseVector v=(DenseVector)o;
        double[] a=v.toArray();
        INDArray arr=Nd4j.create(a);
        INDArray array= net.output(arr,false);
        DataBuffer ob = array.data();
        double[] d=ob.asDouble();
        double zeroProbability=d[0];
        double oneProbability=d[1];
        if (zeroProbability > oneProbability) {
            prediction=0;
        }
        else{
            prediction=1;

        }


        return prediction;
    }


}

你能告诉我错误预测的原因吗?

4

1 回答 1

0

public double predict(Object o)您有以下if声明:

if (zeroProbability > oneProbability) {
    prediction=0;
}
else{
    prediction=1;

}

这会导致返回 0 或 1。更改此方法以获得其他一些预测值。

于 2016-03-08T07:56:48.117 回答