本文整理汇总了Java中cc.mallet.types.SparseVector.valueAtLocation方法的典型用法代码示例。如果您正苦于以下问题:Java SparseVector.valueAtLocation方法的具体用法?Java SparseVector.valueAtLocation怎么用?Java SparseVector.valueAtLocation使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cc.mallet.types.SparseVector
的用法示例。
在下文中一共展示了SparseVector.valueAtLocation方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: euclideanDistance
import cc.mallet.types.SparseVector; //导入方法依赖的package包/类
public double euclideanDistance(SparseVector a, SparseVector b) {
double dist = 0;
double diff;
if (a==null || b==null)
throw new IllegalArgumentException("Distance from a null vector is undefined.");
int aLen = a.numLocations();
int bLen = b.numLocations();
int ia = 0;
int ib = 0;
int indicea, indiceb;
while (ia < aLen && ib < bLen) {
indicea = a.indexAtLocation(ia);
indiceb = b.indexAtLocation(ib);
if(indicea < indiceb) {
diff = a.valueAtLocation(ia);
ia ++;
}
else {
if(indicea == indiceb) {
diff = Math.abs(a.valueAtLocation(ia) - b.valueAtLocation(ib));
ia ++;
ib ++;
}
else
{
diff = b.valueAtLocation(ib);
ib ++;
}
}
dist += diff * diff;
}
while(ia < aLen) {
diff = a.valueAtLocation(ia);
dist += diff * diff;
}
while(ib < bLen) {
diff = b.valueAtLocation(ib);
dist += diff * diff;
}
dist = Math.sqrt(dist);
return dist;
}
示例2: log
import cc.mallet.types.SparseVector; //导入方法依赖的package包/类
static void log(SparseVector v, double factor) {
for (int i = 0; i < v.numLocations(); i++) {
double value = factor * v.valueAtLocation(i);
v.setValueAtLocation(i, Math.log(value));
}
}
示例3: train
import cc.mallet.types.SparseVector; //导入方法依赖的package包/类
public void train(InstanceList instances) {
this.instances = instances;
SparseVector[] ws;
if (getThreads() > 1) {
ws = trainByMultiThread(getThreads());
} else {
ws = trainBySingleThread();
}
int dim = instances.getDataAlphabet().size();
for (int i = 0; i < ws.length; i++) {
SparseVector w = ws[i];
int count = 0;
for (int l = 0; l < w.numLocations(); l++) {
double val = w.valueAtLocation(l);
if (val < 0) {
w.setValueAtLocation(l, 0);
count++;
}
}
logger.info("Turn " + count + " negative values to 0 for w_" + i);
}
// svd the weight matrix [w_1 w_2 ... w_m]
// for each group SVD
// each group has a map from its name to its indices[]
// after SVD, read values[] from Ut, and build SparseVector with
// indices[]
try {
SVDLIBC svd = new SVDLIBC();
File matF = svd.newFile(svd.input);
// write the matrix
// it's not necessary
// but it saves time for trying different h
List<SparseVector> mat = Arrays.asList(ws);
svd.writeSparse(matF, mat, dim, ws.length);
// do the SVD
svd(mat);
} catch (IOException e) {
e.printStackTrace();
}
}
示例4: distance
import cc.mallet.types.SparseVector; //导入方法依赖的package包/类
public double distance(SparseVector a, SparseVector b) {
double dist = 0;
double diff;
if (a==null || b==null) {
throw new IllegalArgumentException("Distance from a null vector is undefined.");
}
int leftLength = a.numLocations();
int rightLength = b.numLocations();
int leftIndex = 0;
int rightIndex = 0;
int leftFeature, rightFeature;
// We assume that features are sorted in ascending order.
// We'll walk through the two feature lists in order, checking
// whether the two features are the same.
while (leftIndex < leftLength && rightIndex < rightLength) {
leftFeature = a.indexAtLocation(leftIndex);
rightFeature = b.indexAtLocation(rightIndex);
if (leftFeature < rightFeature) {
diff = a.valueAtLocation(leftIndex);
leftIndex ++;
}
else if (leftFeature == rightFeature) {
diff = a.valueAtLocation(leftIndex) - b.valueAtLocation(rightIndex);
leftIndex ++;
rightIndex ++;
}
else {
diff = b.valueAtLocation(rightIndex);
rightIndex ++;
}
dist += diff * diff;
}
// Pick up any additional features at the end of the two lists.
while (leftIndex < leftLength) {
diff = a.valueAtLocation(leftIndex);
dist += diff * diff;
leftIndex++;
}
while (rightIndex < rightLength) {
diff = b.valueAtLocation(rightIndex);
dist += diff * diff;
rightIndex++;
}
return Math.sqrt(dist);
}