Java Code Examples for org.apache.mahout.math.Vector#size()

The following examples show how to use org.apache.mahout.math.Vector#size() . 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: 1000021_CDbwEvaluator_s.java    From coming with MIT License 6 votes vote down vote up
private void setStDev(int cI) {
  List<VectorWritable> repPts = representativePoints.get(cI);
  //if (repPts == null) {
  //  System.out.println();
  //}
  int s0 = 0;
  Vector s1 = null;
  Vector s2 = null;
  for (VectorWritable vw : repPts) {
    s0++;
    Vector v = vw.get();
    s1 = s1 == null ? v.clone() : s1.plus(v);
    s2 = s2 == null ? v.times(v) : s2.plus(v.times(v));
  }
  Vector std = s2.times(s0).minus(s1.times(s1)).assign(new SquareRootFunction()).divide(s0);
  double d = std.zSum() / std.size();
  //System.out.println("stDev[" + cI + "]=" + d);
  stDevs.put(cI, d);
}
 
Example 2
Source File: 1000021_CDbwEvaluator_t.java    From coming with MIT License 6 votes vote down vote up
private void setStDev(int cI) {
   List<VectorWritable> repPts = representativePoints.get(cI);
   //if (repPts == null) {
   //  System.out.println();
   //}
   int s0 = 0;
   Vector s1 = null;
   Vector s2 = null;
   for (VectorWritable vw : repPts) {
     s0++;
     Vector v = vw.get();
     s1 = s1 == null ? v.clone() : s1.plus(v);
     s2 = s2 == null ? v.times(v) : s2.plus(v.times(v));
   }
  if (s0 > 1) {
   Vector std = s2.times(s0).minus(s1.times(s1)).assign(new SquareRootFunction()).divide(s0);
   double d = std.zSum() / std.size();
   //System.out.println("stDev[" + cI + "]=" + d);
   stDevs.put(cI, d);
 }
}
 
Example 3
Source File: Vectors.java    From pyramid with Apache License 2.0 6 votes vote down vote up
public static double dot(Vector vector1, Vector vector2){
    if (vector1.size()!=vector2.size()){
        throw new IllegalArgumentException("vector1.size()!=vector2.size()");
    }

    boolean vector1Dense = vector1.isDense();
    boolean vector2Dense = vector2.isDense();

    if (vector1Dense&&vector2Dense){
        return dotDenseDense(vector1,vector2);
    } else if (vector1Dense && !vector2Dense){
        return dotDenseSparse(vector1,vector2);
    } else if (!vector1Dense && vector2Dense){
        return dotDenseSparse(vector2,vector1);
    } else {
        throw new UnsupportedOperationException("sparse dot sparse is not supported");
    }

}
 
Example 4
Source File: KMeans.java    From pyramid with Apache License 2.0 6 votes vote down vote up
private void updateCenters(int k){

        Vector center = new DenseVector(dataSet.getNumFeatures());
        double count = 0;
        for (int i=0;i<dataSet.getNumDataPoints();i++){
            if (assignments[i]==k){
                Vector instance = dataSet.getRow(i);
                for (int j=0;j<instance.size();j++){
                    center.set(j, center.get(j)+instance.get(j));
                }
                count += 1;
            }
        }
        center = center.divide(count);
        centers[k] = center;
        System.out.println("update the centroid of cluster "+(k+1)+" based on "+(int)count+" instances in the cluster");
    }
 
Example 5
Source File: GeneralF1Predictor.java    From pyramid with Apache License 2.0 6 votes vote down vote up
public static MultiLabel exhaustiveSearch(int numClasses, Matrix lossMatrix, List<Double> probabilities){
    double bestScore = Double.POSITIVE_INFINITY;
    Vector vector = new DenseVector(probabilities.size());
    for (int i=0;i<vector.size();i++){
        vector.set(i,probabilities.get(i));
    }
    List<MultiLabel> multiLabels = Enumerator.enumerate(numClasses);
    MultiLabel multiLabel = null;
    for (int j=0;j<lossMatrix.numCols();j++){
        Vector column = lossMatrix.viewColumn(j);
        double score = column.dot(vector);
        System.out.println("column "+j+", expected loss = "+score);
        if (score < bestScore){
            bestScore = score;
            multiLabel = multiLabels.get(j);
        }
    }
    return multiLabel;
}
 
