List of usage examples for weka.classifiers.lazy IBk IBk
public IBk()
From source file:machinelearning_cw.MachineLearning_CW.java
/** * @param args the command line arguments *//*from w w w . ja v a2s. c o m*/ public static void main(String[] args) throws Exception { // TODO code application logic here /* Initializing test datasets */ ArrayList<Instances> trainData = new ArrayList<Instances>(); ArrayList<Instances> testData = new ArrayList<Instances>(); Instances train = WekaLoader.loadData("PitcherTrain.arff"); Instances test = WekaLoader.loadData("PitcherTest.arff"); trainData.add(train); testData.add(test); Instances bananaTrain = WekaLoader.loadData("banana-train.arff"); Instances bananaTest = WekaLoader.loadData("banana-test.arff"); trainData.add(bananaTrain); testData.add(bananaTest); Instances cloudTrain = WekaLoader.loadData("clouds-train.arff"); Instances cloudTest = WekaLoader.loadData("clouds-test.arff"); trainData.add(cloudTrain); testData.add(cloudTest); Instances concentricTrain = WekaLoader.loadData("concentric-train.arff"); Instances concentricTest = WekaLoader.loadData("concentric-test.arff"); trainData.add(concentricTrain); testData.add(concentricTest); // 3 dimensional data set Instances habermanTrain = WekaLoader.loadData("haberman-train.arff"); Instances habermanTest = WekaLoader.loadData("haberman-test.arff"); trainData.add(habermanTrain); testData.add(habermanTest); // >3 dimensional data sets Instances thyroidTrain = WekaLoader.loadData("thyroid-train.arff"); Instances thyroidTest = WekaLoader.loadData("thyroid-test.arff"); trainData.add(thyroidTrain); testData.add(thyroidTest); Instances heartTrain = WekaLoader.loadData("heart-train.arff"); Instances heartTest = WekaLoader.loadData("heart-test.arff"); trainData.add(heartTrain); testData.add(heartTest); Instances liverTrain = WekaLoader.loadData("liver-train.arff"); Instances liverTest = WekaLoader.loadData("liver-test.arff"); trainData.add(liverTrain); testData.add(liverTest); Instances pendigitisTrain = WekaLoader.loadData("pendigitis-train.arff"); Instances pendigitisTest = WekaLoader.loadData("pendigitis-test.arff"); trainData.add(pendigitisTrain); testData.add(pendigitisTest); Instances phonemeTrain = WekaLoader.loadData("phoneme-train.arff"); Instances phonemeTest = WekaLoader.loadData("phoneme-test.arff"); trainData.add(phonemeTrain); testData.add(phonemeTest); Instances yeastTrain = WekaLoader.loadData("yeast-train.arff"); Instances yeastTest = WekaLoader.loadData("yeast-test.arff"); trainData.add(yeastTrain); testData.add(yeastTest); /* Test to see that BasicKNN provides the same results obtained from * the hand exercise. */ System.out.println( "Test to see that BasicKNN provides the same" + " results obtained from the hand exercise:"); System.out.println("(Ties are settled randomly)"); BasicKNN basicKNN = new BasicKNN(); basicKNN.buildClassifier(train); for (int i = 0; i < test.size(); i++) { Instance inst = test.get(i); System.out.println(i + 1 + ": " + basicKNN.classifyInstance(inst)); } /* Initializing alternative classifiers */ IBk wekaKNN = new IBk(); NaiveBayes naiveBayes = new NaiveBayes(); J48 decisionTree = new J48(); SMO svm = new SMO(); /* Tests for experiments 1,2 & 3 */ KNN myKNN = new KNN(); myKNN.setUseStandardisedAttributes(true); myKNN.setAutoDetermineK(false); myKNN.setUseWeightedVoting(true); myKNN.buildClassifier(train); //myKNN.setUseAcceleratedNNSearch(true); System.out.println("\nAccuracy Experiments:"); MachineLearning_CW.performClassifierAccuracyTests(myKNN, trainData, testData, 1); /* Timing tests */ System.out.println("\n\nTiming Experiments:"); MachineLearning_CW.performClassifierTimingTests(wekaKNN, trainData, testData); }
From source file:meddle.TrainModelByDomainOS.java
License:Open Source License
/** * Given the classifierName, return a classifier * * @param classifierName/*from w w w. j a va 2s . co m*/ * e.g. J48, Bagging etc. */ public static Classifier getClassifier(String classifierName) { Classifier classifier = null; if (classifierName.equals("J48")) { J48 j48 = new J48(); j48.setUnpruned(true); classifier = j48; } else if (classifierName.equals("AdaBoostM1")) { AdaBoostM1 adm = new AdaBoostM1(); adm.setNumIterations(10); J48 j48 = new J48(); adm.setClassifier(j48); classifier = adm; } else if (classifierName.equals("Bagging")) { Bagging bagging = new Bagging(); bagging.setNumIterations(10); J48 j48 = new J48(); bagging.setClassifier(j48); classifier = bagging; } else if (classifierName.equals("Stacking")) { Stacking stacking = new Stacking(); stacking.setMetaClassifier(new Logistic()); Classifier cc[] = new Classifier[2]; cc[0] = new J48(); cc[1] = new IBk(); stacking.setClassifiers(cc); classifier = stacking; } else if (classifierName.equals("AdditiveRegression")) { AdditiveRegression ar = new AdditiveRegression(); ar.setClassifier(new J48()); classifier = ar; } else if (classifierName.equals("LogitBoost")) { LogitBoost lb = new LogitBoost(); lb.setClassifier(new J48()); classifier = lb; } return classifier; }
From source file:mulan.classifier.lazy.BRkNN.java
License:Open Source License
/** * Select the best value for k by hold-one-out cross-validation. Hamming * Loss is minimized//from w ww . j a v a 2 s .com * * @throws Exception */ protected void crossValidate() throws Exception { try { // the performance for each different k double[] hammingLoss = new double[cvMaxK]; for (int i = 0; i < cvMaxK; i++) { hammingLoss[i] = 0; } Instances dataSet = train; Instance instance; // the hold out instance Instances neighbours; // the neighboring instances double[] origDistances, convertedDistances; for (int i = 0; i < dataSet.numInstances(); i++) { if (getDebug() && (i % 50 == 0)) { debug("Cross validating " + i + "/" + dataSet.numInstances() + "\r"); } instance = dataSet.instance(i); neighbours = lnn.kNearestNeighbours(instance, cvMaxK); origDistances = lnn.getDistances(); // gathering the true labels for the instance boolean[] trueLabels = new boolean[numLabels]; for (int counter = 0; counter < numLabels; counter++) { int classIdx = labelIndices[counter]; String classValue = instance.attribute(classIdx).value((int) instance.value(classIdx)); trueLabels[counter] = classValue.equals("1"); } // calculate the performance metric for each different k for (int j = cvMaxK; j > 0; j--) { convertedDistances = new double[origDistances.length]; System.arraycopy(origDistances, 0, convertedDistances, 0, origDistances.length); double[] confidences = this.getConfidences(neighbours, convertedDistances); boolean[] bipartition = null; switch (extension) { case NONE: // BRknn MultiLabelOutput results; results = new MultiLabelOutput(confidences, 0.5); bipartition = results.getBipartition(); break; case EXTA: // BRknn-a bipartition = labelsFromConfidences2(confidences); break; case EXTB: // BRknn-b bipartition = labelsFromConfidences3(confidences); break; } double symmetricDifference = 0; // |Y xor Z| for (int labelIndex = 0; labelIndex < numLabels; labelIndex++) { boolean actual = trueLabels[labelIndex]; boolean predicted = bipartition[labelIndex]; if (predicted != actual) { symmetricDifference++; } } hammingLoss[j - 1] += (symmetricDifference / numLabels); neighbours = new IBk().pruneToK(neighbours, convertedDistances, j - 1); } } // Display the results of the cross-validation if (getDebug()) { for (int i = cvMaxK; i > 0; i--) { debug("Hold-one-out performance of " + (i) + " neighbors "); debug("(Hamming Loss) = " + hammingLoss[i - 1] / dataSet.numInstances()); } } // Check through the performance stats and select the best // k value (or the lowest k if more than one best) double[] searchStats = hammingLoss; double bestPerformance = Double.NaN; int bestK = 1; for (int i = 0; i < cvMaxK; i++) { if (Double.isNaN(bestPerformance) || (bestPerformance > searchStats[i])) { bestPerformance = searchStats[i]; bestK = i + 1; } } numOfNeighbors = bestK; if (getDebug()) { System.err.println("Selected k = " + bestK); } } catch (Exception ex) { throw new Error("Couldn't optimize by cross-validation: " + ex.getMessage()); } }
From source file:Neural_Network.NuralN.java
public static void trainNet() { System.out.println();//from w w w . ja va2s .c o m try { loadTrainData(); nN = new IBk(); nN.buildClassifier(trainSet); // "Training Completed; trained = true; } catch (IOException e) { System.out.println("Train file missing...."); System.err.println(e.toString()); } catch (Exception e) { System.err.println(e.toString()); } }
From source file:PointAnalyser.Main.java
public static void trainNNClassifier() throws Exception { // setting class attribute if the data format does not provide this information // For example, the XRFF format saves the class attribute information as well if (data.classIndex() == -1) { data.setClassIndex(data.numAttributes() - 1); }//w w w .java 2 s .c om NumericToNominal nmf = new NumericToNominal(); nmf.setInputFormat(data); data = Filter.useFilter(data, nmf); // build a c4.5 classifier String[] options = new String[1]; // options[0] = "-K 1"; // unpruned tree nn = new IBk(); // new instance of tree // nn.setCrossValidate(true); nn.setKNN(7); nn.setNearestNeighbourSearchAlgorithm(new weka.core.neighboursearch.KDTree(data)); nn.setWindowSize(0); // nn.setOptions(options); // set the options nn.buildClassifier(data); // build classifier // eval Evaluation eval = new Evaluation(data); eval.crossValidateModel(nn, data, 10, new Random(1)); System.out.println(eval.toSummaryString()); System.out.println(eval.toMatrixString()); System.out.println(eval.toClassDetailsString()); }
From source file:semana07.IrisKnn.java
public static void main(String[] args) throws FileNotFoundException, IOException, Exception { // DEFININDO CONJUNTO DE TREINAMENTO // - Definindo o leitor do arquivo arff FileReader baseIris = new FileReader("iris.arff"); // - Definindo o grupo de instancias a partir do arquivo "simpsons.arff" Instances iris = new Instances(baseIris); // - Definindo o indice do atributo classe iris.setClassIndex(4);// w w w . j a va2 s . c om iris = iris.resample(new Debug.Random()); Instances irisTreino = iris.trainCV(3, 0); Instances irisTeste = iris.testCV(3, 0); // DEFININDO EXEMPLO DESCONHECIDO //5.9,3.0,5.1,1.8,Iris-virginica Instance irisInst = new DenseInstance(iris.numAttributes()); irisInst.setDataset(iris); irisInst.setValue(0, 5.9); irisInst.setValue(1, 3.0); irisInst.setValue(2, 5.1); irisInst.setValue(3, 1.8); // DEFININDO ALGORITMO DE CLASSIFICAO //NN IBk vizinhoIris = new IBk(); //kNN IBk knnIris = new IBk(3); // MONTANDO CLASSIFICADOR //NN vizinhoIris.buildClassifier(irisTreino); //kNN knnIris.buildClassifier(irisTreino); // Definindo arquivo a ser escrito FileWriter writer = new FileWriter("iris.csv"); // Escrevendo o cabealho do arquivo writer.append("Classe Real;Resultado NN;Resultado kNN"); writer.append(System.lineSeparator()); // Sada CLI / Console System.out.println("Classe Real;Resultado NN;Resultado kNN"); //Cabealho for (int i = 0; i <= irisTeste.numInstances() - 1; i++) { Instance testeIris = irisTeste.instance(i); // Sada CLI / Console do valor original System.out.print(testeIris.stringValue(4) + ";"); // Escrevendo o valor original no arquivo writer.append(testeIris.stringValue(4) + ";"); // Definindo o atributo classe como indefinido testeIris.setClassMissing(); // CLASSIFICANDO A INSTANCIA // NN double respostaVizinho = vizinhoIris.classifyInstance(testeIris); testeIris.setValue(4, respostaVizinho); String stringVizinho = testeIris.stringValue(4); //kNN double respostaKnn = knnIris.classifyInstance(testeIris); // Atribuindo respota ao valor do atributo do index 4(classe) testeIris.setValue(4, respostaKnn); String stringKnn = testeIris.stringValue(4); // Adicionando resultado ao grupo de instancia iris iris.add(irisInst); //Escrevendo os resultados no arquivo iris.csv writer.append(stringVizinho + ";"); writer.append(stringKnn + ";"); writer.append(System.lineSeparator()); // Exibindo via CLI / Console o resultado System.out.print(respostaVizinho + ";"); System.out.print(respostaKnn + ";"); System.out.println(testeIris.stringValue(4)); } writer.flush(); writer.close(); }
From source file:statistics.BinaryStatisticsEvaluator.java
@Override public double[][] getConfusionMatrix(Instances Training_Instances, Instances Testing_Instances, String classifier) {//from w w w . ja v a2s .c o m Classifier cModel = null; if ("NB".equals(classifier)) { cModel = (Classifier) new NaiveBayes(); try { cModel.buildClassifier(Training_Instances); } catch (Exception ex) { Logger.getLogger(BinaryStatisticsEvaluator.class.getName()).log(Level.SEVERE, null, ex); } } else if ("DT".equals(classifier)) { cModel = (Classifier) new J48(); try { cModel.buildClassifier(Training_Instances); } catch (Exception ex) { Logger.getLogger(BinaryStatisticsEvaluator.class.getName()).log(Level.SEVERE, null, ex); } } else if ("SVM".equals(classifier)) { cModel = (Classifier) new SMO(); try { cModel.buildClassifier(Training_Instances); } catch (Exception ex) { Logger.getLogger(BinaryStatisticsEvaluator.class.getName()).log(Level.SEVERE, null, ex); } } else if ("KNN".equals(classifier)) { cModel = (Classifier) new IBk(); try { cModel.buildClassifier(Training_Instances); } catch (Exception ex) { Logger.getLogger(BinaryStatisticsEvaluator.class.getName()).log(Level.SEVERE, null, ex); } } //Test the model Evaluation eTest; try { eTest = new Evaluation(Training_Instances); eTest.evaluateModel(cModel, Testing_Instances); //Print the result String strSummary = eTest.toSummaryString(); System.out.println(strSummary); String strSummary1 = eTest.toMatrixString(); System.out.println(strSummary1); String strSummary2 = eTest.toClassDetailsString(); System.out.println(strSummary2); //Get the confusion matrix double[][] cmMatrix = eTest.confusionMatrix(); return cmMatrix; } catch (Exception ex) { Logger.getLogger(BinaryStatisticsEvaluator.class.getName()).log(Level.SEVERE, null, ex); } return null; }
From source file:tcc.FeatureExtraction.java
public void knn() throws IOException { //parsing CSV to Arff CSVLoader loader = new CSVLoader(); loader.setSource(new File("/root/TCC/Resultados/Parte 4 - Novos Casos/TamuraHaralickMomentos.csv")); Instances inst = loader.getDataSet(); ArffSaver saver = new ArffSaver(); saver.setInstances(inst);/* w w w . j a va 2s . c om*/ saver.setFile(new File("/root/TCC/Resultados/Parte 4 - Novos Casos/TamuraHaralickMomentos.arff")); saver.setDestination(new File("/root/TCC/Resultados/Parte 4 - Novos Casos/TamuraHaralickMomentos.arff")); saver.writeBatch(); BufferedReader reader = new BufferedReader( new FileReader("/root/TCC/Resultados/Parte 4 - Novos Casos/TamuraHaralickMomentos.arff")); Instances data = new Instances(reader); reader.close(); data.setClassIndex(data.numAttributes() - 1); //Normalizando try { Normalize norm = new Normalize(); norm.setInputFormat(data); data = Filter.useFilter(data, norm); } catch (Exception ex) { Logger.getLogger(FeatureExtraction.class.getName()).log(Level.SEVERE, null, ex); } File csv = new File("/root/TCC/Resultados/knn.csv"); FileWriter fw = new FileWriter(csv); BufferedWriter bw = new BufferedWriter(fw); for (int i = 1; i < 51; i++) { //instanciando o classificador IBk knn = new IBk(); knn.setKNN(i); try { knn.buildClassifier(data); Evaluation eval = new Evaluation(data); //System.out.println(eval.toSummaryString("\nResults\n======\n", false)); eval.crossValidateModel(knn, data, 10, new Random(1), new Object[] {}); double auc = eval.areaUnderROC(1); System.out.println(auc); bw.write(Double.toString(auc)); bw.newLine(); } catch (Exception ex) { Logger.getLogger(FeatureExtraction.class.getName()).log(Level.SEVERE, null, ex); } } bw.close(); }
From source file:wekimini.learning.KNNModelBuilder.java
public KNNModelBuilder() { classifier = new IBk(); ((IBk) classifier).setKNN(defaultNumNeighbors); }
From source file:wekimini.learning.KNNModelBuilder.java
public KNNModelBuilder(int numNeighbors) { this.numNeighbors = numNeighbors; classifier = new IBk(); ((IBk) classifier).setKNN(numNeighbors); }