本文整理汇总了Java中org.apache.mahout.math.Vector.size方法的典型用法代码示例。如果您正苦于以下问题:Java Vector.size方法的具体用法?Java Vector.size怎么用?Java Vector.size使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.mahout.math.Vector
的用法示例。
在下文中一共展示了Vector.size方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: distance
import org.apache.mahout.math.Vector; //导入方法依赖的package包/类
@Override
public double distance(Vector v1, Vector v2) {
if (v1.size() != v2.size()) {
throw new CardinalityException(v1.size(), v2.size());
}
double lengthSquaredv1 = v1.getLengthSquared();
double lengthSquaredv2 = v2.getLengthSquared();
double dotProduct = v2.dot(v1);
double denominator = Math.sqrt(lengthSquaredv1) * Math.sqrt(lengthSquaredv2);
// correct for floating-point rounding errors
if (denominator < dotProduct) {
denominator = dotProduct;
}
// correct for zero-vector corner case
if (denominator == 0 && dotProduct == 0) {
return 1;
}
return 1.0 - dotProduct / denominator;
}
示例2: addInteractionToVector
import org.apache.mahout.math.Vector; //导入方法依赖的package包/类
/**
* Adds a value to a vector.
*
* @param originalForm1 The original form of the first value as a byte array.
* @param originalForm2 The original form of the second value as a byte array.
* @param weight How much to weight this interaction
* @param data The vector to which the value should be added.
*/
public void addInteractionToVector(byte[] originalForm1, byte[] originalForm2, double weight, Vector data) {
String name = getName();
double w = getWeight(originalForm1, originalForm2, weight);
for (int i = 0; i < probes(); i++) {
Iterable<Integer> jValues =
secondEncoder.hashesForProbe(originalForm2, data.size(), name, i % secondEncoder.getProbes());
for (Integer k : firstEncoder.hashesForProbe(originalForm1, data.size(), name, i % firstEncoder.getProbes())) {
for (Integer j : jValues) {
int n = (k + j) % data.size();
if (isTraceEnabled()) {
trace(String.format("%s:%s", Arrays.toString(originalForm1), Arrays.toString(originalForm2)), n);
}
data.set(n, data.get(n) + w);
}
}
}
}
示例3: sampleFromPrior
import org.apache.mahout.math.Vector; //导入方法依赖的package包/类
@Override
public Model<VectorWritable>[] sampleFromPrior(int howMany) {
Model<VectorWritable>[] result = new GaussianCluster[howMany];
for (int i = 0; i < howMany; i++) {
Vector prototype = getModelPrototype().get();
Vector mean = prototype.like();
for (int j = 0; j < prototype.size(); j++) {
mean.set(j, UncommonDistributions.rNorm(0, 1));
}
Vector sd = prototype.like();
for (int j = 0; j < prototype.size(); j++) {
sd.set(j, UncommonDistributions.rNorm(1, 1));
}
result[i] = new GaussianCluster(mean, sd, i);
}
return result;
}
示例4: rMultinom
import org.apache.mahout.math.Vector; //导入方法依赖的package包/类
/** Returns one sample from a multinomial. */
public static int rMultinom(Vector probabilities) {
// our probability argument are not normalized.
double total = probabilities.zSum();
double nextDouble = RANDOM.nextDouble();
double p = nextDouble * total;
for (int i = 0; i < probabilities.size(); i++) {
double pi = probabilities.get(i);
if (p < pi) {
return i;
} else {
p -= pi;
}
}
// can't happen except for round-off error so we don't care what we return here
return 0;
}
示例5: TopicModel
import org.apache.mahout.math.Vector; //导入方法依赖的package包/类
public TopicModel(Matrix topicTermCounts, Vector topicSums, double eta, double alpha,
String[] dictionary, int numThreads, double modelWeight) {
this.dictionary = dictionary;
this.topicTermCounts = topicTermCounts;
this.topicSums = topicSums;
this.numTopics = topicSums.size();
this.numTerms = topicTermCounts.numCols();
this.eta = eta;
this.alpha = alpha;
this.sampler = new Sampler(RandomUtils.getRandom());
this.numThreads = numThreads;
if (modelWeight != 1) {
topicSums.assign(Functions.mult(modelWeight));
for (int x = 0; x < numTopics; x++) {
topicTermCounts.viewRow(x).assign(Functions.mult(modelWeight));
}
}
initializeThreadPool();
}
示例6: sampleFromPrior
import org.apache.mahout.math.Vector; //导入方法依赖的package包/类
@Override
public Model<VectorWritable>[] sampleFromPrior(int howMany) {
Model<VectorWritable>[] result = new DistanceMeasureCluster[howMany];
Vector prototype = getModelPrototype().get().like();
for (int i = 0; i < prototype.size(); i++) {
prototype.setQuick(i, UncommonDistributions.rNorm(0, 1));
}
for (int i = 0; i < howMany; i++) {
result[i] = new DistanceMeasureCluster(prototype, i, measure);
}
return result;
}
示例7: testMAHOUT_1238
import org.apache.mahout.math.Vector; //导入方法依赖的package包/类
@Test
public void testMAHOUT_1238() throws IOException {
Vector v = new SequentialAccessSparseVector(5);
v.set(1, 3.0);
v.set(3, 5.0);
Vector view = new VectorView(v,0,v.size());
doTestVectorWritableEquals(view);
}
示例8: rDirichlet
import org.apache.mahout.math.Vector; //导入方法依赖的package包/类
/**
* Sample from a Dirichlet distribution, returning a vector of probabilities using a stick-breaking
* algorithm
*
* @param totalCounts
* an unnormalized count Vector
* @param alpha0
* a double
* @return a Vector of probabilities
*/
public static Vector rDirichlet(Vector totalCounts, double alpha0) {
Vector pi = totalCounts.like();
double total = totalCounts.zSum();
double remainder = 1.0;
for (int k = 0; k < pi.size(); k++) {
double countK = totalCounts.get(k);
total -= countK;
double betaK = rBeta(1.0 + countK, Math.max(0.0, alpha0 + total));
double piK = betaK * remainder;
pi.set(k, piK);
remainder -= piK;
}
return pi;
}
示例9: distance
import org.apache.mahout.math.Vector; //导入方法依赖的package包/类
@Override
public double distance(Vector v1, Vector v2) {
if (v1.size() != v2.size()) {
throw new CardinalityException(v1.size(), v2.size());
}
return Math.sqrt(v1.minus(v2).dot(Algebra.mult(inverseCovarianceMatrix, v1.minus(v2))));
}
示例10: median
import org.apache.mahout.math.Vector; //导入方法依赖的package包/类
/**
* A quick and dirty hack to compute the median of a vector...
* @param v
* @return
*/
private static double median(Vector v) {
OnlineSummarizer med = new OnlineSummarizer();
if (v.size() < 100) {
return v.zSum() / v.size();
}
for (Vector.Element e : v) {
med.add(e.get());
}
return med.getMedian();
}
示例11: getAverageStd
import org.apache.mahout.math.Vector; //导入方法依赖的package包/类
@Override
public double getAverageStd() {
if (sumWeight == 0.0) {
return 0.0;
} else {
Vector std = getStd();
return std.zSum() / std.size();
}
}
示例12: loadVectors
import org.apache.mahout.math.Vector; //导入方法依赖的package包/类
private static Matrix loadVectors(String vectorPathString, Configuration conf)
throws IOException {
Path vectorPath = new Path(vectorPathString);
FileSystem fs = vectorPath.getFileSystem(conf);
List<Path> subPaths = Lists.newArrayList();
if (fs.isFile(vectorPath)) {
subPaths.add(vectorPath);
} else {
for (FileStatus fileStatus : fs.listStatus(vectorPath, PathFilters.logsCRCFilter())) {
subPaths.add(fileStatus.getPath());
}
}
List<Pair<Integer, Vector>> rowList = Lists.newArrayList();
int numRows = Integer.MIN_VALUE;
int numCols = -1;
boolean sequentialAccess = false;
for (Path subPath : subPaths) {
for (Pair<IntWritable, VectorWritable> record
: new SequenceFileIterable<IntWritable, VectorWritable>(subPath, true, conf)) {
int id = record.getFirst().get();
Vector vector = record.getSecond().get();
if (vector instanceof NamedVector) {
vector = ((NamedVector)vector).getDelegate();
}
if (numCols < 0) {
numCols = vector.size();
sequentialAccess = vector.isSequentialAccess();
}
rowList.add(Pair.of(id, vector));
numRows = Math.max(numRows, id);
}
}
numRows++;
Vector[] rowVectors = new Vector[numRows];
for (Pair<Integer, Vector> pair : rowList) {
rowVectors[pair.getFirst()] = pair.getSecond();
}
return new SparseRowMatrix(numRows, numCols, rowVectors, true, !sequentialAccess);
}