@Test public void tesUSPS() { System.out.println("USPS"); double[][] x = USPS.x; int[] y = USPS.y; double[][] testx = USPS.testx; int[] testy = USPS.testy; double[] score = SumSquaresRatio.of(x, y); int[] index = QuickSort.sort(score); int p = 135; int n = x.length; double[][] xx = new double[n][p]; for (int j = 0; j < p; j++) { for (int i = 0; i < n; i++) { xx[i][j] = x[i][index[255-j]]; } } int testn = testx.length; double[][] testxx = new double[testn][p]; for (int j = 0; j < p; j++) { for (int i = 0; i < testn; i++) { testxx[i][j] = testx[i][index[255-j]]; } } LDA lda = LDA.fit(xx, y); int[] prediction = new int[testn]; for (int i = 0; i < testn; i++) { prediction[i] = lda.predict(testxx[i]); } double accuracy = new Accuracy().measure(testy, prediction); System.out.format("SSR %.2f%%%n", 100 * accuracy); }
public static double[] of(double[][] x, int[] y) { if (x.length != y.length) { throw new IllegalArgumentException(String.format("The sizes of X and Y don't match: %d != %d", x.length, y.length)); } ClassLabels codec = ClassLabels.fit(y); int k = codec.k; y = codec.y; int n = x.length; int p = x[0].length; int[] nc = new int[k]; double[] mu = new double[p]; double[][] condmu = new double[k][p]; for (int i = 0; i < n; i++) { int yi = y[i]; nc[yi]++; for (int j = 0; j < p; j++) { mu[j] += x[i][j]; condmu[yi][j] += x[i][j]; } } for (int j = 0; j < p; j++) { mu[j] /= n; for (int i = 0; i < k; i++) { condmu[i][j] /= nc[i]; } } double[] wss = new double[p]; double[] bss = new double[p]; for (int i = 0; i < n; i++) { int yi = y[i]; for (int j = 0; j < p; j++) { bss[j] += MathEx.sqr(condmu[yi][j] - mu[j]); wss[j] += MathEx.sqr(x[i][j] - condmu[yi][j]); } } for (int j = 0; j < p; j++) { bss[j] /= wss[j]; } return bss; }
SumSquaresRatio implements FeatureRanking { public static double[] of(double[][] x, int[] y) { if (x.length != y.length) { throw new IllegalArgumentException(String.format("The sizes of X and Y don't match: %d != %d", x.length, y.length)); } ClassLabels codec = ClassLabels.fit(y); int k = codec.k; y = codec.y; int n = x.length; int p = x[0].length; int[] nc = new int[k]; double[] mu = new double[p]; double[][] condmu = new double[k][p]; for (int i = 0; i < n; i++) { int yi = y[i]; nc[yi]++; for (int j = 0; j < p; j++) { mu[j] += x[i][j]; condmu[yi][j] += x[i][j]; } } for (int j = 0; j < p; j++) { mu[j] /= n; for (int i = 0; i < k; i++) { condmu[i][j] /= nc[i]; } } double[] wss = new double[p]; double[] bss = new double[p]; for (int i = 0; i < n; i++) { int yi = y[i]; for (int j = 0; j < p; j++) { bss[j] += MathEx.sqr(condmu[yi][j] - mu[j]); wss[j] += MathEx.sqr(x[i][j] - condmu[yi][j]); } } for (int j = 0; j < p; j++) { bss[j] /= wss[j]; } return bss; } }
SumSquaresRatio implements FeatureRanking { public static double[] of(double[][] x, int[] y) { if (x.length != y.length) { throw new IllegalArgumentException(String.format("The sizes of X and Y don't match: %d != %d", x.length, y.length)); } ClassLabels codec = ClassLabels.fit(y); int k = codec.k; y = codec.y; int n = x.length; int p = x[0].length; int[] nc = new int[k]; double[] mu = new double[p]; double[][] condmu = new double[k][p]; for (int i = 0; i < n; i++) { int yi = y[i]; nc[yi]++; for (int j = 0; j < p; j++) { mu[j] += x[i][j]; condmu[yi][j] += x[i][j]; } } for (int j = 0; j < p; j++) { mu[j] /= n; for (int i = 0; i < k; i++) { condmu[i][j] /= nc[i]; } } double[] wss = new double[p]; double[] bss = new double[p]; for (int i = 0; i < n; i++) { int yi = y[i]; for (int j = 0; j < p; j++) { bss[j] += MathEx.sqr(condmu[yi][j] - mu[j]); wss[j] += MathEx.sqr(x[i][j] - condmu[yi][j]); } } for (int j = 0; j < p; j++) { bss[j] /= wss[j]; } return bss; }  }
SumSquaresRatio implements FeatureRanking { public static double[] of(double[][] x, int[] y) { if (x.length != y.length) { throw new IllegalArgumentException(String.format("The sizes of X and Y don't match: %d != %d", x.length, y.length)); } ClassLabels codec = ClassLabels.fit(y); int k = codec.k; y = codec.y; int n = x.length; int p = x[0].length; int[] nc = new int[k]; double[] mu = new double[p]; double[][] condmu = new double[k][p]; for (int i = 0; i < n; i++) { int yi = y[i]; nc[yi]++; for (int j = 0; j < p; j++) { mu[j] += x[i][j]; condmu[yi][j] += x[i][j]; } } for (int j = 0; j < p; j++) { mu[j] /= n; for (int i = 0; i < k; i++) { condmu[i][j] /= nc[i]; } } double[] wss = new double[p]; double[] bss = new double[p]; for (int i = 0; i < n; i++) { int yi = y[i]; for (int j = 0; j < p; j++) { bss[j] += MathEx.sqr(condmu[yi][j] - mu[j]); wss[j] += MathEx.sqr(x[i][j] - condmu[yi][j]); } } for (int j = 0; j < p; j++) { bss[j] /= wss[j]; } return bss; }  @Override double[] rank(double[][] x, int[] y); static double[] of(double[][] x, int[] y); }
SumSquaresRatio implements FeatureRanking { public static double[] of(double[][] x, int[] y) { if (x.length != y.length) { throw new IllegalArgumentException(String.format("The sizes of X and Y don't match: %d != %d", x.length, y.length)); } ClassLabels codec = ClassLabels.fit(y); int k = codec.k; y = codec.y; int n = x.length; int p = x[0].length; int[] nc = new int[k]; double[] mu = new double[p]; double[][] condmu = new double[k][p]; for (int i = 0; i < n; i++) { int yi = y[i]; nc[yi]++; for (int j = 0; j < p; j++) { mu[j] += x[i][j]; condmu[yi][j] += x[i][j]; } } for (int j = 0; j < p; j++) { mu[j] /= n; for (int i = 0; i < k; i++) { condmu[i][j] /= nc[i]; } } double[] wss = new double[p]; double[] bss = new double[p]; for (int i = 0; i < n; i++) { int yi = y[i]; for (int j = 0; j < p; j++) { bss[j] += MathEx.sqr(condmu[yi][j] - mu[j]); wss[j] += MathEx.sqr(x[i][j] - condmu[yi][j]); } } for (int j = 0; j < p; j++) { bss[j] /= wss[j]; } return bss; }  @Override double[] rank(double[][] x, int[] y); static double[] of(double[][] x, int[] y); static final SumSquaresRatio instance; }