Java Code Examples for org.apache.commons.math3.linear.RealMatrix#getData()

The following examples show how to use org.apache.commons.math3.linear.RealMatrix#getData() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MinMaxScaler.java    From clust4j with Apache License 2.0 6 votes vote down vote up
@Override
public MinMaxScaler fit(RealMatrix X) {
	synchronized(fitLock) {
		final int m = X.getRowDimension();
		final int n = X.getColumnDimension();
		
		this.mins = new double[n];
		this.maxes= new double[n];
		double[][] data = X.getData();
		
		for(int col = 0; col < n; col++) {
			double mn = Double.POSITIVE_INFINITY, mx = Double.NEGATIVE_INFINITY;
			
			for(int row = 0; row < m; row++) {
				mn = FastMath.min(mn, data[row][col]);
				mx = FastMath.max(mx, data[row][col]);
			}
			
			this.mins[col] = mn;
			this.maxes[col]= mx;
		}
		
		return this;
	}
}
 
Example 2
Source File: StandardScaler.java    From clust4j with Apache License 2.0 6 votes vote down vote up
@Override
public RealMatrix inverseTransform(RealMatrix X) {
	checkFit();
	
	// This effectively copies, so no need to do a copy later
	double[][] data = X.getData();
	final int m = data.length;
	final int n = data[0].length;
	
	if(n != means.length)
		throw new DimensionMismatchException(n, means.length);
	
	for(int j = 0; j < n; j++) {
		for(int i = 0; i < m; i++) {
			data[i][j] *= stdevs[j]; // first re-scale
			data[i][j] += means[j];  // then add means
		}
	}
	
	return new Array2DRowRealMatrix(data, false);
}
 
Example 3
Source File: RobustScaler.java    From clust4j with Apache License 2.0 6 votes vote down vote up
@Override
public RealMatrix inverseTransform(RealMatrix X) {
	checkFit();
	
	// This effectively copies, so no need to do a copy later
	double[][] data = X.getData();
	final int m = data.length;
	final int n = data[0].length;
	
	if(n != this.centerer.medians.length)
		throw new DimensionMismatchException(n, this.centerer.medians.length);
	
	// First, multiply back by scales
	for(int j = 0; j < n; j++) {
		for(int i = 0; i < m; i++) {
			data[i][j] *= scale[j];
			
			// To avoid a second pass of O(M*N), we
			// won't call the inverseTransform in the centerer,
			// we will just explicitly add the median back here.
			data[i][j] += centerer.medians[j];
		}
	}
	
	return new Array2DRowRealMatrix(data, false);
}
 
Example 4
Source File: SparkConverter.java    From gatk-protected with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Create a distributed matrix given an Apache Commons RealMatrix.
 *
 * @param sc Never {@code null}
 * @param realMat Apache Commons RealMatrix.  Never {@code null}
 * @return A distributed Spark matrix
 */
public static RowMatrix convertRealMatrixToSparkRowMatrix(JavaSparkContext sc, RealMatrix realMat, int numSlices) {
    logger.info("Converting matrix to distributed Spark matrix...");
    final double [][] dataArray = realMat.getData();
    final LinkedList<Vector> rowsList = new LinkedList<>();
    for (final double [] i : dataArray) {
        final Vector currentRow = Vectors.dense(i);
        rowsList.add(currentRow);
    }

    // We may want to swap out this static value for something dynamic (as shown below), but this seems to slow it down.
    // final int totalSpace = realMat.getColumnDimension() * realMat.getRowDimension() * Double.BYTES;
    // // Want the partitions to be ~100KB of space
    // final int slices = totalSpace/100000;
    final JavaRDD<Vector> rows = sc.parallelize(rowsList, numSlices);

    // Create a RowMatrix from JavaRDD<Vector>.
    final RowMatrix mat = new RowMatrix(rows.rdd());
    logger.info("Done converting matrix to distributed Spark matrix...");
    return mat;
}
 
Example 5
Source File: YeoJohnsonTransformer.java    From clust4j with Apache License 2.0 6 votes vote down vote up
/**
 * Inverse transform your matrix.
 */
@Override
public RealMatrix inverseTransform(RealMatrix X) {
	checkFit();
	
	final int m = X.getRowDimension();
	final int n = X.getColumnDimension();
	
	if(n != shift.length)
		throw new DimensionMismatchException(n, shift.length);
	
	double[][] x = X.getData();
	for(int j = 0; j < n; j++) {
		for(int i = 0; i < m; i++) {
			x[i][j] = yjInvTransSingle(x[i][j], this.lambdas[j]);
		}
	}
	
	// Implicit copy in the getData()
	return new Array2DRowRealMatrix(x, false);
}
 
