List of usage examples for org.apache.commons.math3.linear RealMatrix getColumnDimension
int getColumnDimension();
From source file:org.knime.al.util.noveltydetection.knfst.MatrixFunctions.java
public static RealMatrix concatVertically(final RealMatrix top, final RealMatrix bottom) { if (top.getColumnDimension() != bottom.getColumnDimension()) { throw new IllegalArgumentException("The matrices must have the same column dimension!"); }//from w w w. j a va2 s. c om final double[][] result = new double[top.getRowDimension() + bottom.getRowDimension()][top .getColumnDimension()]; final int tr = top.getRowDimension(); for (int c = 0; c < top.getColumnDimension(); c++) { for (int r = 0; r < top.getRowDimension(); r++) { result[r][c] = top.getEntry(r, c); } for (int r = 0; r < bottom.getRowDimension(); r++) { result[tr + r][c] = bottom.getEntry(r, c); } } return MatrixUtils.createRealMatrix(result); }
From source file:org.knime.al.util.noveltydetection.knfst.MultiClassKNFST.java
public MultiClassKNFST(final KernelCalculator kernel, final String[] labels, final ExecutionMonitor progMon) throws Exception { super(kernel); m_labels = labels;//ww w .j a v a 2 s. com final ExecutionMonitor kernelProgMon = progMon.createSubProgress(0.3); final ExecutionMonitor nullspaceProgMon = progMon.createSubProgress(0.7); final RealMatrix kernelMatrix = kernel.kernelize(kernelProgMon); // obtain unique class labels final ClassWrapper[] classes = ClassWrapper.classes(labels); // calculate projection of KNFST nullspaceProgMon.setMessage("Calculating nullspace projection"); m_projection = projection(kernelMatrix, labels); nullspaceProgMon.setProgress(1.0, "Finished calculating nullspace projection"); // calculate target points ( = projections of training data into the // null space) m_targetPoints = MatrixUtils.createRealMatrix(classes.length, m_projection.getColumnDimension()); int n = 0; int nOld = 0; for (int c = 0; c < classes.length; c++) { n += classes[c].getCount(); m_targetPoints.setRowVector(c, MatrixFunctions.columnMeans(kernelMatrix .getSubMatrix(nOld, n - 1, 0, kernelMatrix.getColumnDimension() - 1).multiply(m_projection))); nOld = n; } // set betweenClassDistances m_betweenClassDistances = MatrixFunctions.calculateRowVectorDistances(m_targetPoints); }
From source file:org.knime.al.util.noveltydetection.knfst.MultiClassKNFST.java
public MultiClassKNFST(final RealMatrix kernelMatrix, final String[] labels) throws KNFSTException { m_labels = labels;/* w ww . jav a 2s . c o m*/ // obtain unique class labels final ClassWrapper[] classes = ClassWrapper.classes(labels); // calculate projection of KNFST m_projection = projection(kernelMatrix, labels); // calculate target points ( = projections of training data into the // null space) m_targetPoints = MatrixUtils.createRealMatrix(classes.length, m_projection.getColumnDimension()); int n = 0; int nOld = 0; for (int c = 0; c < classes.length; c++) { n += classes[c].getCount(); m_targetPoints.setRowVector(c, MatrixFunctions.columnMeans(kernelMatrix .getSubMatrix(nOld, n - 1, 0, kernelMatrix.getColumnDimension() - 1).multiply(m_projection))); nOld = n; } // set betweenClassDistances m_betweenClassDistances = MatrixFunctions.calculateRowVectorDistances(m_targetPoints); }
From source file:org.knime.al.util.noveltydetection.knfst.OneClassKNFST.java
public OneClassKNFST(final KernelCalculator kernel, final ExecutionMonitor progMon) throws Exception { super(kernel); final ExecutionMonitor kernelProgMon = progMon.createSubProgress(0.3); final ExecutionMonitor nullspaceProgMon = progMon.createSubProgress(0.7); // get number of training samples final RealMatrix kernelMatrix = m_kernel.kernelize(kernelProgMon); final int n = kernelMatrix.getRowDimension(); // include dot products of training samples and the origin in feature // space (these dot products are always zero!) final RealMatrix k = MatrixFunctions.concatVertically( MatrixFunctions.concatHorizontally(kernelMatrix, MatrixUtils.createRealMatrix(kernelMatrix.getRowDimension(), 1)), MatrixUtils.createRealMatrix(1, kernelMatrix.getColumnDimension() + 1)); // create one-class labels + a different label for the origin final String[] labels = new String[n + 1]; for (int l = 0; l <= n; l++) { labels[l] = (l == n) ? "0" : "1"; }//from w ww . j ava 2 s .c o m // get model parameters nullspaceProgMon.setMessage("Calculating nullspace projection"); final RealMatrix projection = projection(k, labels); nullspaceProgMon.setProgress(1.0, "Finished calculating nullspace projection"); final int[] indices = new int[n]; for (int i = 0; i < n; i++) { indices[i] = i; } m_targetPoints = MatrixUtils.createRowRealMatrix(MatrixFunctions .columnMeans(k.getSubMatrix(0, n - 1, 0, k.getColumnDimension() - 1).multiply(projection)) .toArray()); m_projection = projection.getSubMatrix(0, n - 1, 0, projection.getColumnDimension() - 1); m_betweenClassDistances = new double[] { Math.abs(m_targetPoints.getEntry(0, 0)) }; }
From source file:org.knime.al.util.noveltydetection.knfst.OneClassKNFST.java
public OneClassKNFST(final RealMatrix kernelMatrix) throws KNFSTException { final int n = kernelMatrix.getRowDimension(); // include dot products of training samples and the origin in feature // space (these dot products are always zero!) final RealMatrix k = MatrixFunctions.concatVertically( MatrixFunctions.concatHorizontally(kernelMatrix, MatrixUtils.createRealMatrix(kernelMatrix.getRowDimension(), 1)), MatrixUtils.createRealMatrix(1, kernelMatrix.getColumnDimension() + 1)); // create one-class labels + a different label for the origin final String[] labels = new String[n + 1]; for (int l = 0; l <= n; l++) { labels[l] = (l == n) ? "0" : "1"; }/*from w ww . j a v a 2s. c o m*/ // get model parameters final RealMatrix projection = projection(k, labels); final int[] indices = new int[n]; for (int i = 0; i < n; i++) { indices[i] = i; } m_targetPoints = MatrixUtils.createRowRealMatrix(MatrixFunctions .columnMeans(k.getSubMatrix(0, n - 1, 0, k.getColumnDimension() - 1).multiply(projection)) .toArray()); m_projection = projection.getSubMatrix(0, n - 1, 0, projection.getColumnDimension() - 1); m_betweenClassDistances = new double[] { Math.abs(m_targetPoints.getEntry(0, 0)) }; }
From source file:org.knime.al.util.noveltydetection.knfst.OneClassKNFST.java
private NoveltyScores score(final RealMatrix kernelMatrix) { // projected test samples: final RealMatrix projectionVectors = kernelMatrix.transpose().multiply(m_projection); // differences to the target value: final RealMatrix diff = projectionVectors.subtract(MatrixFunctions .ones(kernelMatrix.getColumnDimension(), 1).scalarMultiply(m_targetPoints.getEntry(0, 0))); // distances to the target value: final RealVector scoresVector = MatrixFunctions .sqrt(MatrixFunctions.rowSums(MatrixFunctions.multiplyElementWise(diff, diff))); return new NoveltyScores(scoresVector.toArray(), projectionVectors); }
From source file:org.lenskit.mf.MFModel.java
/** * Construct a matrix factorization model. The matrices are not copied, so the caller should * make sure they won't be modified by anyone else. * * @param umat The user feature matrix (users x features). * @param imat The item feature matrix (items x features). * @param uidx The user index mapping./*from w w w . j ava2s . c o m*/ * @param iidx The item index mapping. */ public MFModel(RealMatrix umat, RealMatrix imat, KeyIndex uidx, KeyIndex iidx) { Preconditions.checkArgument(umat.getColumnDimension() == imat.getColumnDimension(), "mismatched matrix sizes"); featureCount = umat.getColumnDimension(); userCount = uidx.size(); itemCount = iidx.size(); Preconditions.checkArgument(umat.getRowDimension() == userCount, "user matrix has %s rows, expected %s", umat.getRowDimension(), userCount); Preconditions.checkArgument(imat.getRowDimension() == itemCount, "item matrix has %s rows, expected %s", imat.getRowDimension(), itemCount); userMatrix = umat; itemMatrix = imat; userIndex = uidx; itemIndex = iidx; }
From source file:org.lenskit.util.math.RowView.java
RowView(RealMatrix mat, int r) { this(mat, r, 0, mat.getColumnDimension()); }
From source file:org.meteoinfo.math.linalg.LinalgUtil.java
/** * Calculates the Cholesky decomposition of a matrix. * The Cholesky decomposition of a real symmetric positive-definite matrix A consists of a * lower triangular matrix L with same size such that: A = LLT. In a sense, this is * the square root of A./*from w w w . ja v a2 s .c o m*/ * @param a The given matrix. * @return Result array. */ public static Array cholesky(Array a) { Array r = Array.factory(DataType.DOUBLE, a.getShape()); double[][] aa = (double[][]) ArrayUtil.copyToNDJavaArray(a); RealMatrix matrix = new Array2DRowRealMatrix(aa, false); CholeskyDecomposition decomposition = new CholeskyDecomposition(matrix); RealMatrix L = decomposition.getL(); int n = L.getColumnDimension(); int m = L.getRowDimension(); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { r.setDouble(i * n + j, L.getEntry(i, j)); } } return r; }
From source file:org.meteoinfo.math.linalg.LinalgUtil.java
/** * Calculates the LUP-decomposition of a square matrix. * The LUP-decomposition of a matrix A consists of three matrices L, U and P that satisfy: * PA = LU. L is lower triangular (with unit diagonal terms), U is upper triangular and P is * a permutation matrix. All matrices are mm. * @param a Given matrix./* ww w .j a v a 2 s . c o m*/ * @return Result P/L/U arrays. */ public static Array[] lu(Array a) { Array Pa = Array.factory(DataType.DOUBLE, a.getShape()); Array La = Array.factory(DataType.DOUBLE, a.getShape()); Array Ua = Array.factory(DataType.DOUBLE, a.getShape()); double[][] aa = (double[][]) ArrayUtil.copyToNDJavaArray(a); RealMatrix matrix = new Array2DRowRealMatrix(aa, false); LUDecomposition decomposition = new LUDecomposition(matrix); RealMatrix P = decomposition.getP(); RealMatrix L = decomposition.getL(); RealMatrix U = decomposition.getU(); int n = L.getColumnDimension(); int m = L.getRowDimension(); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { Pa.setDouble(i * n + j, P.getEntry(i, j)); La.setDouble(i * n + j, L.getEntry(i, j)); Ua.setDouble(i * n + j, U.getEntry(i, j)); } } return new Array[] { Pa, La, Ua }; }