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

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

Introduction

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

Prototype

public void setRow(final int row, final double[] array)
        throws OutOfRangeException, MatrixDimensionMismatchException 

Source Link

Usage

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

/**
 * @param lst/*from  ww  w .  ja  v a  2s. c  o  m*/
 *            An ArrayList with all the values being vectors of the same
 *            dimension
 * @return The RealMatrix with the vectors from the ArrayList on columns
 */
Array2DRowRealMatrix ArrayToRealMatrix(ArrayList<float[]> lst, int size) {
    int length = lst.get(1).length;
    Array2DRowRealMatrix ret = new Array2DRowRealMatrix(size, length);
    int i = 0;
    for (i = 0; i < size; i++) {
        double[] converted = new double[length];
        for (int j = 0; j < length; j++)
            converted[j] = ((lst.get(i))[j]);
        ret.setRow(i, converted);
    }
    return ret;
}

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  ww w .  j  a  v  a 2  s .  c o  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:com.clust4j.algo.MeanShift.java

@Override
protected MeanShift fit() {
    synchronized (fitLock) {

        if (null != labels) // Already fit this model
            return this;

        // Put the results into a Map (hash because tree imposes comparable casting)
        final LogTimer timer = new LogTimer();
        centroids = new ArrayList<double[]>();

        /*/*from   ww  w  .ja  v  a 2 s.co  m*/
         * Get the neighborhoods and center intensity object. Will iterate until
         * either the centers are found, or the max try count is exceeded. For each
         * iteration, will increase bandwidth.
         */
        RadiusNeighbors nbrs = new RadiusNeighbors(this, bandwidth).fit();

        // Compute the seeds and center intensity
        // If parallelism is permitted, try it. 
        CenterIntensity intensity = null;
        if (parallel) {
            try {
                intensity = new ParallelCenterIntensity(nbrs);
            } catch (RejectedExecutionException e) {
                // Shouldn't happen...
                warn("parallel search failed; falling back to serial");
            }
        }

        // Gets here if serial or if parallel failed...
        if (null == intensity)
            intensity = new SerialCenterIntensity(nbrs);

        // Check for points all too far from seeds
        if (intensity.isEmpty()) {
            error(new IllegalClusterStateException("No point " + "was within bandwidth=" + bandwidth
                    + " of any seed; try increasing bandwidth"));
        } else {
            converged = true;
            itersElapsed = intensity.getIters(); // max iters elapsed
        }

        // Extract the centroids
        int idx = 0, m_prime = intensity.size();
        final Array2DRowRealMatrix sorted_centers = new Array2DRowRealMatrix(m_prime, n);

        for (MeanShiftSeed entry : intensity)
            sorted_centers.setRow(idx++, entry.getPair().getKey());

        // Fit the new neighbors model
        nbrs = new RadiusNeighbors(sorted_centers, new RadiusNeighborsParameters(bandwidth)
                .setSeed(this.random_state).setMetric(this.dist_metric).setForceParallel(parallel), true).fit();

        // Post-processing. Remove near duplicate seeds
        // If dist btwn two kernels is less than bandwidth, remove one w fewer pts
        // Create a boolean mask, init true
        final boolean[] unique = new boolean[m_prime];
        for (int i = 0; i < unique.length; i++)
            unique[i] = true;

        // Pre-filtered summaries...
        ArrayList<SummaryLite> allSummary = intensity.getSummaries();

        // Iterate over sorted centers and query radii
        int redundant_ct = 0;
        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...
        SummaryLite summ;
        for (int i = 0; i < unique.length; i++) {
            summ = allSummary.get(i);

            if (unique[i]) {
                summ.retained = true;
                centroids.add(sorted_centers.getRow(i));
            }

            fitSummary.add(summ.toArray());
        }

        // calc redundant ct
        redundant_ct = unique.length - centroids.size();

        // 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 centers = new Array2DRowRealMatrix(centroids.size(), n);
        for (int i = 0; i < centroids.size(); i++)
            centers.setRow(i, centroids.get(i));

        // Build yet another neighbors model...
        NearestNeighbors nn = new NearestNeighbors(centers, new NearestNeighborsParameters(1)
                .setSeed(this.random_state).setMetric(this.dist_metric).setForceParallel(false), true).fit();

        info((numClusters = centroids.size()) + " optimal kernel" + (numClusters != 1 ? "s" : "")
                + " identified");
        info(redundant_ct + " nearly-identical kernel" + (redundant_ct != 1 ? "s" : "") + " removed");

        // Get the nearest...
        final LogTimer clustTimer = new LogTimer();
        Neighborhood knrst = nn.getNeighbors(data.getDataRef());
        labels = MatUtils.flatten(knrst.getIndices());

        // order the labels..
        /* 
         * Reduce labels to a sorted, gapless, list
         * sklearn line: cluster_centers_indices = np.unique(labels)
         */
        ArrayList<Integer> centroidIndices = new ArrayList<Integer>(numClusters);
        for (Integer i : labels) // force autobox
            if (!centroidIndices.contains(i)) // Not race condition because synchronized
                centroidIndices.add(i);

        /*
         * final label assignment...
         * sklearn line: labels = np.searchsorted(cluster_centers_indices, labels)
         */
        for (int i = 0; i < labels.length; i++)
            labels[i] = centroidIndices.indexOf(labels[i]);

        // Wrap up...
        // Count missing
        numNoisey = 0;
        for (int lab : labels)
            if (lab == NOISE_CLASS)
                numNoisey++;
        info(numNoisey + " record" + (numNoisey != 1 ? "s" : "") + " classified noise");

        info("completed cluster labeling in " + clustTimer.toString());

        sayBye(timer);
        return this;
    }

}

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

