Weka create instance and training data using arff


Read training data from arff file:


public static Instances get_instances_from_arff() throws Exception {
	BufferedReader breader = null;
	breader = new BufferedReader( new FileReader(System.getProperty("user.dir")+"/src/weka_usage/exploration_tracks.arff") );	
	Instances training_data = new Instances(breader);
	training_data.setClassIndex(training_data.numAttributes() -1);
	breader.close();	
	return training_data;
}

Create new instance with new provided attributes:



public static Instance create_instance(double[] attr, Instances training_data){
	// Create the instance	
	DenseInstance inst = new DenseInstance(4);
	//Instance inst = new DenseInstance(4);
	inst.setValue(0, attr[0]); //web
	inst.setValue(1, attr[1]); //db
        inst.setValue(2, attr[2]); //arrival rate																																																																																																					                               
	inst.setValue(3, attr[3]); // response time
	
	inst.setDataset(training_data); // assosiate training data with instance to help in its classification
	return inst;
}

build_classifier method:


public static AbstractClassifier build_classifier(String type, Instances data) throws Exception{

	if(type == "RandomForest"){
		RandomForest rF = new RandomForest();
		rF.buildClassifier(data);
		return rF;
	}
	else if(type == "MultilayerPerceptron"){
		MultilayerPerceptron rF = new MultilayerPerceptron();
		rF.buildClassifier(data);
		return rF;
	}
	else if(type == "LinearRegression"){
		LinearRegression rF = new LinearRegression();
		rF.buildClassifier(data);
		return rF;
	}
	else{
		//GaussianProcesses is default
		GaussianProcesses rF = new GaussianProcesses();
		rF.buildClassifier(data);
		return rF;			
	}
	
}
Advertisements

I am Senior Software Engineer. I love reading, writing, sharing,developing, hiking, movies, trips, mountains, brooks, hills etc.

Tagged with: , , ,
Posted in Weka

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

StackOverFlow
Categories
Archives
%d bloggers like this: