This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//constructs a naive Bayes binary classifier | |
public NaiveBayes(double in[][], boolean out[]){ | |
int inputs = in[0].length ; | |
//initialize sums and sums of squares for each class | |
double[] poss = new double[inputs], poss2 = new double[inputs]; | |
double[] negs = new double[inputs], negs2 = new double[inputs]; | |
//calculate amount of each class, sums, and sums of squares | |
for(int k=0;k<in.length;k++){//for each data point | |
if(out[k]){ | |
positives++;//keep track of total positives |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public double ProbabilityOfInputIfPositive(double in[]){ | |
double prob = 1/Math.sqrt(2 * Math.PI) ; | |
for(int j=0; j<in.length;j++){ | |
prob*= Math.exp(- (in[j]-posmean[j])*(in[j]-posmean[j]) / (2*posvariance[j]) ) ; | |
} | |
return prob ; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Calculate the probability that the given input is in the positive class | |
public double probability(double in[]){ | |
double relativepositive=0,relativenegative=0; | |
for(int j=0; j<in.length; j++){ | |
relativepositive += (in[j]-posmean[j])*(in[j]-posmean[j]) / posvariance[j] ; | |
relativenegative += (in[j]-negmean[j])*(in[j]-negmean[j]) / negvariance[j] ; | |
} | |
relativepositive = positives*Math.exp(0.5*relativepositive) ; | |
relativenegative = negatives*Math.exp(0.5*relativenegative) ; | |
return relativepositive / (relativepositive + relativenegative) ; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//performs a least squares fit of a polynomial function of the given degree | |
//mapping each input[k] vector to each output[k] vector | |
//returns the coefficients in a matrix | |
public static double[][] fitpolynomial(double input[][], double output[][], int degree){ | |
double[][] X = new double[input.length][]; | |
//Run the input through the polynomialization and add the bias term | |
for (int k = 0; k < input.length; k++){ | |
X[k] = polynomial(input[k], degree); | |
} | |
int inputs = X[0].length ;//number of inputs after the polynomial |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
double[][] L = new double[inputs][ inputs]; | |
double D[] = new double[inputs] ; | |
//for each column j | |
for (int j = 0; j < inputs; j++){ | |
D[j] = XTX[j][j];//calculate Dj | |
for (int k = 0; k < j; k++){ | |
D[j] -= L[j][k] * L[j][k] * D[k]; | |
} | |
//calculate jth column of L | |
L[j][j] = 1 ; // don't really need to save this but its a 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public double[] solvesystem(double L[][], double D[], double XTY[]){ | |
//back substitution with L | |
double p[] = new double[XTY.length] ; | |
for (int j = 0; j < inputs; j++){ | |
p[j] = XTY[j] ; | |
for (int i = 0; i < j; i++){ | |
p[j] -= L[j][i] * p[i]; | |
} | |
} | |
//Multiply by inverse of D matrix |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*A rotation forest algorithm for binary classification with fixed length feature vectors. | |
*created by Alrecenk for inductivebias.com Oct 2013 | |
*/ | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Random; | |
public class RotationForestSimple{ | |
double mean[] ; //the mean of each axis for normalization |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int splitvariable=-1; // split on this variable | |
double splitvalue ;//split at this value | |
// total positives and negatives used for leaf node probabilities | |
int totalpositives,totalnegatives ; | |
Datapoint trainingdata[]; //the training data in this node | |
treenode leftnode,rightnode;//This node's children if it's a branch | |
//splits this node greedily using approximate information gain | |
public void split(){ | |
double bestscore = Maxvalue ;//lower is better so default is very high number |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//bootstrap aggregating of training data for a random forest | |
Random rand = new Random(seed); | |
treenode tree[] = new treenode[trees] ; | |
for(int k=0;k<trees;k++){ | |
ArrayList<Datapoint> treedata = new ArrayList<Datapoint>() | |
for (int j = 0; j < datapermodel; j++){ | |
//add a random data point to the training data for this tree | |
int nj = Math.abs(rand.nextInt())%data.size(); | |
treedata.add(alldata.get(nj)) ; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//makes a vector of length one | |
public static void normalize(double a[]){ | |
double scale = 0 ; | |
for(int k=0;k<a.length;k++){ | |
scale+=a[k]*a[k]; | |
} | |
scale = 1/Math.sqrt(scale); | |
for(int k=0;k<a.length;k++){ | |
a[k]*=scale ; | |
} |
OlderNewer