List of usage examples for weka.classifiers Classifier buildClassifier
public abstract void buildClassifier(Instances data) throws Exception;
From source file:mao.datamining.ModelProcess.java
private void testCV(Classifier classifier, Instances finalTrainDataSet, FileOutputStream testCaseSummaryOut, TestResult result) {/*from w ww.j av a 2s.c o m*/ long start, end, trainTime = 0, testTime = 0; Evaluation evalAll = null; double confusionMatrix[][] = null; // randomize data, and then stratify it into 10 groups Random rand = new Random(1); Instances randData = new Instances(finalTrainDataSet); randData.randomize(rand); if (randData.classAttribute().isNominal()) { //always run with 10 cross validation randData.stratify(folds); } try { evalAll = new Evaluation(randData); for (int i = 0; i < folds; i++) { Evaluation eval = new Evaluation(randData); Instances train = randData.trainCV(folds, i); Instances test = randData.testCV(folds, i); //counting traininig time start = System.currentTimeMillis(); Classifier j48ClassifierCopy = Classifier.makeCopy(classifier); j48ClassifierCopy.buildClassifier(train); end = System.currentTimeMillis(); trainTime += end - start; //counting test time start = System.currentTimeMillis(); eval.evaluateModel(j48ClassifierCopy, test); evalAll.evaluateModel(j48ClassifierCopy, test); end = System.currentTimeMillis(); testTime += end - start; } } catch (Exception e) { ModelProcess.logging(null, e); } //end test by cross validation // output evaluation try { ModelProcess.logging(""); //write into summary file testCaseSummaryOut .write((evalAll.toSummaryString("=== Cross Validation Summary ===", true)).getBytes()); testCaseSummaryOut.write("\n".getBytes()); testCaseSummaryOut.write( (evalAll.toClassDetailsString("=== " + folds + "-fold Cross-validation Class Detail ===\n")) .getBytes()); testCaseSummaryOut.write("\n".getBytes()); testCaseSummaryOut .write((evalAll.toMatrixString("=== Confusion matrix for all folds ===\n")).getBytes()); testCaseSummaryOut.flush(); confusionMatrix = evalAll.confusionMatrix(); result.setConfusionMatrix10Folds(confusionMatrix); } catch (Exception e) { ModelProcess.logging(null, e); } }
From source file:meddle.TrainModelByDomainOS.java
License:Open Source License
public static MetaEvaluationMeasures trainWithClassifier(Classifier classifier, Instances trainingSet, String domainOS, MetaEvaluationMeasures mem) { try {/*from w ww . j a va2 s.co m*/ long t1 = System.nanoTime(); String classifierName = classifier.getClass().toString(); classifierName = classifierName.substring(classifierName.lastIndexOf(".") + 1); if (trainingSet.numInstances() > MANY) { System.out.println( "This might take over 30 minutes for " + trainingSet.numInstances() + " samples ..."); } classifier.buildClassifier(trainingSet); long t2 = System.nanoTime(); double trainingTime = (t2 - t1) / 10e8; if (RConfig.enableCrossValidation) { mem.trainingTime = trainingTime; mem = doEvaluation(classifier, domainOS, trainingSet, mem); Util.appendLineToFile(RConfig.logFolder + "eval.txt", mem.recordJSONFormat()); } if (RConfig.enableGraphicOutput && classifierName.equals("J48")) { doGraphicOutput((J48) classifier, domainOS, mem); } SerializationHelper.write( RConfig.modelFolder + domainOS + "-" + classifier.getClass().toString().substring(6) + ".model", classifier); } catch (Exception e) { e.printStackTrace(); } return mem; }
From source file:meka.classifiers.multilabel.cc.CNode.java
License:Open Source License
/** * Build - Create transformation for this node, and train classifier of type H upon it. * The dataset should have class as index 'j', and remove all indices less than L *not* in paY. *//*from w ww . ja v a 2 s . co m*/ public void build(Instances D, Classifier H) throws Exception { // transform data T = transform(D); // build SLC 'h' h = AbstractClassifier.makeCopy(H); h.buildClassifier(T); // save templates //t_ = new SparseInstance(T.numAttributes()); //t_.setDataset(T); //t_.setClassMissing(); // [?,x,x,x] T.clear(); }
From source file:mlflex.WekaInMemoryLearner.java
License:Open Source License
@Override protected ModelPredictions TrainTest(ArrayList<String> classificationParameters, DataInstanceCollection trainData, DataInstanceCollection testData, DataInstanceCollection dependentVariableInstances) throws Exception { ArrayList<String> dataPointNames = Lists.SortStringList(trainData.GetDataPointNames()); FastVector attVector = GetAttributeVector(dependentVariableInstances, dataPointNames, trainData, testData); Instances wekaTrainingInstances = GetInstances(dependentVariableInstances, attVector, trainData); Instances wekaTestInstances = GetInstances(dependentVariableInstances, attVector, testData); ArrayList<String> dependentVariableClasses = Utilities.ProcessorVault.DependentVariableDataProcessor .GetUniqueDependentVariableValues(); Classifier classifier = GetClassifier(classificationParameters); classifier.buildClassifier(wekaTrainingInstances); Predictions predictions = new Predictions(); for (DataValues testInstance : testData) { String dependentVariableValue = dependentVariableInstances.Get(testInstance.GetID()) .GetDataPointValue(0);// ww w.j ava2s.c om // This is the default before the prediction is made Prediction prediction = new Prediction(testInstance.GetID(), dependentVariableValue, Lists.PickRandomValue(dependentVariableClasses), Lists.CreateDoubleList(0.5, dependentVariableClasses.size())); if (!testInstance.HasOnlyMissingValues()) { Instance wekaTestInstance = GetInstance(wekaTestInstances, attVector, testInstance, null); double clsLabel = classifier.classifyInstance(wekaTestInstance); String predictedClass = wekaTestInstance.classAttribute().value((int) clsLabel); double[] probabilities = classifier.distributionForInstance(wekaTestInstance); ArrayList<Double> classProbabilities = Lists.CreateDoubleList(probabilities); prediction = new Prediction(testInstance.GetID(), dependentVariableValue, predictedClass, classProbabilities); } predictions.Add(prediction); } classifier = null; return new ModelPredictions("", predictions); }
From source file:mlpoc.MLPOC.java
public static Evaluation crossValidate(String filename) { Evaluation eval = null;/*www .j a va 2 s.co m*/ try { BufferedReader br = new BufferedReader(new FileReader(filename)); // loads data and set class index Instances data = new Instances(br); br.close(); /*File csv=new File(filename); CSVLoader loader = new CSVLoader(); loader.setSource(csv); Instances data = loader.getDataSet();*/ data.setClassIndex(data.numAttributes() - 1); // classifier String[] tmpOptions; String classname = "weka.classifiers.trees.J48 -C 0.25"; tmpOptions = classname.split(" "); classname = "weka.classifiers.trees.J48"; tmpOptions[0] = ""; Classifier cls = (Classifier) Utils.forName(Classifier.class, classname, tmpOptions); // other options int seed = 2; int folds = 10; // randomize data Random rand = new Random(seed); Instances randData = new Instances(data); randData.randomize(rand); if (randData.classAttribute().isNominal()) randData.stratify(folds); // perform cross-validation eval = new Evaluation(randData); for (int n = 0; n < folds; n++) { Instances train = randData.trainCV(folds, n); Instances test = randData.testCV(folds, n); // the above code is used by the StratifiedRemoveFolds filter, the // code below by the Explorer/Experimenter: // Instances train = randData.trainCV(folds, n, rand); // build and evaluate classifier Classifier clsCopy = Classifier.makeCopy(cls); clsCopy.buildClassifier(train); eval.evaluateModel(clsCopy, test); } // output evaluation System.out.println(); System.out.println("=== Setup ==="); System.out .println("Classifier: " + cls.getClass().getName() + " " + Utils.joinOptions(cls.getOptions())); System.out.println("Dataset: " + data.relationName()); System.out.println("Folds: " + folds); System.out.println("Seed: " + seed); System.out.println(); System.out.println(eval.toSummaryString("Summary for testing", true)); System.out.println("Correctly Classified Instances: " + eval.correct()); System.out.println("Percentage of Correctly Classified Instances: " + eval.pctCorrect()); System.out.println("InCorrectly Classified Instances: " + eval.incorrect()); System.out.println("Percentage of InCorrectly Classified Instances: " + eval.pctIncorrect()); } catch (Exception ex) { System.err.println(ex.getMessage()); } return eval; }
From source file:moa.classifiers.meta.WEKAClassifier.java
License:Open Source License
public void buildClassifier() { try {/*from www .java 2s .c o m*/ if ((classifier instanceof UpdateableClassifier) == false) { Classifier auxclassifier = weka.classifiers.AbstractClassifier.makeCopy(classifier); auxclassifier.buildClassifier(instancesBuffer); classifier = auxclassifier; isBufferStoring = false; } } catch (Exception e) { System.err.println("Building WEKA Classifier: " + e.getMessage()); } }
From source file:moa.tud.ke.patching.AdaptivePatchingAdwin.java
/** * Learns a specific subset classifier (of the same type as the base * classifier) to improve accuracy on the regions that performed bad before. * * @param subsets/* w ww .j a va2 s . c o m*/ * @param basePerformance * @return */ private Vector createPatches(Vector subsets, Vector basePerformance) { Vector patches = new Vector(); System.out.println("Creating patches: #" + subsets.size()); try { for (int d = 0; d < subsets.size(); d++) { Instances set = (Instances) subsets.get(d); // if(this.useBaseClassAsAttribute.isSet()) { // writeArff("C:\\StAtIC\\experiments\\set"+d+".arff", set); // } // System.out.println("Set " + d + " size: " + set.size()); Classifier patch; if (set.size() < 5) // Too small to do anything properly { patch = null; // null will then default to base classifier } else { patch = getPatchClassifier(); patch.buildClassifier(set); } patches.add(d, patch); } } catch (Exception e) { System.err.println("Error building patches:"); System.err.println(e.getMessage()); } // System.out.println("\n--- Patches ------------"); // for (int i = 0; i < patches.size(); i++) { // Classifier tmp = (Classifier) patches.get(i); // if (tmp != null) { // System.out.print("Patch " + i+" - "); // System.out.println(tmp); // } // } // System.out.println("------------------------"); // System.exit(45768545); return patches; }
From source file:moa.tud.ke.patching.Patching.java
/** * Learns a specific subset classifier (of the same type as the base * classifier) to improve accuracy on the regions that performed bad before. * * @param subsets/* w w w . j av a 2s. c o m*/ * @param basePerformance * @return */ private Vector createPatches(Vector subsets, Vector basePerformance) { Vector patches = new Vector(); System.out.println("Creating patches: #" + subsets.size()); try { for (int d = 0; d < subsets.size(); d++) { Instances set = (Instances) subsets.get(d); Classifier patch; if (set.size() < 5) // Too small to do anything properly { patch = null; // null will then default to base classifier } else { patch = getPatchClassifier(); patch.buildClassifier(set); } patches.add(d, patch); } } catch (Exception e) { System.err.println("Error building patches:"); System.err.println(e.getMessage()); } return patches; }
From source file:myclassifier.wekaCode.java
public static Classifier buildClassifier(Instances dataSet, int classifierType, boolean prune) throws Exception { Classifier classifier = null; 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);/*from w w w . j a v a2s .co m*/ classifier = j48; classifier.buildClassifier(dataSet); } return classifier; }
From source file:myclassifier.wekaCode.java
public static void percentageSplit(Instances data, Classifier classifiers, float percentSplit) throws Exception { //Split data jadi training data dan test data int jumlahDataTrain = Math.round(data.numInstances() * (percentSplit / 100)); int jumlahDataTest = data.numInstances() - jumlahDataTrain; Instances dataTrain = new Instances(data, 0, jumlahDataTrain); Instances dataTest = new Instances(data, jumlahDataTrain, jumlahDataTest); //Evaluate /*ww w .j ava 2 s .com*/ classifiers.buildClassifier(dataTrain); testingTestSet(dataTrain, classifiers, dataTest); }