List of usage examples for weka.classifiers.trees Id3 Id3
Id3
From source file:Control.Classificador.java
public ArrayList<Resultado> classificar(Plano plano, Arquivo arq) { try {/*from w w w . ja v a 2s. c o m*/ FileReader leitor = new FileReader(arq.arquivo); Instances conjunto = new Instances(leitor); conjunto.setClassIndex(conjunto.numAttributes() - 1); Evaluation avaliacao = new Evaluation(conjunto); conjunto = conjunto.resample(new Random()); Instances baseTreino = null, baseTeste = null; Random rand = new Random(1); if (plano.eHoldOut) { baseTeste = conjunto.testCV(3, 0); baseTreino = conjunto.trainCV(3, 0); } else { baseTeste = baseTreino = conjunto; } if (plano.IBK) { try { IB1 vizinho = new IB1(); vizinho.buildClassifier(baseTeste); avaliacao.crossValidateModel(vizinho, baseTeste, (plano.eHoldOut) ? 4 : baseTeste.numInstances(), rand); Resultado resultado = new Resultado("NN", avaliacao.toMatrixString("Algortmo Vizinho Mais Prximo - Matriz de Confuso"), avaliacao.toClassDetailsString("kNN")); resultado.setTaxaErro(avaliacao.errorRate()); resultado.setTaxaAcerto(1 - avaliacao.errorRate()); resultado.setRevocacao(recallToDouble(avaliacao, baseTeste)); resultado.setPrecisao(precisionToDouble(avaliacao, baseTeste)); this.resultados.add(resultado); } catch (UnsupportedAttributeTypeException ex) { Mensagem.erro("Algortmo IB1 no suporta atributos numricos!", "MTCS - ERRO"); } } if (plano.J48) { try { J48 j48 = new J48(); j48.buildClassifier(baseTeste); avaliacao.crossValidateModel(j48, baseTeste, (plano.eHoldOut) ? 4 : baseTeste.numInstances(), rand); Resultado resultado = new Resultado("J48", avaliacao.toMatrixString("Algortmo J48 - Matriz de Confuso"), avaliacao.toClassDetailsString("J48")); resultado.setTaxaErro(avaliacao.errorRate()); resultado.setTaxaAcerto(1 - avaliacao.errorRate()); resultado.setRevocacao(recallToDouble(avaliacao, baseTeste)); resultado.setPrecisao(precisionToDouble(avaliacao, baseTeste)); this.resultados.add(resultado); } catch (UnsupportedAttributeTypeException ex) { Mensagem.erro("Algortmo J48 no suporta atributos nominais!", "MTCS - ERRO"); } } if (plano.KNN) { try { IBk knn = new IBk(3); knn.buildClassifier(baseTeste); avaliacao.crossValidateModel(knn, baseTeste, (plano.eHoldOut) ? 4 : baseTeste.numInstances(), rand); Resultado resultado = new Resultado("KNN", avaliacao.toMatrixString("Algortmo KNN - Matriz de Confuso"), avaliacao.toClassDetailsString("kNN")); resultado.setTaxaErro(avaliacao.errorRate()); resultado.setTaxaAcerto(1 - avaliacao.errorRate()); resultado.setRevocacao(recallToDouble(avaliacao, baseTeste)); resultado.setPrecisao(precisionToDouble(avaliacao, baseTeste)); this.resultados.add(resultado); } catch (UnsupportedAttributeTypeException ex) { Mensagem.erro("Algortmo KNN no suporta atributos numricos!", "MTCS - ERRO"); } } if (plano.Naive) { NaiveBayes naive = new NaiveBayes(); naive.buildClassifier(baseTeste); avaliacao.crossValidateModel(naive, baseTeste, (plano.eHoldOut) ? 4 : baseTeste.numInstances(), rand); Resultado resultado = new Resultado("Naive", avaliacao.toMatrixString("Algortmo NaiveBayes - Matriz de Confuso"), avaliacao.toClassDetailsString("kNN")); resultado.setTaxaErro(avaliacao.errorRate()); resultado.setTaxaAcerto(1 - avaliacao.errorRate()); resultado.setRevocacao(recallToDouble(avaliacao, baseTeste)); resultado.setPrecisao(precisionToDouble(avaliacao, baseTeste)); this.resultados.add(resultado); } if (plano.Tree) { try { Id3 id3 = new Id3(); id3.buildClassifier(baseTeste); avaliacao.crossValidateModel(id3, baseTeste, (plano.eHoldOut) ? 4 : baseTeste.numInstances(), rand); Resultado resultado = new Resultado("ID3", avaliacao.toMatrixString("Algortmo ID3 - Matriz de Confuso"), avaliacao.toClassDetailsString("kNN")); resultado.setTaxaErro(avaliacao.errorRate()); resultado.setTaxaAcerto(1 - avaliacao.errorRate()); resultado.setRevocacao(recallToDouble(avaliacao, baseTeste)); resultado.setPrecisao(precisionToDouble(avaliacao, baseTeste)); this.resultados.add(resultado); } catch (UnsupportedAttributeTypeException ex) { Mensagem.erro("Algortmo Arvore de Deciso no suporta atributos numricos!", "MTCS - ERRO"); } } } catch (FileNotFoundException ex) { Logger.getLogger(Classificador.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(Classificador.class.getName()).log(Level.SEVERE, null, ex); } catch (NullPointerException ex) { Mensagem.erro("Selecione um arquivo para comear!", "MTCS - ERRO"); Logger.getLogger(Classificador.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { Logger.getLogger(Classificador.class.getName()).log(Level.SEVERE, null, ex); } return this.resultados; }
From source file:DataMiningLogHistoriKIRIPercobaan2.DecisionTree.java
public String id3(Instances arff) { tree = new Id3(); try {//from w w w . j a v a 2 s.c om NumericToNominal convert = new NumericToNominal(); String[] options = new String[2]; options[0] = "-R"; options[1] = "1-4"; convert.setOptions(options); convert.setInputFormat(arff); Instances newData = Filter.useFilter(arff, convert); tree.buildClassifier(newData); } catch (Exception ex) { Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex); } return tree.toString(); }
From source file:id3j48.WekaAccess.java
public static void main(String[] args) { initializePath();/*from w w w. j a v a2s . c o m*/ try { cin = new Scanner(System.in); Instances data = null, tempdata; Classifier NBclassifier, ID3classifier, j48classifier; Evaluation NBeval, ID3eval, j48eval; System.out.println("Enter filename below"); String filename = cin.nextLine(); System.out.println("Loading " + filename + "..."); String extension = ""; String name = ""; int i = filename.lastIndexOf('.'); if (i > 0) { extension = filename.substring(i + 1); name = filename.substring(0, i); } if (extension.equalsIgnoreCase("arff")) { try { data = readArff(filename); } catch (Exception ex) { Logger.getLogger(WekaAccess.class.getName()).log(Level.SEVERE, null, ex); } } else if (extension.equalsIgnoreCase("csv")) { try { data = readCsv(filename); } catch (Exception ex) { Logger.getLogger(WekaAccess.class.getName()).log(Level.SEVERE, null, ex); } } else { System.out.println("Invalid extension"); System.exit(0); } System.out.println(data.toString()); System.out.println("Resample data? (y for yes) "); String resample = cin.nextLine(); if (resample.equalsIgnoreCase("y")) { try { tempdata = resampleData(data); System.out.println("-- Resampled data --"); System.out.println(tempdata.toString()); } catch (Exception ex) { Logger.getLogger(WekaAccess.class.getName()).log(Level.SEVERE, null, ex); } } tempdata = removeAttribute(data, data.numAttributes()); System.out.println("-- Remove Attribute --"); System.out.println(tempdata.toString()); NBclassifier = buildClassifier(data, new NaiveBayes()); System.out.println("-- Naive Bayes Classifier --"); System.out.println(NBclassifier.toString()); ID3classifier = buildClassifier(data, new Id3()); System.out.println("-- ID3 Classifier --"); System.out.println(ID3classifier.toString()); j48classifier = buildClassifier(data, new J48()); System.out.println("-- J48 Classifier --"); System.out.println(j48classifier.toString()); Instances test = null; if (extension.equalsIgnoreCase("arff")) test = readArff("test." + filename); else if (extension.equalsIgnoreCase("csv")) test = readCsv("test." + filename); NBeval = testModel(NBclassifier, data, test); System.out.println( NBeval.toSummaryString("-- Training set evaluation results with Naive Bayes --\n", false)); ID3eval = testModel(ID3classifier, data, test); System.out.println(NBeval.toSummaryString("-- Training set evaluation results with ID3 --\n", false)); j48eval = testModel(j48classifier, data, test); System.out.println(NBeval.toSummaryString("-- Training set evaluation results with J48 --\n", false)); NBeval = tenFoldCrossValidation(data, NBclassifier); System.out.println( NBeval.toSummaryString("-- 10-fold cross validation results with Naive Bayes --\n", false)); ID3eval = tenFoldCrossValidation(data, ID3classifier); System.out.println(NBeval.toSummaryString("-- 10-fold cross validation results with ID3 --\n", false)); j48eval = tenFoldCrossValidation(data, j48classifier); System.out.println(NBeval.toSummaryString("-- 10-fold cross validation results with J48 --\n", false)); NBeval = percentageSplit(data, NBclassifier, 66); System.out.println( NBeval.toSummaryString("-- 66% split validation results with Naive Bayes --\n", false)); ID3eval = percentageSplit(data, ID3classifier, 66); System.out.println(NBeval.toSummaryString("-- 66% split validation results with ID3 --\n", false)); j48eval = percentageSplit(data, j48classifier, 66); System.out.println(NBeval.toSummaryString("-- 66% split validation results with J48 --\n", false)); System.out.println("-- Save Naive Bayes Model --"); saveModel("nb." + name + ".model", NBclassifier); System.out.println("-- Save Naive Bayes Model --"); saveModel("id3." + name + ".model", ID3classifier); System.out.println("-- Save Naive Bayes Model --"); saveModel("j48." + name + ".model", j48classifier); System.out.println("-- Save Naive Bayes Model --"); saveModel("nb." + name + ".model", NBclassifier); System.out.println("-- Save ID3 Model --"); saveModel("id3." + name + ".model", ID3classifier); System.out.println("-- Save J48 Model --"); saveModel("j48." + name + ".model", j48classifier); System.out.println("-- Load Naive Bayes Model --"); System.out.println(loadModel("nb." + name + ".model").toString()); System.out.println("-- Load ID3 Model --"); System.out.println(loadModel("id3." + name + ".model").toString()); System.out.println("-- Load J48 Model --"); System.out.println(loadModel("j48." + name + ".model").toString()); System.out.println("-- Classify Naive Bayes Model --"); classify("classify." + filename, NBclassifier); System.out.println("-- Classify ID3 Model --"); classify("classify." + filename, ID3classifier); System.out.println("-- Classify J48 Model --"); classify("classify." + filename, j48classifier); } catch (Exception ex) { Logger.getLogger(WekaAccess.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:myclassifier.wekaCode.java
public static Classifier buildClassifier(Instances dataSet, int classifierType, boolean prune) throws Exception { Classifier classifier = null;/* w w w.j a v a 2 s . com*/ if (classifierType == BAYES) { classifier = new NaiveBayes(); classifier.buildClassifier(dataSet); } else if (classifierType == ID3) { classifier = new Id3(); classifier.buildClassifier(dataSet); } else if (classifierType == J48) { classifier = new J48(); classifier.buildClassifier(dataSet); } else if (classifierType == MyID3) { classifier = new MyID3(); classifier.buildClassifier(dataSet); } else if (classifierType == MyJ48) { MyJ48 j48 = new MyJ48(); j48.setPruning(prune); classifier = j48; classifier.buildClassifier(dataSet); } return classifier; }
From source file:net.sf.bddbddb.order.MyId3.java
License:LGPL
/** * Main method./*from w w w . ja v a 2s. c o m*/ * * @param args the options for the classifier */ public static void main(String[] args) { try { System.out.println(Evaluation.evaluateModel(new Id3(), args)); } catch (Exception e) { System.err.println(e.getMessage()); } }
From source file:net.sf.bddbddb.OrderClassifier.java
License:LGPL
public OrderClassifier(BDDInferenceRule rule, List domainSet, long max_wait_time) { this.rule = rule; this.bottomRelation = (BDDRelation) rule.bottom.getRelation(); this.domainSet = domainSet; this.max_wait_time = max_wait_time; orders = new TreeSet(); classifier = new Id3(); relationAttrPairs = new LinkedList(); domainToAttrPairs = new GenericMultiMap(); attributes = computeAttributes();/* ww w . j av a 2 s .co m*/ attributes.addElement(new Attribute("class")); /* FastVector values = new FastVector(2); values.addElement("good"); values.addElement("bad"); attributes.addElement(new Attribute("Time", values)); */ // data = new Instances("Ordering Constraints", attributes, 30); }
From source file:tubes.ml.pkg1.TubesML1.java
public void akses() throws Exception { Discretize filter;/*from w w w . j ava 2 s . co 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 ww .jav a 2 s. c om 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); }