@Test public void test() { System.out.println("SignalNoiseRatio"); int[] y = new int[Iris.data.size()]; for (int i = 0; i < y.length; i++) { if (Iris.y[i] < 2) y[i] = 0; else y[i] = 1; } double[] ratio = SignalNoiseRatio.of(Iris.x, y); assertEquals(4, ratio.length); assertEquals(0.8743107, ratio[0], 1E-7); assertEquals(0.1502717, ratio[1], 1E-7); assertEquals(1.3446912, ratio[2], 1E-7); assertEquals(1.4757334, ratio[3], 1E-7); }
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); y = codec.y; if (codec.k != 2) { throw new IllegalArgumentException("SignalNoiseRatio is applicable only to binary class."); } int n1 = 0; for (int yi : y) { if (yi == 0) { n1++; } else if (yi != 1) { throw new IllegalArgumentException("Invalid class label: " + yi); } } int n = x.length; int n2 = n - n1; double[][] x1 = new double[n1][]; double[][] x2 = new double[n2][]; for (int i = 0, j = 0, k = 0; i < n; i++) { if (y[i] == 0) { x1[j++] = x[i]; } else { x2[k++] = x[i]; } } double[] mu1 = MathEx.colMeans(x1); double[] mu2 = MathEx.colMeans(x2); double[] sd1 = MathEx.colSds(x1); double[] sd2 = MathEx.colSds(x2); int p = mu1.length; double[] s2n = new double[p]; for (int i = 0; i < p; i++) { s2n[i] = Math.abs(mu1[i] - mu2[i]) / (sd1[i] + sd2[i]); } return s2n; }
SignalNoiseRatio 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); y = codec.y; if (codec.k != 2) { throw new IllegalArgumentException("SignalNoiseRatio is applicable only to binary class."); } int n1 = 0; for (int yi : y) { if (yi == 0) { n1++; } else if (yi != 1) { throw new IllegalArgumentException("Invalid class label: " + yi); } } int n = x.length; int n2 = n - n1; double[][] x1 = new double[n1][]; double[][] x2 = new double[n2][]; for (int i = 0, j = 0, k = 0; i < n; i++) { if (y[i] == 0) { x1[j++] = x[i]; } else { x2[k++] = x[i]; } } double[] mu1 = MathEx.colMeans(x1); double[] mu2 = MathEx.colMeans(x2); double[] sd1 = MathEx.colSds(x1); double[] sd2 = MathEx.colSds(x2); int p = mu1.length; double[] s2n = new double[p]; for (int i = 0; i < p; i++) { s2n[i] = Math.abs(mu1[i] - mu2[i]) / (sd1[i] + sd2[i]); } return s2n; } }
SignalNoiseRatio 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); y = codec.y; if (codec.k != 2) { throw new IllegalArgumentException("SignalNoiseRatio is applicable only to binary class."); } int n1 = 0; for (int yi : y) { if (yi == 0) { n1++; } else if (yi != 1) { throw new IllegalArgumentException("Invalid class label: " + yi); } } int n = x.length; int n2 = n - n1; double[][] x1 = new double[n1][]; double[][] x2 = new double[n2][]; for (int i = 0, j = 0, k = 0; i < n; i++) { if (y[i] == 0) { x1[j++] = x[i]; } else { x2[k++] = x[i]; } } double[] mu1 = MathEx.colMeans(x1); double[] mu2 = MathEx.colMeans(x2); double[] sd1 = MathEx.colSds(x1); double[] sd2 = MathEx.colSds(x2); int p = mu1.length; double[] s2n = new double[p]; for (int i = 0; i < p; i++) { s2n[i] = Math.abs(mu1[i] - mu2[i]) / (sd1[i] + sd2[i]); } return s2n; }  }
SignalNoiseRatio 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); y = codec.y; if (codec.k != 2) { throw new IllegalArgumentException("SignalNoiseRatio is applicable only to binary class."); } int n1 = 0; for (int yi : y) { if (yi == 0) { n1++; } else if (yi != 1) { throw new IllegalArgumentException("Invalid class label: " + yi); } } int n = x.length; int n2 = n - n1; double[][] x1 = new double[n1][]; double[][] x2 = new double[n2][]; for (int i = 0, j = 0, k = 0; i < n; i++) { if (y[i] == 0) { x1[j++] = x[i]; } else { x2[k++] = x[i]; } } double[] mu1 = MathEx.colMeans(x1); double[] mu2 = MathEx.colMeans(x2); double[] sd1 = MathEx.colSds(x1); double[] sd2 = MathEx.colSds(x2); int p = mu1.length; double[] s2n = new double[p]; for (int i = 0; i < p; i++) { s2n[i] = Math.abs(mu1[i] - mu2[i]) / (sd1[i] + sd2[i]); } return s2n; }  @Override double[] rank(double[][] x, int[] y); static double[] of(double[][] x, int[] y); }
SignalNoiseRatio 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); y = codec.y; if (codec.k != 2) { throw new IllegalArgumentException("SignalNoiseRatio is applicable only to binary class."); } int n1 = 0; for (int yi : y) { if (yi == 0) { n1++; } else if (yi != 1) { throw new IllegalArgumentException("Invalid class label: " + yi); } } int n = x.length; int n2 = n - n1; double[][] x1 = new double[n1][]; double[][] x2 = new double[n2][]; for (int i = 0, j = 0, k = 0; i < n; i++) { if (y[i] == 0) { x1[j++] = x[i]; } else { x2[k++] = x[i]; } } double[] mu1 = MathEx.colMeans(x1); double[] mu2 = MathEx.colMeans(x2); double[] sd1 = MathEx.colSds(x1); double[] sd2 = MathEx.colSds(x2); int p = mu1.length; double[] s2n = new double[p]; for (int i = 0; i < p; i++) { s2n[i] = Math.abs(mu1[i] - mu2[i]) / (sd1[i] + sd2[i]); } return s2n; }  @Override double[] rank(double[][] x, int[] y); static double[] of(double[][] x, int[] y); static final SignalNoiseRatio instance; }