org.apache.mahout.math.Matrix Java Examples

The following examples show how to use org.apache.mahout.math.Matrix. 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: 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 #2
Source File: OnlineLogisticRegressionTest.java    From Java-Data-Science-Cookbook with MIT License 5 votes vote down vote up
public static void main(String[] args) throws Exception {
 showAuc = true;
     showConfusion = true;
     Auc collector = new Auc();
     LogisticModelParameters lmp = LogisticModelParameters.loadFrom(new File(modelFile));
     CsvRecordFactory csv = lmp.getCsvRecordFactory();
     OnlineLogisticRegression lr = lmp.createRegression();
     BufferedReader in = OnlineLogisticRegressionTest.open(inputFile);
     String line = in.readLine();
     csv.firstLine(line);
     line = in.readLine();
     PrintWriter output=new PrintWriter(new OutputStreamWriter(System.out, Charsets.UTF_8), true);
     output.println("\"target\",\"model-output\",\"log-likelihood\"");
     while (line != null) {
     	System.out.println("-----" + line);
         Vector v = new SequentialAccessSparseVector(lmp.getNumFeatures());
         int target = csv.processLine(line, v);
         double score = lr.classifyScalarNoLink(v);
         output.printf(Locale.ENGLISH, "%d,%.3f,%.6f%n", target, score, lr.logLikelihood(target, v));
        collector.add(target, score);
         line = in.readLine();
         System.out.println("I am here");
       }
     output.printf(Locale.ENGLISH, "AUC = %.2f%n", collector.auc());
     Matrix m = collector.confusion();
     output.printf(Locale.ENGLISH, "confusion: [[%.1f, %.1f], [%.1f, %.1f]]%n",
       m.get(0, 0), m.get(1, 0), m.get(0, 1), m.get(1, 1));
     m = collector.entropy();
     output.printf(Locale.ENGLISH, "entropy: [[%.1f, %.1f], [%.1f, %.1f]]%n",
       m.get(0, 0), m.get(1, 0), m.get(0, 1), m.get(1, 1));
}
 
Example #3
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 #4
Source File: F1PredictorTest.java    From pyramid with Apache License 2.0 5 votes vote down vote up
private static void test2(){
    int numLabels = 5;
    Matrix matrix = LossMatrixGenerator.matrix(numLabels,"f1");
    List<MultiLabel> multiLabels = Enumerator.enumerate(numLabels);
    List<Double> dis = LossMatrixGenerator.sampleDistribution(numLabels);
    GeneralF1Predictor generalF1Predictor = new GeneralF1Predictor();
    generalF1Predictor.setMaxSize(3);
    MultiLabel pred = generalF1Predictor.predict(numLabels,multiLabels,dis);
    MultiLabel search = GeneralF1Predictor.exhaustiveSearch(numLabels,matrix,dis);
    System.out.println("pred = "+pred);
    System.out.println("search = "+search);



}
 
Example #5
Source File: HDFSHelper.java    From laser with Apache License 2.0 4 votes vote down vote up
public static Matrix readMatrix(Path path, FileSystem fs, Configuration conf)
		throws IOException {
	FSDataInputStream in = fs.open(path);
	return MatrixWritable.readMatrix(in);
}
 
Example #6
Source File: HDFSHelper.java    From laser with Apache License 2.0 4 votes vote down vote up
public static void writeMatrix(Matrix m, Path path, FileSystem fs,
		Configuration conf) throws IOException {
	FSDataOutputStream out = fs.create(path);
	MatrixWritable.writeMatrix(out, m);
	out.close();
}
 
Example #7
Source File: CachedAccessOnlyVector.java    From pyramid with Apache License 2.0 4 votes vote down vote up
@Override
public Matrix cross(Vector vector) {
    return null;
}
 
Example #8
Source File: MultiClassNaiveBayes.java    From arx with Apache License 2.0 votes vote down vote up
@Override public Matrix cross(Vector arg0) { throw new UnsupportedOperationException(); }