Example usage for org.apache.commons.math3.linear Array2DRowRealMatrix getRowDimension

List of usage examples for org.apache.commons.math3.linear Array2DRowRealMatrix getRowDimension

Introduction

In this page you can find the example usage for org.apache.commons.math3.linear Array2DRowRealMatrix getRowDimension.

Prototype

@Override
public int getRowDimension() 

Source Link

Usage

From source file:edu.cmu.sphinx.speakerid.SpeakerIdentification.java

/**
 * /*from   w  w  w.  ja v a 2s. co  m*/
 * @param features
 *            Matrix with feature vectors as rows
 * @return A list with all changing points detected in the file
 */
private LinkedList<Integer> getAllChangingPoints(Array2DRowRealMatrix features) {
    LinkedList<Integer> ret = new LinkedList<Integer>();
    ret.add(0);
    int framesCount = features.getRowDimension(), step = 500;
    int start = 0, end = step, cp;
    while (end < framesCount) {
        cp = getPoint(start, end - start + 1, step / 10, features);
        if (cp > 0) {
            start = cp;
            end = start + step;
            ret.add(cp);
        } else
            end += step;
    }
    ret.add(framesCount);
    return ret;
}

From source file:edu.cmu.sphinx.speakerid.SpeakerIdentification.java

/**
 * // w  ww  .  j  a  va  2s  . c o m
 * @param bicValue
 *            The bicValue of the model represented by only one Gaussian.
 *            This parameter it's useful when this function is called
 *            repeatedly for different frame values and the same features
 *            parameter
 * @param frame
 *            the frame which is tested for being a change point
 * @param features
 *            the feature vectors matrix
 * @return the likelihood ratio
 */
double getLikelihoodRatio(double bicValue, int frame, Array2DRowRealMatrix features) {
    double bicValue1, bicValue2;
    int d = Segment.FEATURES_SIZE;
    double penalty = 0.5 * (d + 0.5 * d * (d + 1)) * Math.log(features.getRowDimension()) * 2;
    int nrows = features.getRowDimension(), ncols = features.getColumnDimension();
    Array2DRowRealMatrix sub1, sub2;
    sub1 = (Array2DRowRealMatrix) features.getSubMatrix(0, frame - 1, 0, ncols - 1);
    sub2 = (Array2DRowRealMatrix) features.getSubMatrix(frame, nrows - 1, 0, ncols - 1);
    bicValue1 = getBICValue(sub1);
    bicValue2 = getBICValue(sub2);
    return (bicValue - bicValue1 - bicValue2 - penalty);
}

From source file:edu.cmu.sphinx.speakerid.SpeakerIdentification.java

double computeDistance(SpeakerCluster c1, SpeakerCluster c2) {
    int rowDim = c1.getFeatureMatrix().getRowDimension() + c2.getFeatureMatrix().getRowDimension();
    int colDim = c1.getFeatureMatrix().getColumnDimension();
    Array2DRowRealMatrix combinedFeatures = new Array2DRowRealMatrix(rowDim, colDim);
    combinedFeatures.setSubMatrix(c1.getFeatureMatrix().getData(), 0, 0);
    combinedFeatures.setSubMatrix(c2.getFeatureMatrix().getData(), c1.getFeatureMatrix().getRowDimension(), 0);
    double bicValue = getBICValue(combinedFeatures);
    double d = Segment.FEATURES_SIZE;
    double penalty = 0.5 * (d + 0.5 * d * (d + 1)) * Math.log(combinedFeatures.getRowDimension()) * 2;
    return bicValue - c1.getBicValue() - c2.getBicValue() - penalty;
}

From source file:com.clust4j.algo.MeanShiftTests.java

@Test(expected = IllegalArgumentException.class)
public void testSeededIAE2() {
    Array2DRowRealMatrix iris = data_;

    new MeanShift(iris, new MeanShiftParameters()
            .setSeeds(MatUtils.randomGaussian(iris.getRowDimension() + 1, iris.getColumnDimension())));
}

From source file:com.clust4j.algo.MeanShiftTests.java