Example 6
Source File: Vectors.java    From pyramid with Apache License 2.0 5 votes vote down vote up
private static double dotDenseDense(Vector vector1, Vector vector2){
    int size = vector1.size();
    double sum = 0;
    for (int d=0;d<size;d++){
        sum += vector1.getQuick(d)*vector2.getQuick(d);
    }
    return sum;
}
 
Example 7
Source File: Vectors.java    From pyramid with Apache License 2.0 5 votes vote down vote up
public static double[] toArray(Vector vector){
    double[] arr = new double[vector.size()];
    for (Vector.Element nonZero: vector.nonZeroes()){
        int index = nonZero.index();
        double v = nonZero.get();
        arr[index] = v;
    }
    return arr;
}
 
Example 8
Source File: LogisticLoss.java    From pyramid with Apache License 2.0 5 votes vote down vote up
private Vector penaltyGradient(){
    Vector weightsVector = this.logisticRegression.getWeights().getAllWeights();
    Vector penalty = new DenseVector(weightsVector.size());

    penalty = penalty.plus(weightsVector.divide(priorGaussianVariance));

    for (int j:logisticRegression.getWeights().getAllBiasPositions()){
        penalty.set(j,0);
    }
    return penalty;
}
 
Example 9
Source File: Weights.java    From pyramid with Apache License 2.0 5 votes vote down vote up
/**
 * truncate the weights below the threshold to 0
 * @param threshold
 */
void truncateByThreshold(double threshold){
    for (int k=0;k<numClasses;k++){
        Vector vector  = getWeightsWithoutBiasForClass(k);
        for (int d=0;d<vector.size();d++){
            if (Math.abs(vector.get(d))<threshold){
                vector.set(d,0);
            }
        }
    }
}
 
Example 10
Source File: Weights.java    From pyramid with Apache License 2.0 5 votes vote down vote up
public Weights(int numClasses, int numFeatures, Vector weightVector) {
    this.numClasses = numClasses;
    this.numFeatures = numFeatures;
    if (weightVector.size()!=(numFeatures + 1)*numClasses){
        throw new IllegalArgumentException("weightVector.size()!=(numFeatures + 1)*numClasses");
    }
    this.weightVector = weightVector;
}
 
Example 11
Source File: RidgeBinaryLogisticLoss.java    From pyramid with Apache License 2.0 5 votes vote down vote up
public void grad(Vector w, Vector g) {

        int[] y = labels;
        for (int i = 0; i < numRows; i++) {
            scores.set(i, 1 / (1 + Math.exp(-y[i] * scores.get(i))));
            diagonals.set(i, scores.get(i) * (1 - scores.get(i)));
            scores.set(i, regularization.get(i) * (scores.get(i) - 1) * y[i]);
            //it seems that scores are messed up at this point of time
        }
        XTv(scores, g);

        for (int i=0;i<g.size();i++){
            g.set(i,w.get(i)+g.get(i));
        }
    }
 
Example 12
Source File: TrustRegionNewtonOptimizer.java    From pyramid with Apache License 2.0 5 votes vote down vote up
/**
 * scales a vector by a constant
 */
private static void scale(double constant, Vector vector) {
    if (constant == 1.0) return;
    for (int i = 0; i < vector.size(); i++) {
        vector.set(i, vector.get(i) * constant);
    }

}
 
Example 13
Source File: Weights.java    From pyramid with Apache License 2.0 5 votes vote down vote up
public Weights(int numFeatures, Vector weightVector) {
    this.numFeatures = numFeatures;
    if (weightVector.size()!=(numFeatures + 1)){
        throw new IllegalArgumentException("weightVector.size()!=(numFeatures + 1)");
    }
    this.weightVector = weightVector;
    this.serializableWeights = new double[(numFeatures + 1)];
}
 
Example 14
Source File: LinearRegWeights.java    From pyramid with Apache License 2.0 5 votes vote down vote up
public LinearRegWeights(int numFeatures, Vector weightVector) {
    this.numFeatures = numFeatures;
    if (weightVector.size()!=numFeatures){
        throw new IllegalArgumentException("weightVector.size()!=(numFeatures)");
    }
    this.weightVector = weightVector;
}
 
