Package org.apache.mahout.math.matrix.impl

Examples of org.apache.mahout.math.matrix.impl.DenseDoubleMatrix2D


    }

    @Test
    public void testKMeans()
    {
        DoubleMatrix2D expectedU = new DenseDoubleMatrix2D(new double [] []
        {
            {
                0.7380, 0
            },
            {
                0, 0.6832
            },
            {
                0, 0.6832
            },
            {
                0.3481, 0.2575
            },
            {
                0.5779, 0
            }
        });

        DoubleMatrix2D expectedV = new DenseDoubleMatrix2D(new double [] []
        {
            {
                0, 1
            },
            {
View Full Code Here


    }

    @Test
    public void testOrderedNMFED()
    {
        DoubleMatrix2D expectedU = new DenseDoubleMatrix2D(new double [] []
        {
            {
                0.99592, 6.1201e-011
            },
            {
                6.9024e-070, 1.3886
            },
            {
                8.2856e-070, 1.3886
            },
            {
                7.4263e-066, 0.82488
            },
            {
                0.87124, 5.8999e-011
            }
        });

        DoubleMatrix2D expectedV = new DenseDoubleMatrix2D(new double [] []
        {
            {
                1.0265e-069, 0.43087
            },
            {
View Full Code Here

    }

    @Test
    public void testNMFKL()
    {
        DoubleMatrix2D expectedU = new DenseDoubleMatrix2D(new double [] []
        {
            {
                1.31E-11, 0.56085
            },
            {
                0.34477, 1.34E-12
            },
            {
                0.34477, 1.67E-12
            },
            {
                0.31047, 2.89E-12
            },
            {
                1.35E-11, 0.43915
            }
        });

        DoubleMatrix2D expectedV = new DenseDoubleMatrix2D(new double [] []
        {
            {
                1.7, 3.18E-12
            },
            {
View Full Code Here

    }

    @Test
    public void testLNMF()
    {
        DoubleMatrix2D expectedU = new DenseDoubleMatrix2D(new double [] []
        {
            {
                1.06E-193, 0.56085
            },
            {
                0.34477, 5.97E-184
            },
            {
                0.34477, 8.61E-184
            },
            {
                0.31047, 3.73E-179
            },
            {
                2.68E-192, 0.43915
            }
        });

        DoubleMatrix2D expectedV = new DenseDoubleMatrix2D(new double [] []
        {
            {
                1.3038, 4.47E-05
            },
            {
View Full Code Here

        {
            result.get(i % partitions).add(i);
        }

        // Matrices for centroids and document-centroid similarities
        final DoubleMatrix2D centroids = new DenseDoubleMatrix2D(selected.rows(),
            partitions).assign(selected.viewPart(0, 0, selected.rows(), partitions));
        final DoubleMatrix2D similarities = new DenseDoubleMatrix2D(partitions,
            selected.columns());

        // Run a fixed number of K-means iterations
        for (int it = 0; it < iterations; it++)
        {
            // Update centroids
            for (int i = 0; i < result.size(); i++)
            {
                final IntArrayList cluster = result.get(i);
                for (int k = 0; k < selected.rows(); k++)
                {
                    double sum = 0;
                    for (int j = 0; j < cluster.size(); j++)
                    {
                        sum += selected.get(k, cluster.get(j));
                    }
                    centroids.setQuick(k, i, sum / cluster.size());
                }
            }

            if (it < iterations - 1)
            {
                previousResult = result;
                result = Lists.newArrayList();
                for (int i = 0; i < partitions; i++)
                {
                    result.add(new IntArrayList(selected.columns()));
                }
            }

            // Calculate similarity to centroids
            centroids.zMult(selected, similarities, 1, 0, true, false);

            // Assign documents to the nearest centroid
            for (int c = 0; c < similarities.columns(); c++)
            {
                int maxRow = 0;
                double max = similarities.get(0, c);
                for (int r = 1; r < similarities.rows(); r++)
                {
                    if (max < similarities.get(r, c))
                    {
                        max = similarities.get(r, c);
                        maxRow = r;
                    }
                }

                result.get(maxRow).add(c);
View Full Code Here

        int m = A.rows();
        int n = A.columns();
        double eps = 1e-9;

        // Seed U and V with initial values
        U = new DenseDoubleMatrix2D(m, k);
        V = new DenseDoubleMatrix2D(n, k);
        seedingStrategy.seed(A, U, V);

        // Temporary matrices
        DoubleMatrix2D Aeps = A.copy();
        Aeps.assign(Functions.plus(eps));
        DoubleMatrix2D UV = new DenseDoubleMatrix2D(m, n);
        DoubleMatrix2D VT = new DenseDoubleMatrix2D(n, k);
        DoubleMatrix2D UT = new DenseDoubleMatrix2D(m, k);
        double [] work = new double [U.columns()];

        // Colt functions
        DoubleDoubleFunction invDiv = Functions.swapArgs(Functions.DIV);
        DoubleFunction plusEps = Functions.plus(eps);
View Full Code Here

    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);
View Full Code Here

        //

        double eps = 1e-9;

        // Seed U and V with initial values
        U = new DenseDoubleMatrix2D(A.rows(), k);
        V = new DenseDoubleMatrix2D(A.columns(), k);
        seedingStrategy.seed(A, U, V);

        // Temporary matrices
        DoubleMatrix2D Aeps = A.copy();
        Aeps.assign(Functions.plus(eps));
        DoubleMatrix2D UV = new DenseDoubleMatrix2D(A.rows(), A.columns());
        DoubleMatrix2D VT = new DenseDoubleMatrix2D(A.columns(), k);
        DoubleMatrix2D UT = new DenseDoubleMatrix2D(A.rows(), k);
        double [] work = new double [U.columns()];

        // Colt functions
        DoubleDoubleFunction invDiv = Functions.swapArgs(Functions.DIV);
        DoubleDoubleFunction sqrtMult = Functions.chain(Functions.SQRT, Functions.MULT);
View Full Code Here

        }
    }

    private static DenseDoubleMatrix2D toColtMatrix(Matrix m)
    {
        DenseDoubleMatrix2D result = new DenseDoubleMatrix2D(m.rowSize(), m.columnSize());
        for (int r = 0; r < result.rows(); r++)
        {
            for (int c = 0; c < result.columns(); c++)
            {
                result.setQuick(r, c, m.getQuick(r, c));
            }           
        }
        return result;
    }
View Full Code Here

        // end

        double eps = 1e-9;

        // Seed U and V with initial values
        U = new DenseDoubleMatrix2D(A.rows(), k);
        V = new DenseDoubleMatrix2D(A.columns(), k);
        seedingStrategy.seed(A, U, V);

        // Temporary matrices
        DoubleMatrix2D T = new DenseDoubleMatrix2D(k, k);
        DoubleMatrix2D UT1 = new DenseDoubleMatrix2D(A.rows(), k);
        DoubleMatrix2D UT2 = new DenseDoubleMatrix2D(A.rows(), k);
        DoubleMatrix2D VT1 = new DenseDoubleMatrix2D(A.columns(), k);
        DoubleMatrix2D VT2 = new DenseDoubleMatrix2D(A.columns(), k);
        DoubleFunction plusEps = Functions.plus(eps);

        if (stopThreshold >= 0)
        {
            updateApproximationError();
        }

        for (int i = 0; i < maxIterations; i++)
        {
            // Update V
            U.zMult(U, T, 1, 0, true, false); // T <- U'U
            A.zMult(U, VT1, 1, 0, true, false); // VT1 <- A'U
            V.zMult(T, VT2, 1, 0, false, false); // VT2 <- VT
            VT1.assign(plusEps); // TODO: shift this to the dividing function?
            VT2.assign(plusEps);
            VT1.assign(VT2, Functions.DIV); // VT1 <- VT1 ./ VT2
            V.assign(VT1, Functions.MULT); // V <- V .* VT1

            // Update U
            V.zMult(V, T, 1, 0, true, false); // T <- V'V
View Full Code Here

TOP

Related Classes of org.apache.mahout.math.matrix.impl.DenseDoubleMatrix2D

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.