org.apache.mahout.math.SequentialAccessSparseVector Java Examples

The following examples show how to use org.apache.mahout.math.SequentialAccessSparseVector. 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: 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 #2
Source File: Mapper.java    From laser with Apache License 2.0 5 votes vote down vote up
protected void setup(Context context) throws IOException,
		InterruptedException {
	Configuration conf = context.getConfiguration();
	String offlineModel = conf.get("com.b5m.laser.offline.model");
	Path offlinePath = new Path(offlineModel);
	FileSystem fs = offlinePath.getFileSystem(conf);
	beta = readVector(new Path(offlinePath, "beta"), fs, conf);
	A = readMatrix(new Path(offlinePath, "A"), fs, conf);
	advec = new SequentialAccessSparseVector(A.numCols());
	AStable = new ArrayList<Float>(A.numRows());
}
 
Example #3
Source File: AdClusteringInfo.java    From laser with Apache License 2.0 5 votes vote down vote up
public AdClusteringInfo(Integer id, SparseVector sv, int dimension) {
	clusteringId = id;
	info = new SequentialAccessSparseVector(dimension);
	while (sv.hasNext()) {
		info.set(sv.getIndex(), sv.get());
	}
}
 
Example #4
Source File: ALSWRFactorizer.java    From elasticsearch-taste with Apache License 2.0 5 votes vote down vote up
protected Vector sparseItemRatingVector(final PreferenceArray prefs) {
    final SequentialAccessSparseVector ratings = new SequentialAccessSparseVector(
            Integer.MAX_VALUE, prefs.length());
    for (final Preference preference : prefs) {
        ratings.set((int) preference.getUserID(), preference.getValue());
    }
    return ratings;
}
 
Example #5
Source File: ALSWRFactorizer.java    From elasticsearch-taste with Apache License 2.0 5 votes vote down vote up
protected Vector sparseUserRatingVector(final PreferenceArray prefs) {
    final SequentialAccessSparseVector ratings = new SequentialAccessSparseVector(
            Integer.MAX_VALUE, prefs.length());
    for (final Preference preference : prefs) {
        ratings.set((int) preference.getItemID(), preference.getValue());
    }
    return ratings;
}
 
Example #6
Source File: SequentialSparseDataSet.java    From pyramid with Apache License 2.0 5 votes vote down vote up
public SequentialSparseDataSet(int numDataPoints, int numFeatures, boolean missingValue) {
    super(numDataPoints,numFeatures,missingValue);
    this.featureRows = new SequentialAccessSparseVector[numDataPoints];
    for (int i=0;i<numDataPoints;i++){
        this.featureRows[i] = new SequentialAccessSparseVector(numFeatures);
    }
    this.featureColumns = new SequentialAccessSparseVector[numFeatures];
    for (int j=0;j<numFeatures;j++){
        this.featureColumns[j] = new SequentialAccessSparseVector(numDataPoints);
    }
}
 
Example #7
Source File: SequentialSparseMLClfDataSet.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 SequentialAccessSparseVector[serFeatureRows.length];
    for (int i=0;i<featureRows.length;i++){
        featureRows[i] = (SequentialAccessSparseVector) serFeatureRows[i].getVector();
    }

    SerializableVector[] serFeatureColumns = (SerializableVector[])in.readObject();
    featureColumns = new SequentialAccessSparseVector[serFeatureColumns.length];
    for (int i=0;i<featureColumns.length;i++){
        featureColumns[i] = (SequentialAccessSparseVector) serFeatureColumns[i].getVector();
    }
}
 
Example #8
Source File: CMLCRFElasticNet.java    From pyramid with Apache License 2.0 5 votes vote down vote up
public void iterate() {
//        System.out.println("weights: " + cmlcrf.getWeights().getAllWeights());
        // O(NdL)
//        System.out.println(Arrays.toString(cmlcrf.getCombinationLabelPartScores()));
        updateClassScoreMatrix();
        cmlcrf.updateCombLabelPartScores();
        updateAssignmentScoreMatrix();
        updateAssignmentProbMatrix();
        updateCombProbSums();
        updatePredictedCounts();
        updateClassProbMatrix();
        // update for each support label set
        Vector accumulateWeights = new SequentialAccessSparseVector(numParameters);
        Vector oldWeights = cmlcrf.getWeights().deepCopy().getAllWeights();
        for (int l=0; l<numSupport; l++) {
//            System.out.println("label: " + supportedCombinations.get(l));
            DataSet newData = expandData(l);
            iterateForOneComb(newData, l);
            accumulateWeights = accumulateWeights.plus(cmlcrf.getWeights().getAllWeights());
            cmlcrf.getWeights().setWeightVector(oldWeights);
        }
        // lineSearch
        if (true) {
            Vector searchDirection = accumulateWeights;
            Vector gradient = this.predictedCounts.minus(empiricalCounts).divide(numData);
            lineSearch(searchDirection, gradient);
        }

        this.terminator.add(getValue());
    }
 
