List of usage examples for org.apache.commons.math3.linear Array2DRowRealMatrix Array2DRowRealMatrix
public Array2DRowRealMatrix(final double[][] d, final boolean copyArray) throws DimensionMismatchException, NoDataException, NullArgumentException
From source file:com.itemanalysis.psychometrics.factoranalysis.GPArotation.java
private RealMatrix randomStart(int ncol) { NormalDistribution norm = new NormalDistribution(0.0, 1.0); RealMatrix T = new Array2DRowRealMatrix(ncol, ncol); for (int i = 0; i < ncol; i++) { for (int j = 0; j < ncol; j++) { T.setEntry(i, j, norm.sample()); }/*from w ww . ja va 2 s. c o m*/ } QRDecomposition qr = new QRDecomposition(T); return qr.getQ(); }
From source file:com.clust4j.algo.DBSCANTests.java
License:asdf
/** * Asser that when all of the matrix entries are exactly the same, * the algorithm will still converge, yet produce one label: 0 *//*from ww w. j a v a 2s . 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 DBSCAN(X, new DBSCANParameters(1).setVerbose(true)).fit().getLabels(); assertTrue(new VecUtils.IntSeries(labels, Inequality.EQUAL_TO, labels[0]).all()); // these might be noise in DBSCAN labels = new DBSCAN(X, new DBSCANParameters().setVerbose(true)).fit().getLabels(); assertTrue(new VecUtils.IntSeries(labels, Inequality.EQUAL_TO, labels[0]).all()); }
From source file:gamlss.utilities.WLSMultipleLinearRegression.java
/** * Add a column with 1s for the Intercept. * @param X the design matrix//from w ww . j a va 2 s . com */ private void setDesignMatrix(RealMatrix X) { double[][] x = X.getData(); final int nVars = x[0].length; final double[][] xAug = new double[x.length][nVars + 1]; for (int i = 0; i < x.length; i++) { if (x[i].length != nVars) { throw new DimensionMismatchException(x[i].length, nVars); } xAug[i][0] = 1.0d; System.arraycopy(x[i], 0, xAug[i], 1, nVars); } this.X = new Array2DRowRealMatrix(xAug, false); }
From source file:com.clust4j.data.DataSet.java
public void addColumns(String[] s, double[][] cols) { MatUtils.checkDims(cols);/*w w w . j a va 2 s .co m*/ final int m = cols.length; if (m != data.getRowDimension()) throw new DimensionMismatchException(m, data.getRowDimension()); int i, j; final int n = data.getColumnDimension(), newN = n + cols[0].length; // build headers if (null == s) { s = new String[cols[0].length]; for (i = 0, j = n; i < cols[0].length; i++, j++) s[i] = COL_PREFIX + j; } else { // Ensure no nulls for (i = 0, j = n; i < cols[0].length; i++, j++) s[i] = null == s[i] ? (COL_PREFIX + j) : s[i]; } String[] newHeaders = new String[newN]; double[][] newData = new double[m][newN]; double[][] oldData = data.getDataRef(); for (i = 0; i < m; i++) { for (j = 0; j < newN; j++) { if (i == 0) { newHeaders[j] = j < n ? headers[j] : s[j - n]; } newData[i][j] = j < n ? oldData[i][j] : cols[i][j - n]; } } this.headers = newHeaders; this.data = new Array2DRowRealMatrix(newData, false); }
From source file:edu.cmu.sphinx.speakerid.SpeakerIdentification.java
/** * @param features The feature vectors to be used for clustering * @return A cluster for each speaker detected based on the feature vectors provided *//* ww w .ja v a 2 s. co m*/ public ArrayList<SpeakerCluster> cluster(ArrayList<float[]> features) { ArrayList<SpeakerCluster> ret = new ArrayList<SpeakerCluster>(); Array2DRowRealMatrix featuresMatrix = ArrayToRealMatrix(features, features.size()); LinkedList<Integer> l = getAllChangingPoints(featuresMatrix); Iterator<Integer> it = l.iterator(); int curent, previous = it.next(); while (it.hasNext()) { curent = it.next(); Segment s = new Segment(previous * Segment.FRAME_LENGTH, (curent - previous) * (Segment.FRAME_LENGTH)); Array2DRowRealMatrix featuresSubset = (Array2DRowRealMatrix) featuresMatrix.getSubMatrix(previous, curent - 1, 0, 12); ret.add(new SpeakerCluster(s, featuresSubset, getBICValue(featuresSubset))); previous = curent; } int clusterCount = ret.size(); Array2DRowRealMatrix distance; distance = new Array2DRowRealMatrix(clusterCount, clusterCount); distance = updateDistances(ret); while (true) { double distmin = 0; int imin = -1, jmin = -1; for (int i = 0; i < clusterCount; i++) for (int j = 0; j < clusterCount; j++) if (i != j) distmin += distance.getEntry(i, j); distmin /= (clusterCount * (clusterCount - 1) * 4); for (int i = 0; i < clusterCount; i++) { for (int j = 0; j < clusterCount; j++) { if (distance.getEntry(i, j) < distmin && i != j) { distmin = distance.getEntry(i, j); imin = i; jmin = j; } } } if (imin == -1) { break; } ret.get(imin).mergeWith(ret.get(jmin)); updateDistances(ret, imin, jmin, distance); ret.remove(jmin); clusterCount--; } return ret; }
From source file:ellipsoidFit.FitPoints.java
/** * Translate the algebraic form of the ellipsoid to the center. * // w w w. j av a 2 s.c o m * @param center * vector containing the center of the ellipsoid. * @param a * the algebraic form of the polynomial. * @return the center translated form of the algebraic ellipsoid. */ private RealMatrix translateToCenter(RealVector center, RealMatrix a) { // Form the corresponding translation matrix. RealMatrix t = MatrixUtils.createRealIdentityMatrix(4); RealMatrix centerMatrix = new Array2DRowRealMatrix(1, 3); centerMatrix.setRowVector(0, center); t.setSubMatrix(centerMatrix.getData(), 3, 0); // Translate to the center. RealMatrix r = t.multiply(a).multiply(t.transpose()); return r; }
From source file:edu.cudenver.bios.matrix.MatrixUtils.java
/** * Horizontally append two matrices//from www . ja va 2s. co m * @param matrix * @param column * @return the combined matrix * @throws IllegalArgumentException */ public static RealMatrix getHorizontalAppend(RealMatrix m1, RealMatrix m2) throws IllegalArgumentException { if (m1 == null || m2 == null) throw new IllegalArgumentException("Missing required argument"); if (m1.getRowDimension() != m2.getRowDimension()) throw new IllegalArgumentException("Row dimensions must be equal"); RealMatrix newMatrix = new Array2DRowRealMatrix(m1.getRowDimension(), m1.getColumnDimension() + m2.getColumnDimension()); newMatrix.setSubMatrix(m1.getData(), 0, 0); newMatrix.setSubMatrix(m2.getData(), 0, m1.getColumnDimension()); return newMatrix; }
From source file:com.clust4j.algo.AbstractClusterer.java
final private Array2DRowRealMatrix initData(final RealMatrix data) { final int m = data.getRowDimension(), n = data.getColumnDimension(); final double[][] ref = new double[m][n]; final HashSet<Double> unique = new HashSet<>(); // Used to compute variance on the fly for summaries later... double[] sum = new double[n]; double[] sumSq = new double[n]; double[] maxes = VecUtils.rep(Double.NEGATIVE_INFINITY, n); double[] mins = VecUtils.rep(Double.POSITIVE_INFINITY, n); // This will store summaries for each column + a header ModelSummary summaries = new ModelSummary( new Object[] { "Feature #", "Variance", "Std. Dev", "Mean", "Max", "Min" }); /*// w ww. j a va 2 s .com * Internally performs the copy */ double entry; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { entry = data.getEntry(i, j); if (Double.isNaN(entry)) { error(new NaNException("NaN in input data. " + "Select a matrix imputation method for " + "incomplete records")); } else { // copy the entry ref[i][j] = entry; unique.add(entry); // capture stats... sumSq[j] += entry * entry; sum[j] += entry; maxes[j] = FastMath.max(entry, maxes[j]); mins[j] = FastMath.min(entry, mins[j]); // if it's the last row, we can compute these: if (i == m - 1) { double var = (sumSq[j] - (sum[j] * sum[j]) / (double) m) / ((double) m - 1.0); if (var == 0) { warn("zero variance in feature " + j); } summaries.add(new Object[] { j, // feature num var, // var m < 2 ? Double.NaN : FastMath.sqrt(var), // std dev sum[j] / (double) m, // mean maxes[j], // max mins[j] // min }); } } } } // Log the summaries summaryLogger(formatter.format(summaries)); if (unique.size() == 1) this.singular_value = true; /* * Don't need to copy again, because already internally copied... */ return new Array2DRowRealMatrix(ref, false); }
From source file:com.clust4j.algo.preprocess.PreProcessorTests.java
@Test public void testRobustScaler() { 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); RobustScaler norm = new RobustScaler().fit(d); RealMatrix scaled = norm.transform(d); // Test inverse transform. assertTrue(MatUtils.equalsWithTolerance(data, norm.inverseTransform(scaled).getData(), 1e-8)); // copy coverage norm.copy();// w w w . j av a 2s. co m // test dim mismatch boolean a = false; try { norm.inverseTransform(TestSuite.getRandom(2, 2)); } catch (DimensionMismatchException dim) { a = true; } finally { assertTrue(a); } // test not fit a = false; try { new RobustScaler().transform(d); } catch (ModelNotFitException dim) { a = true; } finally { assertTrue(a); } }
From source file:com.clust4j.algo.RadiusNeighborsTests.java
/** * Asser that when all of the matrix entries are exactly the same, * the algorithm will still converge, yet produce one label: 0 *//*from www . jav a 2s. co m*/ @Override @Test public void testAllSame() { final double[][] x = MatUtils.rep(-1, 3, 3); final Array2DRowRealMatrix X = new Array2DRowRealMatrix(x, false); Neighborhood neighb = new RadiusNeighbors(X, new RadiusNeighborsParameters(3.0).setVerbose(true)).fit() .getNeighbors(); assertTrue(new MatUtils.MatSeries(neighb.getDistances(), Inequality.EQUAL_TO, 0).all()); System.out.println(); /* * Test default constructor */ neighb = new RadiusNeighbors(X, new RadiusNeighborsParameters().setVerbose(true)).fit().getNeighbors(); assertTrue(new MatUtils.MatSeries(neighb.getDistances(), Inequality.EQUAL_TO, 0).all()); System.out.println(); /* * Test BallTree */ neighb = new RadiusNeighbors(X, new RadiusNeighborsParameters(3.0).setVerbose(true).setAlgorithm(NeighborsAlgorithm.BALL_TREE)) .fit().getNeighbors(); assertTrue(new MatUtils.MatSeries(neighb.getDistances(), Inequality.EQUAL_TO, 0).all()); System.out.println(); }