本文整理汇总了Java中cc.mallet.types.SparseVector.numLocations方法的典型用法代码示例。如果您正苦于以下问题:Java SparseVector.numLocations方法的具体用法?Java SparseVector.numLocations怎么用?Java SparseVector.numLocations使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cc.mallet.types.SparseVector
的用法示例。
在下文中一共展示了SparseVector.numLocations方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: checkAnswer
import cc.mallet.types.SparseVector; //导入方法依赖的package包/类
private void checkAnswer (SparseVector actual, double[] ans)
{
assertEquals ("Wrong number of locations:",
ans.length, actual.numLocations());
for (int i = 0; i < actual.numLocations(); i++) {
assertEquals ("Value incorrect at location "+i+": ",
ans[i], actual.valueAtLocation (i) , 0.0);
}
}
示例2: testPlusEquals
import cc.mallet.types.SparseVector; //导入方法依赖的package包/类
public void testPlusEquals ()
{
SparseVector s = (SparseVector) s1.cloneMatrix ();
s.plusEqualsSparse (s2, 2.0);
checkAnswer (s, new double[] { 3, 5, 7, 6, 7 });
SparseVector s2p = new SparseVector
(new int[] { 13 },
new double[] { 0.8 });
s.plusEqualsSparse (s2p, 1.0);
checkAnswer (s, new double[] { 3, 5, 7, 6.8, 7 });
SparseVector s3p = new SparseVector
(new int[] { 14 },
new double[] { 0.8 });
s.plusEqualsSparse (s3p, 1.0);
checkAnswer (s, new double[] { 3, 5, 7, 6.8, 7 }); // verify s unchanged
SparseVector s4 = new SparseVector
(new int[] { 7, 14, 15 },
new double[] { 0.2, 0.8, 1.2 });
s.plusEqualsSparse (s4, 1.0);
checkAnswer (s, new double[] { 3, 5, 7.2, 6.8, 8.2 });
SparseVector s5 = new SparseVector (new int[] { 7 }, new double[] { 0.2 });
s5.plusEqualsSparse (s1);
for (int i = 0; i < s5.numLocations(); i++) {
assertEquals (7, s5.indexAtLocation (i));
assertEquals (3.2, s5.valueAtLocation (i), 0.0);
}
SparseVector s6 = new SparseVector (new int[] { 7 }, new double[] { 0.2 });
s6.plusEqualsSparse (s1, 3.5);
for (int i = 0; i < s6.numLocations(); i++) {
assertEquals (7, s6.indexAtLocation (i));
assertEquals (10.7, s6.valueAtLocation (i), 0.0);
}
}
示例3: testIncrementValue
import cc.mallet.types.SparseVector; //导入方法依赖的package包/类
public void testIncrementValue ()
{
SparseVector s = (SparseVector) s1.cloneMatrix ();
s.incrementValue (5, 0.75);
double[] ans = new double[] {1, 2.75, 3, 4, 5};
for (int i = 0; i < s.numLocations(); i++) {
assertTrue (s.valueAtLocation (i) == ans[i]);
}
}
示例4: testSetValue
import cc.mallet.types.SparseVector; //导入方法依赖的package包/类
public void testSetValue ()
{
SparseVector s = (SparseVector) s1.cloneMatrix ();
s.setValue (5, 0.3);
double[] ans = new double[] {1, 0.3, 3, 4, 5};
for (int i = 0; i < s.numLocations(); i++) {
assertTrue (s.valueAtLocation (i) == ans[i]);
}
}
示例5: testCloneMatrixZeroed
import cc.mallet.types.SparseVector; //导入方法依赖的package包/类
public void testCloneMatrixZeroed ()
{
SparseVector s = (SparseVector) s1.cloneMatrixZeroed ();
for (int i = 0; i < s.numLocations(); i++) {
assertTrue (s.valueAtLocation (i) == 0.0);
assertTrue (s.indexAtLocation (i) == idxs [i]);
}
}
示例6: print
import cc.mallet.types.SparseVector; //导入方法依赖的package包/类
public void print (PrintWriter out)
{
out.println ("*** CRF STATES ***");
for (int i = 0; i < numStates (); i++) {
State s = (State) getState (i);
out.print ("STATE NAME=\"");
out.print (s.name); out.print ("\" ("); out.print (s.destinations.length); out.print (" outgoing transitions)\n");
out.print (" "); out.print ("initialWeight = "); out.print (parameters.initialWeights[i]); out.print ('\n');
out.print (" "); out.print ("finalWeight = "); out.print (parameters.finalWeights[i]); out.print ('\n');
out.println (" transitions:");
for (int j = 0; j < s.destinations.length; j++) {
out.print (" "); out.print (s.name); out.print (" -> "); out.println (s.getDestinationState (j).getName ());
for (int k = 0; k < s.weightsIndices[j].length; k++) {
out.print (" WEIGHTS = \"");
int widx = s.weightsIndices[j][k];
out.print (parameters.weightAlphabet.lookupObject (widx).toString ());
out.print ("\"\n");
}
}
out.println ();
}
if (parameters.weights == null)
out.println ("\n\n*** NO WEIGHTS ***");
else {
out.println ("\n\n*** CRF WEIGHTS ***");
for (int widx = 0; widx < parameters.weights.length; widx++) {
out.println ("WEIGHTS NAME = " + parameters.weightAlphabet.lookupObject (widx));
out.print (": <DEFAULT_FEATURE> = "); out.print (parameters.defaultWeights[widx]); out.print ('\n');
SparseVector transitionWeights = parameters.weights[widx];
if (transitionWeights.numLocations () == 0)
continue;
RankedFeatureVector rfv = new RankedFeatureVector (inputAlphabet, transitionWeights);
for (int m = 0; m < rfv.numLocations (); m++) {
double v = rfv.getValueAtRank (m);
//int index = rfv.indexAtLocation (rfv.getIndexAtRank (m)); // This doesn't make any sense. How did this ever work? -akm 12/2007
int index = rfv.getIndexAtRank (m);
Object feature = inputAlphabet.lookupObject (index);
if (v != 0) {
out.print (": "); out.print (feature); out.print (" = "); out.println (v);
}
}
}
}
out.flush ();
}
示例7: distance
import cc.mallet.types.SparseVector; //导入方法依赖的package包/类
/** Gives the Minkowski distance between two vectors.
*
* distance(x,y) := \left( \Sum_i=0^d-1 \left| x_i - y_i \right|^q \right)^\frac{1}{q}
*
* for 1<=q<infinity. For q=infinity
*
* distance(x,y) := max_i \left| x_i - y_i \right|
*/
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.");
//assert (a != null);
//assert (b != null);
if (a.numLocations() != b.numLocations() )
throw new IllegalArgumentException("Vectors must be of the same dimension.");
//assert (a.numLocations() == b.numLocations() );
for (int i=0 ; i< a.numLocations() ; i++ )
{
diff = Math.abs( a.valueAtLocation(i) - b.valueAtLocation(i));
if (q==1)
dist += diff;
else if (q==2)
dist += diff*diff;
else if (q==Double.POSITIVE_INFINITY)
if ( diff > dist)
dist = diff;
else
dist += Math.pow( diff, q );
}
if (q==1 || q==Double.POSITIVE_INFINITY)
return dist;
else if (q==2)
return Math.sqrt( dist );
else
return Math.pow( dist, 1/q);
}
示例8: 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;
}
示例9: 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));
}
}
示例10: square
import cc.mallet.types.SparseVector; //导入方法依赖的package包/类
static void square(SparseVector v) {
for (int i = 0; i < v.numLocations(); i++) {
v.setValueAtLocation(i, v.valueAtLocation(i) * v.valueAtLocation(i));
}
}
示例11: 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();
}
}
示例12: 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);
}
示例13: distance
import cc.mallet.types.SparseVector; //导入方法依赖的package包/类
public double distance(SparseVector a, SparseVector b) {
double dist = 0.0;
double diff = 0.0;
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 = Math.abs(a.valueAtLocation(leftIndex));
leftIndex ++;
}
else if (leftFeature == rightFeature) {
diff = Math.abs(a.valueAtLocation(leftIndex) - b.valueAtLocation(rightIndex));
leftIndex ++;
rightIndex ++;
}
else {
diff = Math.abs(b.valueAtLocation(rightIndex));
rightIndex ++;
}
dist += diff;
}
// Pick up any additional features at the end of the two lists.
while (leftIndex < leftLength) {
diff = Math.abs(a.valueAtLocation(leftIndex));
dist += diff;
leftIndex++;
}
while (rightIndex < rightLength) {
diff = Math.abs(b.valueAtLocation(rightIndex));
dist += diff;
rightIndex++;
}
return dist;
}
示例14: distance
import cc.mallet.types.SparseVector; //导入方法依赖的package包/类
public double distance(SparseVector a, SparseVector b) {
double maxDiff = 0.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 = Math.abs(a.valueAtLocation(leftIndex));
leftIndex ++;
}
else if (leftFeature == rightFeature) {
diff = Math.abs(a.valueAtLocation(leftIndex) - b.valueAtLocation(rightIndex));
leftIndex ++;
rightIndex ++;
}
else {
diff = Math.abs(b.valueAtLocation(rightIndex));
rightIndex ++;
}
if (diff > maxDiff) { maxDiff = diff; }
}
// Pick up any additional features at the end of the two lists.
while (leftIndex < leftLength) {
diff = Math.abs(a.valueAtLocation(leftIndex));
if (diff > maxDiff) { maxDiff = diff; }
leftIndex++;
}
while (rightIndex < rightLength) {
diff = Math.abs(b.valueAtLocation(rightIndex));
if (diff > maxDiff) { maxDiff = diff; }
rightIndex++;
}
return maxDiff;
}
示例15: distance
import cc.mallet.types.SparseVector; //导入方法依赖的package包/类
/** Gives the Minkowski distance between two vectors.
*
* distance(x,y) := \left( \Sum_i=0^d-1 \left| x_i - y_i \right|^q \right)^\frac{1}{q}
*/
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 = Math.abs(a.valueAtLocation(leftIndex));
leftIndex ++;
}
else if (leftFeature == rightFeature) {
diff = Math.abs(a.valueAtLocation(leftIndex) - b.valueAtLocation(rightIndex));
leftIndex ++;
rightIndex ++;
}
else {
diff = Math.abs(b.valueAtLocation(rightIndex));
rightIndex ++;
}
dist += Math.pow(diff, q);
}
// Pick up any additional features at the end of the two lists.
while (leftIndex < leftLength) {
diff = Math.abs(a.valueAtLocation(leftIndex));
dist += Math.pow(diff, q);
leftIndex++;
}
while (rightIndex < rightLength) {
diff = Math.abs(b.valueAtLocation(rightIndex));
dist += Math.pow(diff, q);
rightIndex++;
}
return Math.pow(dist, oneOverQ);
}