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

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

Introduction

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

Prototype

public Array2DRowRealMatrix(final double[][] d, final boolean copyArray)
        throws DimensionMismatchException, NoDataException, NullArgumentException 

Source Link

Document

Create a new RealMatrix using the input array as the underlying data array.

Usage

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

public double[] dropCol(int idx) {
    double[] res;
    if (idx >= numCols() || idx < 0)
        throw new IllegalArgumentException("illegal column index: " + idx);

    final int m = numRows(), n = numCols();
    final double[][] dataRef = data.getDataRef();

    // We know idx won't throw exception
    res = data.getColumn(idx);//from  ww  w  .j a va2s  .  c o  m

    if (n == 1) {
        throw new IllegalStateException("cannot drop last column");
    } else {
        double[][] newData = new double[m][n - 1];
        String[] newHeader = new String[n - 1];

        for (int i = 0; i < m; i++) {
            int k = 0;
            for (int j = 0; j < n; j++) {
                if (j == idx)
                    continue;
                else {
                    if (i == 0) // On first iter, also reassign headers
                        newHeader[k] = headers[j];
                    newData[i][k] = dataRef[i][j];
                    k++;
                }
            }
        }

        data = new Array2DRowRealMatrix(newData, false);
        headers = newHeader;
    }

    return res;
}

From source file:gamlss.utilities.MatrixFunctions.java

/**
* <p>Compute the "hat" matrix.//from  ww w  .j  a va 2 s.co  m
* </p>
* <p>The hat matrix is defined in terms of the design matrix X
*  by X(X<sup>T</sup>X)<sup>-1</sup>X<sup>T</sup>
* </p>
* <p>The implementation here uses the QR decomposition to compute the
* hat matrix as Q I<sub>p</sub>Q<sup>T</sup> where I<sub>p</sub> is the
* p-dimensional identity matrix augmented by 0's.  This computational
* formula is from "The Hat Matrix in Regression and ANOVA",
* David C. Hoaglin and Roy E. Welsch,
* <i>The American Statistician</i>, Vol. 32, No. 1 (Feb., 1978), pp. 17-22.
*@param m - matrix
* @return the hat matrix
*/
public static RealMatrix calculateHat(final BlockRealMatrix m) {
    QRDecomposition qr = new QRDecomposition(m);
    // Create augmented identity matrix
    RealMatrix qM = qr.getQ();
    final int p = qr.getR().getColumnDimension();
    final int n = qM.getColumnDimension();
    Array2DRowRealMatrix augI = new Array2DRowRealMatrix(n, n);
    double[][] augIData = augI.getDataRef();
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (i == j && i < p) {
                augIData[i][j] = 1d;
            } else {
                augIData[i][j] = 0d;
            }
        }
    }
    // Compute and return Hat matrix
    return qM.multiply(augI).multiply(qM.transpose());
}

From source file:com.itemanalysis.psychometrics.mixture.MvNormalMixtureModel.java

private void setCommonCovariance(RealMatrix[] cov) {
    RealMatrix tempS = new Array2DRowRealMatrix(dimensions, dimensions);
    for (int g = 0; g < groups; g++) {
        tempS = tempS.add(cov[g].scalarMultiply(compDistribution[g].getMixingProportion()));
    }//w  w w.  j  a v  a 2  s  .c om
    for (int g = 0; g < groups; g++) {
        cov[g] = tempS;
    }
}

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  va 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.preprocess.PCA.java

