List of usage examples for weka.core Instances firstInstance
publicInstance firstInstance()
From source file:milk.visualize.MIPlot2D.java
License:Open Source License
/** * Renders this component/*from ww w . jav a2 s. c om*/ * @param gx the graphics context */ public void paintComponent(Graphics gx) { //if(!isEnabled()) // return; super.paintComponent(gx); if (plotExemplars != null) { gx.setColor(m_axisColour); // Draw the axis name String xname = plotExemplars.attribute(m_xIndex).name(), yname = plotExemplars.attribute(m_yIndex).name(); gx.drawString(yname, m_XaxisStart + m_labelMetrics.stringWidth("M"), m_YaxisStart + m_labelMetrics.getAscent() / 2 + m_tickSize); gx.drawString(xname, m_XaxisEnd - m_labelMetrics.stringWidth(yname) + m_tickSize, (int) (m_YaxisEnd - m_labelMetrics.getAscent() / 2)); // Draw points Attribute classAtt = plotExemplars.classAttribute(); for (int j = 0; j < m_plots.size(); j++) { PlotData2D temp_plot = (PlotData2D) (m_plots.elementAt(j)); Instances instances = temp_plot.getPlotInstances(); StringTokenizer st = new StringTokenizer( instances.firstInstance().stringValue(plotExemplars.idIndex()), "_"); //////////////////// TLD stuff ///////////////////////////////// /* double[] mu = new double[plotExemplars.numAttributes()], sgm = new double[plotExemplars.numAttributes()]; st.nextToken(); // Squeeze first element int p=0; while(p<mu.length){ if((p==plotExemplars.idIndex()) || (p==plotExemplars.classIndex())) p++; if(p<mu.length){ mu[p] = Double.parseDouble(st.nextToken()); sgm[p] = Double.parseDouble(st.nextToken()); p++; } } Instance ins = instances.firstInstance(); gx.setColor((Color)m_colorList.elementAt((int)ins.classValue())); double mux=mu[m_xIndex], muy=mu[m_yIndex], sgmx=sgm[m_xIndex], sgmy=sgm[m_yIndex]; double xs = convertToPanelX(mux-3*sgmx), xe = convertToPanelX(mux+3*sgmx), xleng = Math.abs(xe-xs); double ys = convertToPanelY(muy+3*sgmy), ye = convertToPanelY(muy-3*sgmy), yleng = Math.abs(ye-ys); // Draw oval gx.drawOval((int)xs,(int)ys,(int)xleng,(int)yleng); // Draw a dot gx.fillOval((int)convertToPanelX(mux)-2, (int)convertToPanelY(muy)-2, 4, 4); */ //////////////////// TLD stuff ///////////////////////////////// //////////////////// instance-based stuff ///////////////////////////////// /* double[] core = new double[plotExemplars.numAttributes()], range=new double[plotExemplars.numAttributes()]; st.nextToken(); // Squeeze first element int p=0; while(p<range.length){ if((p==plotExemplars.idIndex()) || (p==plotExemplars.classIndex())) p++; if(p<range.length) range[p++] = Double.parseDouble(st.nextToken()); } p=0; while(st.hasMoreTokens()){ if((p==plotExemplars.idIndex()) || (p==plotExemplars.classIndex())) p++; core[p++] = Double.parseDouble(st.nextToken()); } Instance ins = instances.firstInstance(); gx.setColor((Color)m_colorList.elementAt((int)ins.classValue())); double rgx=range[m_xIndex], rgy=range[m_yIndex]; double x1 = convertToPanelX(core[m_xIndex]-rgx/2), y1 = convertToPanelY(core[m_yIndex]-rgy/2), x2 = convertToPanelX(core[m_xIndex]+rgx/2), y2 = convertToPanelY(core[m_yIndex]+rgy/2), x = convertToPanelX(core[m_xIndex]), y = convertToPanelY(core[m_yIndex]); // Draw a rectangle gx.drawLine((int)x1, (int)y1, (int)x2, (int)y1); gx.drawLine((int)x1, (int)y1, (int)x1, (int)y2); gx.drawLine((int)x2, (int)y1, (int)x2, (int)y2); gx.drawLine((int)x1, (int)y2, (int)x2, (int)y2); // Draw a dot gx.fillOval((int)x-3, (int)y-3, 6, 6); // Draw string StringBuffer text =new StringBuffer(temp_plot.getPlotName()+":"+instances.numInstances()); gx.drawString(text.toString(), (int)x1, (int)y2+m_labelMetrics.getHeight()); */ //////////////////// instance-based stuff ///////////////////////////////// //////////////////// normal graph ///////////////////////////////// // Paint numbers for (int i = 0; i < instances.numInstances(); i++) { Instance ins = instances.instance(i); if (!ins.isMissing(m_xIndex) && !ins.isMissing(m_yIndex)) { if (classAtt.isNominal()) gx.setColor((Color) m_colorList.elementAt((int) ins.classValue())); else { double r = (ins.classValue() - m_minC) / (m_maxC - m_minC); r = (r * 240) + 15; gx.setColor(new Color((int) r, 150, (int) (255 - r))); } double x = convertToPanelX(ins.value(m_xIndex)); double y = convertToPanelY(ins.value(m_yIndex)); String id = temp_plot.getPlotName(); gx.drawString(id, (int) (x - m_labelMetrics.stringWidth(id) / 2), (int) (y + m_labelMetrics.getHeight() / 2)); } } //////////////////// normal graph ///////////////////////////////// } } //////////////////// TLD stuff ///////////////////////////////// // Draw two Guassian contour with 3 stdDev // (-1, -1) with stdDev 1, 2 // (1, 1) with stdDev 2, 1 /*gx.setColor(Color.black); double mu=-1.5, sigmx, sigmy; // class 0 if(m_xIndex == 1) sigmx = 1; else sigmx = 2; if(m_yIndex == 1) sigmy = 1; else sigmy = 2; double x1 = convertToPanelX(mu-3*sigmx), x2 = convertToPanelX(mu+3*sigmx), xlen = Math.abs(x2-x1); double y1 = convertToPanelY(mu+3*sigmy), y2 = convertToPanelY(mu-3*sigmy), ylen = Math.abs(y2-y1); // Draw heavy oval gx.drawOval((int)x1,(int)y1,(int)xlen,(int)ylen); gx.drawOval((int)x1-1,(int)y1-1,(int)xlen+2,(int)ylen+2); gx.drawOval((int)x1+1,(int)y1+1,(int)xlen-2,(int)ylen-2); // Draw a dot gx.fillOval((int)convertToPanelX(mu)-3, (int)convertToPanelY(mu)-3, 6, 6); mu=1.5; // class 1 if(m_xIndex == 1) sigmx = 1; else sigmx = 2; if(m_yIndex == 1) sigmy = 1; else sigmy = 2; x1 = convertToPanelX(mu-3*sigmx); x2 = convertToPanelX(mu+3*sigmx); xlen = Math.abs(x2-x1); y1 = convertToPanelY(mu+3*sigmy); y2 = convertToPanelY(mu-3*sigmy); ylen = Math.abs(y2-y1); // Draw heavy oval gx.drawOval((int)x1,(int)y1,(int)xlen,(int)ylen); gx.drawOval((int)x1-1,(int)y1-1,(int)xlen+2,(int)ylen+2); gx.drawOval((int)x1+1,(int)y1+1,(int)xlen-2,(int)ylen-2); // Draw a dot gx.fillOval((int)convertToPanelX(mu)-3, (int)convertToPanelY(mu)-3, 6, 6); */ //////////////////// TLD stuff ///////////////////////////////// //////////////////// instance-based stuff ///////////////////////////////// /* // Paint a log-odds line: 1*x0+2*x1=0 double xstart, xend, ystart, yend, xCoeff, yCoeff; if(m_xIndex == 1) xCoeff = 1; else xCoeff = 2; if(m_yIndex == 1) yCoeff = 1; else yCoeff = 2; xstart = m_minX; ystart = -xstart*xCoeff/yCoeff; if(ystart > m_maxY){ ystart = m_maxY; xstart = -ystart*yCoeff/xCoeff; } yend = m_minY; xend = -yend*yCoeff/xCoeff; if(xend > m_maxX){ xend = m_maxX; yend = -xend*xCoeff/yCoeff; } // Draw a heavy line gx.setColor(Color.black); gx.drawLine((int)convertToPanelX(xstart), (int)convertToPanelY(ystart), (int)convertToPanelX(xend), (int)convertToPanelY(yend)); gx.drawLine((int)convertToPanelX(xstart)+1, (int)convertToPanelY(ystart)+1, (int)convertToPanelX(xend)+1, (int)convertToPanelY(yend)+1); gx.drawLine((int)convertToPanelX(xstart)-1, (int)convertToPanelY(ystart)-1, (int)convertToPanelX(xend)-1, (int)convertToPanelY(yend)-1); */ //////////////////// instance-based stuff ///////////////////////////////// }
From source file:moa.tasks.EvaluateNonStationaryDynamicStream.java
License:Open Source License
/** * Manage testing, training, and the reporting thereof * * @param D Data set (or "chunk") to process * @param learningCurve for graphing//from w w w .ja v a2s. co m * @param evaluateStartTime for tracking processing time */ protected void processChunk(Instances D, LearningCurve learningCurve, long evaluateStartTime) { // Certain algorithms (like AHOT) have issue when the training data set is too small and they emit an exception // So we need to wrap a TRY/Catch pattern around the processing to gracefully handle those issues if (knownLabels == null) { knownLabels = new int[D.firstInstance().numClasses() + 2]; Arrays.fill(knownLabels, 0); } //try { // TEST all data instances in stream... if (inWarmupPhase) { try { for (Instance x : D) { this.evaluator.addResult(x, null); this.knownLabels[(int) x.classValue()]++; } } catch (Exception e) { } ; // don't care, just avoid problems with sending null votes if not our own evaluator } else { startTime = TimingUtils.getNanoCPUTimeOfCurrentThread(); samplesTested += test(D); sampleTestTime += TimingUtils.getNanoCPUTimeOfCurrentThread() - startTime; } // Train models adhering to latency and semi-supervised reduced training parameters... startTime = TimingUtils.getNanoCPUTimeOfCurrentThread(); samplesTrained += train(); sampleTrainTime += TimingUtils.getNanoCPUTimeOfCurrentThread() - startTime; // } catch (Exception e) { // System.err.println("Caught Exception: " + e.toString() + " (" + e.getCause() + ": " + e.getMessage()+ ")"); // } // Result output and MOA framework housekeeping... if (!inWarmupPhase && (this.instancesProcessed % this.sampleFrequencyOption.getValue() == 0)) { double RAMHoursIncrement = learner.measureByteSize() * BYTES_TO_GIGABYTES; //GBs RAMHoursIncrement *= (TimingUtils.nanoTimeToSeconds(sampleTrainTime + sampleTestTime) / SECONDS_TO_HOURS); //Hours RAMHours += RAMHoursIncrement; double avgTrainTime = TimingUtils.nanoTimeToSeconds(sampleTrainTime) / ((double) this.sampleFrequencyOption.getValue() / samplesTrained); double avgTestTime = TimingUtils.nanoTimeToSeconds(sampleTestTime) / ((double) this.sampleFrequencyOption.getValue() / samplesTested); learningCurve.insertEntry(new LearningEvaluation( new Measurement[] { new Measurement("learning evaluation instances", instancesProcessed), new Measurement("evaluation time (" + (preciseCPUTiming ? "cpu " : "") + "seconds)", TimingUtils.nanoTimeToSeconds( TimingUtils.getNanoCPUTimeOfCurrentThread() - evaluateStartTime)), new Measurement("model cost (RAM-Hours)", RAMHours), new Measurement("average chunk train time", avgTrainTime), new Measurement("average chunk train speed", samplesTrained / avgTrainTime), new Measurement("average chunk test time", avgTestTime), new Measurement("average chunk test speed", samplesTested / avgTestTime) }, this.evaluator, this.learner)); if (immediateResultStream != null) { if (firstDump) { immediateResultStream.println(learningCurve.headerToString()); firstDump = false; } immediateResultStream.println(learningCurve.entryToString(learningCurve.numEntries() - 1)); immediateResultStream.flush(); } samplesTested = 0; sampleTestTime = 0; samplesTrained = 0; sampleTrainTime = 0; } }
From source file:myclassifier.Main.java
public static void main(String[] args) throws Exception { System.out.println("C"); File f = new File("weather.nominal.arrf"); if (f.exists() && !f.isDirectory()) { System.out.println("A"); } else {// www . j av a 2 s . c om System.out.println("B"); } System.out.println("C"); WekaAccessor access = new WekaAccessor(); Instances train_data = access.loadArff("weather.nominal.arrf"); train_data.toString(); train_data.firstInstance().toString(); MyId3 id3 = new MyId3(); id3.buildClassifier(train_data); access.tenFoldCrossValidation(id3, train_data); }
From source file:myclassifier.MyClassifier.java
/** * @param args the command line arguments *//*from ww w . java 2s . c om*/ public static void main(String[] args) throws Exception { WekaAccessor access = new WekaAccessor(); Instances train_data = access.loadArff("weather.nominal.arff"); train_data.toString(); train_data.firstInstance().toString(); MyId3 tree1 = new MyId3(); MyC45 tree2 = new MyC45(); J48 tree3 = access.buildC45Classifier(train_data); Id3 tree4 = access.buildId3Classifier(train_data); tree1.buildClassifier(train_data); tree2.buildClassifier(train_data); System.out.println("=== My Id3 ==="); access.tenFoldCrossValidation(tree1, train_data); System.out.println("=== My C45 ==="); access.tenFoldCrossValidation(tree2, train_data); System.out.println("=== Weka C45 ==="); access.tenFoldCrossValidation(tree3, train_data); System.out.println("=== Weka Id3 ==="); access.tenFoldCrossValidation(tree4, train_data); }
From source file:NaiveBayesPckge.NaiveBayesMain.java
public static void printCoba(Instances instance) { System.out.println(""); System.out.println("1. first instance : " + instance.firstInstance()); System.out.println("2. banyaknya atribut :" + instance.numAttributes()); System.out.println("3. " + instance.attribute(0).numValues()); System.out.println("4. " + instance.attribute(0).weight()); System.out.println("5. " + instance.attribute(instance.numAttributes() - 1).numValues()); System.out.println("6. " + instance.get(0)); System.out.println("7. " + instance.get(0).stringValue(4)); System.out.println("8. " + instance.numInstances()); System.out.println("9. " + instance.attribute(instance.numAttributes() - 1).numValues()); System.out.println("10. " + instance.get(1).stringValue(instance.numAttributes() - 1)); System.out.println("11. " + instance.attribute(instance.numAttributes() - 1).value(0)); System.out.println("12. " + instance.attribute(instance.numAttributes() - 1).name()); System.out.println("13. " + instance.numClasses()); System.out.println("14. Banyaknya kelas yang diuji : " + instance.classIndex()); // System.out.println("15. " + (String.valueOf(instance.attribute(0).value(34)).equals(String.valueOf(4.3)))); // System.out.println("16. " + instance); }
From source file:OAT.trading.classification.Weka.java
License:Open Source License
@Override public Prediction predict(InputSample input) { if (classifier == null) { log(Level.WARNING, "null classifier"); return null; }// ww w . ja v a 2 s .c o m Instances data = getInstances(input); if (data == null) { log(Level.WARNING, "null data"); return null; } if (!isCrossValidating()) { if (isLoggable(Level.FINER)) { log(Level.FINER, data.toString()); } } try { double output = new Evaluation(data).evaluateModelOnce(classifier, data.firstInstance()); return Prediction.valueOf(output < 0.5 ? -1 : 1); } catch (Exception ex) { log(Level.SEVERE, null, ex); } return null; }
From source file:recsys.BuildModel.java
public static void main(String args[]) throws Exception { //Opening the training file int own_training = StaticVariables.own_training; DataSource sourceTrain;/*from www . j ava2s . c o m*/ if (own_training == 1) sourceTrain = new DataSource("D://own_training//item//feature data//train_feature.arff"); else sourceTrain = new DataSource("E://recsys//item//feature data//train_feature.arff"); Instances train = sourceTrain.getDataSet(); String[] options = new String[2]; options[0] = "-R"; // "range" options[1] = "1,2,4"; // first attribute //options[2] = "2"; // first attribute //options[3] = "4"; //options[2] = "9"; // first attribute //options[3] = "3"; // first attribute //options[4] = "4"; // first attribute Remove remove = new Remove(); // new instance of filter remove.setOptions(options); // set options remove.setInputFormat(train); // inform filter about dataset **AFTER** setting options Instances newData = Filter.useFilter(train, remove); // apply filter System.out.println("number of attributes " + newData.numAttributes()); System.out.println(newData.firstInstance()); if (newData.classIndex() == -1) { newData.setClassIndex(newData.numAttributes() - 1); } Resample sampler = new Resample(); String Fliteroptions = "-B 1.0"; sampler.setOptions(weka.core.Utils.splitOptions(Fliteroptions)); sampler.setRandomSeed((int) System.currentTimeMillis()); sampler.setInputFormat(newData); newData = Resample.useFilter(newData, sampler); //Normalize normalize = new Normalize(); //normalize.toSource(Fliteroptions, newData); //Remove remove = new Remove(); // new instance of filter //remove.setOptions(options); // set options //remove.setInputFormat(train); // inform filter about dataset **AFTER** setting options //Instances newData = Filter.useFilter(train, remove); // apply filter //rm.setAttributeIndices("2"); //rm.setAttributeIndices("3"); //rm.setAttributeIndices("4"); //rm.setAttributeIndices("5"); //rm.setAttributeIndices("6"); //rm.setAttributeIndices("6"); //rm.setAttributeIndices("5"); //Remove rm = new Remove(); //rm.setInputFormat(train); //rm.setAttributeIndices("1"); //FilteredClassifier fc = new FilteredClassifier(); //cls.setOptions(args); //J48 cls = new J48(); //LibSVM cls = new LibSVM(); //SMO cls = new SMO(); //Logistic cls = new Logistic(); //BayesianLogisticRegression cls = new BayesianLogisticRegression(); //cls.setThreshold(0.52); //AdaBoostM1 cls = new AdaBoostM1(); //NaiveBayes cls = new NaiveBayes(); //weka.classifiers.meta.Bagging cls = new Bagging(); //weka.classifiers.functions.IsotonicRegression cls = new IsotonicRegression(); //j48.setUnpruned(true); // using an unpruned J48 // meta-classifier //BayesNet cls = new BayesNet(); RandomForest cls = new RandomForest(); //cls.setNumTrees(100); //cls.setMaxDepth(3); //cls.setNumFeatures(3); //fc.setClassifier(cls); //fc.setFilter(rm); // train and make predictions //System.out.println(fc.globalInfo()); //System.out.println(fc.getFilter()); //fc.buildClassifier(train); cls.buildClassifier(newData); //Evaluation eval = new Evaluation(newData); //Random rand = new Random(1); // using seed = 1 //int folds = 2; //eval.crossValidateModel(cls, newData, folds, rand); //System.out.println(eval.toSummaryString()); //System.out.println("precision on buy " + eval.precision(newData.classAttribute().indexOfValue("buy"))); //System.out.println("recall on buy " + eval.recall(newData.classAttribute().indexOfValue("buy"))); //System.out.println(eval.confusionMatrix().toString()); //System.out.println("Precision " + eval.precision(newData.classIndex()-1)); //System.out.println("Recall " + eval.recall(newData.classIndex()-1)); //Classfier cls = new weka.classifiers.bayes.NaiveBayes(); //FilteredClassifier fc = new FilteredClassifier(); //fc.setFilter(rm); //fc.setClassifier(cls); //train and make predictions //fc.buildClassifier(train); // serialize model ObjectOutputStream oos; if (own_training == 1) oos = new ObjectOutputStream(new FileOutputStream("D://own_training//item//model//train.model")); else oos = new ObjectOutputStream(new FileOutputStream("E://recsys//item//model//train.model")); oos.writeObject(cls); oos.flush(); oos.close(); }
From source file:recsys.ResultProcessing.java
public static void main(String args[]) throws Exception { int own_training = StaticVariables.own_training; //opening the testing file DataSource sourceTest;/*w ww.j av a 2 s . c o m*/ if (own_training == 1) { sourceTest = new DataSource("D://own_training//item//feature data//test_feature.arff"); } else { sourceTest = new DataSource("E://recsys//item//feature data//test_feature.arff"); } //DataSource sourceTest = new DataSource("D://own_training//test_featureFile.arff"); //System.out.println("working"); Instances test = sourceTest.getDataSet(); PrintFile solutionFile; if (own_training == 1) { solutionFile = new PrintFile(null, new File("D://own_training//item//solution//solution.dat")); } else { solutionFile = new PrintFile(null, new File("E://solution.dat")); } //PrintFile solutionFile = new PrintFile(null, new File("D://own_training//solution.dat")); if (test.classIndex() == -1) { test.setClassIndex(test.numAttributes() - 1); } //System.out.println("hello"); ObjectInputStream ois; if (own_training == 1) { ois = new ObjectInputStream(new FileInputStream("D://own_training//item//model//train.model")); } else { ois = new ObjectInputStream(new FileInputStream("E:\\recsys\\item\\model\\train.model")); //sois = new ObjectInputStream(new FileInputStream("E:\\recsys\\my best performances\\39127.6\\train.model")); } //AdaBoostM1 cls = (AdaBoostM1)ois.readObject(); //BayesNet cls = (BayesNet)ois .readObject(); RandomForest cls = (RandomForest) ois.readObject(); //Logistic cls = (Logistic) ois.readObject(); //System.out.println(cls.globalInfo()); //System.out.println(cls.getNumFeatures()); //System.out.println(cls.toString()); //BayesianLogisticRegression cls = (BayesianLogisticRegression)ois.readObject(); //NaiveBayes cls = (NaiveBayes) ois.readObject(); //FilteredClassifier fc = (FilteredClassifier) ois.readObject(); System.out.println(cls.toString()); ois.close(); String[] options = new String[2]; options[0] = "-R"; // "range" options[1] = "1,2,4"; // first attribute //options[2] = "2"; //options[3] = "4"; Remove remove = new Remove(); // new instance of filter remove.setOptions(options); // set options remove.setInputFormat(test); // inform filter about dataset **AFTER** setting options Instances newData = Filter.useFilter(test, remove); // apply filter System.out.println(newData.firstInstance()); int totalSessionCount = 0; int buySessionCount = 0; int b = 0; Scanner sc; if (own_training == 0) sc = new Scanner(new File("E:\\recsys\\session\\solution\\solution.dat")); //sc = new Scanner(new File("E:\\recsys\\my best performances\\best performance\\solution_session.dat")); else sc = new Scanner(new File("D:\\own_training\\session\\solution\\solution.dat")); //sc = new Scanner(new File("D:\\own_training\\session\\data\\original_solution.csv")); HashMap<Integer, Integer> a = new HashMap<Integer, Integer>(); while (sc.hasNext()) { String temp = sc.next(); StringTokenizer st = new StringTokenizer(temp, ",;"); a.put(Integer.parseInt(st.nextToken()), 1); } System.out.println("size " + a.size()); Integer tempSessionId = (int) test.instance(0).value(0); ArrayList<Integer> buy = new ArrayList<>(); String result = String.valueOf(tempSessionId) + ";"; //int lengthVector[] = new int[300]; int testSessionCount = 0, currentSessionLength = 0; //int sessionLengthCount=0; for (int i = 0; i < test.numInstances(); i++) { if ((int) test.instance(i).value(0) != tempSessionId) { if (a.containsKey(tempSessionId)) { //if(test.instance(i-1).value(3)< StaticVariables.length) { //System.out.println(test.instance(i-1).value(3)); totalSessionCount++; if (buy.size() > 0) { for (int j = 0; j < buy.size(); j++) { result += buy.get(j) + ","; } solutionFile.writeFile(result.substring(0, result.length() - 1)); } //lengthVector[sessionLengthCount]++; } /*}else{ if(buy.size()>= 3){ for (int j = 0; j < buy.size(); j++) { result += buy.get(j) + ","; } solutionFile.writeFile(result.substring(0, result.length() - 1)); } }*/ //testSessionCount=0; tempSessionId = (int) test.instance(i).value(0); result = String.valueOf(tempSessionId) + ";"; //sessionLengthCount=0; buy.clear(); } //currentSessionLength = test.instance(i).value(3); //testSessionCount++; //System.out.println("working"); //sessionLengthCount++; double pred = cls.classifyInstance(newData.instance(i)); if (test.classAttribute().value((int) pred).equals("buy")) { b++; Integer item = (int) test.instance(i).value(1); buy.add(item); } //System.out.print(", actual: " + test.classAttribute().value((int) test.instance(i).classValue())); //System.out.println(", predicted: " + test.classAttribute().value((int) pred)); } System.out.println(totalSessionCount); //System.out.println(totalSessionCount); //System.out.println(b); if (buy.size() > 0) { solutionFile.writeFile(result.substring(0, result.length() - 1)); } /*for(int p:lengthVector) System.out.println(p);*/ solutionFile.closeFile(); }
From source file:tubes1.myClassifiers.myC45.java
public TreeNode C45Node(Instances i, double parentGain) { TreeNode treeNode = new TreeNode(); int[] count = calculateCount(i); for (int j = 0; j < count.length; j++) { int c = count[j]; if (c == i.numInstances()) { treeNode.label = j;/*ww w.j a v a2 s . com*/ return treeNode; } } if (i.numAttributes() <= 1) { int maxc = -1; int maxcj = -1; for (int j = 0; j < count.length; j++) { if (count[j] > maxc) { maxc = count[j]; maxcj = j; } } treeNode.label = maxcj; return treeNode; } Attribute bestA = null; double bestAIG = -1; double entropyOfSet = entropy(i); for (int j = 0; j < i.numAttributes(); j++) { Attribute a = i.attribute(j); if (a != i.classAttribute()) { double aIG = infoGain(i, a, entropyOfSet); if (aIG > bestAIG) { bestAIG = aIG; bestA = a; } } } double childGainRatio = gainRatio(bestAIG, entropyOfSet); treeNode.decision = bestA; if (childGainRatio > parentGain) { Instances[] subSets = splitData(i, bestA); for (Instances subSet : subSets) { if (subSet.numInstances() > 0) { double attributeValue = subSet.firstInstance().value(bestA); subSet.deleteAttributeAt(bestA.index()); TreeNode newBranch = C45Node(subSet, childGainRatio); newBranch.branchValue = attributeValue; treeNode.addBranch(newBranch); } } } else { TreeNode newBranch = new TreeNode(); newBranch.label = vote(i, bestA); newBranch.branchValue = treeNode.branchValue; treeNode.addBranch(newBranch); } return treeNode; }
From source file:tubes1.myClassifiers.myID3.java
public TreeNode id3Node(Instances i) { TreeNode treeNode = new TreeNode(); int[] count = calculateCount(i); for (int j = 0; j < count.length; j++) { int c = count[j]; if (c == i.numInstances()) { treeNode.label = j;/*from ww w .j a v a2 s.co m*/ return treeNode; } } if (i.numAttributes() <= 1) { int maxc = -1; int maxcj = -1; for (int j = 0; j < count.length; j++) { if (count[j] > maxc) { maxc = count[j]; maxcj = j; } } treeNode.label = maxcj; return treeNode; } Attribute bestA = null; double bestAIG = -1; double entropyOfSet = entropy(i); for (int j = 0; j < i.numAttributes(); j++) { Attribute a = i.attribute(j); if (a != i.classAttribute()) { double aIG = infoGain(i, a, entropyOfSet); if (aIG > bestAIG) { bestAIG = aIG; bestA = a; } } } treeNode.decision = bestA; Instances[] subSets = splitData(i, bestA); for (Instances subSet : subSets) { if (subSet.numInstances() > 0) { double attributeValue = subSet.firstInstance().value(bestA); subSet.deleteAttributeAt(bestA.index()); TreeNode newBranch = id3Node(subSet); newBranch.branchValue = attributeValue; treeNode.addBranch(newBranch); } else { } } return treeNode; }