private Array2DRowRealMatrix getReducedBusAdmittance() {
    int numOfBus = opfNet.getNoActiveBus();

    // form the reduced bus admittance matrix by omitting the row
    // corresponding to the SWING bus ;
    // here B1 formed by InterPSS itself is under consideration to be reused
    // ;/*from  ww w.  ja  va 2  s . c o  m*/

    Array2DRowRealMatrix tempBusAdm = new Array2DRowRealMatrix(numOfBus, numOfBus);

    for (Bus b : opfNet.getBusList()) {
        DclfOpfBus busi = (DclfOpfBus) b;
        int i = busi.getSortNumber();
        double Bii = 0;
        for (Branch bra : busi.getBranchList()) {
            //if (bra.isAclfBranch()) {
            DclfOpfBranch aclfBranch = (DclfOpfBranch) bra;
            Bus busj = bra.getToBus().getId().equals(busi.getId()) ? bra.getFromBus() : bra.getToBus();
            int j = busj.getSortNumber();
            double Bij = 1.0 / aclfBranch.getZ().getImaginary();// aclfBranch.b1ft();
            tempBusAdm.setEntry(i, j, -Bij);
            Bii += Bij;
            //}
        }
        tempBusAdm.setEntry(i, i, Bii);
    }

    Array2DRowRealMatrix busAdmReduced = new Array2DRowRealMatrix(numOfBus - 1, numOfBus); // reduced bus admittance matrix
    int[] selectedRows = this.getNonSwingBusRows();
    for (int index = 0; index < selectedRows.length; index++) {
        busAdmReduced.setRow(index, tempBusAdm.getRow(selectedRows[index]));
    }
    return busAdmReduced;
}

From source file:outlineDescriptor.EllipticalKernelGenerator.java

private Array2DRowRealMatrix generateBounds(Ellipse ellipse) {

    Array2DRowRealMatrix out;

    int xMax = (int) getXMax(ellipse);

    out = new Array2DRowRealMatrix(xMax * 2 + 1, 3);

    for (int i = -xMax; i <= xMax; i++) {

        double[] row = new double[3];

        double[] yCoords = getYAt(i, ellipse);

        row[0] = i;//from  w ww . j ava 2 s . c o m
        row[1] = yCoords[0];
        row[2] = yCoords[1];

        out.setRow(i + xMax, row);

    }

    return out;
}

From source file:xyz.lejon.sampling.FastMultivariateNormalDistribution.java

/**
 * Creates a multivariate normal distribution with the given mean vector and
 * covariance matrix.//  w w  w  . j  a v  a 2 s  .  co m
 * <br/>
 * The number of dimensions is equal to the length of the mean vector
 * and to the number of rows and columns of the covariance matrix.
 * It is frequently written as "p" in formulae.
 *
 * @param rng Random Number Generator.
 * @param means Vector of means.
 * @param covariances Covariance matrix.
 * @throws DimensionMismatchException if the arrays length are
 * inconsistent.
 * @throws SingularMatrixException if the eigenvalue decomposition cannot
 * be performed on the provided covariance matrix.
 * @throws NonPositiveDefiniteMatrixException if any of the eigenvalues is
 * negative.
 */
public FastMultivariateNormalDistribution(RandomGenerator rng, final double[] means,
        final double[][] covariances)
        throws SingularMatrixException, DimensionMismatchException, NonPositiveDefiniteMatrixException {
    super(rng, means.length);

    final int dim = means.length;

    if (covariances.length != dim) {
        throw new DimensionMismatchException(covariances.length, dim);
    }

    for (int i = 0; i < dim; i++) {
        if (dim != covariances[i].length) {
            throw new DimensionMismatchException(covariances[i].length, dim);
        }
    }

    this.means = MathArrays.copyOf(means);

    covarianceMatrix = new Array2DRowRealMatrix(covariances);

    // Covariance matrix eigen decomposition.
    final EigenDecomposition covMatDec = new EigenDecomposition(covarianceMatrix);

    // Compute and store the inverse.
    covarianceMatrixInverse = covMatDec.getSolver().getInverse();
    // Compute and store the determinant.
    covarianceMatrixDeterminant = covMatDec.getDeterminant();

    // Eigenvalues of the covariance matrix.
    final double[] covMatEigenvalues = covMatDec.getRealEigenvalues();

    for (int i = 0; i < covMatEigenvalues.length; i++) {
        if (covMatEigenvalues[i] < 0) {
            throw new NonPositiveDefiniteMatrixException(covMatEigenvalues[i], i, 0);
        }
    }

    // Matrix where each column is an eigenvector of the covariance matrix.
    final Array2DRowRealMatrix covMatEigenvectors = new Array2DRowRealMatrix(dim, dim);
    final Array2DRowRealMatrix tmpMatrix = new Array2DRowRealMatrix(dim, dim);
    for (int v = 0; v < dim; v++) {
        final double factor = FastMath.sqrt(covMatEigenvalues[v]);
        final double[] evec = covMatDec.getEigenvector(v).toArray();
        covMatEigenvectors.setColumn(v, evec);
        tmpMatrix.setRow(v, evec);
        for (int col = 0; col < dim; col++) {
            tmpMatrix.multiplyEntry(v, col, factor);
        }
    }

    samplingMatrix = covMatEigenvectors.multiply(tmpMatrix);
}