List of usage examples for org.apache.mahout.math Vector getNumNonZeroElements
int getNumNonZeroElements();
From source file:com.scaleunlimited.classify.vectors.SetNormalizer.java
License:Apache License
public void normalize(Vector vector) { // First count the number of non-zero values. double valueCount = vector.getNumNonZeroElements(); // Set each non-zero value to 1/count of non-zero values, so that // it's as if these all have a count of 1, so they have equal TF. vector.assign(new DoubleDoubleFunction() { @Override// w w w . j a v a 2s. com public double apply(double curValue, double normalizedValue) { return (curValue > 0.0 ? normalizedValue : 0); } }, 1.0 / valueCount); }
From source file:edu.rosehulman.mahout.math.VectorWritable.java
License:Apache License
public static void writeVector(DataOutput out, Vector vector, boolean laxPrecision) throws IOException { boolean dense = vector.isDense(); boolean sequential = vector.isSequentialAccess(); boolean named = vector instanceof NamedVector; out.writeByte((dense ? FLAG_DENSE : 0) | (sequential ? FLAG_SEQUENTIAL : 0) | (named ? FLAG_NAMED : 0) | (laxPrecision ? FLAG_LAX_PRECISION : 0)); Varint.writeUnsignedVarInt(vector.size(), out); if (dense) {//from ww w . jav a2s .com for (Vector.Element element : vector.all()) { if (laxPrecision) { out.writeFloat((float) element.get()); } else { out.writeDouble(element.get()); } } } else { Varint.writeUnsignedVarInt(vector.getNumNonZeroElements(), out); Iterator<Element> iter = vector.nonZeroes().iterator(); if (sequential) { int lastIndex = 0; while (iter.hasNext()) { Vector.Element element = iter.next(); if (element.get() == 0) { continue; } int thisIndex = element.index(); // Delta-code indices: Varint.writeUnsignedVarInt(thisIndex - lastIndex, out); lastIndex = thisIndex; if (laxPrecision) { out.writeFloat((float) element.get()); } else { out.writeDouble(element.get()); } } } else { while (iter.hasNext()) { Vector.Element element = iter.next(); if (element.get() == 0) { // TODO(robinanil): Fix the damn iterator for the zero element. continue; } Varint.writeUnsignedVarInt(element.index(), out); if (laxPrecision) { out.writeFloat((float) element.get()); } else { out.writeDouble(element.get()); } } } } if (named) { String name = ((NamedVector) vector).getName(); out.writeUTF(name == null ? "" : name); } }