Example usage for org.apache.commons.math3.linear SingularValueDecomposition getV

List of usage examples for org.apache.commons.math3.linear SingularValueDecomposition getV

Introduction

In this page you can find the example usage for org.apache.commons.math3.linear SingularValueDecomposition getV.

Prototype

public RealMatrix getV() 

Source Link

Document

Returns the matrix V of the decomposition.

Usage

From source file:movierecommend.MovieRecommend.java

public static void main(String[] args) throws ClassNotFoundException, SQLException {
    String url = "jdbc:sqlserver://localhost;databaseName=MovieDB;integratedSecurity=true";
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    Connection conn = DriverManager.getConnection(url);

    Statement stm = conn.createStatement();
    ResultSet rsRecnik = stm.executeQuery("SELECT Recnik FROM Recnik WHERE (ID_Zanra = 1)"); //citam recnik iz baze za odredjeni zanr
    String recnik[] = null;/*from w  ww .  j a  va  2s  . com*/

    while (rsRecnik.next()) {
        recnik = rsRecnik.getString("Recnik").split(","); //delim recnik na reci

    }

    ResultSet rsFilmovi = stm.executeQuery(
            "SELECT TOP (200) Naziv_Filma, LemmaPlots, " + "ID_Filma FROM Film WHERE (ID_Zanra = 1)");
    List<Film> listaFilmova = new ArrayList<>();
    Film f = null;
    int rb = 0;
    while (rsFilmovi.next()) {
        f = new Film(rb, Integer.parseInt(rsFilmovi.getString("ID_Filma")), rsFilmovi.getString("Naziv_Filma"),
                rsFilmovi.getString("LemmaPlots"));
        listaFilmova.add(f);
        rb++;

    }
    //kreiranje vektorskog modela
    M = MatrixUtils.createRealMatrix(recnik.length, listaFilmova.size());
    System.out.println("Prva tezinska matrica");

    for (int i = 0; i < recnik.length; i++) {
        String recBaza = recnik[i];
        for (Film film : listaFilmova) {
            for (String lemmaRec : film.getPlotLema()) {
                if (recBaza.equals(lemmaRec)) {
                    M.setEntry(i, film.getRb(), M.getEntry(i, film.getRb()) + 1);
                }
            }
        }
    }
    //racunanje tf-idf
    System.out.println("td-idf");
    M = LSA.calculateTfIdf(M);
    System.out.println("SVD");
    //SVD
    SingularValueDecomposition svd = new SingularValueDecomposition(M);
    RealMatrix V = svd.getV();
    RealMatrix Vk = V.getSubMatrix(0, V.getRowDimension() - 1, 0, brojDimenzija - 1); //dimenzija je poslednji argument
    //kosinusna slicnost
    System.out.println("Cosin simmilarity");
    CallableStatement stmTop = conn.prepareCall("{call Dodaj_TopList(?,?,?)}");

    for (int j = 0; j < listaFilmova.size(); j++) {
        Film fl = listaFilmova.get(j);
        List<Film> lFilmova1 = new ArrayList<>();
        lFilmova1.add(listaFilmova.get(j));
        double sim = 0.0;
        for (int k = 0; k < listaFilmova.size(); k++) {
            // System.out.println(listaFilmova.size());                
            sim = LSA.cosinSim(j, k, Vk.transpose());
            listaFilmova.get(k).setSimilarity(sim);
            lFilmova1.add(listaFilmova.get(k));
        }
        Collections.sort(lFilmova1);
        for (int k = 2; k < 13; k++) {
            stmTop.setString(1, fl.getID() + "");
            stmTop.setString(2, lFilmova1.get(k).getID() + "");
            stmTop.setString(3, lFilmova1.get(k).getSimilarity() + "");
            stmTop.execute();
        }

    }

    stm.close();
    rsRecnik.close();
    rsFilmovi.close();
    conn.close();

}

From source file:cooccurrence.Omer_Levy.java

