List of usage examples for weka.classifiers.functions MultilayerPerceptron buildClassifier
@Override public void buildClassifier(Instances i) throws Exception
From source file:predictor.Predictor.java
public static void multilayerPerceptron() throws Exception { DataSource train = new DataSource(configuration.getWorkspace() + "train_common.arff"); DataSource test = new DataSource(configuration.getWorkspace() + "test_common.arff"); Instances trainInstances = train.getDataSet(); Instances testInstances = test.getDataSet(); //last attribute classify trainInstances.setClassIndex(trainInstances.numAttributes() - 1); testInstances.setClassIndex(testInstances.numAttributes() - 1); // //from w w w . j a va 2 s .co m // Classifier cModel = (Classifier)new MultilayerPerceptron(); // cModel.buildClassifier(trainInstances); // // weka.core.SerializationHelper.write("/some/where/nBayes.model", cModel); // // Classifier cls = (Classifier) weka.core.SerializationHelper.read("/some/where/nBayes.model"); // // // Test the model // Evaluation eTest = new Evaluation(trainInstances); // eTest.evaluateModel(cls, testInstances); MultilayerPerceptron mlp = new MultilayerPerceptron(); mlp.buildClassifier(trainInstances); mlp.setHiddenLayers(configuration.getHiddenLayers()); mlp.setLearningRate(configuration.getLearningRate()); mlp.setTrainingTime(configuration.getEpocs()); mlp.setMomentum(configuration.getMomentum()); // train classifier Classifier cls = new MultilayerPerceptron(); cls.buildClassifier(trainInstances); // evaluate classifier and print some statistics Evaluation eval = new Evaluation(trainInstances); eval.evaluateModel(cls, testInstances); System.out.println(eval.toSummaryString()); }
From source file:predictors.HelixPredictor.java
License:Open Source License
/** * Trains the Weka Classifer./*from ww w. j a v a2s.c om*/ */ public void trainClassifier() { try { MultilayerPerceptron classifier = new weka.classifiers.functions.MultilayerPerceptron(); Instances data = this.dataset; if (data.classIndex() == -1) { data.setClassIndex(data.numAttributes() - 1); } data.randomize(new Random(data.size())); String[] optClassifier = weka.core.Utils .splitOptions("-L 0.01 -M 0.8 -N 256 -V 20 -S 0 -E 5 -H 25 -B -I -D -C"); classifier.setOptions(optClassifier); classifier.setSeed(data.size()); classifier.buildClassifier(data); this.classifier = classifier; this.isTrained = true; } catch (Exception e) { ErrorUtils.printError(HelixPredictor.class, "Training failed", e); } }