Example usage for org.apache.mahout.math.function Mult div

List of usage examples for org.apache.mahout.math.function Mult div

Introduction

In this page you can find the example usage for org.apache.mahout.math.function Mult div.

Prototype

public static Mult div(double constant) 

Source Link

Document

a / constant.

Usage

From source file:org.carrot2.matrix.factorization.KMeansMatrixFactorization.java

License:Open Source License

public void compute() {
    int n = A.columns();

    // Distances to centroids
    DoubleMatrix2D D = new DenseDoubleMatrix2D(k, n);

    // Object-cluster assignments
    V = new DenseDoubleMatrix2D(n, k);

    // Initialize the centroids with some document vectors
    U = new DenseDoubleMatrix2D(A.rows(), k);
    U.assign(A.viewPart(0, 0, A.rows(), k));

    int[] minIndices = new int[D.columns()];
    double[] minValues = new double[D.columns()];

    for (iterationsCompleted = 0; iterationsCompleted < maxIterations; iterationsCompleted++) {
        // Calculate cosine distances
        U.zMult(A, D, 1, 0, true, false);

        V.assign(0);/*from  w  w w. ja  va  2 s.  c  om*/
        U.assign(0);

        // For each object
        MatrixUtils.maxInColumns(D, minIndices, minValues);
        for (int i = 0; i < minIndices.length; i++) {
            V.setQuick(i, minIndices[i], 1);
        }

        // Update centroids
        for (int c = 0; c < V.columns(); c++) {
            // Sum
            int count = 0;
            for (int d = 0; d < V.rows(); d++) {
                if (V.getQuick(d, c) != 0) {
                    count++;
                    U.viewColumn(c).assign(A.viewColumn(d), Functions.PLUS);
                }
            }

            // Divide
            U.viewColumn(c).assign(Mult.div(count));
            MatrixUtils.normalizeColumnL2(U, null);
        }

    }
}