Example 6
Source File: PearsonsCorrelationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testConsistency() {
    RealMatrix matrix = createRealMatrix(longleyData, 16, 7);
    PearsonsCorrelation corrInstance = new PearsonsCorrelation(matrix);
    double[][] data = matrix.getData();
    double[] x = matrix.getColumn(0);
    double[] y = matrix.getColumn(1);
    Assert.assertEquals(new PearsonsCorrelation().correlation(x, y),
            corrInstance.getCorrelationMatrix().getEntry(0, 1), Double.MIN_VALUE);
    TestUtils.assertEquals("Correlation matrix", corrInstance.getCorrelationMatrix(),
            new PearsonsCorrelation().computeCorrelationMatrix(data), Double.MIN_VALUE);
}
 
Example 7
Source File: SpearmansRankCorrelationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
@Test
public void testConsistency() {
    RealMatrix matrix = createRealMatrix(longleyData, 16, 7);
    SpearmansCorrelation corrInstance = new SpearmansCorrelation(matrix);
    double[][] data = matrix.getData();
    double[] x = matrix.getColumn(0);
    double[] y = matrix.getColumn(1);
    Assert.assertEquals(new SpearmansCorrelation().correlation(x, y),
            corrInstance.getCorrelationMatrix().getEntry(0, 1), Double.MIN_VALUE);
    TestUtils.assertEquals("Correlation matrix", corrInstance.getCorrelationMatrix(),
            new SpearmansCorrelation().computeCorrelationMatrix(data), Double.MIN_VALUE);
}
 
Example 8
Source File: SpearmansRankCorrelationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
@Test
public void testConsistency() {
    RealMatrix matrix = createRealMatrix(longleyData, 16, 7);
    SpearmansCorrelation corrInstance = new SpearmansCorrelation(matrix);
    double[][] data = matrix.getData();
    double[] x = matrix.getColumn(0);
    double[] y = matrix.getColumn(1);
    Assert.assertEquals(new SpearmansCorrelation().correlation(x, y),
            corrInstance.getCorrelationMatrix().getEntry(0, 1), Double.MIN_VALUE);
    TestUtils.assertEquals("Correlation matrix", corrInstance.getCorrelationMatrix(),
            new SpearmansCorrelation().computeCorrelationMatrix(data), Double.MIN_VALUE);
}
 
Example 9
Source File: BoxCoxTransformer.java    From clust4j with Apache License 2.0 5 votes vote down vote up
/**
 * Inverse transform your matrix. Note: this suffers some
 * accuracy issues due to the log base
 */
@Override
public RealMatrix inverseTransform(RealMatrix X) {
	checkFit();
	
	final int m = X.getRowDimension();
	final int n = X.getColumnDimension();
	
	if(n != shift.length)
		throw new DimensionMismatchException(n, shift.length);
	
	double[][] x = X.getData();
	for(int j = 0; j < n; j++) {
		double lam = lambdas[j];
		double ool = 1.0 / lam;
		
		for(int i = 0; i < m; i++) {
			// If the lambda is near zero, exp to reverse the log:
			if(lam < zero) {
				x[i][j] = FastMath.exp(x[i][j]);
			} else {
				x[i][j] *= lam;
				x[i][j] += 1;
				x[i][j] = FastMath.pow(x[i][j], ool);
			}
			
			// Add back the shift value:
			x[i][j] += shift[j];
		}
	}
	
	// Implicit copy in the getData()
	return new Array2DRowRealMatrix(x, false);
}
 
Example 10
Source File: CommonsMathWrapperTest.java    From Strata with Apache License 2.0 5 votes vote down vote up
@Test
public void testVectorAsMatrix() {
  RealMatrix commons = CommonsMathWrapper.wrapAsMatrix(OG_VECTOR);
  double[][] data = commons.getData();
  assertThat(data.length).isEqualTo(OG_VECTOR.size());
  assertThat(data[0].length).isEqualTo(1);
}
 
