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

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

Introduction

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

Prototype

public SingularValueDecomposition(final RealMatrix matrix) 

Source Link

Document

Calculates the compact Singular Value Decomposition of the given matrix.

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 av  a 2  s.co m*/

    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;//from ww  w. ja  va  2 s .c o m
    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:dataminning2.SVDDecomposition.java

public double[][] DecompositionU(double[][] Dataarraytest) {
    Array2DRowRealMatrix rmA = new Array2DRowRealMatrix(Dataarraytest);
    SingularValueDecomposition svdObj = new SingularValueDecomposition(rmA);
    double[][] DataU = svdObj.getU().getData();

    return DataU;
}

From source file:com.opengamma.strata.math.impl.linearalgebra.SVDecompositionCommons.java

@Override
public SVDecompositionResult apply(DoubleMatrix x) {
    ArgChecker.notNull(x, "x");
    MatrixValidate.notNaNOrInfinite(x);/*from  w w  w  . ja  v a 2 s .c  o  m*/
    RealMatrix commonsMatrix = CommonsMathWrapper.wrap(x);
    SingularValueDecomposition svd = new SingularValueDecomposition(commonsMatrix);
    return new SVDecompositionCommonsResult(svd);
}

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:com.opengamma.strata.math.impl.matrix.CommonsMatrixAlgebra.java

@Override
public double getCondition(Matrix m) {
    ArgChecker.notNull(m, "m");
    if (m instanceof DoubleMatrix) {
        RealMatrix temp = CommonsMathWrapper.wrap((DoubleMatrix) m);
        SingularValueDecomposition svd = new SingularValueDecomposition(temp);
        return svd.getConditionNumber();
    }/*from w w w . java  2s. c o  m*/
    throw new IllegalArgumentException("Can only find condition number of DoubleMatrix; have " + m.getClass());
}

From source file:dataminning2.SVDDecomposition.java

public double[][] DecompositionS(double[][] Dataarraytest) {
    Array2DRowRealMatrix rmA = new Array2DRowRealMatrix(Dataarraytest);
    SingularValueDecomposition svdObj = new SingularValueDecomposition(rmA);
    double[][] Sobj = svdObj.getS().getData();
    return Sobj;//from w ww  . j a va 2  s  .  co  m
}

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

/**
 * Computes the inverse of a matrix using the singular value decomposition.
 * //from   ww w . j av a 2 s .  co  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:com.cloudera.oryx.kmeans.computation.covariance.CovarianceDataBuilder.java

public DistanceData getDistanceData() {
    return new DistanceData(means, new SingularValueDecomposition(rawCoMoment).getSolver().getInverse());
}

From source file:com.caseystella.analytics.outlier.batch.rpca.RidgeRegression.java

public void updateCoefficients(double l2penalty) {
    if (this.X_svd == null) {
        this.X_svd = new SingularValueDecomposition(X);
    }/*from   ww  w  . ja  v  a 2s  . c o m*/
    RealMatrix V = this.X_svd.getV();
    double[] s = this.X_svd.getSingularValues();
    RealMatrix U = this.X_svd.getU();

    for (int i = 0; i < s.length; i++) {
        s[i] = s[i] / (s[i] * s[i] + l2penalty);
    }
    RealMatrix S = MatrixUtils.createRealDiagonalMatrix(s);

    RealMatrix Z = V.multiply(S).multiply(U.transpose());

    this.coefficients = Z.operate(this.Y);

    this.fitted = this.X.operate(this.coefficients);
    double errorVariance = 0;
    for (int i = 0; i < residuals.length; i++) {
        this.residuals[i] = this.Y[i] - this.fitted[i];
        errorVariance += this.residuals[i] * this.residuals[i];
    }
    errorVariance = errorVariance / (X.getRowDimension() - X.getColumnDimension());

    RealMatrix errorVarianceMatrix = MatrixUtils.createRealIdentityMatrix(this.Y.length)
            .scalarMultiply(errorVariance);
    RealMatrix coefficientsCovarianceMatrix = Z.multiply(errorVarianceMatrix).multiply(Z.transpose());
    this.standarderrors = getDiagonal(coefficientsCovarianceMatrix);
}