List of usage examples for org.apache.mahout.math MatrixSlice nonZeroes
@Override
public Iterable<Element> nonZeroes()
From source file:edu.snu.dolphin.bsp.examples.ml.algorithms.clustering.em.EMMainCmpTask.java
License:Apache License
/** * Return a new matrix containing the product of each value of the recipient and the argument. * This method exploits sparsity of the matrix, that is, considers only non-zero entries. *//*from w w w . java 2 s . c o m*/ private Matrix times(final Matrix matrix, final double scala) { final Matrix result = matrix.clone(); final Iterator<MatrixSlice> sliceIterator = matrix.iterator(); while (sliceIterator.hasNext()) { final MatrixSlice slice = sliceIterator.next(); final int row = slice.index(); for (final Vector.Element e : slice.nonZeroes()) { final int col = e.index(); result.set(row, col, e.get() * scala); } } return result; }
From source file:edu.snu.dolphin.bsp.examples.ml.data.ClusterStats.java
License:Apache License
/** * Compute the covariance matrix from the statistics. * @return// w ww. ja va2s. c o m */ public Matrix computeCovariance() { final Vector mean = computeMean(); final Matrix covariance = outProdSum.clone(); final Iterator<MatrixSlice> sliceIterator = outProdSum.iterator(); while (sliceIterator.hasNext()) { final MatrixSlice slice = sliceIterator.next(); final int row = slice.index(); for (final Vector.Element e : slice.nonZeroes()) { final int col = e.index(); final double squaredSum = e.get(); covariance.set(row, col, squaredSum / probSum - mean.get(row) * mean.get(col)); } } return covariance; }