@Test
public void testAutoEstimationWithScale() {
    Array2DRowRealMatrix iris = (Array2DRowRealMatrix) new StandardScaler().fit(data_).transform(data_);
    final double[][] X = iris.getData();

    // MS estimates bw at 1.6041295821313855
    final double bandwidth = 1.6041295821313855;

    assertTrue(Precision.equals(//from www . j a v a 2s .  com
            MeanShift.autoEstimateBW(iris, 0.3, Distance.EUCLIDEAN, GlobalState.DEFAULT_RANDOM_STATE, false),
            bandwidth, 1e-9));

    assertTrue(Precision.equals(
            MeanShift.autoEstimateBW(iris, 0.3, Distance.EUCLIDEAN, GlobalState.DEFAULT_RANDOM_STATE, true),
            bandwidth, 1e-9));

    // Asserting fit works without breaking things...
    RadiusNeighbors r = new RadiusNeighbors(iris, new RadiusNeighborsParameters(bandwidth)).fit();

    TreeSet<MeanShiftSeed> centers = new TreeSet<>();
    for (double[] seed : X)
        centers.add(MeanShift.singleSeed(seed, r, X, 300));

    assertTrue(centers.size() == 4);

    double[][] expected_dists = new double[][] {
            new double[] { 0.50161528154395962, -0.31685274298813487, 0.65388162422893481,
                    0.65270450741975761 },
            new double[] { 0.52001211065400177, -0.29561728795619946, 0.67106269515983397,
                    0.67390853215763813 },
            new double[] { 0.54861244890482475, -0.25718786696105495, 0.68964559485632182,
                    0.69326664641211422 },
            new double[] { -1.0595457115461515, 0.74408909010240054, -1.2995708885010491,
                    -1.2545442961404225 } };

    int[] expected_centers = new int[] { 82, 80, 77, 45 };

    int idx = 0;
    for (MeanShiftSeed seed : centers) {
        assertTrue(VecUtils.equalsWithTolerance(seed.dists, expected_dists[idx], 1e-1));
        assertTrue(seed.count == expected_centers[idx]);
        idx++;
    }

    ArrayList<EntryPair<double[], Integer>> center_intensity = new ArrayList<>();
    for (MeanShiftSeed seed : centers) {
        if (null != seed) {
            center_intensity.add(seed.getPair());
        }
    }

    final ArrayList<EntryPair<double[], Integer>> sorted_by_intensity = center_intensity;

    // test getting the unique vals
    idx = 0;
    final int m_prime = sorted_by_intensity.size();
    final Array2DRowRealMatrix sorted_centers = new Array2DRowRealMatrix(m_prime, iris.getColumnDimension());
    for (Map.Entry<double[], Integer> e : sorted_by_intensity)
        sorted_centers.setRow(idx++, e.getKey());

    // Create a boolean mask, init true
    final boolean[] unique = new boolean[m_prime];
    for (int i = 0; i < unique.length; i++)
        unique[i] = true;

    // Fit the new neighbors model
    RadiusNeighbors nbrs = new RadiusNeighbors(sorted_centers,
            new RadiusNeighborsParameters(bandwidth).setVerbose(false)).fit();

    // Iterate over sorted centers and query radii
    int[] indcs;
    double[] center;
    for (int i = 0; i < m_prime; i++) {
        if (unique[i]) {
            center = sorted_centers.getRow(i);
            indcs = nbrs.getNeighbors(new double[][] { center }, bandwidth, false).getIndices()[0];

            for (int id : indcs) {
                unique[id] = false;
            }

            unique[i] = true; // Keep this as true
        }
    }

    // Now assign the centroids...
    int redundant_ct = 0;
    final ArrayList<double[]> centroids = new ArrayList<>();
    for (int i = 0; i < unique.length; i++) {
        if (unique[i]) {
            centroids.add(sorted_centers.getRow(i));
        }
    }

    redundant_ct = unique.length - centroids.size();

    assertTrue(redundant_ct == 2);
    assertTrue(centroids.size() == 2);
    assertTrue(VecUtils.equalsWithTolerance(centroids.get(0),
            new double[] { 0.4999404345258691, -0.3157948009929614, 0.6516983739795399, 0.6505251874544873 },
            1e-6));

    assertTrue(VecUtils.equalsExactly(centroids.get(1),
            new double[] { -1.0560079864392702, 0.7416046454700266, -1.295231741534238, -1.2503554887998656 }));

    // also put the centroids into a matrix. We have to
    // wait to perform this op, because we have to know
    // the size of centroids first...
    Array2DRowRealMatrix clust_centers = new Array2DRowRealMatrix(centroids.size(), iris.getColumnDimension());
    for (int i = 0; i < clust_centers.getRowDimension(); i++)
        clust_centers.setRow(i, centroids.get(i));

    // The final nearest neighbors model -- if this works, we are in the clear...
    new NearestNeighbors(clust_centers, new NearestNeighborsParameters(1)).fit();
}

