List of usage examples for org.apache.mahout.math DenseVector setQuick
@Override
public void setQuick(int index, double value)
From source file:org.qcri.pca.ReconstructionErrJob.java
static void denseVectorPlusAbsDenseDiff(DenseVector denseVector, Vector sparseVector, DenseVector meanVector) { for (int i = 0; i < denseVector.size(); i++) { double denseV = denseVector.getQuick(i); double v = sparseVector.getQuick(i); double mean = meanVector.getQuick(i); denseVector.setQuick(i, denseV + Math.abs(v - mean)); }//w w w . j a v a 2 s . c o m }
From source file:org.qcri.pca.ReconstructionErrJob.java
static void denseVectorPlusAbsSparseVector(DenseVector denseVector, Vector sparseVector) { Iterator<Vector.Element> nonZeroElements = sparseVector.nonZeroes().iterator(); while (nonZeroElements.hasNext()) { Vector.Element e = nonZeroElements.next(); int index = e.index(); double v = e.get(); double prevV = denseVector.getQuick(index); denseVector.setQuick(index, prevV + Math.abs(v)); }//from ww w. j a v a2 s. c o m }
From source file:org.qcri.pca.ReconstructionErrJob.java
static void denseVectorSubtractSparseSubtractDense(DenseVector mainVector, Vector subtractor1, DenseVector subtractor2) {/*from ww w. j a v a 2 s. co m*/ int nCols = mainVector.size(); for (int c = 0; c < nCols; c++) { double v = mainVector.getQuick(c); v -= subtractor1.getQuick(c); v -= subtractor2.getQuick(c); mainVector.setQuick(c, v); } }
From source file:root.hap.availability.AvailabilityReducer.java
License:Apache License
/** * <p>/*from w w w .j a v a 2 s . co m*/ * This method serves to update the availability matrix by accessing a * single row 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); // data structures to reconstruct the rows we're working on DenseVector A = new DenseVector(N); DenseVector R = new DenseVector(N); DenseVector RLevelBelow = new DenseVector(N); DenseVector S = new DenseVector(N); DenseVector T = new DenseVector(1); DenseVector P = new DenseVector(1); DenseVector C = new DenseVector(1); DenseVector CLevelBelow = new DenseVector(1); int reducerColNum = 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 id = textData[KeyUtilities.ID]; String row = textData[KeyUtilities.INDEX]; String level = textData[KeyUtilities.LEVEL]; String value = textData[KeyUtilities.VALUE]; int rowInt = Integer.valueOf(row); double valDouble = Double.valueOf(value); int levelInt = Integer.valueOf(level); switch (id.charAt(0)) { case 'R': if (reducerLevelNum == levelInt) { R.setQuick(rowInt, valDouble); } else { RLevelBelow.setQuick(rowInt, valDouble); } break; case 'A': A.setQuick(rowInt, valDouble); break; case 'S': S.setQuick(rowInt, valDouble); break; case 'C': if (reducerLevelNum == levelInt) { C.setQuick(0, valDouble); } else { CLevelBelow.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, R, RLevelBelow, S, T, P, C, CLevelBelow); outputPhi(context, P, reducerColNum, reducerLevelNum, "P"); outputExemplars(context, C, reducerColNum, reducerLevelNum, "C"); if (reducerLevelNum != 0) { updateTau(context, RLevelBelow, CLevelBelow, T, reducerColNum, reducerLevelNum, N, "T"); } else { outputTau(context, T, reducerColNum, reducerLevelNum, "T"); } if (reducerLevelNum == 0) { outputSimilarity(context, S, reducerColNum, reducerLevelNum, "S"); } outputResponsibility(context, R, reducerColNum, reducerLevelNum, "R"); updateAvailability(context, A, R, S, P, C, reducerColNum, reducerLevelNum, N, "A"); keyIn = null; valIn = null; A = null; R = null; RLevelBelow = null; S = null; T = null; P = null; C = null; CLevelBelow = null; System.gc(); }
From source file:root.hap.availability.AvailabilityReducer.java
License:Apache License
private void updateTau(Context context, DenseVector RLevelBelow, DenseVector CLevelBelow, DenseVector T, int reducerColNum, int reducerLevelNum, int N, String tau) throws IOException, InterruptedException { double sumOfPositives = 0.0; for (int rowNum = 0; rowNum < N; rowNum++) { if (rowNum == reducerColNum) { continue; }//from ww w . j a v a 2s. c o m double value = RLevelBelow.get(rowNum); sumOfPositives += (value > 0) ? value : 0; } double rhoValue = RLevelBelow.get(reducerColNum); double exemplarValue = CLevelBelow.get(0); // "CValue" double tauValue = rhoValue + exemplarValue + sumOfPositives; T.setQuick(0, tauValue); VectorWritable TWritable = new VectorWritable(T); context.write(new Text(reducerColNum + "\t" + reducerLevelNum + "\t" + tau), TWritable); }
From source file:root.hap.availability.AvailabilityReducer.java
License:Apache License
private void updateAvailability(Context context, DenseVector A, DenseVector R, DenseVector S, DenseVector P, DenseVector C, int reducerColNum, int reducerLevelNum, int N, String availability) throws IOException, InterruptedException { DenseVector oldA = A.clone();// www . j av a2 s . c o m // get positive values into RPositive DenseVector RPositive = R.clone(); for (int rowNum = 0; rowNum < N; rowNum++) { double RValue = RPositive.get(rowNum); if (RValue < 0) { RPositive.setQuick(rowNum, 0); } } // reset diagonal value from R RPositive.setQuick(reducerColNum, R.get(reducerColNum)); // sum R Positive values double RPSum = RPositive.zSum(); double CVal = C.get(0); double PVal = P.get(0); double CHat = CVal + PVal; // sum together CHat and RPSum into a vector A.assign(CHat + RPSum); A = (DenseVector) A.minus(RPositive); for (int rowNum = 0; rowNum < N; rowNum++) { if (rowNum == reducerColNum) { continue; } double value = A.get(rowNum) < 0 ? A.get(rowNum) : 0; A.setQuick(rowNum, value); } double lambda = context.getConfiguration().getFloat("lambda", 0); A = (DenseVector) A.times(1 - lambda); oldA = (DenseVector) oldA.times(lambda); A = (DenseVector) A.plus(oldA); VectorWritable AWritable = new VectorWritable(A); context.write(new Text(reducerColNum + "\t" + reducerLevelNum + "\t" + availability), AWritable); }
From source file:root.hap.cluster.ClusterReducer.java
License:Apache License
/** * <p>/*from www .j a va 2 s . c o m*/ * This method serves to extract cluster data by accessing a * single row and the diagonal of each matrix. Diagonal values are used * to determine if the exemplar is in a valid location (specifically if * the diagonal value is positive). * </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); // data structures to reconstruct the rows we're working on DenseVector A = new DenseVector(N); DenseVector diagA = new DenseVector(N); DenseVector R = new DenseVector(N); DenseVector diagR = new DenseVector(N); 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 id = textData[KeyUtilities.ID]; String row = textData[KeyUtilities.INDEX]; String value = textData[KeyUtilities.VALUE]; int rowInt = Integer.valueOf(row); double valDouble = Double.valueOf(value); switch (id.charAt(0)) { case 'R': R.setQuick(rowInt, valDouble); break; case 'r': diagR.setQuick(rowInt, valDouble); break; case 'A': A.setQuick(rowInt, valDouble); break; case 'a': diagA.setQuick(rowInt, valDouble); break; default: System.err.println("[ERROR]: Invalid matrix ID."); System.exit(1); break; } } // printInput( A, diagA, R, diagR ); updateExemplars(context, A, diagA, R, diagR, reducerRowNum, reducerLevelNum, N); keyIn = null; valIn = null; A = null; diagA = null; R = null; diagR = null; System.gc(); }
From source file:root.hap.responsibility.ResponsibilityReducer.java
License:Apache License
/** * <p>/*from ww w .ja v a 2 s. c o m*/ * 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 updateExemplars(Context context, DenseVector A, DenseVector R, DenseVector diagA, DenseVector diagR, DenseVector C, int reducerRowNum, int reducerLevelNum, int N, String exemplar) throws IOException, InterruptedException { boolean[] validExemplars = new boolean[N]; boolean validExist = false; Arrays.fill(validExemplars, false); for (int diagIter = 0; diagIter < N; diagIter++) { double diagVal = diagA.get(diagIter) + diagR.get(diagIter); if (diagVal > 0) { validExemplars[diagIter] = true; validExist = true;//w w w . j a v a 2 s . c o m } } DenseVector sum = (DenseVector) A.plus(R); double maxValue = Double.NEGATIVE_INFINITY; if (validExist) { for (int diagIter = 0; diagIter < N; diagIter++) { double validValue = sum.get(diagIter); if (validExemplars[diagIter] && validValue > maxValue) { maxValue = validValue; } } } else { maxValue = sum.maxValue(); } C.setQuick(0, maxValue); VectorWritable CWritable = new VectorWritable(C); context.write(new Text(reducerRowNum + "\t" + reducerLevelNum + "\t" + exemplar), CWritable); }
From source file:root.hap.responsibility.ResponsibilityReducer.java
License:Apache License
private void updatePhi(Context context, DenseVector ALevelAbove, DenseVector SLevelAbove, DenseVector P, int reducerRowNum, int reducerLevelNum, int N, String phi) throws IOException, InterruptedException { DenseVector sum = (DenseVector) ALevelAbove.plus(SLevelAbove); double maxValue = sum.maxValue(); P.setQuick(0, maxValue); VectorWritable PWritable = new VectorWritable(P); context.write(new Text(reducerRowNum + "\t" + reducerLevelNum + "\t" + phi), PWritable); }