Example 11
Source File: GaussianProcess.java    From BLELocalization with MIT License 5 votes vote down vote up
public GaussianProcess fit(double[][] X, double[][] Y){
	int ns = X.length;
	this.X = X;
	this.Y = Y;

	// Compute Gram Matrix (Symmetric matrix)
	K = computeGramMatrix(X);
	RealMatrix KMat = MatrixUtils.createRealMatrix(K);
	RealMatrix sigma_n2I = MatrixUtils.createRealIdentityMatrix(ns).scalarMultiply(sigmaN*sigmaN);
	RealMatrix Ky = KMat.add(sigma_n2I);
	this.Ky = Ky.getData();
	LUDecomposition LUDecomp = new  LUDecomposition(Ky);
	detKy = LUDecomp.getDeterminant();

	RealMatrix invKyMat = LUDecomp.getSolver().getInverse();
	invKy = invKyMat.getData();

	// Precompute dY = Y - mX
	int ny=Y[0].length;
	this.mX = new double[ns][ny];
	this.dY = new double[ns][ny];
	for(int i=0; i<ns; i++){
		for(int j=0; j<ny; j++){
			mX[i][j] = meanFunc(X[i],j);
			dY[i][j] = Y[i][j]-mX[i][j];
		}
	}
	
	if(optConstVar==1){
		invKyDY = computeInvKyDY(invKy, dY);
	}
	
	return this;
}
 
Example 12
Source File: GaussianProcessLDPLMean.java    From BLELocalization with MIT License 5 votes vote down vote up
@Override
public double looMSE(){

	double[][] Y = getY();
	double[][] dY = getdY();
	double[][] mask = getMask();

	int ns = X.length;
	int ny = Y[0].length;
	RealMatrix invKy = MatrixUtils.createRealMatrix(this.invKy);
	RealMatrix K = MatrixUtils.createRealMatrix(this.K);
	RealMatrix Hmat = invKy.multiply(K);
	double[][] H = Hmat.getData(); // H = (K+sI)^(-1) K = invKy K

	double sum=0;
	double count=0;
	for(int j=0;j<ny; j++){
		for(int i=0; i<ns; i++){
			if(mask[i][j]>0.0){
				double preddY=0;
				for(int k=0; k<ns; k++){
					preddY += H[i][k]*dY[k][j];
				}
				double diff = (dY[i][j] - preddY)/(1.0 - H[i][i]);
				sum += (diff*diff) * mask[i][j];
				count += mask[i][j];
			}
		}
	}
	sum/=count;
	return sum;
}
 
Example 13
Source File: PearsonsCorrelationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testConsistency() {
    RealMatrix matrix = createRealMatrix(longleyData, 16, 7);
    PearsonsCorrelation corrInstance = new PearsonsCorrelation(matrix);
    double[][] data = matrix.getData();
    double[] x = matrix.getColumn(0);
    double[] y = matrix.getColumn(1);
    Assert.assertEquals(new PearsonsCorrelation().correlation(x, y),
            corrInstance.getCorrelationMatrix().getEntry(0, 1), Double.MIN_VALUE);
    TestUtils.assertEquals("Correlation matrix", corrInstance.getCorrelationMatrix(),
            new PearsonsCorrelation().computeCorrelationMatrix(data), Double.MIN_VALUE);
}
 
Example 14
Source File: SpearmansRankCorrelationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
@Test
public void testConsistency() {
    RealMatrix matrix = createRealMatrix(longleyData, 16, 7);
    SpearmansCorrelation corrInstance = new SpearmansCorrelation(matrix);
    double[][] data = matrix.getData();
    double[] x = matrix.getColumn(0);
    double[] y = matrix.getColumn(1);
    Assert.assertEquals(new SpearmansCorrelation().correlation(x, y),
            corrInstance.getCorrelationMatrix().getEntry(0, 1), Double.MIN_VALUE);
    TestUtils.assertEquals("Correlation matrix", corrInstance.getCorrelationMatrix(),
            new SpearmansCorrelation().computeCorrelationMatrix(data), Double.MIN_VALUE);
}
 
Example 15
Source File: PearsonsCorrelationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testConsistency() {
    RealMatrix matrix = createRealMatrix(longleyData, 16, 7);
    PearsonsCorrelation corrInstance = new PearsonsCorrelation(matrix);
    double[][] data = matrix.getData();
    double[] x = matrix.getColumn(0);
    double[] y = matrix.getColumn(1);
    Assert.assertEquals(new PearsonsCorrelation().correlation(x, y),
            corrInstance.getCorrelationMatrix().getEntry(0, 1), Double.MIN_VALUE);
    TestUtils.assertEquals("Correlation matrix", corrInstance.getCorrelationMatrix(),
            new PearsonsCorrelation().computeCorrelationMatrix(data), Double.MIN_VALUE);
}
 
