List of usage examples for org.apache.mahout.math Vector getNumNondefaultElements
int getNumNondefaultElements();
From source file:com.innometrics.integration.app.recommender.ml.als.ImplicitFeedbackAlternatingLeastSquaresSolver.java
License:Apache License
/** Y' (Cu - I) Y + I */ private Matrix getYtransponseCuMinusIYPlusLambdaI(Vector userRatings) { Preconditions.checkArgument(userRatings.isSequentialAccess(), "need sequential access to ratings!"); /* (Cu -I) Y */ OpenIntObjectHashMap<Vector> CuMinusIY = new OpenIntObjectHashMap<Vector>( userRatings.getNumNondefaultElements()); for (Element e : userRatings.nonZeroes()) { CuMinusIY.put(e.index(), Y.get(e.index()).times(confidence(e.get()) - 1)); }/*w w w.ja v a2 s.co m*/ Matrix YtransponseCuMinusIY = new DenseMatrix(numFeatures, numFeatures); /* Y' (Cu -I) Y by outer products */ for (Element e : userRatings.nonZeroes()) { for (Vector.Element feature : Y.get(e.index()).all()) { Vector partial = CuMinusIY.get(e.index()).times(feature.get()); YtransponseCuMinusIY.viewRow(feature.index()).assign(partial, Functions.PLUS); } } /* Y' (Cu - I) Y + I add lambda on the diagonal */ for (int feature = 0; feature < numFeatures; feature++) { YtransponseCuMinusIY.setQuick(feature, feature, YtransponseCuMinusIY.getQuick(feature, feature) + lambda); } return YtransponseCuMinusIY; }
From source file:com.netease.news.classifier.naivebayes.AbstractThetaTrainer.java
License:Apache License
protected AbstractThetaTrainer(Vector weightsPerFeature, Vector weightsPerLabel, double alphaI) { Preconditions.checkNotNull(weightsPerFeature); Preconditions.checkNotNull(weightsPerLabel); this.weightsPerFeature = weightsPerFeature; this.weightsPerLabel = weightsPerLabel; this.alphaI = alphaI; perLabelThetaNormalizer = weightsPerLabel.like(); totalWeightSum = weightsPerLabel.zSum(); numFeatures = weightsPerFeature.getNumNondefaultElements(); }
From source file:com.netease.news.classifier.naivebayes.NaiveBayesModel.java
License:Apache License
public NaiveBayesModel(Matrix weightMatrix, Vector weightsPerFeature, Vector weightsPerLabel, Vector thetaNormalizer, float alphaI) { this.weightsPerLabelAndFeature = weightMatrix; this.weightsPerFeature = weightsPerFeature; this.weightsPerLabel = weightsPerLabel; this.perlabelThetaNormalizer = thetaNormalizer; this.numFeatures = weightsPerFeature.getNumNondefaultElements(); this.totalWeightSum = weightsPerLabel.zSum(); this.alphaI = alphaI; // this.minThetaNormalizer = thetaNormalizer.maxValue(); }
From source file:com.netease.news.classifier.naivebayes.WeightsMapper.java
License:Apache License
@Override protected void map(IntWritable index, VectorWritable value, Context ctx) throws IOException, InterruptedException { Vector instance = value.get(); if (weightsPerFeature == null) { weightsPerFeature = new RandomAccessSparseVector(instance.size(), instance.getNumNondefaultElements()); }//w w w . ja va2 s.c o m int label = index.get(); weightsPerFeature.assign(instance, Functions.PLUS); weightsPerLabel.set(label, weightsPerLabel.get(label) + instance.zSum()); }
From source file:com.pocketx.gravity.recommender.cf.similarity.mapreduce.ToItemVectorsMapper.java
License:Apache License
@Override protected void map(VarLongWritable rowIndex, VectorWritable vectorWritable, Context ctx) throws IOException, InterruptedException { Vector userRatings = vectorWritable.get(); int numElementsBeforeSampling = userRatings.getNumNondefaultElements(); userRatings = Vectors.maybeSample(userRatings, sampleSize); int numElementsAfterSampling = userRatings.getNumNondefaultElements(); int column = TasteHadoopUtils.idToIndex(rowIndex.get()); VectorWritable itemVector = new VectorWritable(new RandomAccessSparseVector(Integer.MAX_VALUE, 1)); itemVector.setWritesLaxPrecision(true); ///*ww w.j a v a2s . c o m*/ Iterator<Vector.Element> iterator = userRatings.nonZeroes().iterator(); // while (iterator.hasNext()) { Vector.Element elem = iterator.next(); itemVector.get().setQuick(column, elem.get()); ctx.write(new IntWritable(elem.index()), itemVector); } ctx.getCounter(Elements.USER_RATINGS_USED).increment(numElementsAfterSampling); ctx.getCounter(Elements.USER_RATINGS_NEGLECTED) .increment(numElementsBeforeSampling - numElementsAfterSampling); }
From source file:com.scaleunlimited.classify.model.RawFeaturesLibLinearModel.java
License:Apache License
private FeatureNode[] vectorToFeatureNodes(Vector vector) { int featureCount = vector.getNumNondefaultElements(); FeatureNode[] x = new FeatureNode[featureCount]; int arrayIndex = 0; int cardinality = vector.size(); for (int i = 0; i < cardinality; i++) { double value = vector.getQuick(i); if (value != 0.0) { // (At least) Linear.train assumes that FeatureNode.index // is 1-based, and we don't really have to map back to our // term indexes, so just add one. YUCK! x[arrayIndex++] = new FeatureNode(i + 1, value); }/* www. ja va 2 s .c o m*/ } return x; }
From source file:com.twitter.algebra.AlgebraCommon.java
License:Apache License
/** * Multiply a vector with transpose of a matrix * @param vector V/*from ww w. j a va 2 s.c o m*/ * @param transpose of matrix M * @param resVector will be filled with V * M * @return V * M */ public static Vector vectorTimesMatrixTranspose(Vector vector, Matrix matrixTranspose, Vector resVector) { int nCols = matrixTranspose.numRows(); for (int c = 0; c < nCols; c++) { Vector col = matrixTranspose.viewRow(c); double resDouble = 0d; boolean hasNonZero = col.getNumNondefaultElements() != 0; if (hasNonZero) resDouble = vector.dot(col); resVector.set(c, resDouble); } return resVector; }
From source file:com.ydy.cf.solver.impl.AlternatingLeastSquaresSolver.java
License:Apache License
private Vector solve(Iterable<Vector> featureVectors, Vector ratingVector, double lambda, int numFeatures) { Preconditions.checkNotNull(featureVectors, "Feature vectors cannot be null"); Preconditions.checkArgument(!Iterables.isEmpty(featureVectors)); Preconditions.checkNotNull(ratingVector, "rating vector cannot be null"); Preconditions.checkArgument(ratingVector.getNumNondefaultElements() > 0, "Rating vector cannot be empty"); Preconditions.checkArgument(Iterables.size(featureVectors) == ratingVector.getNumNondefaultElements()); int nui = ratingVector.getNumNondefaultElements(); Matrix MiIi = createMiIi(featureVectors, numFeatures); Matrix RiIiMaybeTransposed = createRiIiMaybeTransposed(ratingVector); /* compute Ai = MiIi * t(MiIi) + lambda * nui * E */ Matrix Ai = addLambdaTimesNuiTimesE(MiIi.times(MiIi.transpose()), lambda, nui); /* compute Vi = MiIi * t(R(i,Ii)) */ Matrix Vi = MiIi.times(RiIiMaybeTransposed); /* compute Ai * ui = Vi */ return solve(Ai, Vi); }
From source file:com.ydy.cf.solver.impl.AlternatingLeastSquaresSolver.java
License:Apache License
private Matrix createRiIiMaybeTransposed(Vector ratingVector) { Preconditions.checkArgument(ratingVector.isSequentialAccess()); Matrix RiIiMaybeTransposed = new DenseMatrix(ratingVector.getNumNondefaultElements(), 1); Iterator<Vector.Element> ratingsIterator = ratingVector.iterateNonZero(); int index = 0; while (ratingsIterator.hasNext()) { Vector.Element elem = ratingsIterator.next(); RiIiMaybeTransposed.setQuick(index++, 0, elem.get()); }/*from ww w.j a v a 2s . c o m*/ return RiIiMaybeTransposed; }
From source file:de.isabeldrostfromm.sof.naive.VectoriserTest.java
License:Open Source License
@Test public void testBodyVectorisation() { Vectoriser vectorise = new Vectoriser(); Document doc = Document.of("first", "", "", 0.0, new HashSet<String>()); Vector vec = vectorise.vectorise(doc); assertEquals("Adding one term should result in two dimensions set to one.", 2, vec.getNumNondefaultElements()); }