List of usage examples for weka.classifiers.trees Id3 buildClassifier
@Override public void buildClassifier(Instances data) throws Exception
From source file:Control.Classificador.java
public ArrayList<Resultado> classificar(Plano plano, Arquivo arq) { try {// w ww .j a v a2 s . 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:tubes.ml.pkg1.TubesML1.java
public void akses() throws Exception { Discretize filter;/*from w ww. jav a 2 s.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(); }