public static void main(String args[]) {
    String path = "";
    String writePath = "";
    BufferedReader br = null;/*  ww w . j a  v  a  2 s . c  om*/
    ArrayList<String> files = new ArrayList<>();
    //reading all the files in the directory
    //each file is PPMI matrix for an year
    listFilesForFolder(new File(path), files);
    for (String filePath : files) {
        System.out.println(filePath);
        String fileName = new File(filePath).getName();

        //data structure to store the PPMI matrix in the file
        HashMap<String, HashMap<String, Double>> cooccur = new HashMap<>();
        readFileContents(filePath, cooccur); //reading the file and storing the content in the hashmap
        //Because Matrices are identified by row and col id, the following 
        //lists maps id to corresponding string. Note that matrix is symmetric. 
        ArrayList<String> rowStrings = new ArrayList<>(cooccur.keySet());
        ArrayList<String> colStrings = new ArrayList<>(cooccur.keySet());

        //creating matrix with given dimensions and initializing it to 0
        RealMatrix matrixR = MatrixUtils.createRealMatrix(rowStrings.size(), colStrings.size());

        //creating the matrices for storing top rank-d matrices of SVD 
        RealMatrix matrixUd = MatrixUtils.createRealMatrix(D, D);
        RealMatrix matrixVd = MatrixUtils.createRealMatrix(D, D);
        RealMatrix coVarD = MatrixUtils.createRealMatrix(D, D);

        //populating the matrices based on the co-occur hashmap
        populateMatrixR(matrixR, cooccur, rowStrings, colStrings);

        //computing the svd
        SingularValueDecomposition svd = new SingularValueDecomposition(matrixR);

        //extracting the components of SVD factorization
        RealMatrix U = svd.getU();
        RealMatrix V = svd.getV();
        RealMatrix coVariance = svd.getCovariance(-1);

        //list to store indices of top-D singular values of coVar. 
        //Use this with rowsString (colStrings) to get the corresponding word and context
        ArrayList<Integer> indicesD = new ArrayList<>();
        //Extract topD singular value from covariance to store in coVarD and
        //extract corresponding columns from U and V to store in Ud and Vd
        getTopD(U, V, coVariance, matrixUd, matrixVd, coVarD, indicesD);
        //calulate the squareRoot of coVarD
        RealMatrix squareRootCoVarD = squareRoot(coVarD);
        RealMatrix W_svd = matrixUd.multiply(squareRootCoVarD);
        RealMatrix C_svd = matrixVd.multiply(squareRootCoVarD);
    }
}

From source file:edu.oregonstate.eecs.mcplan.ml.MatrixAlgorithms.java

/**
 * Computes the inverse of a matrix using the singular value decomposition.
 * /*from w  ww .  java  2 s  .  c o m*/
 * The input matrix M is assumed to be positive definite up to numerical
 * precision 'eps'. That is, for all eigenvalues lambda of M, it must be
 * the case that lambda + eps > 0. For eigenvalues with |lambda| < eps, the
 * eigenvalue is set to 'eps' before inverting. Throws an exception if
 * any lambda < -eps.
 * @param M
 * @param eps
 * @return
 */
public static RealMatrix robustInversePSD(final RealMatrix M, final double eps) {
    assert (eps > 0.0);
    final SingularValueDecomposition svd = new SingularValueDecomposition(M);
    final RealMatrix Sigma = svd.getS().copy();
    final int N = Math.min(Sigma.getColumnDimension(), Sigma.getRowDimension());
    for (int i = 0; i < N; ++i) {
        final double lambda = Sigma.getEntry(i, i);
        System.out.println("lambda_" + i + " = " + lambda);
        if (Math.abs(lambda) < eps) {
            System.out.println("Corrected " + i);
            Sigma.setEntry(i, i, 1.0 / eps);
        } else if (lambda < 0.0) {
            throw new IllegalArgumentException("Negative eigenvalue " + lambda);
        } else {
            Sigma.setEntry(i, i, 1.0 / lambda);
        }
    }
    return svd.getV().multiply(Sigma.transpose()).multiply(svd.getUT());
}

From source file:edu.cmu.tetrad.util.MatrixUtils.java

public static double[][] pseudoInverse(double[][] x) {
    SingularValueDecomposition svd = new SingularValueDecomposition(new Matrix(x));

    Matrix U = svd.getU();/*  ww  w  .  j  a v a  2 s.co  m*/
    Matrix V = svd.getV();
    Matrix S = svd.getS();

    for (int i = 0; i < S.getRowDimension(); i++) {
        for (int j = 0; j < S.getColumnDimension(); j++) {
            double v = S.get(i, j);
            S.set(i, j, v == 0 ? 0.0 : 1.0 / v);
        }
    }

    return V.times(S.times(U.transpose())).getArray();
}

From source file:edu.cmu.tetrad.util.MatrixUtils1.java

public static double[][] pseudoInverse(double[][] x) {
    SingularValueDecomposition svd = new SingularValueDecomposition(new BlockRealMatrix(x));

    RealMatrix U = svd.getU();//from   w w w  .  j a  v  a2s  . c  om
    RealMatrix V = svd.getV();
    RealMatrix S = svd.getS();

    for (int i = 0; i < S.getRowDimension(); i++) {
        for (int j = 0; j < S.getColumnDimension(); j++) {
            double v = S.getEntry(i, j);
            S.setEntry(i, j, v == 0 ? 0.0 : 1.0 / v);
        }
    }

    return V.multiply(S.multiply(U.transpose())).getData();
}

