/** * Standardize the matrix entries by row- or column-wise z-scores (z=(x-u)/sigma) * * @param isByRow standardize by row if true; otherwise by column */ public void standardize(boolean isByRow) { int iters = isByRow ? numRows : numColumns; for (int iter = 0; iter < iters; iter++) { SparseVector vec = isByRow ? row(iter) : column(iter); if (vec.getCount() > 0) { double[] data = vec.getData(); double mu = Stats.mean(data); double sigma = Stats.sd(data, mu); for (VectorEntry ve : vec) { int idx = ve.index(); double val = ve.get(); double z = (val - mu) / sigma; if (isByRow) this.set(iter, idx, z); else this.set(idx, iter, z); } } } }
/** @return sum of matrix data */ public double sum() { return Stats.sum(rowData); }