android studio中的神经网络(weka) - weka.core.UnassignedDatasetException:DenseInstance无法访问数据集

时间:2018-03-19 05:59:05

标签: java android

这是我的神经网络课程。它将从主要调用。我有一个4项目的对象,神经网络必须预测第5个值。

public class NeuralNet {

private Instances isTrainingSet;
private MultilayerPerceptron mlp;
private FastVector fvClassVal;
private FastVector fvWekaAttributes;
private int val = 0;

public void readAndTrain(Object[][] data) throws IOException {

    //Declare numeric attributes
    Attribute oldValue = new Attribute("oldValue");
    Attribute temp = new Attribute("temperature");
    Attribute hum = new Attribute("humidity");
    Attribute wind = new Attribute("wind");
    Attribute newVal = new Attribute("newValue");

    //Assign attributes to weka attributes
    fvWekaAttributes = new FastVector(5);
    fvWekaAttributes.addElement(oldValue);
    fvWekaAttributes.addElement(temp);
    fvWekaAttributes.addElement(hum);
    fvWekaAttributes.addElement(wind);

    fvWekaAttributes.addElement(newVal);

    //Creating dataset Size
    isTrainingSet = new Instances("Rel", fvWekaAttributes,data.length);

    for (int i = 0; i < data.length; i++) {

        Instance trainInstance = new DenseInstance(5);

        trainInstance.setValue((Attribute)fvWekaAttributes.elementAt(0),(int)data[i][0] );
        trainInstance.setValue((Attribute)fvWekaAttributes.elementAt(1),(int)data[i][1] );
        trainInstance.setValue((Attribute)fvWekaAttributes.elementAt(2),(int)data[i][2] );
        trainInstance.setValue((Attribute)fvWekaAttributes.elementAt(3),(int)data[i][3] );
        trainInstance.setValue((Attribute)fvWekaAttributes.elementAt(4),(int)data[i][4] );
        isTrainingSet.add(trainInstance);
    }
    isTrainingSet.setClassIndex(4);
}

public void setupNeuralNet() throws Exception {
    mlp  = new MultilayerPerceptron();
    mlp.setLearningRate(0.1);
    mlp.setMomentum(0.2);
    mlp.setTrainingTime(2000);
    mlp.setHiddenLayers("3");
    mlp.buildClassifier(isTrainingSet);
}

public void evaluateNeuralNet() throws Exception {
    Evaluation eval = new Evaluation(isTrainingSet);
    eval.evaluateModel(mlp, isTrainingSet);
    System.out.println(eval.errorRate()); //Printing Training Mean root squared Error
    System.out.println(eval.toSummaryString()); //Summary of Training
}

public double predictStyle(Object[][] predictData) throws Exception {
    //When am running the app, am getting this exception:weka.core.UnassignedDatasetException: DenseInstance doesn't have access to a dataset!

    Instance predict = new DenseInstance(5);
    predict.setValue((Attribute)fvWekaAttributes.elementAt(0),(int)predictData[0][0] );
    predict.setValue((Attribute)fvWekaAttributes.elementAt(1),(int)predictData[0][1] );
    predict.setValue((Attribute)fvWekaAttributes.elementAt(2),(int)predictData[0][2] );
    predict.setValue((Attribute)fvWekaAttributes.elementAt(3),(int)predictData[0][3] );
    predict.setValue((Attribute)fvWekaAttributes.elementAt(4), Utils.missingValue());

    int clsLabel = (int) mlp.classifyInstance(predict);
    predict.setClassValue(clsLabel);
    return clsLabel;
}

}

这是我调用NeuralNet类的地方。当我在运行应用程序后单击按钮时,出现此错误:

  

weka.core.UnassignedDatasetException:DenseInstance没有   访问数据集!

btnMachine.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                machineLearning();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
public void machineLearning() throws Exception {
    Object[][] weatherData = new Object[][]{
            {0, 27, 60, 17, 7}, {7, 26, 68, 17, 30},
            {30, 27, 57, 14, 14}, {14, 24, 73, 13, 30},
            {30, 26, 64, 18, 20}, {20, 27, 62, 17, 18},
            {18, 27, 63, 12, 18}, {18, 26, 70, 15, 46},
            {46, 26, 66, 18, 33}, {33, 27, 62, 21, 22},
            {22, 27, 64, 16, 29}, {29, 26, 62, 15, 23},
            {23, 25, 66, 17, 34}, {34, 28, 53, 13, 9},
            {9, 28, 66, 18, 10}, {10, 25, 74, 18, 27},
            {27, 27, 68, 19, 12}, {12, 26, 70, 12, 29},
            {29, 24, 78, 19, 40}, {40, 26, 63, 25, 10},
            {10, 25, 66, 18, 18}, {18, 26, 69, 15, 17},
            {17, 24, 76, 15, 25}, {25, 24, 80, 11, 31}
    };

    NeuralNet neuralNetwork = new NeuralNet();   //Call the NeuralNetwork class
    neuralNetwork.readAndTrain(weatherData);     //Read and train the data given in weatherDate object
    neuralNetwork.setupNeuralNet();

    //Data to predict
    Object[][] predictData = new Object[][]{
            {30, 27, 70, 18}
    };

    System.out.println("The new Value is " + neuralNetwork.predictStyle(predictData));
    //machineTxt.setText(String.valueOf(neuralNetwork.predictStyle(predictData)));
}

请帮帮我。

0 个答案:

没有答案