org.apache.mahout.math.RandomAccessSparseVector Java Examples

The following examples show how to use org.apache.mahout.math.RandomAccessSparseVector. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CBM.java    From pyramid with Apache License 2.0 6 votes vote down vote up
public String toString() {
    Vector vector = new RandomAccessSparseVector(numFeatures);
    double[] mixtureCoefficients = multiClassClassifier.predictClassProbs(vector);
    final StringBuilder sb = new StringBuilder("CBM{\n");
    sb.append("numLabels=").append(numLabels).append("\n");
    sb.append("numComponents=").append(numComponents).append("\n");
    for (int k = 0; k< numComponents; k++){
        sb.append("cluster ").append(k).append(":\n");
        sb.append("proportion = ").append(mixtureCoefficients[k]).append("\n");
    }

    sb.append("multi-class component = \n");
    sb.append(multiClassClassifier);
    sb.append("binary components = \n");
    for (int k = 0; k< numComponents; k++){
        for (int l=0;l<numLabels;l++){
            sb.append("component ").append(k).append(" class ").append(l).append("\n");
            sb.append(binaryClassifiers[k][l]).append("\n");
        }
    }
    sb.append('}');
    return sb.toString();
}
 
Example #2
Source File: HashingFeatureEncoder.java    From ml-models with Apache License 2.0 5 votes vote down vote up
Vector getVector(Map<String, Object> features) {
    Vector v = new RandomAccessSparseVector(vectorSize);
    if (hasIntercept) interceptAdder.addToVector("1", v);
    for (Map.Entry<String, Object> feature : features.entrySet()) {
        String key = feature.getKey();
        Object value = feature.getValue();
        switch (types.get(key)) {
            case _class: featureAdder.addToVector(key + ":" + (String) value, 1, v); break;
            case _float: featureAdder.addToVector(key, (double) value, v); break;
        }
    }
    return v;
}
 
Example #3
Source File: LabelBinaryFeatureExtractor.java    From pyramid with Apache License 2.0 5 votes vote down vote up
@Override
public Vector extractFeatures(PredictionCandidate prediction) {
    Vector vector = new RandomAccessSparseVector(numLabelsInModel);
    for (int l: prediction.multiLabel.getMatchedLabels()){
        if (l<numLabelsInModel){
            vector.set(l,1);
        }

    }
    return vector;
}
 
Example #4
Source File: SerializableVector.java    From pyramid with Apache License 2.0 5 votes vote down vote up
public SerializableVector(Vector vector) {
    this.vector = vector;
    this.size = vector.size();
    if (vector instanceof DenseVector){
        type = Type.DENSE;
    } else if (vector instanceof RandomAccessSparseVector){
        type = Type.SPARSE_RANDOM;
    } else {
        type = Type.SPARSE_SEQUENTIAL;
    }
}
 
Example #5
Source File: SparseMLClfDataSet.java    From pyramid with Apache License 2.0 5 votes vote down vote up
private void readObject(java.io.ObjectInputStream in)
        throws IOException, ClassNotFoundException{
    in.defaultReadObject();
    SerializableVector[] serFeatureRows = (SerializableVector[])in.readObject();
    featureRows = new RandomAccessSparseVector[serFeatureRows.length];
    for (int i=0;i<featureRows.length;i++){
        featureRows[i] = (RandomAccessSparseVector) serFeatureRows[i].getVector();
    }

    SerializableVector[] serFeatureColumns = (SerializableVector[])in.readObject();
    featureColumns = new RandomAccessSparseVector[serFeatureColumns.length];
    for (int i=0;i<featureColumns.length;i++){
        featureColumns[i] = (RandomAccessSparseVector) serFeatureColumns[i].getVector();
    }
}
 
Example #6
Source File: MultiLabel.java    From pyramid with Apache License 2.0 5 votes vote down vote up
/**
 * return binary vector
 * @param length
 * @return
 */
public Vector toVectorRandomSparse(int length){
    Vector vector = new RandomAccessSparseVector(length);
    for (int i = labels.nextSetBit(0); i >= 0; i = labels.nextSetBit(i+1)){
        vector.set(i,1);
    }
    return vector;
}
 
