public static Tuple3<DoubleMatrix, DoubleVector, DoubleVector> meanNormalizeColumns(
DoubleMatrix x) {
DenseDoubleMatrix toReturn = new DenseDoubleMatrix(x.getRowCount(),
x.getColumnCount());
final int length = x.getColumnCount();
DoubleVector meanVector = new DenseDoubleVector(length);
DoubleVector stddevVector = new DenseDoubleVector(length);
for (int col = 0; col < length; col++) {
DoubleVector column = x.getColumnVector(col);
double mean = column.sum() / column.getLength();
meanVector.set(col, mean);
double var = column.subtract(mean).pow(2).sum() / column.getLength();
stddevVector.set(col, Math.sqrt(var));
}
for (int col = 0; col < length; col++) {
DoubleVector column = x.getColumnVector(col)
.subtract(meanVector.get(col)).divide(stddevVector.get(col));
toReturn.setColumn(col, column.toArray());
}
return new Tuple3<>(toReturn, meanVector, stddevVector);
}