List of usage examples for org.apache.mahout.math DenseVector set
@Override public void set(int index, double value)
From source file:com.twitter.algebra.AlgebraCommon.java
License:Apache License
/** * Multiply a vector with a matrix//from w w w . j a v a2s .com * @param vector V * @param matrix M * @param resVector will be filled with V * M * @return V * M */ public static Vector vectorTimesMatrix(Vector vector, Matrix matrix, DenseVector resVector) { int nCols = matrix.numCols(); for (int c = 0; c < nCols; c++) { Double resDouble = vector.dot(matrix.viewColumn(c)); resVector.set(c, resDouble); } return resVector; }
From source file:org.eclipse.tracecompass.internal.totalads.algorithms.hiddenmarkovmodel.HmmMahout.java
License:Open Source License
/** * Initializes HMM with the customized transition, emission and initial * probabilities rather than using Mahout's initialization. Specially this * function makes sure that initial probabilities are equal. * * @param numSymbols//from www.j a v a2 s .c o m * Number of Symbols * @param numStates * Number of States */ public void initializeHMMWithCustomizeInitialValues(int numSymbols, int numStates) { // Generating transition probabilities with random numbers Random random = new Random(); double start = 0.0001; double end = 1.0000; DenseMatrix tansitionProbabilities = new DenseMatrix(numStates, numStates); // Measuring Transition Probabilities double[] rowSums = new double[numStates]; Arrays.fill(rowSums, 0.0); for (int row = 0; row < numStates; row++) { for (int col = 0; col < numStates; col++) { tansitionProbabilities.set(row, col, getRandomRealNumber(start, end, random)); rowSums[row] += tansitionProbabilities.get(row, col); } } for (int row = 0; row < numStates; row++) { for (int col = 0; col < numStates; col++) { tansitionProbabilities.set(row, col, (tansitionProbabilities.get(row, col) / rowSums[row])); } } // Assigning initial state probabilities Pi; i.e. probabilities at time // 1 DenseVector initialProbabilities = new DenseVector(numStates); double initialProb = 1 / ((double) numStates); for (int idx = 0; idx < numStates; idx++) { initialProbabilities.set(idx, initialProb); } // Measuring Emission probabilities of each symbol DenseMatrix emissionProbabilities = new DenseMatrix(numStates, numSymbols); Arrays.fill(rowSums, 0.0);// Utilizing the same rowSums variable random = new Random(); for (int row = 0; row < numStates; row++) { for (int col = 0; col < numSymbols; col++) { emissionProbabilities.set(row, col, getRandomRealNumber(start, end, random)); rowSums[row] += emissionProbabilities.get(row, col); } } for (int row = 0; row < numStates; row++) { for (int col = 0; col < numSymbols; col++) { emissionProbabilities.set(row, col, emissionProbabilities.get(row, col) / rowSums[row]); } } fHmm = new HmmModel(tansitionProbabilities, emissionProbabilities, initialProbabilities); }
From source file:org.qcri.pca.PCACommon.java
static Vector sparseVectorTimesMatrix(Vector vector, Matrix matrix, DenseVector resVector) { int nCols = matrix.numCols(); for (int c = 0; c < nCols; c++) { Double resDouble = vector.dot(matrix.viewColumn(c)); resVector.set(c, resDouble); }/*from w ww .j a v a 2 s . c om*/ return resVector; }
From source file:org.qcri.pca.PCACommon.java
static Vector denseVectorTimesMatrix(DenseVector vector, Matrix matrix, DenseVector resVector) { int nRows = matrix.numRows(); int nCols = matrix.numCols(); for (int c = 0; c < nCols; c++) { double dotres = 0; for (int r = 0; r < nRows; r++) dotres += vector.getQuick(r) * matrix.getQuick(r, c); resVector.set(c, dotres); }/* w ww . j a v a 2s. com*/ return resVector; }
From source file:org.qcri.pca.PCACommon.java
static Vector vectorTimesMatrixTranspose(Vector vector, Matrix matrix, DenseVector resVector) { int nRows = matrix.numRows(); int nCols = matrix.numCols(); for (int r = 0; r < nRows; r++) { double dotres = 0; for (int c = 0; c < nCols; c++) dotres += vector.getQuick(c) * matrix.getQuick(r, c); resVector.set(r, dotres); }// www . j ava2 s . co m return resVector; }
From source file:root.hap.responsibility.ResponsibilityReducer.java
License:Apache License
/** * <p>/* w w w . j a v a2s. com*/ * This method serves to update the responsibility matrix by accessing a * single column of each matrix. * </p> * * @param keyIn vector identification block * @param valIn vector data */ public void reduce(Text keyIn, Iterable<Text> valIn, Context context) throws IOException, InterruptedException { int N = context.getConfiguration().getInt("matrixN", -1); int numLevels = context.getConfiguration().getInt("numLevels", -1); // data structures to reconstruct the rows we're working on DenseVector A = new DenseVector(N); DenseVector diagA = new DenseVector(N); DenseVector ALevelAbove = new DenseVector(N); DenseVector R = new DenseVector(N); DenseVector diagR = new DenseVector(N); DenseVector S = new DenseVector(N); DenseVector T = new DenseVector(1); DenseVector P = new DenseVector(1); DenseVector C = new DenseVector(1); int reducerRowNum = Integer.valueOf(keyIn.toString().split("\t")[0]); int reducerLevelNum = Integer.valueOf(keyIn.toString().split("\t")[1]); for (Text text : valIn) { // **** important, do not remove this conversion **** // we must do this for some reason..not sure why. // if we don't the loop doesn't iterate properly. // **** important, do not remove this conversion **** text = new Text(text.toString()); String[] textData = KeyUtilities.explode(text, true); String col = textData[KeyUtilities.INDEX]; String val = textData[KeyUtilities.VALUE]; String level = textData[KeyUtilities.LEVEL]; String id = textData[KeyUtilities.ID]; int colInt = Integer.valueOf(col); double valDouble = Double.valueOf(val); int levelInt = Integer.valueOf(level); switch (id.charAt(0)) { case 'R': R.setQuick(colInt, valDouble); break; case 'r': diagR.setQuick(colInt, valDouble); break; case 'A': if (reducerLevelNum == levelInt) { A.setQuick(colInt, valDouble); } else { ALevelAbove.set(colInt, valDouble); } break; case 'a': diagA.setQuick(colInt, valDouble); break; case 'S': S.setQuick(colInt, valDouble); break; case 'C': C.setQuick(0, valDouble); break; case 'T': T.setQuick(0, valDouble); break; case 'P': P.setQuick(0, valDouble); break; default: System.err.println("[ERROR]: Invalid matrix ID."); System.exit(1); break; } } // printInput(A, ALevelAbove, diagA, R, diagR, S, T, P, C); outputTau(context, T, reducerRowNum, reducerLevelNum, "T"); // We want this part to get skipped on the 1st iteration int numIteration = context.getConfiguration().getInt("numIteration", -1); if (numIteration == 0) { outputExemplars(context, C, reducerRowNum, reducerLevelNum, "C"); } else { updateExemplars(context, A, R, diagA, diagR, C, reducerRowNum, reducerLevelNum, N, "C"); } if (reducerLevelNum != numLevels - 1 && numIteration != 0) { updatePhi(context, ALevelAbove, S, P, reducerRowNum, reducerLevelNum, N, "P"); } else { outputPhi(context, P, reducerRowNum, reducerLevelNum, "P"); } outputAvailability(context, A, reducerRowNum, reducerLevelNum, "A"); if (reducerLevelNum == 0) { outputSimilarity(context, S, reducerRowNum, reducerLevelNum, "S"); } updateResponsibility(context, A, S, R, T, reducerLevelNum, reducerRowNum, N, "R"); keyIn = null; valIn = null; A = null; diagA = null; ALevelAbove = null; R = null; diagR = null; S = null; T = null; P = null; C = null; System.gc(); }
From source file:root.hap.responsibility.ResponsibilityReducer.java
License:Apache License
private void updateResponsibility(Context context, DenseVector A, DenseVector S, DenseVector R, DenseVector T, int reducerLevelNum, int reducerRowNum, int N, String responsibilty) throws IOException, InterruptedException { DenseVector oldR = R.clone();//w w w. jav a 2 s. c o m DenseVector sum = (DenseVector) A.plus(S); double maxValue = sum.maxValue(); int maxValueIndex = sum.maxValueIndex(); maxValue *= -1; sum.set(maxValueIndex, Double.NEGATIVE_INFINITY); double tau = T.get(0); double YH = Math.min(maxValue, tau); double actualMax = sum.maxValue(); actualMax *= -1; double YH2 = Math.min(actualMax, tau); R = (DenseVector) S.plus(YH); R.setQuick(maxValueIndex, S.get(maxValueIndex) + YH2); // Dampen double lambda = context.getConfiguration().getFloat("lambda", 0); R = (DenseVector) R.times(1 - lambda); oldR = (DenseVector) oldR.times(lambda); R = (DenseVector) R.plus(oldR); VectorWritable RWritable = new VectorWritable(R); context.write(new Text(reducerRowNum + "\t" + reducerLevelNum + "\t" + responsibilty), RWritable); }
From source file:root.input.util.SimilarityMatrixMapper.java
License:Apache License
@Override protected void map(WritableComparable<?> key, VectorWritable value, Context context) throws IOException, InterruptedException { smatDiagScale = context.getConfiguration().getLong("diagScale", -1); int levels = context.getConfiguration().getInt("numLevels", -1); String keyName = key.toString().substring(1); Vector valVec = value.get();//from w w w . ja v a2 s . c o m int N = seedVectors.size(); DenseVector outSVec = new DenseVector(N); outSVec.assign(0.0); DenseVector out0Vec = new DenseVector(N); out0Vec.assign(0.0); Text outSKey = new Text(keyName + "\t0\tS"); Text outRKey = new Text(); Text outAKey = new Text(); for (NamedVector seedVector : seedVectors) { double distance = measure.distance(seedVector, valVec); String seedVectorName = seedVector.getName().substring(1); int seedVectorPos = Integer.valueOf(seedVectorName); if (keyName.equals(seedVectorName)) { double diagValue = generateSMatDiagValue(smatDiagScale); outSVec.set(seedVectorPos, diagValue); } else { outSVec.set(seedVectorPos, -1 * distance); } } VectorWritable outSVecWritable = new VectorWritable(outSVec); context.write(outSKey, outSVecWritable); VectorWritable out0VecWritable = new VectorWritable(out0Vec); for (int i = 0; i < levels; i++) { outRKey = new Text(keyName + "\t" + i + "\tR"); outAKey = new Text(keyName + "\t" + i + "\tA"); context.write(outRKey, out0VecWritable); context.write(outAKey, out0VecWritable); } Text outCKey = new Text(); Text outTKey = new Text(); Text outPKey = new Text(); out0Vec = new DenseVector(1); out0Vec.assign(0.0); DenseVector outTVec = new DenseVector(1); // outTVec.assign(Double.MAX_VALUE / 2); outTVec.assign(TAU_INIT); out0VecWritable = new VectorWritable(out0Vec); VectorWritable outTVecWritable = new VectorWritable(outTVec); for (int i = 0; i < levels; i++) { outCKey = new Text(keyName + "\t" + i + "\tC"); outTKey = new Text(keyName + "\t" + i + "\tT"); outPKey = new Text(keyName + "\t" + i + "\tP"); context.write(outCKey, out0VecWritable); context.write(outTKey, outTVecWritable); context.write(outPKey, out0VecWritable); } }