@Test public void test() { System.out.println("SumSquaresRatio"); double[] ratio = SumSquaresRatio.of(Iris.x, Iris.y); assertEquals(4, ratio.length); assertEquals( 1.6226463, ratio[0], 1E-6); assertEquals( 0.6444144, ratio[1], 1E-6); assertEquals(16.0412833, ratio[2], 1E-6); assertEquals(13.0520327, ratio[3], 1E-6); }
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; }