List of usage examples for weka.classifiers Classifier buildClassifier
public abstract void buildClassifier(Instances data) throws Exception;
From source file:es.bsc.autonomic.powermodeller.tools.classifiers.RepTreeClassifier.java
License:Apache License
@Override public Classifier buildClassifier(DataSet training_ds) { logger.debug("Building RepTree classifier."); Classifier model; // Get the independent variable index String independent = training_ds.getIndependent(); if (independent == null) throw new WekaWrapperException("Independent variable is not set in dataset."); try {/*from w ww . j a v a 2 s.c om*/ // Read all the instances in the file (ARFF, CSV, XRFF, ...) ConverterUtils.DataSource source = new ConverterUtils.DataSource(training_ds.getFilePath()); Instances instances = source.getDataSet(); // Set the independent variable (powerWatts). instances.setClassIndex(instances.attribute(independent).index()); // Builds a regression model for the given data. model = new weka.classifiers.trees.REPTree(); // Build Linear Regression model.buildClassifier(instances); } catch (WekaWrapperException e) { logger.error("Error while creating Linear Regression classifier.", e); throw new WekaWrapperException("Error while creating Linear Regression classifier."); } catch (Exception e) { logger.error("Error while applying Linear Regression to data set instances.", e); throw new WekaWrapperException("Error while applying Linear Regression to data set instances."); } return model; }
From source file:es.jarias.FMC.FMC.java
License:Open Source License
public static void buildModel(MultiLabelInstances trainData, MultiLabelInstances testData, int fold, String baseClassifierClass, String discType, String fss, String outPath, String prune) throws Exception { double start = System.nanoTime(); try {/*from w ww . ja va2 s . c o m*/ // DATA PREPROCESING: weka.filters.unsupervised.attribute.Discretize m_unsuperDiscretize = null; if (discType.equals("supervised")) { // pass // Supervised discretization is applied to each model later during the training step. } else if (discType.equals("unsupervised")) { // Apply a baseline discretization filter: m_unsuperDiscretize = new weka.filters.unsupervised.attribute.Discretize(); m_unsuperDiscretize.setUseEqualFrequency(false); m_unsuperDiscretize.setBins(3); m_unsuperDiscretize.setInputFormat(trainData.getDataSet()); trainData = trainData .reintegrateModifiedDataSet(Filter.useFilter(trainData.getDataSet(), m_unsuperDiscretize)); } else throw new Exception("Invalid Discretization Type"); if (!fss.equals("no") && !fss.equals("CFS")) throw new Exception("Invalid FSS strategy"); if (!prune.equals("full") && !prune.equals("tree") && !prune.equals("best") && !prune.equals("hiton") && !prune.equals("bdeu")) throw new Exception("Invalid Pruning strategy"); // Label information int m_numLabels = trainData.getNumLabels(); int[] m_labelIndices = trainData.getLabelIndices(); // Map for reference: HashMap<Integer, Integer> mapLabels = new HashMap<Integer, Integer>(m_numLabels); String[] mapLabelsName = new String[m_numLabels]; for (int l = 0; l < m_numLabels; l++) { mapLabels.put(trainData.getLabelIndices()[l], l); mapLabelsName[l] = trainData.getDataSet().attribute(trainData.getLabelIndices()[l]).name(); } // Get label combinations: int m_numPairs = (m_labelIndices.length * (m_labelIndices.length - 1)) / 2; int[][] labelCombinations = new int[m_numPairs][2]; int counter = 0; for (int i = 0; i < m_labelIndices.length; i++) { for (int j = i + 1; j < m_labelIndices.length; j++) { labelCombinations[counter] = new int[] { m_labelIndices[i], m_labelIndices[j] }; counter++; } } // Select the pairs: int m_numSelected = m_numPairs; int m_numSingleton = 0; int[] ordered; boolean[] selectedPair = new boolean[m_numPairs]; boolean[] singleton = new boolean[m_numLabels]; for (int i = 0; i < m_numPairs; i++) selectedPair[i] = true; if (!prune.equals("full")) { m_numSelected = 0; selectedPair = new boolean[m_numPairs]; // Info gain for pruned model: double[][] mutualInfoPairs = mutualInfo(trainData.getDataSet(), trainData.getLabelIndices()); double[] mutualInfo = new double[m_numPairs]; counter = 0; for (int i = 0; i < m_labelIndices.length; i++) { Instances tempInstances = new Instances(trainData.getDataSet()); tempInstances.setClassIndex(m_labelIndices[i]); for (int j = i + 1; j < m_labelIndices.length; j++) { mutualInfo[counter] = mutualInfoPairs[i][j]; counter++; } } ordered = orderBy(mutualInfo); if (prune.equals("tree")) { // Each labels correspond to its own connex component HashMap<Integer, ArrayList<Integer>> tree_compo = new HashMap<Integer, ArrayList<Integer>>( m_numLabels); HashMap<Integer, Integer> tree_index = new HashMap<Integer, Integer>(m_numLabels); for (int i = 0; i < m_numLabels; i++) { tree_compo.put(i, new ArrayList<Integer>()); tree_compo.get(i).add(i); tree_index.put(i, i); } for (int i = 0; i < m_numPairs; i++) { if (m_numSelected >= m_numLabels - 1) break; int pairIndex = ordered[i]; int pair_i = mapLabels.get(labelCombinations[pairIndex][0]); int pair_j = mapLabels.get(labelCombinations[pairIndex][1]); int conex_i = tree_index.get(pair_i); int conex_j = tree_index.get(pair_j); if (conex_i != conex_j) { ArrayList<Integer> family = tree_compo.get(conex_j); tree_compo.get(conex_i).addAll(family); for (int element : family) { tree_index.put(element, conex_i); } selectedPair[pairIndex] = true; m_numSelected++; } } } // End of the chow-liu algorithm if (prune.equals("best") || prune.equals("tree")) { int amount = 0; if (prune.equals("best")) amount = (int) (m_numLabels * 2); int index = 0; while (m_numSelected < amount && index < m_numPairs) { if (!selectedPair[ordered[index]]) { m_numSelected++; selectedPair[ordered[index]] = true; } index++; } } // End of the linear tree and best procedures if (prune.equals("hiton")) { weka.filters.unsupervised.attribute.Remove m_remove = new weka.filters.unsupervised.attribute.Remove(); m_remove.setAttributeIndicesArray(trainData.getLabelIndices()); m_remove.setInvertSelection(true); m_remove.setInputFormat(trainData.getDataSet()); Instances hitonData = Filter.useFilter(trainData.getDataSet(), m_remove); HITON hiton = new HITON(hitonData); HashSet<Integer>[] markovBlanket = new HashSet[m_numLabels]; for (int l = 0; l < m_numLabels; l++) markovBlanket[l] = hiton.HITONMB(l); for (int p = 0; p < m_numPairs; p++) { int p_i = mapLabels.get(labelCombinations[p][0]); int p_j = mapLabels.get(labelCombinations[p][1]); if (markovBlanket[p_i].contains(p_j) || markovBlanket[p_j].contains(p_i)) { selectedPair[p] = true; m_numSelected++; } } } // end of the hiton pruning algorithm if (prune.equals("bdeu")) { weka.filters.unsupervised.attribute.Remove m_remove = new weka.filters.unsupervised.attribute.Remove(); m_remove.setAttributeIndicesArray(trainData.getLabelIndices()); m_remove.setInvertSelection(true); m_remove.setInputFormat(trainData.getDataSet()); Instances hitonData = Filter.useFilter(trainData.getDataSet(), m_remove); BDeu hiton = new BDeu(hitonData); double[] scores = hiton.singleScore; double[] pairScores = new double[m_numPairs]; double[] sumScores = new double[m_numLabels]; for (int p = 0; p < m_numPairs; p++) { int head = mapLabels.get(labelCombinations[p][0]); int tail = mapLabels.get(labelCombinations[p][1]); pairScores[p] = -1 * (scores[tail] - (hiton.localBdeuScore(tail, new Integer[] { head }))); sumScores[tail] += pairScores[p]; sumScores[head] += pairScores[p]; } HashSet<Integer>[] parents = new HashSet[m_numLabels]; for (int i = 0; i < m_numLabels; i++) parents[i] = new HashSet<Integer>(); ordered = orderBy(pairScores); int[] topologicalOrdering = orderBy(sumScores); int[] relevance = new int[m_numLabels]; for (int i = 0; i < m_numLabels; i++) relevance[topologicalOrdering[i]] = i; for (int p = 0; p < m_numPairs; p++) { int pair = ordered[p]; int head = mapLabels.get(labelCombinations[pair][0]); int tail = mapLabels.get(labelCombinations[pair][1]); if (relevance[head] > relevance[tail]) { int aux = head; head = tail; tail = aux; } // Check if adding this improves parents[tail].add(head); double scoreAdd = hiton.localBdeuScore(tail, parents[tail].toArray(new Integer[parents[tail].size()])); double diff = scores[tail] - scoreAdd; if (diff < 0) { scores[tail] = scoreAdd; selectedPair[pair] = true; m_numSelected++; } else { parents[tail].remove(head); } } // End of the BDeu procedure } // End of the Pruning algorithms // // Determine singleton variables for (int i = 0; i < m_labelIndices.length; i++) singleton[i] = true; for (int p = 0; p < m_numPairs; p++) { if (selectedPair[p]) { singleton[mapLabels.get(labelCombinations[p][0])] = false; singleton[mapLabels.get(labelCombinations[p][1])] = false; } } for (int i = 0; i < m_labelIndices.length; i++) if (singleton[i]) m_numSingleton++; mutualInfo = null; } // Generate single class datasets from the full ML data and learn models: HashMap<Integer, Classifier> models = new HashMap<Integer, Classifier>(); HashMap<Integer, Classifier> singletonModels = new HashMap<Integer, Classifier>(); HashMap<Integer, weka.filters.supervised.attribute.AttributeSelection> singletonFilterSel = new HashMap<Integer, weka.filters.supervised.attribute.AttributeSelection>(); HashMap<Integer, weka.filters.supervised.attribute.Discretize> singletonFilter = new HashMap<Integer, weka.filters.supervised.attribute.Discretize>(); weka.filters.supervised.attribute.AttributeSelection[] m_selecters = new weka.filters.supervised.attribute.AttributeSelection[m_numPairs]; weka.filters.supervised.attribute.Discretize[] m_discretizers = new weka.filters.supervised.attribute.Discretize[m_numPairs]; ClassCompoundTransformation[] converters = new ClassCompoundTransformation[m_numPairs]; for (int i = 0; i < m_numPairs; i++) { if (!selectedPair[i]) { continue; } MultiLabelInstances filteredLabelData = trainData .reintegrateModifiedDataSet(RemoveAllLabels.transformInstances(trainData.getDataSet(), complement(m_labelIndices, labelCombinations[i]))); converters[i] = new ClassCompoundTransformation(); Instances singleLabelData = converters[i].transformInstances(filteredLabelData); if (discType.equals("supervised")) { m_discretizers[i] = new Discretize(); m_discretizers[i].setInputFormat(singleLabelData); singleLabelData = Filter.useFilter(singleLabelData, m_discretizers[i]); } if (fss.equals("CFS")) { m_selecters[i] = new weka.filters.supervised.attribute.AttributeSelection(); m_selecters[i].setSearch(new weka.attributeSelection.BestFirst()); m_selecters[i].setEvaluator(new weka.attributeSelection.CfsSubsetEval()); m_selecters[i].setInputFormat(singleLabelData); singleLabelData = Filter.useFilter(singleLabelData, m_selecters[i]); } models.put(i, (Classifier) Class.forName("weka.classifiers." + baseClassifierClass).newInstance()); models.get(i).buildClassifier(singleLabelData); } // Learn singleton models: for (int i = 0; i < m_labelIndices.length; i++) { if (singleton[i]) { Instances singleLabelData = new Instances(trainData.getDataSet()); singleLabelData.setClassIndex(m_labelIndices[i]); singleLabelData = RemoveAllLabels.transformInstances(singleLabelData, complement(m_labelIndices, new int[] { m_labelIndices[i] })); if (discType.equals("supervised")) { singletonFilter.put(i, new Discretize()); singletonFilter.get(i).setInputFormat(singleLabelData); singleLabelData = Filter.useFilter(singleLabelData, singletonFilter.get(i)); } if (fss.equals("CFS")) { weka.filters.supervised.attribute.AttributeSelection tempFilter = new weka.filters.supervised.attribute.AttributeSelection(); tempFilter.setSearch(new weka.attributeSelection.BestFirst()); tempFilter.setEvaluator(new weka.attributeSelection.CfsSubsetEval()); tempFilter.setInputFormat(singleLabelData); singletonFilterSel.put(i, tempFilter); singleLabelData = Filter.useFilter(singleLabelData, singletonFilterSel.get(i)); } Classifier single; single = (Classifier) Class.forName("weka.classifiers." + baseClassifierClass).newInstance(); single.buildClassifier(singleLabelData); singletonModels.put(i, single); } } // // END OF THE LEARNING STAGE // double train = System.nanoTime() - start; start = System.nanoTime(); Writer writerConf = null; Writer writerDist = null; Writer writerSing = null; Writer writerLayo = null; try { writerConf = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(outPath + "/conf_" + fold + ".txt"), "utf-8")); writerDist = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(outPath + "/dist_" + fold + ".txt"), "utf-8")); writerSing = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(outPath + "/sing_" + fold + ".txt"), "utf-8")); writerLayo = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(outPath + "/layo_" + fold + ".txt"), "utf-8")); for (int l = 0; l < m_numLabels; l++) { writerLayo.write(trainData.getDataSet().attribute(m_labelIndices[l]).numValues() + "\t"); } writerLayo.write("\n"); writerLayo.write(m_numSelected + "\t" + m_numSingleton); writerLayo.close(); // Get distributions for instance for each variable pairs: double[] distributions; for (int i = 0; i < testData.getDataSet().size(); i++) { for (int l : testData.getLabelIndices()) writerConf.write((int) testData.getDataSet().instance(i).value(l) + "\t"); writerConf.write("\n"); Instance inst = testData.getDataSet().get(i); if (discType.equals("unsupervised")) { m_unsuperDiscretize.input(inst); inst = m_unsuperDiscretize.output(); } for (int p = 0; p < m_numPairs; p++) { if (!selectedPair[p]) { continue; } Instance processed = converters[p].transformInstance(inst, testData.getLabelIndices()); if (discType.equals("supervised")) { m_discretizers[p].input(processed); processed = m_discretizers[p].output(); // m_removers[p].input(processed); // processed = m_removers[p].output(); } if (!fss.equals("no")) { m_selecters[p].input(processed); processed = m_selecters[p].output(); } distributions = models.get(p).distributionForInstance(processed); writerDist.write(mapLabels.get(labelCombinations[p][0]) + "\t" + mapLabels.get(labelCombinations[p][1]) + "\t"); for (int d = 0; d < distributions.length; d++) writerDist.write(distributions[d] + "\t"); writerDist.write("\n"); } // Get predictions for singleton labels: for (int m = 0; m < m_labelIndices.length; m++) { if (singleton[m]) { Instance processed = RemoveAllLabels.transformInstance(inst, complement(m_labelIndices, new int[] { m_labelIndices[m] })); if (discType.equals("supervised")) { singletonFilter.get(m).input(processed); processed = singletonFilter.get(m).output(); } if (!fss.equals("no")) { singletonFilterSel.get(m).input(processed); processed = singletonFilterSel.get(m).output(); } double[] distribution = singletonModels.get(m).distributionForInstance(processed); double maxValue = 0; int conf = -1; for (int v = 0; v < distribution.length; v++) { if (distribution[v] > maxValue) { maxValue = distribution[v]; conf = v; } } writerSing.write(i + "\t" + m + "\t" + conf + "\n"); } } } writerConf.close(); writerDist.close(); writerSing.close(); double test = System.nanoTime() - start; // train /= 1000000000.0; // test /= 1000000000.0; // System.out.println(java.lang.String.format("FMC-%s\t%s\t%s\t%d\t%s\t%s\t%.4f\t%.4f",prune,baseClassifierClass,dbName,fold,discType,fss,train,test)); } catch (IOException ex) { // report } finally { try { writerConf.close(); } catch (Exception ex) { } try { writerDist.close(); } catch (Exception ex) { } } } catch (Exception e) { e.printStackTrace(); } }
From source file:es.ubu.XRayDetector.modelo.Fachada.java
License:Open Source License
/** * Creates a model training a classifier using bagging. * //w w w.j a va 2s . com * @param data Contains all the instances of the ARFF file * @param sizeWindow The size of the window */ public void createModel(Instances data, String sizeWindow) { // se crea, opciones, setiputformat Classifier cls = null; //String separator = System.getProperty("file.separator"); String path = prop.getPathModel(); int opcionClasificacion = prop.getTipoClasificacion(); switch (opcionClasificacion) { case 0: //CLASIFICADOR CLASES NOMINALES (TRUE,FALSE) Classifier base; base = new REPTree(); cls = new Bagging(); ((Bagging) cls).setNumIterations(25); ((Bagging) cls).setBagSizePercent(100); ((Bagging) cls).setNumExecutionSlots(Runtime.getRuntime().availableProcessors()); ((Bagging) cls).setClassifier(base); break; case 1: //REGRESIN LINEAL (CLASES NUMRICAS, 1,0) cls = new REPTree(); break; } ObjectOutputStream oos = null; try { data.setClassIndex(data.numAttributes() - 1); cls.buildClassifier(data); /*if (arffName.contains("mejores")) oos = new ObjectOutputStream(new FileOutputStream((path + separator + "Modelos" + separator + "Bagging_" + "mejores_" + sizeWindow + ".model"))); if (arffName.contains("todas"))*/ oos = new ObjectOutputStream(new FileOutputStream((path + "todas_" + sizeWindow + ".model"))); oos.writeObject(cls); oos.flush(); oos.close(); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:es.upm.dit.gsi.barmas.launcher.WekaClassifiersValidator.java
License:Open Source License
/** * @throws Exception// w w w . j a v a 2 s .c o m * * */ public void validateClassifier(Classifier classifier) throws Exception { String classifierName = classifier.getClass().getSimpleName(); logger.info("--> Starting validation for classfier " + classifierName); int ratioint = (int) ((1 / (double) folds) * 100); double roundedratio = ((double) ratioint) / 100; String[] row; Classifier copiedClassifier; try { // Central Agent HashMap<Integer, double[][]> resultsMap = new HashMap<Integer, double[][]>(); HashMap<Integer, double[][]> resultsNoEssMap = new HashMap<Integer, double[][]>(); for (int leba : lebas) { resultsMap.put(leba, new double[this.folds][2]); resultsNoEssMap.put(leba, new double[this.folds][2]); } logger.info("Starting validation for BayesCentralAgent dataset with " + classifierName + " done for dataset: " + this.dataset); for (int iteration = 0; iteration < this.folds; iteration++) { String inputPath = this.inputFolder + "/" + roundedratio + "testRatio/iteration-" + iteration; Instances testData = WekaClassifiersValidator.getDataFromCSV(inputPath + "/test-dataset.arff"); Instances trainData = WekaClassifiersValidator .getDataFromCSV(inputPath + "/bayes-central-dataset.arff"); try { logger.info("Learning model..."); copiedClassifier = Classifier.makeCopy(classifier); copiedClassifier.buildClassifier(trainData); logger.info("Finishing learning process. Model built for classifier " + classifierName + " in iteration " + iteration); } catch (Exception e) { logger.severe("Problems training model for " + classifier.getClass().getSimpleName()); logger.severe(e.getMessage()); throw e; } for (int leba : lebas) { double[][] results = resultsMap.get(leba); double[] pcts = this.getValidation(copiedClassifier, trainData, testData, leba); results[iteration][0] = results[iteration][0] + pcts[0]; results[iteration][1] = results[iteration][1] + pcts[1]; resultsMap.put(leba, results); row = new String[this.columns]; row[0] = this.dataset; row[1] = Integer.toString(this.folds); row[2] = classifierName; row[3] = Integer.toString(iteration); row[4] = Double.toString(pcts[0]); row[5] = Double.toString(pcts[1]); row[6] = "BayesCentralAgent"; row[7] = "1"; row[8] = Integer.toString(leba); writer.writeRecord(row); } /* * Instances testDataNoEssentials = * WekaClassifiersValidator.getDataFromCSV(inputPath + * "/test-dataset.arff"); Instances trainDataNoEssentials = * WekaClassifiersValidator.getDataFromCSV(inputPath + * "/bayes-central-dataset-noEssentials.arff"); try { * logger.info("Learning model..."); copiedClassifier = * Classifier.makeCopy(classifier); * copiedClassifier.buildClassifier(trainDataNoEssentials); * * logger.info( * "Finishing learning process. Model built for classifier " + * classifierName + " in iteration " + iteration + * " without essentials"); } catch (Exception e) { * logger.severe("Problems training model for " + * classifier.getClass().getSimpleName()); * logger.severe(e.getMessage()); throw e; } * * for (int leba = this.minLEBA; leba <= this.maxLEBA; leba++) { * double[][] resultsNoEss = resultsNoEssMap.get(leba); double[] * pcts = this.getValidation(copiedClassifier, * trainDataNoEssentials, testDataNoEssentials, leba); * resultsNoEss[iteration][0] = resultsNoEss[iteration][0] + * pcts[0]; resultsNoEss[iteration][1] = * resultsNoEss[iteration][1] + pcts[1]; * * resultsNoEssMap.put(leba, resultsNoEss); * * row = new String[this.columns]; row[0] = this.dataset; row[1] * = Integer.toString(this.folds); row[2] = classifierName; * row[3] = Integer.toString(iteration); row[4] = * Double.toString(pcts[0]); row[5] = Double.toString(pcts[1]); * row[6] = "BayesCentralAgent-NoEssentials"; row[7] = "1"; * row[8] = Integer.toString(leba); writer.writeRecord(row); } */ // ------------------------------------------------------------- // --------------------------- FOR AGENTS DATASETS ISOLATED - NO // SENSE BECAUSE WEKA CLASSIFIER WERE DESIGNED TO BE CENTRALISED // ------------------------------------------------------------- // // Agents combinations // for (int i = this.minAgents; i <= this.maxAgents; i++) { // logger.info("Validation for agents datasets with " + // classifierName // + " done for dataset: " + this.dataset + " with LEBA=" + // leba); // HashMap<Integer, Double> successRatio = new HashMap<Integer, // Double>(); // HashMap<Integer, Double> wrongRatio = new HashMap<Integer, // Double>(); // for (int j = 0; j < i; j++) { // successRatio.put(j, 0.0); // wrongRatio.put(j, 0.0); // } // for (int iteration = 0; iteration < this.folds; iteration++) // { // String inputPath = this.inputFolder + "/" + roundedratio // + "testRatio/iteration-" + iteration; // Instances testData = this.getDataFromCSV(inputPath + // "/test-dataset.csv"); // for (int j = 0; j < i; j++) { // Instances trainData = this.getDataFromCSV(inputPath + "/" + i // + "agents/agent-" + j + "-dataset.csv"); // double[] pcts = this.getValidation(classifier, trainData, // testData, // leba); // successRatio.put(j, successRatio.get(j) + pcts[0]); // wrongRatio.put(j, wrongRatio.get(j) + pcts[1]); // // row = new String[this.columns]; // row[0] = this.dataset; // row[1] = Integer.toString(this.folds); // row[2] = classifierName; // row[3] = Integer.toString(iteration); // row[4] = Double.toString(pcts[0]); // row[5] = Double.toString(pcts[1]); // row[6] = "Agent" + j; // row[7] = Integer.toString(i); // row[8] = Integer.toString(leba); // writer.writeRecord(row); // } // // writer.flush(); // } // // for (int j = 0; j < i; j++) { // row = new String[this.columns]; // row[0] = this.dataset; // row[1] = Integer.toString(this.folds); // row[2] = classifierName; // row[3] = "AVERAGE"; // row[4] = Double.toString(successRatio.get(j) / this.folds); // row[5] = Double.toString(wrongRatio.get(j) / this.folds); // row[6] = "Agent" + j; // row[7] = Integer.toString(i); // row[8] = Integer.toString(leba); // writer.writeRecord(row); // // logger.info("Validation for Agent" + j + " dataset (for " + i // + " agents configuration) with " + classifierName // + " done for dataset: " + this.dataset + " with LEBA=" + // leba); // } // // writer.flush(); // } // ------------------------------------------------------------- // ---------- END FOR AGENTS DATASETS ISOLATED ----------------- // ------------------------------------------------------------- logger.info("<-- Validation for classfier " + classifierName + " done for dataset: " + this.dataset + " for iteration " + iteration); } for (int leba : lebas) { double[] sum = new double[2]; double[][] results = resultsMap.get(leba); for (int iteration = 0; iteration < this.folds; iteration++) { sum[0] = sum[0] + results[iteration][0]; sum[1] = sum[1] + results[iteration][1]; } row = new String[this.columns]; row[0] = this.dataset; row[1] = Integer.toString(this.folds); row[2] = classifierName; row[3] = "AVERAGE"; row[4] = Double.toString(sum[0] / this.folds); row[5] = Double.toString(sum[1] / this.folds); row[6] = "BayesCentralAgent"; row[7] = "1"; row[8] = Integer.toString(leba); writer.writeRecord(row); logger.info("Validation for BayesCentralAgent dataset with " + classifierName + " done for dataset: " + this.dataset + " with LEBA=" + leba); writer.flush(); } // for (int leba : lebas) { // double[] sum = new double[2]; // double[][] results = resultsNoEssMap.get(leba); // for (int iteration = 0; iteration < this.folds; iteration++) { // sum[0] = sum[0] + results[iteration][0]; // sum[1] = sum[1] + results[iteration][1]; // } // // row = new String[this.columns]; // row[0] = this.dataset; // row[1] = Integer.toString(this.folds); // row[2] = classifierName; // row[3] = "AVERAGE"; // row[4] = Double.toString(sum[0] / this.folds); // row[5] = Double.toString(sum[1] / this.folds); // row[6] = "BayesCentralAgent-NoEssentials"; // row[7] = "1"; // row[8] = Integer.toString(leba); // writer.writeRecord(row); // // logger.info("Validation for BayesCentralAgent dataset with " + // classifierName // + " done for dataset: " + this.dataset + " with LEBA=" + leba); // writer.flush(); // } logger.info("Validation for BayesCentralAgent dataset with " + classifierName + " done for dataset: " + this.dataset); writer.flush(); } catch (Exception e) { logger.severe("Problem validating classifier " + classifierName); logger.severe(e.getMessage()); e.printStackTrace(); throw e; } }
From source file:etc.aloe.cscw2013.TrainingImpl.java
License:Open Source License
@Override public WekaModel train(ExampleSet examples) { System.out.println("SMO Options: " + SMO_OPTIONS); SMO smo = new SMO(); try {//from ww w. jav a2 s . c o m smo.setOptions(Utils.splitOptions(SMO_OPTIONS)); } catch (Exception ex) { System.err.println("Unable to configure SMO."); System.err.println("\t" + ex.getMessage()); return null; } //Build logistic models if desired smo.setBuildLogisticModels(isBuildLogisticModel()); Classifier classifier = smo; if (useCostTraining) { CostSensitiveClassifier cost = new CostSensitiveClassifier(); cost.setClassifier(smo); CostMatrix matrix = new CostMatrix(2); matrix.setElement(0, 0, 0); matrix.setElement(0, 1, falsePositiveCost); matrix.setElement(1, 0, falseNegativeCost); matrix.setElement(1, 1, 0); cost.setCostMatrix(matrix); classifier = cost; System.out.print("Wrapping SMO in CostSensitiveClassifier " + matrix.toMatlab()); if (useReweighting) { cost.setMinimizeExpectedCost(false); System.out.println(" using re-weighting."); } else { cost.setMinimizeExpectedCost(true); System.out.println(" using min-cost criterion."); } } try { System.out.print("Training SMO on " + examples.size() + " examples... "); classifier.buildClassifier(examples.getInstances()); System.out.println("done."); WekaModel model = new WekaModel(classifier); return model; } catch (Exception ex) { System.err.println("Unable to train SMO."); System.err.println("\t" + ex.getMessage()); return null; } }
From source file:etc.aloe.oilspill2010.TrainingImpl.java
@Override public WekaModel train(ExampleSet examples) { //These settings aren't terrible SMO smo = new SMO(); RBFKernel rbf = new RBFKernel(); rbf.setGamma(0.5);//w ww . j a v a 2 s . c o m smo.setKernel(rbf); smo.setC(1.5); //These also work pretty ok Logistic log = new Logistic(); log.setRidge(100); Classifier classifier = log; try { System.out.print("Training on " + examples.size() + " examples... "); classifier.buildClassifier(examples.getInstances()); System.out.println("done."); WekaModel model = new WekaModel(classifier); return model; } catch (Exception ex) { System.err.println("Unable to train classifier."); System.err.println("\t" + ex.getMessage()); return null; } }
From source file:experimentalclassifier.ExperimentalClassifier.java
/** * @param args the command line arguments *///w ww. j av a2s . c o m public static void main(String[] args) throws Exception { DataSource source = new DataSource("data/iris.csv"); Instances data = source.getDataSet(); if (data.classIndex() == -1) { data.setClassIndex(data.numAttributes() - 1); } data.randomize(new Random()); String[] options = weka.core.Utils.splitOptions("-P 30"); RemovePercentage remove = new RemovePercentage(); remove.setOptions(options); remove.setInputFormat(data); Instances train = Filter.useFilter(data, remove); remove.setInvertSelection(true); remove.setInputFormat(data); Instances test = Filter.useFilter(data, remove); Classifier classifier = new HardCodedClassifier(); classifier.buildClassifier(train);//Currently, this does nothing Evaluation eval = new Evaluation(train); eval.evaluateModel(classifier, test); System.out.println(eval.toSummaryString("\nResults\n======\n", false)); }
From source file:expshell.ExpShell.java
/** * @param args the command line arguments * @throws java.lang.Exception//from www. j a v a2 s. co m */ public static void main(String[] args) throws Exception { String file = "C:\\Users\\YH Jonathan Kwok\\Documents\\NetBeansProjects\\ExpShell\\src\\expshell\\iris.csv"; DataSource source = new DataSource(file); Instances data = source.getDataSet(); if (data.classIndex() == -1) data.setClassIndex(data.numAttributes() - 1); //Randomize it data.randomize(new Random(1)); RemovePercentage rp = new RemovePercentage(); rp.setPercentage(70); rp.setInputFormat(data); Instances training = Filter.useFilter(data, rp); rp.setInvertSelection(true); rp.setInputFormat(data); Instances test = Filter.useFilter(data, rp); //standardize the data Standardize filter = new Standardize(); filter.setInputFormat(training); Instances newTest = Filter.useFilter(test, filter); Instances newTraining = Filter.useFilter(training, filter); //Part 5 - Now it's a knn Classifier knn = new NeuralClassifier(); knn.buildClassifier(newTraining); Evaluation eval = new Evaluation(newTraining); eval.evaluateModel(knn, newTest); System.out.println(eval.toSummaryString("***** Overall results: *****", false)); }
From source file:farm_ads.MyClassifier.java
public Classifier classifierNB(Instances instances) throws Exception { Classifier classifier = new NaiveBayes(); classifier.buildClassifier(instances); return classifier; }
From source file:ffnn.TucilWeka.java
public static Evaluation crossValidation(Instances data) { //10-fold cross validation Evaluation eval = null;//ww w .ja va2s .c o m try { eval = new Evaluation(data); Classifier cls = new FFNNTubesAI(); if (cls == null) { System.out.println("MODEL CANNOT BE USED"); } else { System.out.println("MODEL IS USED"); } cls.buildClassifier(data); //crossValidateModel: //param 1 = tipe classifier (disini J48) //param 2 = Instances data //param 3 = jumlah fold //param 4 = Randomizer (seed) eval.crossValidateModel(cls, data, 10, new Random(1)); } catch (Exception ex) { Logger.getLogger(TucilWeka.class.getName()).log(Level.SEVERE, null, ex); } return eval; }