From source file:com.clust4j.data.DataSet.java

public DataSet(Array2DRowRealMatrix data, int[] labels, String[] hdrz, MatrixFormatter formatter,
        boolean copyData) {

    /*// we should allow this behavior...
    if(null == labels)// w w  w.  j av a 2 s  . c  o  m
       throw new IllegalArgumentException("labels cannot be null");
    */

    if (null == data)
        throw new IllegalArgumentException("data cannot be null");
    if (null == hdrz)
        this.headers = genHeaders(data.getColumnDimension());
    else
        this.headers = VecUtils.copy(hdrz);

    // Check to make sure dims match up...
    if ((null != labels) && labels.length != data.getRowDimension())
        throw new DimensionMismatchException(labels.length, data.getRowDimension());
    if (this.headers.length != data.getColumnDimension())
        throw new DimensionMismatchException(this.headers.length, data.getColumnDimension());

    this.data = copyData ? (Array2DRowRealMatrix) data.copy() : data;
    this.labels = VecUtils.copy(labels);
    this.formatter = null == formatter ? DEF_FORMATTER : formatter;
}

From source file:com.clust4j.algo.pipeline.PipelineTest.java

@Test
public void testUnsupervisedFitToPredict() {
    DataSet data = TestSuite.IRIS_DATASET.shuffle();

    Array2DRowRealMatrix training = data.getData(); // all 150
    Array2DRowRealMatrix holdout = new Array2DRowRealMatrix(MatUtils.slice(training.getData(), 0, 50), false); // just take the first 50

    /*//  w w  w .  ja va2s .  c  o m
     * Initialize pipe
     */
    UnsupervisedPipeline<KMeans> pipeline = new UnsupervisedPipeline<KMeans>(
            new KMeansParameters(3).setVerbose(true).setMetric(new GaussianKernel()), new StandardScaler(),
            new MinMaxScaler());

    /*
     * Pre-fit, test that we throw exceptions if called too early
     */
    boolean a = false;
    try {
        pipeline.getLabels();
    } catch (ModelNotFitException m) {
        a = true;
    } finally {
        assertTrue(a);
    }

    /*
     * Fit the pipe
     */
    pipeline.fit(training);
    System.out.println("Silhouette: " + pipeline.silhouetteScore());
    System.out.println("Affinity:   " + pipeline.indexAffinityScore(data.getLabels()));

    // let's get predictions...
    int[] fit_labels = VecUtils.slice(pipeline.getLabels(), 0, holdout.getRowDimension()); // only first 50!!
    int[] predicted_labels = pipeline.predict(holdout);

    // let's examine the affinity of the fit, and the predicted:
    double affinity = SupervisedMetric.INDEX_AFFINITY.evaluate(fit_labels, predicted_labels);
    System.out.println("Predicted affinity: " + affinity);
}

From source file:com.clust4j.algo.pipeline.PipelineTest.java