Example #7
Source File: SparseDataSet.java    From pyramid with Apache License 2.0 5 votes vote down vote up
public SparseDataSet(int numDataPoints, int numFeatures, boolean missingValue) {
    super(numDataPoints,numFeatures,missingValue);
    this.featureRows = new RandomAccessSparseVector[numDataPoints];
    for (int i=0;i<numDataPoints;i++){
        this.featureRows[i] = new RandomAccessSparseVector(numFeatures);
    }
    this.featureColumns = new RandomAccessSparseVector[numFeatures];
    for (int j=0;j<numFeatures;j++){
        this.featureColumns[j] = new RandomAccessSparseVector(numDataPoints);
    }
}
 
Example #8
Source File: SparseDataSet.java    From pyramid with Apache License 2.0 5 votes vote down vote up
public SparseDataSet(int numDataPoints, int numFeatures, boolean missingValue, IdTranslator idTranslator) {
    super(numDataPoints,numFeatures,missingValue, idTranslator);
    this.featureRows = new RandomAccessSparseVector[numDataPoints];
    for (int i=0;i<numDataPoints;i++){
        this.featureRows[i] = new RandomAccessSparseVector(numFeatures);
    }
    this.featureColumns = new RandomAccessSparseVector[numFeatures];
    for (int j=0;j<numFeatures;j++){
        this.featureColumns[j] = new RandomAccessSparseVector(numDataPoints);
    }
}
 
Example #9
Source File: Step2.java    From recsys-offline with Apache License 2.0 5 votes vote down vote up
public void reduce(IntWritable itemIndex1,Iterable<IntWritable> itemPrefs,Context context) throws IOException, InterruptedException{  
    // RandomAccessSparseVector(int cardinality, int initialCapacity)   
    Vector itemVector=new RandomAccessSparseVector(Integer.MAX_VALUE,10);  
    for(IntWritable itemPref:itemPrefs){  
        int itemIndex2=itemPref.get();  
        itemVector.set(itemIndex2, itemVector.get(itemIndex2)+1.0);  
    }  
    context.write(itemIndex1, new VectorWritable(itemVector));  
  System.out.println(itemIndex1+"  ,"+itemVector);  
}
 
Example #10
Source File: Step1.java    From recsys-offline with Apache License 2.0 5 votes vote down vote up
public void reduce(VarLongWritable userID,   Iterable<LongAndFloat> itemPrefs, Context context)   throws IOException, InterruptedException {
    Vector userVector = new RandomAccessSparseVector(Integer.MAX_VALUE, 10);
    for (LongAndFloat itemPref : itemPrefs) {
        userVector.set( Integer.parseInt(itemPref.getFirst().toString()),  Float.parseFloat(itemPref.getSecond().toString()));
    }
    context.write(userID, new VectorWritable(userVector));
}
 
Example #11
Source File: CachedAccessOnlyVector.java    From pyramid with Apache License 2.0 4 votes vote down vote up
public CachedAccessOnlyVector(RandomAccessSparseVector randomAccessSparseVector) {
    this.randomAccessSparseVector = randomAccessSparseVector;
    this.cachedValues = new double[randomAccessSparseVector.size()];
    this.cached = new boolean[randomAccessSparseVector.size()];
}
 
Example #12
Source File: IMLGradientBoosting.java    From pyramid with Apache License 2.0 4 votes vote down vote up
double[] predictClassScoresCachedInput(Vector vector){
    Vector cachedVector = new CachedAccessOnlyVector((RandomAccessSparseVector) vector);
    return predictClassScores(cachedVector);
}
 
Example #13
Source File: IMLGradientBoosting.java    From pyramid with Apache License 2.0 4 votes vote down vote up
double[] predictClassScoresCachedInput(Vector vector, boolean[] shouldStop){
    Vector cachedVector = new CachedAccessOnlyVector((RandomAccessSparseVector) vector);
    return predictClassScores(cachedVector, shouldStop);
}