/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package edu.uci.jforests.learning;

import edu.uci.jforests.learning.trees.Ensemble;
import edu.uci.jforests.learning.trees.decision.DecisionTree;
import edu.uci.jforests.learning.trees.regression.RegressionTree;
import edu.uci.jforests.sample.Sample;

/**
 * @author Yasser Ganjisaffar 
 */

public class LearningUtils {
	
	public static void updateScores(Sample sampleSet, double[] scores, Ensemble ensemble) {
		updateScores(sampleSet, scores, ensemble, null);
	}
	
	public static void updateScores(Sample sampleSet, double[] scores, Ensemble ensemble, LearningProgressListener progressListener) {
		for (int t = 0; t < ensemble.getNumTrees(); t++) {
			RegressionTree tree = (RegressionTree) ensemble.getTreeAt(t);
			double treeWeight = ensemble.getWeightAt(t);
			for (int i = 0; i < sampleSet.size; i++) {
				scores[i] += treeWeight * tree.getOutput(sampleSet.dataset, sampleSet.indicesInDataset[i]);
			}
			if (progressListener != null) {
				progressListener.onScoreEval();
			}
		}
	}
	
	public static void updateScores(Sample sampleSet, double[] scores, RegressionTree tree, double treeWeight) {
		if (sampleSet.indicesInDataset == null) {
			for (int i = 0; i < sampleSet.size; i++) {
				scores[i] += treeWeight * tree.getOutput(sampleSet.dataset, i);
			}	
		} else {
			for (int i = 0; i < sampleSet.size; i++) {
				scores[i] += treeWeight * tree.getOutput(sampleSet.dataset, sampleSet.indicesInDataset[i]);
			}
		}
	}
	
	public static void updateDistributions(Sample sampleSet, double[][] dist, DecisionTree tree, double treeWeight) {
		for (int i = 0; i < sampleSet.size; i++) {
			double[] curDist = tree.getDistributionForInstance(sampleSet.dataset, sampleSet.indicesInDataset[i]);
			for (int c = 0; c < curDist.length; c++) {
				dist[i][c] += treeWeight * curDist[c];
			}
		}
	}

	public static void updateProbabilities(double[] prob, double[] scores, int size) {
		for (int i = 0; i < size; i++) {
			prob[i] = 1.0 / (1.0 + Math.exp(-2.0 * scores[i]));
		}
	}
	
	public static void updateProbabilities(double[] prob, double[] scores, int[] instances, int size) {
		for (int i = 0; i < size; i++) {
			int instance = instances[i];
			prob[instance] = 1.0 / (1.0 + Math.exp(-2.0 * scores[instance]));
		}
	}
	
}

 

Ads help maintain this website.