@Override
public PCA fit(RealMatrix X) {
    synchronized (fitLock) {
        this.centerer = new MeanCenterer().fit(X);
        this.m = X.getRowDimension();
        this.n = X.getColumnDimension();

        // ensure n_components not too large
        if (this.n_components > n)
            this.n_components = n;

        final RealMatrix data = this.centerer.transform(X);
        SingularValueDecomposition svd = new SingularValueDecomposition(data);
        RealMatrix U = svd.getU(), S = svd.getS(), V = svd.getV().transpose();

        // flip Eigenvectors' sign to enforce deterministic output
        EntryPair<RealMatrix, RealMatrix> uv_sign_swap = eigenSignFlip(U, V);

        U = uv_sign_swap.getKey();//from ww  w .j  a  v  a  2  s.c o  m
        V = uv_sign_swap.getValue();
        RealMatrix components_ = V;

        // get variance explained by singular value
        final double[] s = MatUtils.diagFromSquare(S.getData());
        this.variabilities = new double[s.length];
        for (int i = 0; i < s.length; i++) {
            variabilities[i] = (s[i] * s[i]) / (double) m;
            total_var += variabilities[i];
        }

        // get variability ratio
        this.variability_ratio = new double[s.length];
        for (int i = 0; i < s.length; i++) {
            variability_ratio[i] = variabilities[i] / total_var;
        }

        // post-process number of components if in var_mode
        double[] ratio_cumsum = VecUtils.cumsum(variability_ratio);
        if (this.var_mode) {
            for (int i = 0; i < ratio_cumsum.length; i++) {
                if (ratio_cumsum[i] >= this.variability) {
                    this.n_components = i + 1;
                    break;
                }

                // if it never hits the if block, the n_components is
                // equal to the number of columns in its entirety
            }
        }

        // get noise variance
        if (n_components < FastMath.min(n, m)) {
            this.noise_variance = VecUtils.mean(VecUtils.slice(variabilities, n_components, s.length));
        } else {
            this.noise_variance = 0.0;
        }

        // Set the components and other sliced variables
        this.components = new Array2DRowRealMatrix(MatUtils.slice(components_.getData(), 0, n_components),
                false);
        this.variabilities = VecUtils.slice(variabilities, 0, n_components);
        this.variability_ratio = VecUtils.slice(variability_ratio, 0, n_components);

        if (retain) {
            this.U = new Array2DRowRealMatrix(MatUtils.slice(U.getData(), 0, n_components), false);
            ;
            this.S = new Array2DRowRealMatrix(MatUtils.slice(S.getData(), 0, n_components), false);
            ;
        }

        return this;
    }
}

From source file:com.clust4j.algo.preprocess.PreProcessorTests.java

@Test
public void testCenterScale() {
    final double[][] data = new double[][] { new double[] { 0.005, 0.182751, 0.1284 },
            new double[] { 3.65816, 0.29518, 2.123316 }, new double[] { 4.1234, 0.27395, 1.8900002 } };

    final Array2DRowRealMatrix d = new Array2DRowRealMatrix(data, false);
    final StandardScaler norm = new StandardScaler().fit(d);
    final RealMatrix normed = norm.transform(d);
    final double[][] operated = normed.getData();

    assertTrue(Precision.equals(VecUtils.mean(MatUtils.getColumn(operated, 0)), 0, 1e-12));
    assertTrue(Precision.equals(VecUtils.mean(MatUtils.getColumn(operated, 1)), 0, 1e-12));
    assertTrue(Precision.equals(VecUtils.mean(MatUtils.getColumn(operated, 2)), 0, 1e-12));

    assertTrue(Precision.equals(VecUtils.stdDev(MatUtils.getColumn(operated, 0)), 1, 1e-12));
    assertTrue(Precision.equals(VecUtils.stdDev(MatUtils.getColumn(operated, 1)), 1, 1e-12));
    assertTrue(Precision.equals(VecUtils.stdDev(MatUtils.getColumn(operated, 2)), 1, 1e-12));

    // test inverse transform
    assertTrue(MatUtils.equalsWithTolerance(data, norm.inverseTransform(normed).getData(), 1e-8));

    // test dim mismatch
    boolean a = false;
    try {/*from w  ww  .j  a v a2 s .  c  o m*/
        norm.inverseTransform(TestSuite.getRandom(2, 2));
    } catch (DimensionMismatchException dim) {
        a = true;
    } finally {
        assertTrue(a);
    }

    // assert that fewer than two features will throw exception
    a = false;
    try {
        norm.fit(TestSuite.getRandom(1, 4));
    } catch (IllegalArgumentException i) {
        a = true;
    } finally {
        assertTrue(a);
    }
}

From source file:edu.oregonstate.eecs.mcplan.ml.GaussianMixtureModel.java