Example 16
Source File: PearsonsCorrelationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testConsistency() {
    RealMatrix matrix = createRealMatrix(longleyData, 16, 7);
    PearsonsCorrelation corrInstance = new PearsonsCorrelation(matrix);
    double[][] data = matrix.getData();
    double[] x = matrix.getColumn(0);
    double[] y = matrix.getColumn(1);
    Assert.assertEquals(new PearsonsCorrelation().correlation(x, y),
            corrInstance.getCorrelationMatrix().getEntry(0, 1), Double.MIN_VALUE);
    TestUtils.assertEquals("Correlation matrix", corrInstance.getCorrelationMatrix(),
            new PearsonsCorrelation().computeCorrelationMatrix(data), Double.MIN_VALUE);
}
 
Example 17
Source File: SpearmansRankCorrelationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
@Test
public void testConsistency() {
    RealMatrix matrix = createRealMatrix(longleyData, 16, 7);
    SpearmansCorrelation corrInstance = new SpearmansCorrelation(matrix);
    double[][] data = matrix.getData();
    double[] x = matrix.getColumn(0);
    double[] y = matrix.getColumn(1);
    Assert.assertEquals(new SpearmansCorrelation().correlation(x, y),
            corrInstance.getCorrelationMatrix().getEntry(0, 1), Double.MIN_VALUE);
    TestUtils.assertEquals("Correlation matrix", corrInstance.getCorrelationMatrix(),
            new SpearmansCorrelation().computeCorrelationMatrix(data), Double.MIN_VALUE);
}
 
Example 18
Source File: ArrayUtils.java    From BLELocalization with MIT License 4 votes vote down vote up
public static double[][] inverseMat(double[][] mat){
	RealMatrix realMatrix = MatrixUtils.createRealMatrix(mat);
	LUDecomposition lu = new LUDecomposition(realMatrix);
	RealMatrix invMat = lu.getSolver().getInverse();
	return invMat.getData();
}
 
Example 19
Source File: CovarianceTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Verify that diagonal entries are consistent with Variance computation and matrix matches
 * column-by-column covariances
 */
@Test
public void testConsistency() {
    final RealMatrix matrix = createRealMatrix(swissData, 47, 5);
    final RealMatrix covarianceMatrix = new Covariance(matrix).getCovarianceMatrix();

    // Variances on the diagonal
    Variance variance = new Variance();
    for (int i = 0; i < 5; i++) {
        Assert.assertEquals(variance.evaluate(matrix.getColumn(i)), covarianceMatrix.getEntry(i,i), 10E-14);
    }

    // Symmetry, column-consistency
    Assert.assertEquals(covarianceMatrix.getEntry(2, 3),
            new Covariance().covariance(matrix.getColumn(2), matrix.getColumn(3), true), 10E-14);
    Assert.assertEquals(covarianceMatrix.getEntry(2, 3), covarianceMatrix.getEntry(3, 2), Double.MIN_VALUE);

    // All columns same -> all entries = column variance
    RealMatrix repeatedColumns = new Array2DRowRealMatrix(47, 3);
    for (int i = 0; i < 3; i++) {
        repeatedColumns.setColumnMatrix(i, matrix.getColumnMatrix(0));
    }
    RealMatrix repeatedCovarianceMatrix = new Covariance(repeatedColumns).getCovarianceMatrix();
    double columnVariance = variance.evaluate(matrix.getColumn(0));
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            Assert.assertEquals(columnVariance, repeatedCovarianceMatrix.getEntry(i, j), 10E-14);
        }
    }

    // Check bias-correction defaults
    double[][] data = matrix.getData();
    TestUtils.assertEquals("Covariances",
            covarianceMatrix, new Covariance().computeCovarianceMatrix(data),Double.MIN_VALUE);
    TestUtils.assertEquals("Covariances",
            covarianceMatrix, new Covariance().computeCovarianceMatrix(data, true),Double.MIN_VALUE);

    double[] x = data[0];
    double[] y = data[1];
    Assert.assertEquals(new Covariance().covariance(x, y),
            new Covariance().covariance(x, y, true), Double.MIN_VALUE);
}
 
Example 20
Source File: NearestNeighborHeapSearch.java    From clust4j with Apache License 2.0 2 votes vote down vote up
/**
 * Constructor with logger object
 * @param X
 * @param leaf_size
 * @param dist
 * @param logger
 */
public NearestNeighborHeapSearch(final RealMatrix X, int leaf_size, DistanceMetric dist, Loggable logger) {
	this(X.getData(), leaf_size, dist, logger);
}