List of usage examples for org.apache.mahout.math Vector like
Vector like();
From source file:Vectors.java
License:Apache License
public static Vector maybeSample(Vector original, int sampleSize) { if (original.getNumNondefaultElements() <= sampleSize) { return original; }//from w w w.ja v a 2 s. c o m Vector sample = original.like(); Iterator<Vector.Element> sampledElements = new FixedSizeSamplingIterator<Vector.Element>(sampleSize, original.iterateNonZero()); while (sampledElements.hasNext()) { Vector.Element elem = sampledElements.next(); sample.setQuick(elem.index(), elem.get()); } return sample; }
From source file:Vectors.java
License:Apache License
public static Vector topKElements(int k, Vector original) { if (original.getNumNondefaultElements() <= k) { return original; }//from w w w . j ava 2s . co m TopK<Vector.Element> topKQueue = new TopK<Vector.Element>(k, BY_VALUE); Iterator<Vector.Element> nonZeroElements = original.iterateNonZero(); while (nonZeroElements.hasNext()) { Vector.Element nonZeroElement = nonZeroElements.next(); topKQueue.offer(new Vectors.TemporaryElement(nonZeroElement)); } Vector topKSimilarities = original.like(); for (Vector.Element topKSimilarity : topKQueue.retrieve()) { topKSimilarities.setQuick(topKSimilarity.index(), topKSimilarity.get()); } return topKSimilarities; }
From source file:com.elex.dmp.core.TopicModel.java
License:Apache License
public Vector infer(Vector original, Vector docTopics) { Vector pTerm = original.like(); Iterator<Vector.Element> it = original.iterateNonZero(); while (it.hasNext()) { Vector.Element e = it.next(); int term = e.index(); // p(a) = sum_x (p(a|x) * p(x|i)) double pA = 0; for (int x = 0; x < numTopics; x++) { pA += (topicTermCounts.viewRow(x).get(term) / topicSums.get(x)) * docTopics.get(x); }/*from w w w . j av a 2 s . c o m*/ pTerm.set(term, pA); } return pTerm; }
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:mlbench.bayes.BayesUtils.java
License:Apache License
public static NaiveBayesModel readModelFromDir(Path base, Configuration conf) { float alphaI = conf.getFloat(ThetaMapper.ALPHA_I, 1.0f); // read feature sums and label sums Vector scoresPerLabel = null; Vector scoresPerFeature = null; for (Pair<Text, VectorWritable> record : new SequenceFileDirIterable<Text, VectorWritable>( new Path(base, TrainNaiveBayesJob.WEIGHTS), PathType.LIST, PathFilters.partFilter(), conf)) { String key = record.getFirst().toString(); VectorWritable value = record.getSecond(); if (key.equals(TrainNaiveBayesJob.WEIGHTS_PER_FEATURE)) { scoresPerFeature = value.get(); } else if (key.equals(TrainNaiveBayesJob.WEIGHTS_PER_LABEL)) { scoresPerLabel = value.get(); }/*from w w w. j ava2 s . c o m*/ } // Preconditions.checkNotNull(scoresPerFeature); // Preconditions.checkNotNull(scoresPerLabel); Matrix scoresPerLabelAndFeature = new SparseMatrix(scoresPerLabel.size(), scoresPerFeature.size()); for (Pair<IntWritable, VectorWritable> entry : new SequenceFileDirIterable<IntWritable, VectorWritable>( new Path(base, TrainNaiveBayesJob.SUMMED_OBSERVATIONS), PathType.LIST, PathFilters.partFilter(), conf)) { scoresPerLabelAndFeature.assignRow(entry.getFirst().get(), entry.getSecond().get()); } Vector perlabelThetaNormalizer = scoresPerLabel.like(); /* * for (Pair<Text,VectorWritable> entry : new * SequenceFileDirIterable<Text,VectorWritable>( new Path(base, * TrainNaiveBayesJob.THETAS), PathType.LIST, PathFilters.partFilter(), * conf)) { if (entry.getFirst().toString().equals(TrainNaiveBayesJob. * LABEL_THETA_NORMALIZER)) { perlabelThetaNormalizer = * entry.getSecond().get(); } } * * Preconditions.checkNotNull(perlabelThetaNormalizer); */ return new NaiveBayesModel(scoresPerLabelAndFeature, scoresPerFeature, scoresPerLabel, perlabelThetaNormalizer, alphaI, false); }