Example #9
Source File: SerializableVectorTest.java    From pyramid with Apache License 2.0 5 votes vote down vote up
private static void test1() throws Exception{
    Vector vector = new SequentialAccessSparseVector(10);
    vector.set(0,2);
    vector.set(5,8);
    SerializableVector serializableVector = new SerializableVector(vector);
    Serialization.serialize(serializableVector, new File(TMP,"v.ser"));
    Vector loaded = ((SerializableVector)Serialization.deserialize(new File(TMP,"v.ser"))).getVector();
    System.out.println(loaded.size());
    System.out.println(loaded.getClass().getName());
    System.out.println(loaded);
}
 
Example #10
Source File: GeneralMesseageConsumer.java    From laser with Apache License 2.0 4 votes vote down vote up
@Override
public boolean write(B5MEvent b5mEvent) throws IOException {
	Map<CharSequence, CharSequence> args = b5mEvent.getArgs();
	CharSequence actionId = args.get(ACTION_ID_LABEL);
	if (null == actionId) {
		return false;
	}

	CharSequence uuid = args.get(UUID_LABEL);
	if (null == uuid) {
		return false;
	}
	String user = uuid.toString();

	String item = null;
	CharSequence title = args.get(TT_LABEL);
	if (null == title) {
		title = args.get(TI_LABEL);
		if (null == title) {
			return false;
		}
	}
	item = title.toString();

	Integer action = 1;
	if (actionId.toString().startsWith("103")) {
		action = 1;
	} else {
		action = -1;
	}

	Vector userFeature = new SequentialAccessSparseVector(userDimension);
	setUserProfile(user, userFeature);
	Vector itemFeature = new SequentialAccessSparseVector(itemDimension);
	if (!setItemProfile(item, itemFeature)) {
		return false;
	}

	Request req = new Request(userFeature, itemFeature, action);
	Text key = new Text(user);
	appendOffline(key, req);

	Double offset = knownOffset(req);
	appendOnline(key, new OnlineVectorWritable(offset, action, itemFeature));
	return true;
}
 
Example #11
Source File: WebScaleMessageConsumer.java    From laser with Apache License 2.0 4 votes vote down vote up
@Override
public boolean write(B5MEvent b5mEvent) throws IOException {
	CharSequence str = b5mEvent.getArgs().get(DOCID_TAG);
	if (null == str) {
		return true;
	}
	String DOCID = str.toString();

	Vector ad = new SequentialAccessSparseVector(adFeatureDimension);
	Object[] req = new Object[1];
	req[0] = DOCID;
	AdInfo adInfo = (AdInfo) client.asyncRead(req, "getAdInfoByDOCID",
			AdInfo.class);
	if (adInfo.DOCID.isEmpty()) {
		return true;
	}

	while (adInfo.context.hasNext()) {
		ad.set(adInfo.context.getIndex(), adInfo.context.get());
	}
	//TODO
	Vector user = new DenseVector(userFeatureDimension);
	for (int i = 0; i < userFeatureDimension; i++) {
		user.set(i, RANDOM.nextDouble());
	}
	//TODO
	Integer action = 0;

	Request request = new Request(user, ad, action);
	Text key = new Text(DOCID);
	appendOffline(key, request);

	Double offset = knownOffset(request);
	OnlineVectorWritable online = new OnlineVectorWritable(offset, action,
			user);
	appendOnline(key, online);

	if (!adInfo.clusteringId.isEmpty()) {
		appendOnline(new Text(adInfo.clusteringId), online);
	}
	return false;
}
 
Example #12
Source File: LinearRegWeights.java    From pyramid with Apache License 2.0 4 votes vote down vote up
public LinearRegWeights(int numFeatures) {
    this.numFeatures = numFeatures;
    this.weightVector = new SequentialAccessSparseVector(numFeatures);
}