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

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

Introduction

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

Prototype

@Override
public double[][] getData() 

Source Link

Usage

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

public SpeakerCluster(Segment s, Array2DRowRealMatrix featureMatrix, double bicValue) {
    this.segmentSet = new TreeSet<Segment>();
    this.featureMatrix = new Array2DRowRealMatrix(featureMatrix.getData());
    this.bicValue = bicValue;
    addSegment(s);/* w ww.  j a  v a 2 s  .  com*/
}

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

public void mergeWith(SpeakerCluster target) throws NullPointerException {
    if (target == null)
        throw new NullPointerException();
    Iterator<Segment> it = target.segmentSet.iterator();
    while (it.hasNext()) {
        if (!this.addSegment(it.next()))
            System.out.println("Something doesn't work in mergeWith method, Cluster class");
    }/*w ww . j a v a  2  s  .com*/
    int rowDim = featureMatrix.getRowDimension() + target.getFeatureMatrix().getRowDimension();
    int colDim = featureMatrix.getColumnDimension();
    Array2DRowRealMatrix combinedFeatures = new Array2DRowRealMatrix(rowDim, colDim);
    combinedFeatures.setSubMatrix(featureMatrix.getData(), 0, 0);
    combinedFeatures.setSubMatrix(target.getFeatureMatrix().getData(), featureMatrix.getRowDimension(), 0);
    bicValue = SpeakerIdentification.getBICValue(combinedFeatures);
    featureMatrix = new Array2DRowRealMatrix(combinedFeatures.getData());
}

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  .  j  a v  a 2  s .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   ww w  .  j  a v a  2s .  com*/
     * 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.MeanShiftTests.java