From source file:dataminning2.SVDDecomposition.java

public double[][] DecompositionV(double[][] Dataarraytest) {
    Array2DRowRealMatrix rmA = new Array2DRowRealMatrix(Dataarraytest);
    SingularValueDecomposition svdObj = new SingularValueDecomposition(rmA);
    double[][] DataV = svdObj.getV().getData();
    return DataV;
}

From source file:hivemall.utils.math.MatrixUtilsTest.java

@Test
public void testPower1() {
    RealMatrix A = new Array2DRowRealMatrix(
            new double[][] { new double[] { 1, 2, 3 }, new double[] { 4, 5, 6 } });

    double[] x = new double[3];
    x[0] = Math.random();//from www.  j  av  a 2 s . c  o  m
    x[1] = Math.random();
    x[2] = Math.random();

    double[] u = new double[2];
    double[] v = new double[3];

    double s = MatrixUtils.power1(A, x, 2, u, v);

    SingularValueDecomposition svdA = new SingularValueDecomposition(A);

    Assert.assertArrayEquals(svdA.getU().getColumn(0), u, 0.001d);
    Assert.assertArrayEquals(svdA.getV().getColumn(0), v, 0.001d);
    Assert.assertEquals(svdA.getSingularValues()[0], s, 0.001d);
}

From source file:com.clust4j.algo.preprocess.PCA.java

@Override
public PCA fit(RealMatrix X) {
    synchronized (fitLock) {
        this.centerer = new MeanCenterer().fit(X);
        this.m = X.getRowDimension();
        this.n = X.getColumnDimension();

        // ensure n_components not too large
        if (this.n_components > n)
            this.n_components = n;

        final RealMatrix data = this.centerer.transform(X);
        SingularValueDecomposition svd = new SingularValueDecomposition(data);
        RealMatrix U = svd.getU(), S = svd.getS(), V = svd.getV().transpose();

        // flip Eigenvectors' sign to enforce deterministic output
        EntryPair<RealMatrix, RealMatrix> uv_sign_swap = eigenSignFlip(U, V);

        U = uv_sign_swap.getKey();//ww  w  .  j a v a 2  s .  c  o  m
        V = uv_sign_swap.getValue();
        RealMatrix components_ = V;

        // get variance explained by singular value
        final double[] s = MatUtils.diagFromSquare(S.getData());
        this.variabilities = new double[s.length];
        for (int i = 0; i < s.length; i++) {
            variabilities[i] = (s[i] * s[i]) / (double) m;
            total_var += variabilities[i];
        }

        // get variability ratio
        this.variability_ratio = new double[s.length];
        for (int i = 0; i < s.length; i++) {
            variability_ratio[i] = variabilities[i] / total_var;
        }

        // post-process number of components if in var_mode
        double[] ratio_cumsum = VecUtils.cumsum(variability_ratio);
        if (this.var_mode) {
            for (int i = 0; i < ratio_cumsum.length; i++) {
                if (ratio_cumsum[i] >= this.variability) {
                    this.n_components = i + 1;
                    break;
                }

                // if it never hits the if block, the n_components is
                // equal to the number of columns in its entirety
            }
        }

        // get noise variance
        if (n_components < FastMath.min(n, m)) {
            this.noise_variance = VecUtils.mean(VecUtils.slice(variabilities, n_components, s.length));
        } else {
            this.noise_variance = 0.0;
        }

        // Set the components and other sliced variables
        this.components = new Array2DRowRealMatrix(MatUtils.slice(components_.getData(), 0, n_components),
                false);
        this.variabilities = VecUtils.slice(variabilities, 0, n_components);
        this.variability_ratio = VecUtils.slice(variability_ratio, 0, n_components);

        if (retain) {
            this.U = new Array2DRowRealMatrix(MatUtils.slice(U.getData(), 0, n_components), false);
            ;
            this.S = new Array2DRowRealMatrix(MatUtils.slice(S.getData(), 0, n_components), false);
            ;
        }

        return this;
    }
}

From source file:eagle.security.userprofile.model.eigen.UserProfileEigenModeler.java