@Test
public void testSupervisedFitToPredict() {
    DataSet data = TestSuite.BC_DATASET.shuffle();

    Array2DRowRealMatrix training = data.getData(); // all 300+
    Array2DRowRealMatrix holdout = new Array2DRowRealMatrix(MatUtils.slice(training.getData(), 0, 50), false); // just take the first 50

    /*/*from  w  w w  .ja v  a  2  s.c  o m*/
     * Initialize pipe
     */
    SupervisedPipeline<NearestCentroid> pipeline = new SupervisedPipeline<NearestCentroid>(
            new NearestCentroidParameters().setVerbose(true).setMetric(new GaussianKernel()),
            new StandardScaler(), new MinMaxScaler());

    /*
     * Pre-fit, test that we throw exceptions if called too early
     */
    boolean a = false;
    try {
        pipeline.getLabels();
    } catch (ModelNotFitException m) {
        a = true;
    } finally {
        assertTrue(a);
    }

    /*
     * Fit the pipe
     */
    pipeline.fit(training, data.getLabels());
    System.out.println("Default score: " + pipeline.score());
    System.out.println("Metric score:  " + pipeline.score(BINOMIAL_ACCURACY));
    assertNotNull(pipeline.getTrainingLabels());
    assertNotNull(pipeline.getLabels());

    // let's get predictions...
    int[] fit_labels = VecUtils.slice(pipeline.getTrainingLabels(), 0, holdout.getRowDimension()); // only first 50!!
    int[] predicted_labels = pipeline.predict(holdout);

    // let's examine the accuracy of the fit, and the predicted:
    System.out.println("Predicted accuracy: " + BINOMIAL_ACCURACY.evaluate(fit_labels, predicted_labels));
}

From source file:com.clust4j.algo.HDBSCANTests.java

@Test
public void testMutualReachability() {
    Array2DRowRealMatrix X = new Array2DRowRealMatrix(
            MatUtils.reshape(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, 3, 3), false);

    final double[][] dist = Pairwise.getDistance(X, Distance.EUCLIDEAN, false, false);

    // first test the partition by row
    final int minPts = FastMath.min(X.getRowDimension() - 1, 5);
    final double[] core_distances = MatUtils.sortColsAsc(dist)[minPts];
    assertTrue(VecUtils.equalsExactly(core_distances,
            new double[] { 10.392304845413264, 5.196152422706632, 10.392304845413264 }));

    final double[][] expected = new double[][] {
            new double[] { 10.392304845413264, 10.392304845413264, 10.392304845413264 },
            new double[] { 10.392304845413264, 5.196152422706632, 10.392304845413264 },
            new double[] { 10.392304845413264, 10.392304845413264, 10.392304845413264 }, };

    double[][] mr = HDBSCAN.LinkageTreeUtils.mutualReachability(dist, minPts, 1.0);
    assertTrue(MatUtils.equalsExactly(mr, expected));
}

From source file:fp.overlapr.algorithmen.StressMajorization.java

/**
 * Fhrt die Stress-Majorization durch. siehe: Gansner, Koren, North: Graph
 * Drawing by Stress Majorization, 2004//from w  w  w.  ja  va 2 s .co  m
 * 
 * @param graph
 *            Graph, dessen Knoten-Positionen neu berechnet werden sollen
 * @param d
 *            Matrix, die die idealen Distanzen (d_ij) zwischen den Knoten
 *            enthlt
 * @return Matrix, die die neuen x- und y-Werte der einzelnen Knoten enthlt
 */
