List of usage examples for weka.classifiers.trees J48 J48
J48
From source file:tubes.ml.pkg1.TubesML1.java
public void akses() throws Exception { Discretize filter;/*from w ww . j av a 2s.c o m*/ int fold = 10; int fold3 = 3; int trainNum, testNum; PrintWriter file = new PrintWriter("model.txt"); /***dataset 1***/ file.println("***DATASET 1***"); fileReader tets = new fileReader("./src/data/iris.arff"); try { tets.read(); } catch (IOException ex) { Logger.getLogger(TubesML1.class.getName()).log(Level.SEVERE, null, ex); } Instances data = tets.getData(); filter = new Discretize(); try { filter.setInputFormat(data); } catch (Exception ex) { Logger.getLogger(TubesML1.class.getName()).log(Level.SEVERE, null, ex); } /*ID3*/ Instances discreteData; discreteData = Filter.useFilter(data, filter); trainNum = discreteData.numInstances() * 3 / 4; testNum = discreteData.numInstances() / 4; for (int i = 0; i < fold; i++) { try { Instances train = discreteData.trainCV(fold, i); Instances test = discreteData.testCV(fold, i); Id3 iTiga = new Id3(); Evaluation validation = new Evaluation(train); try { iTiga.buildClassifier(train); System.out.println(iTiga.toString()); file.println(iTiga.toString()); } catch (Exception ex) { Logger.getLogger(TubesML1.class.getName()).log(Level.SEVERE, null, ex); } validation.evaluateModel(iTiga, test); System.out.println(validation.toSummaryString()); file.println("Validation " + (i + 1)); file.println(validation.toSummaryString()); } catch (Exception ex) { Logger.getLogger(TubesML1.class.getName()).log(Level.SEVERE, null, ex); } } /*J48*/ trainNum = data.numInstances() * 3 / 4; testNum = data.numInstances() / 4; J48 jKT = new J48(); for (int i = 0; i < fold; i++) { Instances train = data.trainCV(fold, i); Instances test = data.testCV(fold, i); try { Evaluation validation = new Evaluation(train); try { jKT.buildClassifier(data); } catch (Exception ex) { Logger.getLogger(TubesML1.class.getName()).log(Level.SEVERE, null, ex); } validation.evaluateModel(jKT, test); System.out.println(validation.toSummaryString()); file.println("Validation " + (i + 1)); file.println(validation.toSummaryString()); // System.out.println(jKT.toString()); } catch (Exception ex) { Logger.getLogger(TubesML1.class.getName()).log(Level.SEVERE, null, ex); } } /*dataset 2*/ file.println("***DATASET 2***"); tets.setFilepath("./src/data/weather.arff"); try { tets.read(); } catch (IOException ex) { Logger.getLogger(TubesML1.class.getName()).log(Level.SEVERE, null, ex); } data = new Instances(tets.getData()); /*ID3*/ discreteData = Filter.useFilter(data, filter); trainNum = discreteData.numInstances() * 3 / 4; testNum = discreteData.numInstances() / 4; for (int i = 0; i < fold3; i++) { try { Instances train = discreteData.trainCV(trainNum, i); Instances test = discreteData.testCV(testNum, i); Id3 iTiga = new Id3(); Evaluation validation = new Evaluation(train); try { iTiga.buildClassifier(train); System.out.println(iTiga.toString()); //file.println(iTiga.toString()); } catch (Exception ex) { Logger.getLogger(TubesML1.class.getName()).log(Level.SEVERE, null, ex); } validation.evaluateModel(iTiga, test); System.out.println(validation.toSummaryString()); file.println("Validation " + (i + 1)); file.println(validation.toSummaryString()); } catch (Exception ex) { Logger.getLogger(TubesML1.class.getName()).log(Level.SEVERE, null, ex); } } System.out.println(testNum); file.println("Test Number"); file.println(testNum); /*J48*/ trainNum = data.numInstances() * 3 / 4; testNum = data.numInstances() / 4; for (int i = 0; i < fold; i++) { Instances train = data.trainCV(fold, i); Instances test = data.testCV(fold, i); try { Evaluation validation = new Evaluation(train); try { jKT.buildClassifier(data); } catch (Exception ex) { Logger.getLogger(TubesML1.class.getName()).log(Level.SEVERE, null, ex); } validation.evaluateModel(jKT, test); System.out.println(validation.toSummaryString()); file.println(validation.toSummaryString()); System.out.println(jKT.toString()); file.println(jKT.toString()); } catch (Exception ex) { Logger.getLogger(TubesML1.class.getName()).log(Level.SEVERE, null, ex); } } /*dataset 3*/ file.println("***DATASET 3***"); tets.setFilepath("./src/data/weather.nominal.arff"); try { tets.read(); } catch (IOException ex) { Logger.getLogger(TubesML1.class.getName()).log(Level.SEVERE, null, ex); } data = new Instances(tets.getData()); /*ID3*/ discreteData = Filter.useFilter(data, filter); trainNum = discreteData.numInstances() * 3 / 4; testNum = discreteData.numInstances() / 4; for (int i = 0; i < fold3; i++) { try { Instances train = discreteData.trainCV(fold, i); Instances test = discreteData.testCV(fold, i); Id3 iTiga = new Id3(); Evaluation validation = new Evaluation(train); try { iTiga.buildClassifier(train); System.out.println(iTiga.toString()); file.println(iTiga.toString()); } catch (Exception ex) { Logger.getLogger(TubesML1.class.getName()).log(Level.SEVERE, null, ex); } validation.evaluateModel(iTiga, test); System.out.println(validation.toSummaryString()); file.println(validation.toSummaryString()); } catch (Exception ex) { Logger.getLogger(TubesML1.class.getName()).log(Level.SEVERE, null, ex); } } System.out.println(testNum); file.println("Test Number"); file.println(testNum); /*J48*/ trainNum = data.numInstances() * 3 / 4; testNum = data.numInstances() / 4; for (int i = 0; i < fold; i++) { Instances train = data.trainCV(fold, i); Instances test = data.testCV(fold, i); try { Evaluation validation = new Evaluation(train); try { jKT.buildClassifier(data); } catch (Exception ex) { Logger.getLogger(TubesML1.class.getName()).log(Level.SEVERE, null, ex); } validation.evaluateModel(jKT, test); System.out.println(validation.toSummaryString()); file.println(validation.toSummaryString()); System.out.println(jKT.toString()); file.println(jKT.toString()); } catch (Exception ex) { Logger.getLogger(TubesML1.class.getName()).log(Level.SEVERE, null, ex); } } /*RESULTT*/ System.out.println(jKT.toString()); file.println("RESULT"); file.println(jKT.toString()); file.close(); }
From source file:tubes1.Main.java
/** * @param args the command line arguments *//* w w w.j a va2s . c o m*/ public static void main(String[] args) throws IOException, Exception { // TODO code application logic here String filename = "weather"; //Masih belum mengerti tipe .csv yang dapat dibaca seperti apa //CsvToArff convert = new CsvToArff(filename+".csv"); //LOAD FILE BufferedReader datafile = readDataFile("src/" + filename + ".arff"); Instances data = new Instances(datafile); data.setClassIndex(data.numAttributes() - 1); //END OF LOAD FILE CustomFilter fil = new CustomFilter(); //REMOVE USELESS ATTRIBUTE data = fil.removeAttribute(data); System.out.println(data); Instances[] allData = new Instances[4]; //data for Id3 allData[0] = fil.resampling(fil.convertNumericToNominal(data)); //data for J48 allData[1] = fil.convertNumericToNominal(fil.resampling(data)); //data for myId3 allData[2] = allData[0]; //data for myC4.5 allData[3] = fil.resampling(fil.convertNumericToNominal(fil.convertNumericRange(data))); data = fil.convertNumericToNominal(data); // BUILD CLASSIFIERS Classifier[] models = { new Id3(), //C4.5 new J48(), new myID3(), new myC45() }; for (int j = 0; j < models.length; j++) { FastVector predictions = new FastVector(); //FOR TEN-FOLD CROSS VALIDATION Instances[][] split = crossValidationSplit(allData[j], 10); // Separate split into training and testing arrays Instances[] trainingSplits = split[0]; Instances[] testingSplits = split[1]; System.out.println("\n---------------------------------"); for (int i = 0; i < trainingSplits.length; i++) { try { // System.out.println("Building for training Split : " + i); Evaluation validation = classify(models[j], trainingSplits[i], testingSplits[i]); predictions.appendElements(validation.predictions()); // Uncomment to see the summary for each training-testing pair. // System.out.println(models[j].toString()); } catch (Exception ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } // Calculate overall accuracy of current classifier on all splits double accuracy = calculateAccuracy(predictions); // Print current classifier's name and accuracy in a complicated, // but nice-looking way. System.out.println(String.format("%.2f%%", accuracy)); } models[j].buildClassifier(allData[j]); Model.save(models[j], models[j].getClass().getSimpleName()); } //test instance Instances trainingSet = new Instances("Rel", getFvWekaAttributes(data), 10); trainingSet.setClassIndex(data.numAttributes() - 1); Instance testInstance = new Instance(data.numAttributes()); for (int i = 0; i < data.numAttributes() - 1; i++) { System.out.print("Masukkan " + data.attribute(i).name() + " : "); Scanner in = new Scanner(System.in); String att = in.nextLine(); if (isNumeric(att)) { att = fil.convertToFit(att, data, i); } testInstance.setValue(data.attribute(i), att); } // System.out.println(testInstance); // System.out.println(testInstance.classAttribute().index()); trainingSet.add(testInstance); Classifier Id3 = Model.load("Id3"); Classifier J48 = Model.load("J48"); Classifier myID3 = Model.load("myID3"); Classifier MyC45 = Model.load("myC45"); // Classifier MyId3 = Model.load("myID3"); Instance A = trainingSet.instance(0); Instance B = trainingSet.instance(0); Instance C = trainingSet.instance(0); Instance D = trainingSet.instance(0); //test with ID3 WEKA A.setClassValue(Id3.classifyInstance(trainingSet.instance(0))); System.out.println("Id3 Weka : " + A); //test with C4.5 WEKA B.setClassValue(J48.classifyInstance(trainingSet.instance(0))); System.out.println("C4.5 Weka : " + B); //test with my C4.5 C.setClassValue(MyC45.classifyInstance(trainingSet.instance(0))); System.out.println("My C4.5 : " + C); //test with my ID3 D.setClassValue(myID3.classifyInstance(trainingSet.instance(0))); System.out.println("My ID3 : " + D); }
From source file:tucil.dua.ai.TucilDuaAi.java
public static void fullTrainingSet() throws Exception { Classifier j48 = new J48(); j48.buildClassifier(datas);//from w w w. jav a 2 s.c om Evaluation eval = new Evaluation(datas); eval.evaluateModel(j48, datas); System.out.println("=====Run Information======"); System.out.println("======Classifier Model======"); System.out.println(j48.toString()); System.out.println(eval.toSummaryString("====Stats======\n", false)); System.out.println(eval.toClassDetailsString("====Detailed Result=====\n")); System.out.println(eval.toMatrixString("======Confusion Matrix======\n")); }
From source file:tucil.dua.ai.TucilDuaAi.java
public static void crossValidation() throws Exception { Evaluation evaluation = new Evaluation(datas); Classifier attr_tree = new J48(); attr_tree.buildClassifier(datas);/*from w w w . java 2 s. c o m*/ evaluation.crossValidateModel(attr_tree, datas, 10, new Random(1)); System.out.println("=====Run Information======"); System.out.println("======Classifier Model======"); System.out.println(attr_tree.toString()); System.out.println(evaluation.toSummaryString("====Stats======\n", false)); System.out.println(evaluation.toClassDetailsString("====Detailed Result=====\n")); System.out.println(evaluation.toMatrixString("======Confusion Matrix======\n")); }
From source file:tucil2ai.Tucil2AI.java
/** * * @param args/*from w w w. j a v a 2s . c om*/ * @throws Exception */ public static void main(String[] args) throws Exception { // TODO code application logic here DataSource source; Instances data; Instances data_filtered; Discretize filter; Classifier clsJ48 = null; source = new DataSource("res/iris.arff"); data = source.getDataSet(); data.setClassIndex(data.numAttributes() - 1); System.out.println("Data berhasil dibaca"); // filter filter = new Discretize(); filter.setInputFormat(data); data_filtered = Filter.useFilter(data, filter); System.out.println("Data berhasil difilter"); int pil = 0; while (pil != 3) { menu(); Scanner sc = new Scanner(System.in); pil = sc.nextInt(); switch (pil) { case 1: clsJ48 = loadModel(); System.out.println("Model berhasil dimuat"); break; case 2: clsJ48 = new J48(); clsJ48.buildClassifier(data_filtered); int pil3; System.out.println("Apakah Anda mau menyimpan model ini?"); System.out.println("1. Ya"); System.out.println("2. Tidak"); pil3 = sc.nextInt(); if (pil3 == 1) { saveModel(clsJ48); } break; default: break; } if (pil == 3) continue; System.out.println("Pilih metode evaluasi:"); System.out.println("1. 10-fold cross validation"); System.out.println("2. Full-test"); System.out.println("3. Klasifikasikan datatest"); int pilmethod; pilmethod = sc.nextInt(); while (pilmethod < 1 || pilmethod > 3) { System.out.println("Masukan salah!"); pilmethod = sc.nextInt(); } if (pilmethod != 3) { Evaluation eval = evalJ48(clsJ48, data_filtered, pilmethod == 1); printEval(eval); } else { ClassifyJ48(clsJ48, "res/datatest.arff", filter); System.out.println("Terbentuk 3 instance baru untuk diklasifikasi"); } } }
From source file:uv.datamining.tp2.WekaModeler.java
public static void generarArbol(File file, float cm) throws Exception { ArffLoader loader = new ArffLoader(); loader.setFile(file);/*from w w w . ja v a2 s . co m*/ Instances data = loader.getDataSet(); data.setClassIndex(data.numAttributes() - 1); //columna con el atributo clase J48 tree = new J48(); tree.setConfidenceFactor(cm); tree.buildClassifier(data); Evaluation eval = new Evaluation(data); eval.evaluateModel(tree, data); System.out.println(eval.toSummaryString()); weka.core.SerializationHelper.write( file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf(".")) + ".model", tree); }
From source file:wekimini.learning.AdaboostModelBuilder.java
public AdaboostModelBuilder() { classifier = new AdaBoostM1(); // ((AdaBoostM1) classifier).setClassifier(new DecisionStump()); ((AdaBoostM1) classifier).setClassifier(new J48()); ((AdaBoostM1) classifier).setNumIterations(defaultNumRounds); }
From source file:wekimini.learning.AdaboostModelBuilder.java
public void setBaseLearnerType(BaseLearner t) { baseLearnerType = t;/* ww w.ja v a2s.co m*/ if (t == BaseLearner.DECISION_STUMP) { ((AdaBoostM1) classifier).setClassifier(new DecisionStump()); } else { ((AdaBoostM1) classifier).setClassifier(new J48()); } }
From source file:wekimini.learning.J48ModelBuilder.java
public J48ModelBuilder() { classifier = new J48(); }
From source file:wtute.engine.AnalysisEngine.java
public void train() throws Exception { Instances trainingInstances = createInstances("TRAINING INS"); for (int i = 0; i < data.numInstances(); i++) { Instance instance = convertInstance(data.instance(i)); instance.setDataset(trainingInstances); trainingInstances.add(instance); }// w ww. j a va 2s. c o m System.out.println(data); J48 classifier = new J48(); try { //classifier training code classifier.buildClassifier(trainingInstances); //storing the trained classifier to a file for future use weka.core.SerializationHelper.write("J48.model", classifier); } catch (Exception ex) { System.out.println("Exception in training the classifier."); } }