@Override
public void run() {
    init();//from  ww  w . j  ava 2  s .  co m
    System.out.println("Init");
    for (int i = 0; i < mu().length; ++i) {
        System.out.println("Mu " + i + ": " + mu()[i]);
        System.out.println("Sigma " + i + ": " + Sigma()[i]);
    }

    int iterations = 0;
    while (!converged_ && iterations++ < max_iterations_) {
        // Expectation
        makeMixture();
        for (int i = 0; i < n_; ++i) {
            for (int j = 0; j < k_; ++j) {
                c_[i][j] = posterior(data_[i], j);
            }
            Fn.normalize_inplace(c_[i]);
        }

        // Maximization
        for (int j = 0; j < k_; ++j) {
            double Z = 0.0;
            final RealVector mu_j = new ArrayRealVector(d_);
            RealMatrix Sigma_j = new Array2DRowRealMatrix(d_, d_);
            for (int i = 0; i < n_; ++i) {
                final double c_ij = c_[i][j];
                Z += c_ij;
                final RealVector x_i = new ArrayRealVector(data_[i]);
                // mu_j += c_ij * x_i
                mu_j.combineToSelf(1.0, 1.0, x_i.mapMultiply(c_ij));
                final RealVector v = x_i.subtract(mu_[j]);
                // Sigma_j += c_ij * |v><v|
                Sigma_j = Sigma_j.add(v.outerProduct(v).scalarMultiply(c_ij));
            }
            final double Zinv = 1.0 / Z;
            final double pi_j = Z / n_;
            mu_j.mapMultiplyToSelf(Zinv);
            Sigma_j = Sigma_j.scalarMultiply(Zinv);
            //            converged &= hasConverged( j, pi_j, mu_j, Sigma_j );
            pi_[j] = pi_j;
            mu_[j] = mu_j;
            Sigma_[j] = Sigma_j;
        }
        //         debug();

        final double log_likelihood = logLikelihood();
        if (Math.abs(log_likelihood - old_log_likelihood_) < epsilon_) {
            converged_ = true;
        }
        old_log_likelihood_ = log_likelihood;
    }
}

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

/**
 * Asser that when all of the matrix entries are exactly the same,
 * the algorithm will still converge, yet produce one label: 0
 */// w w  w . j  a  va  2  s. c  o  m
@Override
@Test
public void testAllSame() {
    final double[][] x = MatUtils.rep(-1, 3, 3);
    final Array2DRowRealMatrix X = new Array2DRowRealMatrix(x, false);

    int[] labels = new HierarchicalAgglomerative(X,
            new HierarchicalAgglomerativeParameters(Linkage.AVERAGE).setVerbose(true)).fit().getLabels();
    assertTrue(new VecUtils.IntSeries(labels, Inequality.EQUAL_TO, 0).all());

    labels = new HierarchicalAgglomerative(X,
            new HierarchicalAgglomerativeParameters(Linkage.COMPLETE).setVerbose(true)).fit().getLabels();
    assertTrue(new VecUtils.IntSeries(labels, Inequality.EQUAL_TO, 0).all());

    labels = new HierarchicalAgglomerative(X,
            new HierarchicalAgglomerativeParameters(Linkage.WARD).setVerbose(true)).fit().getLabels();
    assertTrue(new VecUtils.IntSeries(labels, Inequality.EQUAL_TO, 0).all());
}

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

/**
 * @param Clustering/* w w w .j a  v  a 2s.  c o  m*/
 *            The array of clusters
 */
Array2DRowRealMatrix updateDistances(ArrayList<SpeakerCluster> clustering) {
    int clusterCount = clustering.size();
    Array2DRowRealMatrix distance = new Array2DRowRealMatrix(clusterCount, clusterCount);
    for (int i = 0; i < clusterCount; i++) {
        for (int j = 0; j <= i; j++) {
            distance.setEntry(i, j, computeDistance(clustering.get(i), clustering.get(j)));
            distance.setEntry(j, i, distance.getEntry(i, j));
        }
    }
    return distance;
}

From source file:com.analog.lyric.dimple.solvers.sumproduct.customFactors.MutlivariateGaussianMatrixProduct.java

public double[][] MatrixMult(double[][] A, double[][] B) {
    return new Array2DRowRealMatrix(A, false).multiply(new Array2DRowRealMatrix(B, false)).getDataRef();
}