List of usage examples for weka.classifiers.trees J48 J48
J48
From source file:function.FileModel.java
public static void SaveModel(String sourcepath, String outputpath) throws IOException, Exception { // create J48 //kayanya sih ntar ganti sama class clasifiernya Classifier cls = new J48(); // train//from w w w .j a va2 s . c o m Instances inst = new Instances(new BufferedReader(new FileReader(sourcepath))); inst.setClassIndex(inst.numAttributes() - 1); cls.buildClassifier(inst); // serialize model weka.core.SerializationHelper.write(outputpath, cls); }
From source file:gov.va.chir.tagline.TagLineEvaluator.java
License:Open Source License
public void evaluate(final ClassifierType type, final String... options) throws Exception { Classifier model = null;//from www .j a v a2 s. co m if (type == null) { throw new IllegalArgumentException("Classifier type must be specified"); } if (type.equals(ClassifierType.J48)) { model = new J48(); } else if (type.equals(ClassifierType.LMT)) { model = new LMT(); } else if (type.equals(ClassifierType.RandomForest)) { model = new RandomForest(); } else if (type.equals(ClassifierType.SVM)) { model = new LibSVM(); } else { throw new IllegalArgumentException(String.format("Classifier type not supported (%s)", type)); } if (model != null) { // Set classifier options if (options != null && options.length > 0) { if (model instanceof AbstractClassifier) { ((AbstractClassifier) model).setOptions(options); } } fc.setClassifier(model); final Attribute attrDocId = instances.attribute(DatasetUtil.DOC_ID); if (attrDocId == null) { throw new IllegalStateException(String.format("%s attribute must exist", DatasetUtil.DOC_ID)); } final List<Set<Object>> foldDocIds = getFoldDocIds(attrDocId); final RemoveWithValues rmv = new RemoveWithValues(); // RemoveWithValues filter is not zero-based! rmv.setAttributeIndex(String.valueOf(attrDocId.index() + 1)); rmv.setModifyHeader(false); final Evaluation eval = new Evaluation(instances); // Perform cross-validation for (int i = 0; i < numFolds; i++) { rmv.setNominalIndicesArr(getAttributeIndexValues(attrDocId, foldDocIds.get(i))); rmv.setInvertSelection(false); rmv.setInputFormat(instances); // Must be called AFTER all options final Instances train = Filter.useFilter(instances, rmv); rmv.setInvertSelection(true); rmv.setInputFormat(instances); // Must be called AFTER all options final Instances test = Filter.useFilter(instances, rmv); fc.buildClassifier(train); eval.evaluateModel(fc, test); } evaluationSummary = String.format("%s%s%s%s%s", eval.toSummaryString(), System.getProperty("line.separator"), eval.toMatrixString(), System.getProperty("line.separator"), eval.toClassDetailsString()); } }
From source file:gov.va.chir.tagline.TagLineTrainer.java
License:Open Source License
public TagLineTrainer(final ClassifierType type, final String... options) throws Exception { Classifier model = null;//from ww w . j a va 2 s. c o m if (type == null) { throw new IllegalArgumentException("Classifier type must be specified"); } if (type.equals(ClassifierType.J48)) { model = new J48(); } else if (type.equals(ClassifierType.LMT)) { model = new LMT(); } else if (type.equals(ClassifierType.RandomForest)) { model = new RandomForest(); } else if (type.equals(ClassifierType.SVM)) { model = new LibSVM(); } else { throw new IllegalArgumentException(String.format("Classifier type not supported (%s)", type)); } // Set classifier options if (options != null && options.length > 0) { if (model instanceof AbstractClassifier) { ((AbstractClassifier) model).setOptions(options); } } tagLineModel = new TagLineModel(); tagLineModel.setModel(model); instances = null; extractor = null; }
From source file:GroupProject.DMChartUI.java
private void buildJClassifier() { CSVtoArff converter = new CSVtoArff(); Instances students = null;//from w w w . j av a 2 s . c om Instances students2 = null; try { converter.convert("studentTemp.csv", "studentTemp.arff"); } catch (IOException ex) { Logger.getLogger(DMChartUI.class.getName()).log(Level.SEVERE, null, ex); } try { students = new Instances(new BufferedReader(new FileReader("studentTemp.arff"))); students2 = new Instances(new BufferedReader(new FileReader("studentTemp.arff"))); } catch (IOException ex) { Logger.getLogger(DMChartUI.class.getName()).log(Level.SEVERE, null, ex); } int target = dataSelector.getSelectedIndex() + 1; System.out.printf("this is the target: %d\n", target); if (target == 14 || target == 15 || target == 18 || target == 19) { System.out.println("Please select a nominal category"); equationDisplayArea.setText("Please select a nominal category"); return; } //set target students.setClassIndex(target); students2.setClassIndex(target); Jmodel = (Classifier) new J48(); try { System.out.println("im goin to build the modle"); Jmodel.buildClassifier(students); System.out.println("i finsihed building the mdoel "); } catch (Exception ex) { Logger.getLogger(DMChartUI.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:hero.unstable.util.classification.wekaClassifier.java
public wekaClassifier() { this.classifier = new J48(); this.nameClassifier = classifier.getClass().getName(); this.seed = 1; this.folds = 10; }
From source file:ia02classificacao.IA02Classificacao.java
/** * @param args the command line arguments *///from w ww . j av a2 s .c o m public static void main(String[] args) throws Exception { // abre o banco de dados arff e mostra a quantidade de instancias (linhas) DataSource arquivo = new DataSource("data/zoo.arff"); Instances dados = arquivo.getDataSet(); System.out.println("Instancias lidas: " + dados.numInstances()); // FILTER: remove o atributo nome do animal da classificao String[] parametros = new String[] { "-R", "1" }; Remove filtro = new Remove(); filtro.setOptions(parametros); filtro.setInputFormat(dados); dados = Filter.useFilter(dados, filtro); AttributeSelection selAtributo = new AttributeSelection(); InfoGainAttributeEval avaliador = new InfoGainAttributeEval(); Ranker busca = new Ranker(); selAtributo.setEvaluator(avaliador); selAtributo.setSearch(busca); selAtributo.SelectAttributes(dados); int[] indices = selAtributo.selectedAttributes(); System.out.println("Selected attributes: " + Utils.arrayToString(indices)); // Usa o algoritimo J48 e mostra a classificao dos dados em forma textual String[] opcoes = new String[1]; opcoes[0] = "-U"; J48 arvore = new J48(); arvore.setOptions(opcoes); arvore.buildClassifier(dados); System.out.println(arvore); // Usa o algoritimo J48 e mostra a classificao de dados em forma grafica /* TreeVisualizer tv = new TreeVisualizer(null, arvore.graph(), new PlaceNode2()); JFrame frame = new javax.swing.JFrame("?rvore de Conhecimento"); frame.setSize(800,500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(tv); frame.setVisible(true); tv.fitToScreen(); */ /* * Classificao de novos dados */ System.out.println("\n\nCLASSIFICAO DE NOVOS DADOS"); // criar atributos double[] vals = new double[dados.numAttributes()]; vals[0] = 1.0; // hair vals[1] = 0.0; // feathers vals[2] = 0.0; // eggs vals[3] = 1.0; // milk vals[4] = 1.0; // airborne vals[5] = 0.0; // aquatic vals[6] = 0.0; // predator vals[7] = 1.0; // toothed vals[8] = 1.0; // backbone vals[9] = 1.0; // breathes vals[10] = 0.0; // venomous vals[11] = 0.0; // fins vals[12] = 4.0; // legs vals[13] = 1.0; // tail vals[14] = 1.0; // domestic vals[15] = 1.0; // catsize // Criar uma instncia baseada nestes atributos Instance meuUnicornio = new DenseInstance(1.0, vals); // Adicionar a instncia nos dados meuUnicornio.setDataset(dados); // Classificar esta nova instncia double label = arvore.classifyInstance(meuUnicornio); // Imprimir o resultado da classificao System.out.println("Novo Animal: Unicrnio"); System.out.println("classificacao: " + dados.classAttribute().value((int) label)); /* * Avaliao e predio de erros de mtrica */ System.out.println("\n\nAVALIAO E PREDIO DE ERROS DE MTRICA"); Classifier cl = new J48(); Evaluation eval_roc = new Evaluation(dados); eval_roc.crossValidateModel(cl, dados, 10, new Random(1), new Object[] {}); System.out.println(eval_roc.toSummaryString()); /* * Matriz de confuso */ System.out.println("\n\nMATRIZ DE CONFUSO"); double[][] confusionMatrix = eval_roc.confusionMatrix(); System.out.println(eval_roc.toMatrixString()); }
From source file:ia03classificador.jFrClassificador.java
public void doClassificate() throws Exception { // Quando clicado, a variavel recebe 1, quando no clicado recebe 0 v00 = ((btn00.isSelected()) ? ((double) 1) : ((double) 0)); v01 = ((btn01.isSelected()) ? ((double) 1) : ((double) 0)); v02 = ((btn02.isSelected()) ? ((double) 1) : ((double) 0)); v03 = ((btn03.isSelected()) ? ((double) 1) : ((double) 0)); v04 = ((btn04.isSelected()) ? ((double) 1) : ((double) 0)); v05 = ((btn05.isSelected()) ? ((double) 1) : ((double) 0)); v06 = ((btn06.isSelected()) ? ((double) 1) : ((double) 0)); v07 = ((btn07.isSelected()) ? ((double) 1) : ((double) 0)); v08 = ((btn08.isSelected()) ? ((double) 1) : ((double) 0)); v09 = ((btn09.isSelected()) ? ((double) 1) : ((double) 0)); v10 = ((btn10.isSelected()) ? ((double) 1) : ((double) 0)); v11 = ((btn11.isSelected()) ? ((double) 1) : ((double) 0)); v13 = ((btn13.isSelected()) ? ((double) 1) : ((double) 0)); v14 = ((btn14.isSelected()) ? ((double) 1) : ((double) 0)); v15 = ((btn15.isSelected()) ? ((double) 1) : ((double) 0)); legs = txtLegs.getText();/* w ww . j a v a 2s. c o m*/ legs = ((legs == null || legs.trim().isEmpty() ? "2" : legs)); name = txtName.getText(); // abre o banco de dados arff e guarda os registros no objeto dados ConverterUtils.DataSource arquivo = new ConverterUtils.DataSource("data/zoo.arff"); Instances dados = arquivo.getDataSet(); // FILTER: remove o atributo nome do animal da classificao String[] parametros = new String[] { "-R", "1" }; Remove filtro = new Remove(); filtro.setOptions(parametros); filtro.setInputFormat(dados); dados = Filter.useFilter(dados, filtro); AttributeSelection selAtributo = new AttributeSelection(); InfoGainAttributeEval avaliador = new InfoGainAttributeEval(); Ranker busca = new Ranker(); selAtributo.setEvaluator(avaliador); selAtributo.setSearch(busca); selAtributo.SelectAttributes(dados); int[] indices = selAtributo.selectedAttributes(); //System.out.println("Selected attributes: " + Utils.arrayToString(indices)); // Usa o algoritimo J48 para montar a arvore de dados String[] opcoes = new String[1]; opcoes[0] = "-U"; J48 arvore = new J48(); arvore.setOptions(opcoes); arvore.buildClassifier(dados); // cria o novo elemento para comparao double[] vals = new double[dados.numAttributes()]; vals[0] = v00; // hair vals[1] = v01; // feathers vals[2] = v02; // eggs vals[3] = v03; // milk vals[4] = v04; // airborne vals[5] = v05; // aquatic vals[6] = v06; // predator vals[7] = v07; // toothed vals[8] = v08; // backbone vals[9] = v09; // breathes vals[10] = v10; // venomous vals[11] = v11; // fins vals[12] = Double.parseDouble(legs); // legs vals[13] = v13; // tail vals[14] = v14; // domestic vals[15] = v15; // catsize // Criar uma instncia baseada nestes atributos Instance newAnimal = new DenseInstance(1.0, vals); // Adicionar a instncia nos dados newAnimal.setDataset(dados); // Classificar esta nova instncia double label = arvore.classifyInstance(newAnimal); // Imprimir o resultado da classificao lblClassification.setText(dados.classAttribute().value((int) label)); }
From source file:id3j48.WekaAccess.java
public static void main(String[] args) { initializePath();//w w w . j av a 2 s. 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:kfst.classifier.WekaClassifier.java
License:Open Source License
/** * This method builds and evaluates the decision tree(DT) classifier. * The j48 are used as the DT classifier implemented in the Weka software. * * @param pathTrainData the path of the train set * @param pathTestData the path of the test set * @param confidenceValue The confidence factor used for pruning * @param minNumSampleInLeaf The minimum number of instances per leaf * /*from w ww. j a v a 2s.c om*/ * @return the classification accuracy */ public static double dTree(String pathTrainData, String pathTestData, double confidenceValue, int minNumSampleInLeaf) { double resultValue = 0; try { BufferedReader readerTrain = new BufferedReader(new FileReader(pathTrainData)); Instances dataTrain = new Instances(readerTrain); readerTrain.close(); dataTrain.setClassIndex(dataTrain.numAttributes() - 1); BufferedReader readerTest = new BufferedReader(new FileReader(pathTestData)); Instances dataTest = new Instances(readerTest); readerTest.close(); dataTest.setClassIndex(dataTest.numAttributes() - 1); J48 decisionTree = new J48(); decisionTree.setConfidenceFactor((float) confidenceValue); decisionTree.setMinNumObj(minNumSampleInLeaf); decisionTree.buildClassifier(dataTrain); Evaluation eval = new Evaluation(dataTest); eval.evaluateModel(decisionTree, dataTest); resultValue = 100 - (eval.errorRate() * 100); } catch (Exception ex) { Logger.getLogger(WekaClassifier.class.getName()).log(Level.SEVERE, null, ex); } return resultValue; }
From source file:KFST.featureSelection.embedded.TreeBasedMethods.DecisionTreeBasedMethod.java
License:Open Source License
/** * {@inheritDoc }/*ww w. j a v a 2 s . c o m*/ */ @Override protected String buildClassifier(Instances dataTrain) { try { if (TREE_TYPE == TreeType.C45) { J48 decisionTreeC45 = new J48(); decisionTreeC45.setConfidenceFactor((float) confidenceValue); decisionTreeC45.setMinNumObj(minNumSampleInLeaf); decisionTreeC45.buildClassifier(dataTrain); return decisionTreeC45.toString(); } else if (TREE_TYPE == TreeType.RANDOM_TREE) { RandomTree decisionTreeRandomTree = new RandomTree(); decisionTreeRandomTree.setKValue(randomTreeKValue); decisionTreeRandomTree.setMaxDepth(randomTreeMaxDepth); decisionTreeRandomTree.setMinNum(randomTreeMinNum); decisionTreeRandomTree.setMinVarianceProp(randomTreeMinVarianceProp); decisionTreeRandomTree.buildClassifier(dataTrain); return decisionTreeRandomTree.toString(); } } catch (Exception ex) { Logger.getLogger(DecisionTreeBasedMethod.class.getName()).log(Level.SEVERE, null, ex); } return ""; }