Example 15
Source File: KMeans.java    From pyramid with Apache License 2.0 5 votes vote down vote up
static double distance(Vector vector1, Vector vector2){
    double sum = 0;
    for (int i=0;i<vector1.size();i++){
        double diff = (vector1.get(i)-vector2.get(i));
        sum += diff*diff;
    }
    return Math.pow(sum,0.5);
}
 
Example 16
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 17
Source File: LaserOfflineTrainTask.java    From laser with Apache License 2.0 5 votes vote down vote up
public void writeOrigOfflineModel(Path model, FileSystem fs,
		org.apache.hadoop.conf.Configuration conf, MsgpackClient client)
		throws Exception {
	Vector alpha = readVector(new Path(model, "alpha"), fs, conf);
	Vector beta = readVector(new Path(model, "beta"), fs, conf);
	Matrix A = readMatrix(new Path(model, "A"), fs, conf);

	Object[] req = new Object[3];
	List<Float> alpha1 = new ArrayList<Float>(alpha.size());

	for (int i = 0; i < alpha.size(); i++) {
		alpha1.add((float) (alpha.get(i)));
	}
	req[0] = alpha1;

	List<Float> beta1 = new ArrayList<Float>(beta.size());
	for (int i = 0; i < beta.size(); i++) {
		beta1.add((float) beta.get(i));
	}
	req[1] = beta1;

	List<Float> conjunction = new ArrayList<Float>(A.numRows()
			* A.numCols());
	for (int row = 0; row < A.numRows(); row++) {
		Vector vec = A.viewRow(row);
		for (int col = 0; col < A.numCols(); col++) {
			conjunction.add((float) vec.get(col));
		}
	}
	req[2] = conjunction;
	client.writeIgnoreRetValue(req, "updateLaserOfflineModel");

}
 
Example 18
Source File: LBFGS.java    From pyramid with Apache License 2.0 4 votes vote down vote up
public void iterate(){
        if (logger.isDebugEnabled()){
            logger.debug("start one iteration");
        }

        // we need to make a copy of the gradient; should not use pointer
        Vector oldGradient = new DenseVector(function.getGradient());
        Vector direction = findDirection();
        if (logger.isDebugEnabled()){
            logger.debug("norm of direction = "+direction.norm(2));
        }
        BackTrackingLineSearcher.MoveInfo moveInfo = lineSearcher.moveAlongDirection(direction);

        Vector s = moveInfo.getStep();
        Vector newGradient = function.getGradient();
        Vector y = newGradient.minus(oldGradient);
        double denominator = y.dot(s);

        //todo what to do if denominator is not positive?
        // round-off errors and an ill-conditioned inverse Hessian

        double rho = 0;
        if (denominator>0){
            rho = 1/denominator;
        }
        else {
            terminator.forceTerminate();
            if (logger.isWarnEnabled()){
                logger.warn("denominator <= 0, force to terminate");
            }
//            if (logger.isWarnEnabled()){
//                logger.warn("denominator <= 0, give up the current iteration; reset history, and directly jump to next iteration!");
//            }
//            reset();
//            return;
        }


        if (logger.isDebugEnabled()){
            if (y.size()<100){
                logger.debug("y= "+y);
                logger.debug("s= " + s);
            }
            logger.debug("denominator = "+denominator);
            logger.debug("rho = "+rho);
        }
        sQueue.add(s);
        yQueue.add(y);
        rhoQueue.add(rho);
        if (sQueue.size()>m){
            sQueue.remove();
            yQueue.remove();
            rhoQueue.remove();
        }
        double value = function.getValue();
        terminator.add(value);
        if (logger.isDebugEnabled()){
            logger.debug("finish one iteration. loss = "+value);
        }
    }
 
Example 19
Source File: Weights.java    From pyramid with Apache License 2.0 4 votes vote down vote up
public void setWeightVector(Vector weightVector) {
    if (weightVector.size()!=(numFeatures + 1)*numClasses){
        throw new IllegalArgumentException("weightVector.size()!=(numFeatures + 1)*numClasses");
    }
    this.weightVector = weightVector;
}
 
Example 20
Source File: Weights.java    From pyramid with Apache License 2.0 4 votes vote down vote up
public void setWeightVector(Vector weightVector) {
    if (weightVector.size() != (numWeightsForFeatures + numWeightsForLabels)) {
        throw new IllegalArgumentException("given vector size is wrong: " + weightVector.size());
    }
    this.weightVector = weightVector;
}