List of usage examples for org.apache.commons.math3.linear OpenMapRealMatrix OpenMapRealMatrix
public OpenMapRealMatrix(int rowDimension, int columnDimension) throws NotStrictlyPositiveException, NumberIsTooLargeException
From source file:edu.mit.genecircuits.net.ExpressionMatrix.java
/** Load expression matrix from file */ public void load(String filename) { // Count lines int numNodes = FileParser.countLines(filename) - 1; nodes_ = new String[numNodes]; // Open the file FileParser parser = new FileParser(filename, true); // Header/*from w w w .ja va 2s. co m*/ samples_ = parser.readLine(); // Initialize expression matrix X_ = new OpenMapRealMatrix(numNodes, samples_.length); for (int i = 0;; i++) { String[] nextLine = parser.readLine(); if (nextLine == null) break; // Check length if (nextLine.length != samples_.length + 1) throw new RuntimeException("Incorrect number of columns"); // Parse row nodes_[i] = nextLine[0]; for (int j = 0; j < nextLine.length - 1; j++) X_.setEntry(i, j, Double.parseDouble(nextLine[j + 1])); } parser.close(); }
From source file:com.datumbox.framework.common.dataobjects.MatrixDataframe.java
/** * Method used to generate a training Dataframe to a MatrixDataframe and extracts its contents to Matrixes. It populates the featureIdsReference map with the mappings * between the feature names and the column ids of the matrix. Typically used * to convert the training dataset./*w ww. j a v a 2s .co m*/ * * @param dataset * @param addConstantColumn * @param recordIdsReference * @param featureIdsReference * @return */ public static MatrixDataframe newInstance(Dataframe dataset, boolean addConstantColumn, Map<Integer, Integer> recordIdsReference, Map<Object, Integer> featureIdsReference) { if (!featureIdsReference.isEmpty()) { throw new IllegalArgumentException("The featureIdsReference map should be empty."); } int n = dataset.size(); int d = dataset.xColumnSize(); if (addConstantColumn) { ++d; } MatrixDataframe m = new MatrixDataframe(new OpenMapRealMatrix(n, d), new ArrayRealVector(n)); if (dataset.isEmpty()) { return m; } boolean extractY = (dataset.getYDataType() == TypeInference.DataType.NUMERICAL); int featureId = 0; if (addConstantColumn) { for (int row = 0; row < n; ++row) { m.X.setEntry(row, featureId, 1.0); //put the constant in evey row } featureIdsReference.put(Dataframe.COLUMN_NAME_CONSTANT, featureId); ++featureId; } int rowId = 0; for (Map.Entry<Integer, Record> e : dataset.entries()) { Integer rId = e.getKey(); Record r = e.getValue(); if (recordIdsReference != null) { recordIdsReference.put(rId, rowId); } if (extractY) { m.Y.setEntry(rowId, TypeInference.toDouble(r.getY())); } for (Map.Entry<Object, Object> entry : r.getX().entrySet()) { Object feature = entry.getKey(); Integer knownFeatureId = featureIdsReference.get(feature); if (knownFeatureId == null) { featureIdsReference.put(feature, featureId); knownFeatureId = featureId; ++featureId; } Double value = TypeInference.toDouble(entry.getValue()); if (value != null) { m.X.setEntry(rowId, knownFeatureId, value); } //else the X matrix maintains the 0.0 default value } ++rowId; } return m; }
From source file:edu.cmu.tetrad.util.TetradMatrix.java
public static TetradMatrix sparseMatrix(int m, int n) { return new TetradMatrix(new OpenMapRealMatrix(m, n)); }
From source file:edu.cmu.tetrad.util.TetradMatrix1.java
public static TetradMatrix1 sparseMatrix(int m, int n) { return new TetradMatrix1(new OpenMapRealMatrix(m, n)); }
From source file:com.datumbox.framework.common.dataobjects.MatrixDataframe.java
/** * Parses a testing dataset and converts it to MatrixDataframe by using an already existing mapping between feature names and column ids. Typically used * to parse the testing or validation dataset. * /*from w w w .java2s. c o m*/ * @param newData * @param recordIdsReference * @param featureIdsReference * @return */ public static MatrixDataframe parseDataset(Dataframe newData, Map<Integer, Integer> recordIdsReference, Map<Object, Integer> featureIdsReference) { if (featureIdsReference.isEmpty()) { throw new IllegalArgumentException("The featureIdsReference map should not be empty."); } int n = newData.size(); int d = featureIdsReference.size(); MatrixDataframe m = new MatrixDataframe(new OpenMapRealMatrix(n, d), new ArrayRealVector(n)); if (newData.isEmpty()) { return m; } boolean extractY = (newData.getYDataType() == TypeInference.DataType.NUMERICAL); boolean addConstantColumn = featureIdsReference.containsKey(Dataframe.COLUMN_NAME_CONSTANT); int rowId = 0; for (Map.Entry<Integer, Record> e : newData.entries()) { Integer rId = e.getKey(); Record r = e.getValue(); if (recordIdsReference != null) { recordIdsReference.put(rId, rowId); } if (extractY) { m.Y.setEntry(rowId, TypeInference.toDouble(r.getY())); } if (addConstantColumn) { m.X.setEntry(rowId, 0, 1.0); //add the constant column } for (Map.Entry<Object, Object> entry : r.getX().entrySet()) { Object feature = entry.getKey(); Double value = TypeInference.toDouble(entry.getValue()); if (value != null) { Integer featureId = featureIdsReference.get(feature); if (featureId != null) {//if the feature exists in our database m.X.setEntry(rowId, featureId, value); } } //else the X matrix maintains the 0.0 default value } ++rowId; } return m; }
From source file:com.datumbox.framework.core.common.dataobjects.MapRealVector.java
/** {@inheritDoc} */ @Override//from www . ja va2 s .c o m public RealMatrix outerProduct(RealVector v) { final int m = this.getDimension(); final int n = v.getDimension(); final RealMatrix product; if (m > 1000000) { //use only in big values product = new MapRealMatrix(m, n); } else { product = new OpenMapRealMatrix(m, n); } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { product.setEntry(i, j, this.getEntry(i) * v.getEntry(j)); } } return product; }
From source file:simrankapp.SimRankAlgorithm.java
public static <V> SparseRealMatrix computeSimRank(Graph<V, ?> g, int maxIter) { System.out.println("Inside compute simrank"); int numNodes = g.getVertexCount(); SparseRealMatrix currentR = computeInitialSimRank(g); SparseRealMatrix nextR = new OpenMapRealMatrix(numNodes, numNodes); int[][] status = new int[numNodes][numNodes]; nextR = currentR;/*from w ww . j a va 2 s .c o m*/ for (int k = 0; k < maxIter && maxIter > 0; k++) { for (int l = 0; l < numNodes; l++) { for (int m = 0; m < numNodes; m++) { if (l == m) status[l][m] = 1; status[l][m] = 0; // Unprocessed } } for (V a : g.getVertices()) { for (V b : g.getVertices()) { int i = Integer.valueOf(String.valueOf(a)); int j = Integer.valueOf(String.valueOf(b)); if (status[i][j] == 0) { if (i != j) { float sum = computeSum(g, a, b, currentR); int sia = g.inDegree(a); int sib = g.inDegree(b); // if(i==j) // continue; System.out.println("Inside compute simrank : computing for " + i + " and " + j); if (sia == 0 || sib == 0) { nextR.setEntry(i, j, 0.0f); nextR.setEntry(j, i, 0.0f); } else { nextR.setEntry(i, j, (DEFAULT_C / (sia * sib)) * sum); nextR.setEntry(j, i, (DEFAULT_C / (sia * sib)) * sum); } } } status[i][j] = 1; status[j][i] = 1; } } //System.out.println("After iteration "+k); //print(g, nextR); currentR = nextR; } return currentR; }
From source file:simrankapp.SimRankAlgorithm.java
/** * Compute the initial SimRank for the vertices of the given graph. * This initial SimRank for two vertices (a,b) is 0.0f when * a != b, and 1.0f when a == b/*from ww w . j a va2s. c o m*/ */ private static <V> SparseRealMatrix computeInitialSimRank(Graph<V, ?> g) { System.out.println("Inside compute initial simrank"); int numNodes = g.getVertexCount(); SparseRealMatrix initialSimRankMatrix = new OpenMapRealMatrix(numNodes, numNodes); for (V a : g.getVertices()) { for (V b : g.getVertices()) { int i = Integer.valueOf(String.valueOf(a)); int j = Integer.valueOf(String.valueOf(b)); if (a.equals(b)) { initialSimRankMatrix.setEntry(i, j, 1.0f); } else { initialSimRankMatrix.setEntry(i, j, 0.0f); } } } // ReaderWriterUtil.writeSparseMatrix(initialSimRankMatrix, "InitialSimRank.txt"); return initialSimRankMatrix; }