@Test
public void testAutoEstimation() {
    Array2DRowRealMatrix iris = data_;
    final double[][] X = iris.getData();

    // MS estimates bw at 1.2032034114912584
    final double bandwidth = 1.2032034114912584;
    assertTrue(MeanShift.autoEstimateBW(iris, 0.3, Distance.EUCLIDEAN, GlobalState.DEFAULT_RANDOM_STATE,
            false) == bandwidth);/*from   w  ww .ja  v a  2  s.  co m*/

    // 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() == 7);

    double[][] expected_dists = new double[][] {
            new double[] { 6.2114285714285691, 2.8928571428571428, 4.8528571428571423, 1.6728571428571426 },
            new double[] { 6.1927536231884037, 2.8768115942028984, 4.8188405797101437, 1.6463768115942023 },
            new double[] { 6.1521739130434767, 2.850724637681159, 4.7405797101449272, 1.6072463768115937 },
            new double[] { 6.1852941176470564, 2.8705882352941177, 4.8058823529411754, 1.6397058823529407 },
            new double[] { 6.1727272727272711, 2.874242424242424, 4.7757575757575745, 1.6287878787878785 },
            new double[] { 5.0163265306122451, 3.440816326530614, 1.46734693877551, 0.24285714285714283 },
            new double[] { 5.0020833333333341, 3.4208333333333356, 1.4666666666666668, 0.23958333333333334 } };

    int[] expected_centers = new int[] { 70, 69, 69, 68, 66, 49, 48 };

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

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   w  ww.ja  va2  s.  co m
            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:org.apache.hadoop.hive.ql.abm.simulation.MCSimNode.java

private Array2DRowRealMatrix concat(Array2DRowRealMatrix A, Array2DRowRealMatrix B, Array2DRowRealMatrix C,
        Array2DRowRealMatrix D) {
    int dim = A.getRowDimension() + D.getRowDimension();
    Array2DRowRealMatrix ret = new Array2DRowRealMatrix(dim, dim);

    ret.setSubMatrix(A.getData(), 0, 0);
    ret.setSubMatrix(B.getData(), 0, A.getColumnDimension());
    ret.setSubMatrix(C.getData(), A.getRowDimension(), 0);
    ret.setSubMatrix(D.getData(), A.getRowDimension(), A.getColumnDimension());

    return ret;/*from   w  ww .ja va2  s. com*/
}

From source file:org.apache.solr.client.solrj.io.eval.TransposeEvaluator.java

@Override
public Object doWork(Object value) throws IOException {
    if (null == value) {
        return null;
    } else if (value instanceof Matrix) {
        Matrix matrix = (Matrix) value;//from www . ja  va  2 s  .c  o m
        double[][] data = matrix.getData();
        Array2DRowRealMatrix amatrix = new Array2DRowRealMatrix(data);
        Array2DRowRealMatrix tmatrix = (Array2DRowRealMatrix) amatrix.transpose();
        return new Matrix(tmatrix.getData());
    } else {
        throw new IOException("matrix parameter expected for transpose function");
    }
}

From source file:org.interpss.opf.dc.impl.EqIneqMatrixBuilder.java

public Array2DRowRealMatrix formCeq() {
    int numOfBus = opfNet.getNoActiveBus();
    int numOfGen = opfNet.getNoOfGen();

    Array2DRowRealMatrix genBusAdjacent = new Array2DRowRealMatrix(numOfBus, numOfGen);
    int genCount = 0;
    for (Bus b : opfNet.getBusList()) {
        // set the elements in genBusAdjacent matrix to ONE if there is a
        // generator ;
        /*//from   w  w  w .  j av  a 2 s .  c om
         * in the case of multi-gen ,all are treated as a union;
         * not suitable for a case with multi-generators connected to one
         * bus;
         */
        if (opfNet.isOpfGenBus(b)) {
            genBusAdjacent.setEntry(b.getSortNumber(), genCount, 1);
            genCount++;
        }
    }
    Array2DRowRealMatrix BrTranspose = (Array2DRowRealMatrix) this.getReducedBusAdmittance().transpose();

    Array2DRowRealMatrix CeqTranspose = new Array2DRowRealMatrix(numOfBus, numOfBus + numOfGen - 1);
    CeqTranspose.setSubMatrix(genBusAdjacent.getData(), 0, 0); // Ceq'={genBusAdjacent,BrTranspose}
    CeqTranspose.setSubMatrix(BrTranspose.scalarMultiply(-1).getData(), 0, numOfGen);
    return (Array2DRowRealMatrix) CeqTranspose.transpose();
}

From source file:org.interpss.opf.dc.impl.EqIneqMatrixBuilder.java

public Array2DRowRealMatrix formCiq() {
    int numOfBranch = opfNet.getNoActiveBranch();
    int numOfBus = opfNet.getNoActiveBus();
    int numOfGen = opfNet.getNoOfGen();

    double bij = DEFAULT_BIJ;

    int braIndex = 0;
    Array2DRowRealMatrix braAdmDiag = new Array2DRowRealMatrix(numOfBranch, numOfBranch);
    Array2DRowRealMatrix braBusAdjacent = new Array2DRowRealMatrix(numOfBranch, numOfBus - 1);
    int swingIndex = this.getSwingBusIndex();
    for (Branch bra : opfNet.getBranchList()) {
        //if (bra.isAclfBranch()) {
        DclfOpfBranch aclfBra = (DclfOpfBranch) bra;
        // create branch admittance matrix
        bij = (aclfBra.getZ().getImaginary() > 0.00001) ? 1 / aclfBra.getZ().getImaginary() : DEFAULT_BIJ; // in case x=0;
        braAdmDiag.setEntry(braIndex, braIndex, bij);

        // create branch-bus connected or adjacent matrix;
        if (!((DclfOpfBus) bra.getFromBus()).isSwing()) {
            int fromBusIndex = bra.getFromBus().getSortNumber();
            fromBusIndex = (fromBusIndex < swingIndex) ? fromBusIndex : fromBusIndex - 1; // insure nonswing bus;
            braBusAdjacent.setEntry(braIndex, fromBusIndex, 1);
        }/* w w w  .  ja  v a  2s. co m*/
        if (!((DclfOpfBus) bra.getToBus()).isSwing()) {
            int toBusIndex = bra.getToBus().getSortNumber();
            toBusIndex = (toBusIndex < swingIndex) ? toBusIndex : toBusIndex - 1;
            braBusAdjacent.setEntry(braIndex, toBusIndex, -1);
        }
        braIndex++;
        //}
    }

    int genIndex = 0;
    Array2DRowRealMatrix eyeGenP = new Array2DRowRealMatrix(numOfGen, numOfGen);
    for (Bus bus : opfNet.getBusList()) {
        if (opfNet.isOpfGenBus(bus)) {
            eyeGenP.setEntry(genIndex, genIndex, 1);
            genIndex++;
        }
    }
    Array2DRowRealMatrix DAr = braAdmDiag.multiply(braBusAdjacent);
    // Ciq'={Ot,-braAdmDiag*braBusAdjacent;
    //     -Ot,braAdmDiag*braBusAdjacent;
    //      eyeGenP,Op;
    //      -eyeGenP,-Op}

    Array2DRowRealMatrix CiqTranspose = new Array2DRowRealMatrix(numOfBranch * 2 + numOfGen * 2,
            numOfGen + numOfBus - 1);
    CiqTranspose.setSubMatrix(DAr.scalarMultiply(-1).getData(), 0, numOfGen);
    CiqTranspose.setSubMatrix(DAr.getData(), numOfBranch, numOfGen);
    CiqTranspose.setSubMatrix(eyeGenP.getData(), 2 * numOfBranch, 0);
    CiqTranspose.setSubMatrix(eyeGenP.scalarMultiply(-1).getData(), 2 * numOfBranch + numOfGen, 0);
    // Ciq=CiqTranspose'
    return (Array2DRowRealMatrix) CiqTranspose.transpose();
}