Java: Data Normalization

Berikut merupakan uji coba data normalization menggunakan pemrograman Java.

public class DataNormalization {
	private static final double EXPONENTIAL = 2.718281828;
	private static final double PI = 3.14;

	public static void main(String[] agrs) {
		double[][] data = {
			{3, 5},
			{6, 8},
			{9, 10},
			{12, 14}
		};	

		double[][] result = getSigmoidal(data);

		for (int i = 0; i < result.length; i++) {
			for (int j = 0; j < result[0].length; j++) {
				System.out.print(result[i][j] + "\t");
			}
			System.out.println("");
		}
	}

	public static double[] getMean(double[][] data) {
		double[] mean = new double[data[0].length];

		for (int i = 0; i < data[0].length; i++) {
			for (int j = 0; j < data.length; j++) {
				mean[i] = mean[i] + data[j][i];
			}
			mean[i] = mean[i] / data.length;
		}

		return mean;
	}

	public static double[] getStandardDeviation(double[][] data) {
		double[] stdev = new double[data[0].length];
		double[] mean = getMean(data);

		for (int i = 0; i < data[0].length; i++) {
			for (int j = 0; j < data.length; j++) {
				stdev[i] = stdev[i] + Math.pow((data[j][i] - mean[i]), 2);
			}
			stdev[i] = Math.sqrt(stdev[i] / (data.length - 1));
		}

		return stdev;
	}

	public static double[][] getSigmoidal(double[][] data) {
		double[][] sigmoidal = new double[data.length][data[0].length];
		double[] mean = getMean(data);
		double[] stdev = getStandardDeviation(data);

		for (int i = 0; i < data[0].length; i++) {
			for (int j = 0; j < data.length; j++) {
				sigmoidal[j][i] = (data[j][i] - mean[i]) /  stdev[i];
				sigmoidal[j][i] = (1 - Math.pow(EXPONENTIAL, -sigmoidal[j][i])) / (1 + Math.pow(EXPONENTIAL, -sigmoidal[j][i]));
			}
		}

		return sigmoidal;
	}

	public static double[][] getZScore(double[][] data) {
		double[][] zScore = new double[data.length][data[0].length];
		double[] mean = getMean(data);
		double[] stdev = getStandardDeviation(data);

		for (int i = 0; i < data[0].length; i++) {
			for (int j = 0; j < data.length; j++) {
				zScore[j][i] = (data[j][i] - mean[i]) / stdev[i];
			}
		}

		return zScore;
	}

	public static double[][] getSoftmax(double[][] data) {
		double[][] softmax = new double[data.length][data[0].length];
		double[] mean = getMean(data);
		double[] stdev = getStandardDeviation(data);

		for (int i = 0; i < data[0].length; i++) {
			for (int j = 0; j < data.length; j++) {
				softmax[j][i] = (data[j][i] - mean[i]) / (1 * (stdev[i] / (2 * PI)));
				softmax[j][i] = 1 / (1 + Math.pow(EXPONENTIAL, -softmax[j][i]));
			}
		}

		return softmax;
	}

	public static double[][] getDecimalScaling(double[][] data, int n) {
		double[][] decimalScaling = new double[data.length][data[0].length];

		for (int i = 0; i < data[0].length; i++) {
			for (int j = 0; j < data.length; j++) {
				decimalScaling[j][i] = data[j][i] / Math.pow(10, n);
			}
		}

		return decimalScaling;
	}

	public static double[] getMin(double[][] data) {
		double[] min = new double[data[0].length];

		for (int i = 0; i < min.length; i++) {
			min[i] = Integer.MAX_VALUE;
		}

		for (int i = 0; i < data[0].length; i++) {
			for (int j = 0; j < data.length; j++) {
				if (data[j][i] < min[i]) {
					min[i] = data[j][i];
				}
			}
		}

		return min;
	}

	public static double[] getMax(double[][] data) {
		double[] max = new double[data[0].length];

		for (int i = 0; i < max.length; i++) {
			max[i] = Integer.MIN_VALUE;
		}

		for (int i = 0; i < data[0].length; i++) {
			for (int j = 0; j < data.length; j++) { 				if (data[j][i] > max[i]) {
					max[i] = data[j][i];
				}
			}
		}

		return max;
	}

	public static double[][] getMinMax(double[][] data) {
		double[][] minMax = new double[data.length][data[0].length];
		double[] min = getMin(data);
		double[] max = getMax(data);

		for (int i = 0; i < data[0].length; i++) {
			for (int j = 0; j < data.length; j++) {
				minMax[j][i] = (data[j][i] - min[i]) / (max[i] - min[i]);
			}
		}

		return minMax;
	}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s