private void computeCovarianceAndSVD(RealMatrix inputMat, int containsLowVariantCol) {

    int finalMatrixRow = 0;
    int finalMatrixCol = 0;

    LOG.info("containsLowVariantCol size: " + containsLowVariantCol);
    int colDimension = (inputMat.getColumnDimension() - containsLowVariantCol);
    try {/* ww  w .  j a va 2  s  .  c  o  m*/
        finalMatrixWithoutLowVariantCmds = new Array2DRowRealMatrix(inputMat.getRowDimension(), colDimension);
    } catch (NotStrictlyPositiveException e) {
        LOG.error(String.format("Failed to build matrix [rowDimension:%s, columnDimension: %s]",
                inputMat.getRowDimension(), colDimension), e);
        throw e;
    }

    for (int i = 0; i < inputMat.getRowDimension(); i++) {
        for (int j = 0; j < inputMat.getColumnDimension(); j++) {
            if (!statistics[j].isLowVariant()) {
                finalMatrixWithoutLowVariantCmds.setEntry(finalMatrixRow, finalMatrixCol,
                        inputMat.getEntry(i, j));
                finalMatrixCol++;
            }
        }
        finalMatrixCol = 0;
        finalMatrixRow++;
    }

    Covariance cov;
    try {
        cov = new Covariance(finalMatrixWithoutLowVariantCmds.getData());
    } catch (Exception ex) {
        throw new IllegalArgumentException(String.format("Failed to create covariance from matrix [ %s x %s ]",
                finalMatrixWithoutLowVariantCmds.getRowDimension(),
                finalMatrixWithoutLowVariantCmds.getColumnDimension()), ex);
    }
    covarianceMatrix = cov.getCovarianceMatrix();
    SingularValueDecomposition svd = new SingularValueDecomposition(covarianceMatrix);
    diagonalMatrix = svd.getS();
    uMatrix = svd.getU();
    vMatrix = svd.getV();
}

From source file:com.itemanalysis.psychometrics.factoranalysis.GPArotation.java

/**
 * Conducts orthogonal rotation of factor loadings.
 *
 * @param A matrix of orthogonal factor loadings
 * @return a matrix of rotated factor loadings.
 * @throws ConvergenceException/*  w w w.  j a  v  a  2s  .co m*/
 */
private RotationResults GPForth(RealMatrix A, boolean normalize, int maxIter, double eps)
        throws ConvergenceException {
    int ncol = A.getColumnDimension();

    if (normalize) {
        //elementwise division by normalizing weights
        final RealMatrix W = getNormalizingWeights(A, true);
        A.walkInRowOrder(new DefaultRealMatrixChangingVisitor() {
            @Override
            public double visit(int row, int column, double value) {
                return value / W.getEntry(row, column);
            }
        });
    }

    RealMatrix Tmat = new IdentityMatrix(ncol);
    double alpha = 1;
    RealMatrix L = A.multiply(Tmat);

    gpFunction.computeValues(L);

    double f = gpFunction.getValue();
    RealMatrix VgQ = gpFunction.getGradient();
    RealMatrix G = A.transpose().multiply(VgQ);
    double VgQtF = gpFunction.getValue();
    RealMatrix VgQt = gpFunction.getGradient();
    RealMatrix Tmatt = null;

    int iter = 0;
    double s = eps + 0.5;
    double s2 = 0;
    int innnerIter = 11;

    while (iter < maxIter) {
        RealMatrix M = Tmat.transpose().multiply(G);
        RealMatrix S = (M.add(M.transpose()));
        S = S.scalarMultiply(0.5);
        RealMatrix Gp = G.subtract(Tmat.multiply(S));
        s = Math.sqrt((Gp.transpose().multiply(Gp)).getTrace());
        s2 = Math.pow(s, 2);

        if (s < eps)
            break;
        alpha *= 2.0;

        for (int j = 0; j < innnerIter; j++) {
            Gp = Gp.scalarMultiply(alpha);
            RealMatrix X = (Tmat.subtract(Gp));
            SingularValueDecomposition SVD = new SingularValueDecomposition(X);

            Tmatt = SVD.getU().multiply(SVD.getV().transpose());
            L = A.multiply(Tmatt);
            gpFunction.computeValues(L);
            VgQt = gpFunction.getGradient();
            VgQtF = gpFunction.getValue();

            if (VgQtF < f - 0.5 * s2 * alpha) {
                break;
            }
            alpha /= 2.0;
        }

        Tmat = Tmatt;
        f = VgQtF;
        G = A.transpose().multiply(VgQt);
        iter++;
    }

    boolean convergence = s < eps;
    if (!convergence) {
        throw new ConvergenceException();
    }

    if (normalize) {
        //elementwise multiplication by normalizing weights
        final RealMatrix W = getNormalizingWeights(A, true);
        A.walkInRowOrder(new DefaultRealMatrixChangingVisitor() {
            @Override
            public double visit(int row, int column, double value) {
                return value * W.getEntry(row, column);
            }
        });
    }

    RealMatrix Phi = Tmat.transpose().multiply(Tmat);
    RotationResults result = new RotationResults(gpFunction.getValue(), L, Phi, Tmat, rotationMethod);
    return result;

}