JAVA Program For A.I. Data Normalization
I needed to normalize my data for the sake of machine learning – i.e. training a neural network for automatic classification of unseen data. The formula I used for the normalization is ((value – minimum value)/(maximum value – minimum value)). I then rounded this to 2 decimal places.
In order to actually round off, I did another JAVA class specifically for this and used it in the main class.
This is not the most elegant programs you will ever come across but at least it does the job for me and I am sure it will do the job for someone else. The argument needs to be a text file containing numbers that need to be normalized. The other argument is the name of the text file that you want the output to be written. When you run it, it does the job quietly and doesn’t notify you of success. (It may notify you of any errors!)
You can modify it to use a scanner to input the arguments or even develop it into an applet.
This is the main class then:
import java.util.*;
import java.io.*;
import java.math.*;
public class InputNormalization
{
public static void main(String[] args) throws IOException
{
String originalFile = "numbers.txt"; //PUT THE NAME OF YOUR TEXT FILE HERE
String endFile = "numbersnormalised.txt"; //AND THE TARGET TEXT FILE HERE
int count = 0;
int normalcount = 0;
double num = 0;
double min = 20;
double max = -20;
// double calculatedValue = 0.00;
Scanner fileScan = new Scanner (new File(originalFile));
while (fileScan.hasNext())
{
num = fileScan.nextDouble();
count++;
if (min > num) //Find the minimum value
{
min = num;
}
if (max < num)// Find the maximum value
{
max = num;
}
}
fileScan.close();
fileScan = new Scanner (new File(originalFile));
// new scanner to "rewind" the file
Writer output = null; //for writing to a text file
File file = new File(endFile);
output = new BufferedWriter(new FileWriter(file));
output.write("The number of values in "+originalFile+" is
"+count+"\r\n"); //Number of values in the text file
output.write("The minimum value is "+min+"\r\n");
output.write("The maximum value is "+max+"\r\n");
output.write("The following are the normalized values: \r\n");
double values[] = new double[count];
for (int i = 0; i < values.length; i++)
{
if (fileScan.hasNext())
{
num = fileScan.nextDouble();
if (num == min)
{
output.write("0.00 \r\n");
}
else
{
InputDecimalRounding normalisedValue =
new InputDecimalRounding((num - min)/(max - min));
//Normalize the Data and Round it to 2dp
output.write(normalisedValue+ "\r\n");
}
}
} //END FOR
fileScan.close();
output.close();
} //END MAIN
}//END CLASS
THE INPUTDECIMALROUNDING CLASS FOLLOWS:
import java.text.*;
public class InputDecimalRounding
{
double conversion;
public InputDecimalRounding (double conversion)
{
this.conversion = conversion;
}
public String toString()
{
DecimalFormat normalData = new DecimalFormat("#0.00");
return normalData.format(conversion);
}
}
Facebook Comments