public static double[][] doStressMajorization(Graph graph, double[][] d) {

    int iter = 0;

    // X holen
    Array2DRowRealMatrix X = new Array2DRowRealMatrix(graph.getKnotenAnzahl(), 2);
    for (int i = 0; i < graph.getKnotenAnzahl(); i++) {
        X.setEntry(i, 0, graph.getKnoten().get(i).getX());
        X.setEntry(i, 1, graph.getKnoten().get(i).getY());
    }

    // D holen
    Array2DRowRealMatrix D = new Array2DRowRealMatrix(d);

    // W berechnen
    Array2DRowRealMatrix W = new Array2DRowRealMatrix(D.getRowDimension(), D.getColumnDimension());
    W.walkInRowOrder(new DefaultRealMatrixChangingVisitor() {
        @Override
        public double visit(int row, int column, double value) {
            if (D.getEntry(row, column) == 0) {
                return 0.0;
            } else {
                return 1.0 / (D.getEntry(row, column) * D.getEntry(row, column));
            }
        }
    });

    // LW berechnen
    Array2DRowRealMatrix LW = new Array2DRowRealMatrix(D.getRowDimension(), D.getColumnDimension());
    LW.walkInRowOrder(new DefaultRealMatrixChangingVisitor() {
        @Override
        public double visit(int row, int column, double value) {
            if (row != column) {
                return (-1) * W.getEntry(row, column);
            } else {

                return value;
            }
        }
    });
    LW.walkInRowOrder(new DefaultRealMatrixChangingVisitor() {
        @Override
        public double visit(int row, int column, double value) {
            if (row == column) {

                double sum = 0;

                for (int k = 0; k < LW.getColumnDimension(); k++) {
                    if (k != row) {
                        sum = sum + W.getEntry(row, k);
                    }
                }

                return sum;
            } else {

                return value;
            }
        }
    });

    double[][] x = null;

    while (iter < ITER) {

        iter++;

        // LX berechnen
        Array2DRowRealMatrix LX = new Array2DRowRealMatrix(D.getRowDimension(), D.getColumnDimension());
        LX.walkInRowOrder(new DefaultRealMatrixChangingVisitor() {
            @Override
            public double visit(int row, int column, double value) {
                if (row != column) {

                    // norm 2
                    double term1 = FastMath.pow((X.getEntry(row, 0) - X.getEntry(column, 0)), 2);
                    double term2 = FastMath.pow((X.getEntry(row, 1) - X.getEntry(column, 1)), 2);

                    double abst = Math.sqrt(term1 + term2);

                    return (-1) * W.getEntry(row, column) * D.getEntry(row, column) * inv(abst);
                } else {
                    return value;
                }
            }
        });
        LX.walkInRowOrder(new DefaultRealMatrixChangingVisitor() {
            @Override
            public double visit(int row, int column, double value) {
                if (row == column) {

                    double sum = 0;

                    for (int k = 0; k < LX.getColumnDimension(); k++) {
                        if (k != row) {
                            sum = sum + LX.getEntry(row, k);
                        }
                    }
                    return (-1) * sum;
                } else {
                    return value;
                }
            }
        });

        /*
         * Lineare Gleichungssysteme lsen
         */
        // x-Werte holen
        ArrayRealVector xWerte = new ArrayRealVector(X.getColumn(0));

        // y-Werte holen
        ArrayRealVector yWerte = new ArrayRealVector(X.getColumn(1));

        // b_x berechnen
        ArrayRealVector b_x = (ArrayRealVector) LX.operate(xWerte);

        // b_y berechnen
        ArrayRealVector b_y = (ArrayRealVector) LX.operate(yWerte);

        /*
         * CG-Verfahren anwenden
         */
        // neue x-Werte berechnen mittels PCG
        // xWerte = conjugateGradientsMethod(LW, b_x, xWerte);

        // neue y-Werte berechnen mittels PCG
        // yWerte = conjugateGradientsMethod(LW, b_y, yWerte);

        ConjugateGradient cg = new ConjugateGradient(Integer.MAX_VALUE, TOL, false);
        xWerte = (ArrayRealVector) cg.solve(LW, JacobiPreconditioner.create(LW), b_x);
        yWerte = (ArrayRealVector) cg.solve(LW, JacobiPreconditioner.create(LW), b_y);

        /*
         * neue Positiones-Werte zurckgeben
         */
        x = new double[X.getRowDimension()][2];
        for (int i = 0; i < x.length; i++) {

            x[i][0] = xWerte.getEntry(i);
            x[i][1] = yWerte.getEntry(i);

            X.setEntry(i, 0, xWerte.getEntry(i));
            X.setEntry(i, 1, yWerte.getEntry(i));

        }
    }

    return x;
}