List of usage examples for weka.core Instances instance
publicInstance instance(int index)
From source file:lu.lippmann.cdb.lab.mds.MDSViewBuilder.java
License:Open Source License
/** * /* w ww . j a va 2 s .c o m*/ * @param clusters */ public static void buildKMeansChart(final List<Instances> clusters) { final XYSeriesCollection dataset = new XYSeriesCollection(); final JFreeChart chart = ChartFactory.createScatterPlot("", // title "X", "Y", // axis labels dataset, // dataset PlotOrientation.VERTICAL, true, // legend? yes true, // tooltips? yes false // URLs? no ); final XYPlot xyPlot = (XYPlot) chart.getPlot(); ((NumberAxis) xyPlot.getDomainAxis()).setTickUnit(new NumberTickUnit(2.0)); ((NumberAxis) xyPlot.getRangeAxis()).setTickUnit(new NumberTickUnit(2.0)); Attribute clsAttribute = null; int nbClass = 1; Instances cluster0 = clusters.get(0); if (cluster0.classIndex() != -1) { clsAttribute = cluster0.classAttribute(); nbClass = clsAttribute.numValues(); } if (nbClass <= 1) { dataset.addSeries(new XYSeries("Serie #1", false)); } else { for (int i = 0; i < nbClass; i++) { dataset.addSeries(new XYSeries(clsAttribute.value(i), false)); } } final XYToolTipGenerator gen = new XYToolTipGenerator() { @Override public String generateToolTip(XYDataset dataset, int series, int item) { return "TODO"; } }; for (int i = 0; i < nbClass; i++) { dataset.getSeries(i).clear(); xyPlot.getRenderer().setSeriesToolTipGenerator(i, gen); } final int nbClusters = clusters.size(); for (int i = 0; i < nbClusters; i++) { Instances instances = clusters.get(i); final int nbInstances = instances.numInstances(); for (int j = 0; j < nbInstances; j++) { final Instance oInst = instances.instance(j); dataset.getSeries(i).add(oInst.value(0), oInst.value(1)); } } final TitledBorder titleBorder = new TitledBorder("Kmeans of projection"); ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setMouseWheelEnabled(true); chartPanel.setPreferredSize(new Dimension(1200, 900)); chartPanel.setBorder(titleBorder); chartPanel.setBackground(Color.WHITE); JXFrame frame = new JXFrame(); frame.getContentPane().add(chartPanel); frame.setVisible(true); frame.pack(); }
From source file:lu.lippmann.cdb.lab.mds.UniversalMDS.java
License:Open Source License
public JXPanel buildMDSViewFromDataSet(Instances ds, MDSTypeEnum type) throws Exception { final XYSeriesCollection dataset = new XYSeriesCollection(); final JFreeChart chart = ChartFactory.createScatterPlot("", // title "X", "Y", // axis labels dataset, // dataset PlotOrientation.VERTICAL, true, // legend? yes true, // tooltips? yes false // URLs? no );/*from www. j a v a2 s .co m*/ final XYPlot xyPlot = (XYPlot) chart.getPlot(); chart.setTitle(type.name() + " MDS"); Attribute clsAttribute = null; int nbClass = 1; if (ds.classIndex() != -1) { clsAttribute = ds.classAttribute(); nbClass = clsAttribute.numValues(); } final List<XYSeries> lseries = new ArrayList<XYSeries>(); if (nbClass <= 1) { lseries.add(new XYSeries("Serie #1", false)); } else { for (int i = 0; i < nbClass; i++) { lseries.add(new XYSeries(clsAttribute.value(i), false)); } } dataset.removeAllSeries(); /** * Initialize filtered series */ final List<Instances> filteredInstances = new ArrayList<Instances>(); for (int i = 0; i < lseries.size(); i++) { filteredInstances.add(new Instances(ds, 0)); } for (int i = 0; i < ds.numInstances(); i++) { final Instance oInst = ds.instance(i); int indexOfSerie = 0; if (oInst.classIndex() != -1) { indexOfSerie = (int) oInst.value(oInst.classAttribute()); } lseries.get(indexOfSerie).add(coordinates[i][0], coordinates[i][1]); filteredInstances.get(indexOfSerie).add(oInst); } final List<Paint> colors = new ArrayList<Paint>(); for (final XYSeries series : lseries) { dataset.addSeries(series); } final XYToolTipGenerator gen = new XYToolTipGenerator() { @Override public String generateToolTip(XYDataset dataset, int series, int item) { return InstanceFormatter.htmlFormat(filteredInstances.get(series).instance(item), true); } }; final Shape shape = new Ellipse2D.Float(0f, 0f, 5f, 5f); ((XYLineAndShapeRenderer) xyPlot.getRenderer()).setUseOutlinePaint(true); for (int p = 0; p < nbClass; p++) { xyPlot.getRenderer().setSeriesToolTipGenerator(p, gen); ((XYLineAndShapeRenderer) xyPlot.getRenderer()).setLegendShape(p, shape); xyPlot.getRenderer().setSeriesOutlinePaint(p, Color.BLACK); } for (int ii = 0; ii < nbClass; ii++) { colors.add(xyPlot.getRenderer().getItemPaint(ii, 0)); } final ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setMouseWheelEnabled(true); chartPanel.setPreferredSize(new Dimension(1200, 900)); chartPanel.setBorder(new TitledBorder("MDS Projection")); chartPanel.setBackground(Color.WHITE); final JXPanel allPanel = new JXPanel(); allPanel.setLayout(new BorderLayout()); allPanel.add(chartPanel, BorderLayout.CENTER); return allPanel; }
From source file:LVCoref.WekaWrapper.java
License:Open Source License
public static void main(String[] args) { try {//from www . j a v a 2 s. c o m List<Document> docs = new LinkedList<Document>(); Document d = new Document(); d.readCONLL("data/pipeline/interview_16.lvsem.conll"); d.addAnnotationMMAX("data/interview_16_coref_level.xml"); d.useGoldMentions(); docs.add(d); d = new Document(); d.readCONLL("data/pipeline/interview_23.lvsem.conll"); d.addAnnotationMMAX("data/interview_23_coref_level.xml"); d.useGoldMentions(); docs.add(d); d = new Document(); d.readCONLL("data/pipeline/interview_27.lvsem.conll"); d.addAnnotationMMAX("data/interview_27_coref_level.xml"); d.useGoldMentions(); docs.add(d); d = new Document(); d.readCONLL("data/pipeline/interview_38.lvsem.conll"); d.addAnnotationMMAX("data/interview_38_coref_level.xml"); d.useGoldMentions(); docs.add(d); Instances train = toArff2(docs); train.setClassIndex(train.numAttributes() - 1); String[] options = { "-U" };//, "-C", "0.5"}; Classifier cls = new J48(); cls.setOptions(options); cls.buildClassifier(train); docs = new LinkedList<Document>(); d = new Document(); d.readCONLL("data/pipeline/interview_43.lvsem.conll"); d.addAnnotationMMAX("data/interview_43_coref_level.xml"); d.useGoldMentions(); docs.add(d); d = new Document(); d.readCONLL("data/pipeline/interview_46.lvsem.conll"); d.addAnnotationMMAX("data/interview_46_coref_level.xml"); d.useGoldMentions(); docs.add(d); Evaluation eval = new Evaluation(train); Instances data = toArff2(docs); data.setClassIndex(data.numAttributes() - 1); for (int i = 0; i < data.numInstances(); i++) { double clsLabel = cls.classifyInstance(data.instance(i)); //System.out.println(clsLabel); data.instance(i).setClassValue(clsLabel); System.out.println(data.instance(i).toString(data.classIndex())); } // eval.crossValidateModel(cls, train, 10, new Random(1)); // // generate curve // ThresholdCurve tc = new ThresholdCurve(); // //int classIndex = test.numAttributes()-1; // Instances result = tc.getCurve(eval.predictions());//, classIndex); // // // plot curve // ThresholdVisualizePanel vmc = new ThresholdVisualizePanel(); // vmc.setROCString("(Area under ROC = " + // weka.core.Utils.doubleToString(tc.getROCArea(result), 4) + ")"); // vmc.setName(result.relationName()); // PlotData2D tempd = new PlotData2D(result); // tempd.setPlotName(result.relationName()); // tempd.addInstanceNumberAttribute(); // // specify which points are connected // boolean[] cp = new boolean[result.numInstances()]; // for (int n = 1; n < cp.length; n++) // cp[n] = true; // tempd.setConnectPoints(cp); // // add plot // vmc.addPlot(tempd); // // // display curve // String plotName = vmc.getName(); // final javax.swing.JFrame jf = // new javax.swing.JFrame("Weka Classifier Visualize: "+plotName); // jf.setSize(500,400); // jf.getContentPane().setLayout(new BorderLayout()); // jf.getContentPane().add(vmc, BorderLayout.CENTER); // jf.addWindowListener(new java.awt.event.WindowAdapter() { // public void windowClosing(java.awt.event.WindowEvent e) { // jf.dispose(); // } // }); // jf.setVisible(true); // Instances test = toArff2(docs); // test.setClassIndex(test.numAttributes()-1); // // // Evaluation evals = new Evaluation(train); // // evals.evaluateModel(cls, test); // System.out.println(evals.toSummaryString("\nResults\n======\n", false)); // System.out.println(evals.toMatrixString()); // System.out.println(evals.toClassDetailsString()); // // System.out.println(cls); // //System.out.println(toArff2(docs)); } catch (Exception ex) { Logger.getLogger(WekaWrapper.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:machine_learing_clasifier.MyC45.java
@Override public void buildClassifier(Instances i) throws Exception { if (!i.classAttribute().isNominal()) { throw new Exception("Class not nominal"); }/* w w w .j av a 2 s . c om*/ //penanganan missing value for (int j = 0; j < i.numAttributes(); j++) { Attribute attr = i.attribute(j); for (int k = 0; k < i.numInstances(); k++) { Instance inst = i.instance(k); if (inst.isMissing(attr)) { inst.setValue(attr, fillMissingValue(i, attr)); //bisa dituning lagi performancenya } } } i = new Instances(i); i.deleteWithMissingClass(); makeTree(i); }
From source file:machine_learing_clasifier.MyC45.java
public double fillMissingValue(Instances i, Attribute att) { int[] jumlahvalue = new int[att.numValues()]; for (int k = 0; k < i.numInstances(); k++) { jumlahvalue[(int) i.instance(k).value(att)]++; }//from w w w . ja v a 2s .co m return jumlahvalue[Utils.maxIndex(jumlahvalue)]; }
From source file:machine_learing_clasifier.MyC45.java
public double computeEntropy(Instances inst) { double[] classCount = new double[inst.numClasses()]; for (int i = 0; i < inst.numInstances(); i++) { int temp = (int) inst.instance(i).classValue(); classCount[temp]++;/* w ww . j a v a2 s . co m*/ } double entropy = 0; for (int i = 0; i < inst.numClasses(); i++) { if (classCount[i] > 0) { entropy -= classCount[i] * Utils.log2(classCount[i] / inst.numInstances()); } } entropy /= (double) inst.numInstances(); return entropy; }
From source file:machine_learing_clasifier.MyC45.java
public Instances[] splitData(Instances inst, Attribute attr) { Instances[] split = new Instances[attr.numValues()]; for (int i = 0; i < attr.numValues(); i++) { split[i] = new Instances(inst, inst.numInstances()); }// w w w .j a v a 2s . c om for (int i = 0; i < inst.numInstances(); i++) { int temp = (int) inst.instance(i).value(attr); split[temp].add(inst.instance(i)); } return split; }
From source file:machine_learing_clasifier.MyC45.java
public Instances[] splitDataContinous(Instances inst, Attribute attr, double threshold) { Instances[] split = new Instances[2]; for (int i = 0; i < 2; i++) { split[i] = new Instances(inst, inst.numInstances()); }/*ww w . ja va2 s.c om*/ for (int i = 0; i < inst.numInstances(); i++) { double temp = inst.instance(i).value(attr); if (temp < threshold) { split[0].add(inst.instance(i)); } else { split[1].add(inst.instance(i)); } } return split; }
From source file:machine_learing_clasifier.MyC45.java
public void makeTree(Instances data) throws Exception { if (data.numInstances() == 0) { return;//from ww w . ja va 2 s . c o m } double[] infoGains = new double[data.numAttributes()]; for (int i = 0; i < data.numAttributes(); i++) { Attribute att = data.attribute(i); if (data.classIndex() != att.index()) { if (att.isNominal()) { infoGains[att.index()] = computeInformationGain(data, att); } else { infoGains[att.index()] = computeInformationGainContinous(data, att, BestContinousAttribute(data, att)); } } } m_Attribute = data.attribute(Utils.maxIndex(infoGains)); if (m_Attribute.isNumeric()) { numericAttThreshold = BestContinousAttribute(data, m_Attribute); System.out.println(" ini kalo continous dengan attribut : " + numericAttThreshold); } System.out.println("huhu = " + m_Attribute.toString()); if (Utils.eq(infoGains[m_Attribute.index()], 0)) { m_Attribute = null; m_Distribution = new double[data.numClasses()]; for (int i = 0; i < data.numInstances(); i++) { int inst = (int) data.instance(i).value(data.classAttribute()); m_Distribution[inst]++; } Utils.normalize(m_Distribution); m_ClassValue = Utils.maxIndex(m_Distribution); m_ClassAttribute = data.classAttribute(); } else { Instances[] splitData; if (m_Attribute.isNominal()) { splitData = splitData(data, m_Attribute); } else { splitData = splitDataContinous(data, m_Attribute, numericAttThreshold); } if (m_Attribute.isNominal()) { System.out.println("nominal"); m_Successors = new MyC45[m_Attribute.numValues()]; System.out.println(m_Successors.length); for (int j = 0; j < m_Attribute.numValues(); j++) { m_Successors[j] = new MyC45(head, this); m_Successors[j].buildClassifier(splitData[j]); } } else { System.out.println("numeric"); m_Successors = new MyC45[2]; System.out.println(m_Successors.length); for (int j = 0; j < 2; j++) { m_Successors[j] = new MyC45(head, this); m_Successors[j].buildClassifier(splitData[j]); } } } }
From source file:machine_learing_clasifier.MyID3.java
@Override public void buildClassifier(Instances i) throws Exception { if (!i.classAttribute().isNominal()) { throw new Exception("Class not nominal"); }// ww w .ja v a 2 s. com for (int j = 0; j < i.numAttributes(); j++) { Attribute attr = i.attribute(j); if (!attr.isNominal()) { throw new Exception("Attribute not nominal"); } for (int k = 0; k < i.numInstances(); k++) { Instance inst = i.instance(k); if (inst.isMissing(attr)) { throw new Exception("Missing value"); } } } i = new Instances(i); i.deleteWithMissingClass(); makeTree(i); }