Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

how to generate classattribute for our dataset

  Asked By: Priyanka    Date: Oct 29    Category: Java    Views: 703
  

Hello! I am beginner is java coding using weka packages. After training the classifier by using instances in test set the program should generate the class attributes depending up on the attributes of each instance. I have written the following code to do this task but this code is overwriting the last attribute since i have deleted the class attribute of the test set before generating the class attribute and it is replacing the value of last attribute as completely zero. please can anybody help me to get the proper output?

Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package attributeselectiontest;
import static attributeselectiontest.Dup1J48.useClassifier;
import static attributeselectiontest.DupJ48.useClassifier;
import java.io.*;
import weka.attributeSelection.*;
import weka.core.*;
import weka.core.converters.ConverterUtils.*;
import weka.classifiers.*;
import weka.classifiers.Classifier;
import weka.classifiers.DistributionClassifier;
import weka.classifiers.meta.*;
import weka.classifiers.trees.*;
import weka.filters.*;
import java.util.*;
import weka.classifiers.trees.j48.ClassifierSplitModel;
import weka.classifiers.trees.j48.ClassifierTree;
import weka.classifiers.trees.j48.ModelSelection;
import weka.core.converters.ArffSaver;

/**
* performs attribute selection using CfsSubsetEval and GreedyStepwise
* (backwards) and trains J48 with that. Needs 3.5.5 or higher to compile.
*
* @author FracPete (fracpete at waikato dot ac dot nz)
*/


/**
*
* @author CORI
*/
public class Dup2J48 {
/** The node's successors. */
private Id3[] m_Successors;

/** Attribute used for splitting. */
private Attribute m_Attribute;

/** Class value if node is leaf. */
private double m_ClassValue;

/** Class distribution if node is leaf. */
private double[] m_Distribution;

/** Class attribute of dataset. */
private Attribute m_ClassAttribute;


/**
* uses the meta-classifier
*/
protected static void useClassifier(Instances data) throws Exception {
System.out.println("\n1. Meta-classfier");
AttributeSelectedClassifier classifier = new AttributeSelectedClassifier();
CfsSubsetEval eval = new CfsSubsetEval();
GreedyStepwise search = new GreedyStepwise();
search.setSearchBackwards(true);
J48 base = new J48();
classifier.setClassifier(base);
classifier.setEvaluator(eval);
classifier.setSearch(search);
Evaluation evaluation = new Evaluation(data);
evaluation.crossValidateModel(classifier, data, 10, new Random(1));
System.out.println(evaluation.toSummaryString());
}

/**
* uses the filter
*/
protected static void useFilter(Instances data) throws Exception {
System.out.println("\n2. Filter");
weka.filters.supervised.attribute.AttributeSelection filter = new weka.filters.supervised.attribute.AttributeSelection();
CfsSubsetEval eval = new CfsSubsetEval();
GreedyStepwise search = new GreedyStepwise();
search.setSearchBackwards(true);
filter.setEvaluator(eval);
filter.setSearch(search);
filter.setInputFormat(data);
Instances newData = Filter.useFilter(data, filter);
System.out.println(newData);
}

/**
* uses the low level approach
*/
protected static void useLowLevel(Instances data) throws Exception {
System.out.println("\n3. Low-level");
AttributeSelection attsel = new AttributeSelection();
CfsSubsetEval eval = new CfsSubsetEval();
GreedyStepwise search = new GreedyStepwise();
search.setSearchBackwards(true);
attsel.setEvaluator(eval);
attsel.setSearch(search);
attsel.SelectAttributes(data);
int[] indices = attsel.selectedAttributes();
System.out.println("selected attribute indices (starting with 0):\n" + Utils.arrayToString(indices));
}
// load data
public static void load()throws Exception
{
System.out.println("\n0. Loading data");
DataSource source = new DataSource("C:\\Documents and Settings\\cori\\My Documents\\NetBeansProjects\\attributeselectiontest\\src\\attributeselectiontest\\trainingset1.arff");
Instances data = source.getDataSet();
if (data.classIndex() == -1)
data.setClassIndex(data.numAttributes() - 1);

// 1. meta-classifier
useClassifier(data);

// 2. filter
useFilter(data);

// 3. low-level
useLowLevel(data);
}
public double classifyInstance(Instance instance) throws NoSupportForMissingValuesException {
if(instance.hasMissingValue())
{
throw new NoSupportForMissingValuesException("Id3: no missing values, "+"please.");
}
if (m_Attribute == null) {
return m_ClassValue;
} else {
return m_Successors[(int) instance.value(m_Attribute)].
classifyInstance(instance);

}
}
public double[] distributionForInstance(Instance instance) throws NoSupportForMissingValuesException {
if(instance.hasMissingValue())
{
throw new NoSupportForMissingValuesException("Id3: no missing values, "+"please.");
}

if (m_Attribute == null) {
return m_Distribution;
} else {
return m_Successors[(int) instance.value(m_Attribute)].
distributionForInstance(instance);
}
}


/**
* takes a dataset as first argument
*
* @param args the commandline arguments
* @throws Exception if something goes wrong
*/
public static void main(String[] args) throws Exception {
load();



Dup2J48 tree=new Dup2J48();



try{ BufferedReader reader1 = new BufferedReader(new FileReader(
"C:\\Documents and Settings\\cori\\My Documents\\NetBeansProjects\\attributeselectiontest\\src\\attributeselectiontest\\testset1.arff"));

Instances inst1 = new Instances(reader1);
inst1.deleteAttributeAt(14);
ArffSaver saver1 = new ArffSaver();
saver1.setInstances(inst1);
File destFile1 = new File("C:\\Documents and Settings\\cori\\My Documents\\NetBeansProjects\\attributeselectiontest\\src\\attributeselectiontest\\tstwoca1.arff");
saver1.setFile(destFile1);
saver1.writeBatch();

// to generate class attribute
Instances unlabeled = new Instances(
new BufferedReader(
new FileReader("C:\\Documents and Settings\\cori\\My Documents\\NetBeansProjects\\attributeselectiontest\\src\\attributeselectiontest\\tstwoca1.arff")));
// System.out.println("The number of attributes"+unlabeled.numAttributes());
// set class attribute
unlabeled.setClassIndex(unlabeled.numAttributes()-1 );

// create copy
Instances labeled = new Instances(unlabeled);

// label instances
for (int i = 0; i < unlabeled.numInstances(); i++) {
double clsLabel = tree.classifyInstance(unlabeled.instance(i));
labeled.instance(i).setClassValue(clsLabel);
}
// save labeled data
BufferedWriter writer = new BufferedWriter(
new FileWriter("C:\\Documents and Settings\\cori\\My Documents\\NetBeansProjects\\attributeselectiontest\\src\\attributeselectiontest\\testwca3.arff"));


writer.write(labeled.toString());
writer.newLine();
writer.flush();
writer.close();

}
catch(Exception e)
{
e.printStackTrace();
}
}



}



Share: 

 

No Answers Found. Be the First, To Post Answer.

 
Didn't find what you were looking for? Find more on how to generate classattribute for our dataset Or get search suggestion and latest updates.