/**
* Copyright 2013 Neuroph Project http://neuroph.sourceforge.net
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.neuroph.samples.uci;
import java.util.Arrays;
import org.neuroph.core.NeuralNetwork;
import org.neuroph.core.data.DataSet;
import org.neuroph.core.data.DataSetRow;
import org.neuroph.core.events.LearningEvent;
import org.neuroph.core.events.LearningEventListener;
import org.neuroph.nnet.MultiLayerPerceptron;
import org.neuroph.nnet.learning.BackPropagation;
import org.neuroph.nnet.learning.MomentumBackpropagation;
/**
*
* @author Ivana Bajovic
*/
/*
DATA SET INFORMATION:
These data are the results of a chemical analysis of wines grown in the same region in Italy but derived from three different cultivars. The analysis determined the quantities of 13 constituents found in each of the three types of wines.
I think that the initial data set had around 30 variables, but for some reason I only have the 13 dimensional version. I had a list of what the 30 or so variables were, but a.) I lost it, and b.), I would not know which 13 variables are included in the set.
The Input Variables are:
1) Alcohol
2) Malic acid
3) Ash
4) Alcalinity of ash
5) Magnesium
6) Total phenols
7) Flavanoids
8) Nonflavanoid phenols
9) Proanthocyanins
10)Color intensity
11)Hue
12)OD280/OD315 of diluted wines
13)Proline
In a classification context, this is a well posed problem with "well behaved" class structures. A good data set for first testing of a new classifier, but not very challenging.
Output Variable is type of wine (nominal value).
ATTRIBUTE INFORMATION:
All attributes are continuous
No statistics available, but suggest to standardise variables for certain uses (e.g. for us with classifiers which are NOT scale invariant)
The original data set that will be used in this experiment can be found at link http://archive.ics.uci.edu/ml/datasets/Wine
*/
public class WineClassificationSample implements LearningEventListener {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
(new WineClassificationSample()).run();
}
public void run() {
System.out.println("Creating training set...");
// get path to training set
String trainingSetFileName = "data_sets/wine_classification_data.txt";
int inputsCount = 13;
int outputsCount = 3;
// create training set from file
DataSet dataSet = DataSet.createFromFile(trainingSetFileName, inputsCount, outputsCount, "\t", false);
System.out.println("Creating neural network...");
// create MultiLayerPerceptron neural network
MultiLayerPerceptron neuralNet = new MultiLayerPerceptron(inputsCount, 22, outputsCount);
// attach listener to learning rule
MomentumBackpropagation learningRule = (MomentumBackpropagation) neuralNet.getLearningRule();
learningRule.addListener(this);
// set learning rate and max error
learningRule.setLearningRate(0.2);
learningRule.setMaxError(0.01);
System.out.println("Training network...");
// train the network with training set
neuralNet.learn(dataSet);
System.out.println("Training completed.");
System.out.println("Testing network...");
testNeuralNetwork(neuralNet, dataSet);
System.out.println("Saving network");
// save neural network to file
neuralNet.save("MyNeuralNetWineClassification.nnet");
System.out.println("Done.");
}
public void testNeuralNetwork(NeuralNetwork neuralNet, DataSet testSet) {
for (DataSetRow testSetRow : testSet.getRows()) {
neuralNet.setInput(testSetRow.getInput());
neuralNet.calculate();
double[] networkOutput = neuralNet.getOutput();
System.out.print("Input: " + Arrays.toString(testSetRow.getInput()));
System.out.println(" Output: " + Arrays.toString(networkOutput));
}
}
public void handleLearningEvent(LearningEvent event) {
BackPropagation bp = (BackPropagation) event.getSource();
System.out.println(bp.getCurrentIteration() + ". iteration | Total network error: " + bp.getTotalNetworkError());
}
}
Ads help maintain this website.