List of usage examples for weka.core Instances setClassIndex
public void setClassIndex(int classIndex)
From source file:lu.lippmann.cdb.datasetview.tabs.RegressionTreeTabView.java
License:Open Source License
/** * {@inheritDoc}//from www .ja v a 2s .co m */ @SuppressWarnings("unchecked") @Override public void update0(final Instances dataSet) throws Exception { this.panel.removeAll(); //final Object[] attrNames=WekaDataStatsUtil.getNumericAttributesNames(dataSet).toArray(); final Object[] attrNames = WekaDataStatsUtil.getAttributeNames(dataSet).toArray(); final JComboBox xCombo = new JComboBox(attrNames); xCombo.setBorder(new TitledBorder("Attribute to evaluate")); final JXPanel comboPanel = new JXPanel(); comboPanel.setLayout(new GridLayout(1, 2)); comboPanel.add(xCombo); final JXButton jxb = new JXButton("Compute"); comboPanel.add(jxb); this.panel.add(comboPanel, BorderLayout.NORTH); jxb.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { if (gv != null) panel.remove((Component) gv); dataSet.setClassIndex(xCombo.getSelectedIndex()); final REPTree rt = new REPTree(); rt.setNoPruning(true); //rt.setMaxDepth(3); rt.buildClassifier(dataSet); /*final M5P rt=new M5P(); rt.buildClassifier(dataSet);*/ final Evaluation eval = new Evaluation(dataSet); double[] d = eval.evaluateModel(rt, dataSet); System.out.println("PREDICTED -> " + FormatterUtil.buildStringFromArrayOfDoubles(d)); System.out.println(eval.errorRate()); System.out.println(eval.sizeOfPredictedRegions()); System.out.println(eval.toSummaryString("", true)); final GraphWithOperations gwo = GraphUtil .buildGraphWithOperationsFromWekaRegressionString(rt.graph()); final DecisionTree dt = new DecisionTree(gwo, eval.errorRate()); gv = DecisionTreeToGraphViewHelper.buildGraphView(dt, eventPublisher, commandDispatcher); gv.addMetaInfo("Size=" + dt.getSize(), ""); gv.addMetaInfo("Depth=" + dt.getDepth(), ""); gv.addMetaInfo("MAE=" + FormatterUtil.DECIMAL_FORMAT.format(eval.meanAbsoluteError()) + "", ""); gv.addMetaInfo("RMSE=" + FormatterUtil.DECIMAL_FORMAT.format(eval.rootMeanSquaredError()) + "", ""); final JCheckBox toggleDecisionTreeDetails = new JCheckBox("Toggle details"); toggleDecisionTreeDetails.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (!tweakedGraph) { final Object[] mapRep = WekaDataStatsUtil .buildNodeAndEdgeRepartitionMap(dt.getGraphWithOperations(), dataSet); gv.updateVertexShapeTransformer((Map<CNode, Map<Object, Integer>>) mapRep[0]); gv.updateEdgeShapeRenderer((Map<CEdge, Float>) mapRep[1]); } else { gv.resetVertexAndEdgeShape(); } tweakedGraph = !tweakedGraph; } }); gv.addMetaInfoComponent(toggleDecisionTreeDetails); /*final JButton openInEditorButton = new JButton("Open in editor"); openInEditorButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { GraphUtil.importDecisionTreeInEditor(dtFactory, dataSet, applicationContext, eventPublisher, commandDispatcher); } }); this.gv.addMetaInfoComponent(openInEditorButton);*/ final JButton showTextButton = new JButton("In text"); showTextButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, graphDsl.getDslString(dt.getGraphWithOperations())); } }); gv.addMetaInfoComponent(showTextButton); panel.add(gv.asComponent(), BorderLayout.CENTER); } catch (Exception e1) { e1.printStackTrace(); panel.add(new JXLabel("Error during computation: " + e1.getMessage()), BorderLayout.CENTER); } } }); }
From source file:lu.lippmann.cdb.datasetview.tabs.SymmetricalUncertaintyCorrelationTabView.java
License:Open Source License
/** * {@inheritDoc}//w w w .ja va 2s. com */ @Override public void update0(final Instances dataSet) throws Exception { this.panel.removeAll(); final Object[] attrNames = WekaDataStatsUtil.getAttributeNames(dataSet).toArray(); final JComboBox xCombo = new JComboBox(attrNames); xCombo.setBorder(new TitledBorder("Attribute to evaluate")); final JXPanel comboPanel = new JXPanel(); comboPanel.setLayout(new GridLayout(1, 2)); comboPanel.add(xCombo); final JXButton jxb = new JXButton("Compute"); comboPanel.add(jxb); this.panel.add(comboPanel, BorderLayout.NORTH); jxb.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { final Instances newds = new Instances(dataSet); newds.setClassIndex(xCombo.getSelectedIndex()); final Map<Attribute, Double> mapCoefficients = WekaDataStatsUtil .computeSymmetricUncertaintyCorrelation(newds); final JXPanel tblpanel = new JXPanel(); tblpanel.setLayout(new BorderLayout()); tblpanel.setBorder(new TitledBorder("Coefficients")); final JXTable tbl = new JXTable(); final SingleColumnTableModel mdl = new SingleColumnTableModel("Coeffs"); final java.util.List<String> l = new ArrayList<String>(); for (final Map.Entry<Attribute, Double> entry : mapCoefficients.entrySet()) { l.add(entry.getKey().name() + " -> " + entry.getValue()); } mdl.setData(l); tbl.setModel(mdl); tbl.setEditable(true); tbl.setShowHorizontalLines(false); tbl.setShowVerticalLines(false); tbl.setVisibleRowCount(5); tbl.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); tblpanel.add(tbl, BorderLayout.CENTER); panel.add(tblpanel, BorderLayout.CENTER); } catch (Exception e1) { e1.printStackTrace(); panel.add(new JXLabel("Error during computation: " + e1.getMessage()), BorderLayout.CENTER); } } }); }
From source file:lu.lippmann.cdb.datasetview.tabs.TimeSeriesSimilarityPanel.java
License:Open Source License
private Instances buildFeatureDS(final Instances dataSet) throws Exception { final int numAttributes = dataSet.numAttributes(); final java.util.List<String> namesOfFeaturesToConsider = new ArrayList<String>(); namesOfFeaturesToConsider.addAll(WekaDataStatsUtil.getAttributeNames(dataSet)); namesOfFeaturesToConsider.removeAll(WekaDataStatsUtil.getDateAttributeNames(dataSet)); final double[][] simMatrix = new double[numAttributes][numAttributes]; for (int i = 0; i < numAttributes; i++) { final double[] arrayI = dataSet.attributeToDoubleArray(i); if (this.withNormalization) MathsUtil.normalize(arrayI); simMatrix[i][i] = 0d;/* w w w . ja v a 2 s . co m*/ ; for (int j = i + 1; j < numAttributes; j++) { final double[] arrayJ = dataSet.attributeToDoubleArray(j); if (this.withNormalization) MathsUtil.normalize(arrayJ); simMatrix[i][j] = new DynamicTimeWarping(arrayI, arrayJ).getDistance(); //System.out.println(i+" "+j); } } for (int i = 0; i < numAttributes; i++) { for (int j = 0; j < i + 1; j++) { simMatrix[i][j] = simMatrix[j][i]; } } /*for (int i=0;i<numAttributes;i++) { System.out.println(i+" -> "+FormatterUtil.buildStringFromArrayOfDoubles(simMatrix[i])); }*/ final ArrayList<Attribute> attrs = new ArrayList<Attribute>(numAttributes + 1); for (int i = 0; i < numAttributes; i++) { attrs.add(new Attribute(dataSet.attribute(i).name() + "-feat")); } attrs.add(new Attribute(FEATUREDESC_ATTRNAME, namesOfFeaturesToConsider)); final Instances ds = new Instances("featuresComparisonDs", attrs, 0); ds.setClassIndex(attrs.size() - 1); for (int i = 0; i < simMatrix.length; i++) { final DenseInstance di = new DenseInstance(1.0d, ArraysUtil.concat(simMatrix[i], new double[] { 0d })); di.setDataset(ds); di.setValue(simMatrix.length, dataSet.attribute(i).name()); ds.add(di); } return ds; }
From source file:lu.lippmann.cdb.datasetview.tabs.UnsupervisedFeatureEvaluationTabView.java
License:Open Source License
/** * {@inheritDoc}// w w w . j a v a 2 s . c o m */ @Override public void update0(final Instances dataSet) throws Exception { Instances preparedDataSet = new Instances(dataSet); preparedDataSet.setClassIndex(-1); preparedDataSet = WekaTimeSeriesUtil.buildDataSetWithoutDates(preparedDataSet); preparedDataSet = WekaDataProcessingUtil.buildNumerizedDataSet(preparedDataSet); preparedDataSet = WekaDataProcessingUtil.buildNormalizedDataSet(preparedDataSet); final Instances newds; if (this.combo.getSelectedIndex() == 1) { newds = buildDerivatedDatasetForBestFeatures(preparedDataSet, this.slider.getValue()); } else { newds = buildDerivatedDatasetForFeaturesClusters(preparedDataSet, this.slider.getValue()); } if (this.mdsView != null) this.jxp.remove(this.mdsView); final int limitForCollapsing = 1000; final boolean normalized = false; //TODO checkbox final MDSResult mdsResult = ClassicMDS.doMDS(newds, MDSDistancesEnum.EUCLIDEAN, 2, limitForCollapsing, true, normalized); this.mdsView = MDSViewBuilder.buildMDSViewFromDataSet(newds, mdsResult, limitForCollapsing, new Listener<Instances>() { @Override public void onAction(final Instances parameter) { } }, FEATUREDESC_ATTRNAME); this.jxp.add(this.mdsView, BorderLayout.CENTER); if (this.cl != null) this.slider.removeChangeListener(this.cl); this.cl = new ChangeListener() { @Override public void stateChanged(final ChangeEvent e) { if (!slider.getValueIsAdjusting()) { try { update0(dataSet); } catch (Exception e1) { e1.printStackTrace(); } } } }; this.slider.addChangeListener(this.cl); if (this.al != null) this.combo.removeActionListener(this.al); this.al = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { update0(dataSet); } catch (Exception e1) { e1.printStackTrace(); } } }; this.combo.addActionListener(this.al); this.jxp.updateUI(); }
From source file:lu.lippmann.cdb.datasetview.tabs.UnsupervisedFeatureEvaluationTabView.java
License:Open Source License
private static Instances buildDerivatedDataset(final Instances dataSet, final List<String> possibleValues, final List<Integer> valueForEachFeature) throws Exception { final int numInstances = dataSet.numInstances(); final ArrayList<Attribute> attrs = new ArrayList<Attribute>(numInstances + 2); attrs.add(new Attribute(FEATUREDESC_ATTRNAME, (java.util.List<String>) null)); for (int i = 0; i < numInstances; i++) { attrs.add(new Attribute(i + "_eval")); }//from w w w .j av a 2 s .co m attrs.add(new Attribute("__", possibleValues)); final Instances newds = new Instances("unsupervisedFeaturesEval", attrs, 0); final int numAttributes = dataSet.numAttributes(); for (int j = 0; j < numAttributes; j++) { double[] val = ArraysUtil.concat(dataSet.attributeToDoubleArray(j), new double[] { 0.0d }); val = ArraysUtil.concat(new double[] { 0.0d }, val); newds.add(new DenseInstance(1.0d, val)); } for (int j = 0; j < numAttributes; j++) { newds.instance(j).setValue(0, dataSet.attribute(j).name()); newds.instance(j).setValue(numInstances + 1, possibleValues.get(valueForEachFeature.get(j))); } newds.setClassIndex(numInstances + 1); return newds; }
From source file:lu.lippmann.cdb.ext.hydviga.data.StationsDataProvider.java
License:Open Source License
private Instances getDataSetForMap(final Collection<String> sel, final Collection<String> usable) { final Instances ds = new Instances("ds", new ArrayList<Attribute>(), 0); ds.insertAttributeAt(new Attribute("name", new ArrayList<String>(this.coordinatesMap.keySet())), ds.numAttributes());// w w w . j a v a2 s . c o m ds.insertAttributeAt(new Attribute("x"), ds.numAttributes()); ds.insertAttributeAt(new Attribute("y"), ds.numAttributes()); ds.insertAttributeAt( new Attribute("status", Arrays.asList(new String[] { SELECTED_STATUS, USABLE_STATUS, NOT_USABLE_STATUS })), ds.numAttributes()); ds.setClassIndex(ds.numAttributes() - 1); final Set<String> coordSelected = new HashSet<String>(); for (final String ssel : sel) { final String coordsKey = coordinatesMap.get(ssel)[0] + "-" + coordinatesMap.get(ssel)[1]; coordSelected.add(coordsKey); } final Set<String> coordUsable = new HashSet<String>(); for (final String uu : usable) { final String coordsKey = coordinatesMap.get(uu)[0] + "-" + coordinatesMap.get(uu)[1]; coordUsable.add(coordsKey); } final Set<String> coordAlreadyLoaded = new HashSet<String>(); for (final Map.Entry<String, double[]> entry : this.coordinatesMap.entrySet()) { final String coordsKey = entry.getValue()[0] + "-" + entry.getValue()[1]; if (coordAlreadyLoaded.contains(coordsKey)) continue; final Instance inst = new DenseInstance(1.0d, new double[] { 0d, 0d, 0d, 0d }); inst.setDataset(ds); inst.setValue(0, entry.getKey()); inst.setValue(1, entry.getValue()[0]); inst.setValue(2, entry.getValue()[1]); //System.out.println(sel+" "+entry.getKey()); inst.setValue(3, (coordSelected.contains(coordsKey)) ? SELECTED_STATUS : ((coordUsable.contains(coordsKey)) ? USABLE_STATUS : NOT_USABLE_STATUS)); ds.add(inst); coordAlreadyLoaded.add(coordsKey); } return ds; }
From source file:lu.lippmann.cdb.ext.hydviga.gaps.GapFillerClassifier.java
License:Open Source License
/** * {@inheritDoc}// w ww . ja v a 2 s .c o m */ @Override Instances fillGaps0(final Instances ds) throws Exception { final Instances newds = WekaDataProcessingUtil.buildDataSetWithoutConstantAttributes(ds); final int attrWithMissingIdx = WekaDataStatsUtil.getFirstAttributeWithMissingValue(newds); if (attrWithMissingIdx == -1) throw new IllegalStateException(); final Instances trainingSet = new Instances(newds, 0); for (int i = 0; i < newds.numInstances(); i++) { if (!newds.instance(i).hasMissingValue()) trainingSet.add(newds.instance(i)); } //System.out.println(trainingSet); trainingSet.setClassIndex(attrWithMissingIdx); //System.out.println("Training (size="+trainingSet.numInstances()+") ..."); this.classifier.buildClassifier(trainingSet); //System.out.println("... trained!"); newds.setClassIndex(attrWithMissingIdx); for (int i = 0; i < newds.numInstances(); i++) { if (newds.instance(i).isMissing(attrWithMissingIdx)) { final Instance newrecord = new DenseInstance(newds.instance(i)); newrecord.setDataset(newds); final double newval = this.classifier.classifyInstance(newrecord); newds.instance(i).setValue(attrWithMissingIdx, newval); } } //System.out.println("initial -> "+ds.toSummaryString()); //System.out.println("corrected -> "+newds.toSummaryString()); this.model = this.classifier.toString(); return newds; }
From source file:lu.lippmann.cdb.ext.hydviga.ui.GapsUIUtil.java
License:Open Source License
public static ChartPanel buildGapChartPanelWithCorrection(final Instances pdataSet, final int dateIdx, final Attribute attr, final int gapsize, final int position, final GapFiller gapFiller, final java.util.Collection<String> attrs) throws Exception { final Instances dataSetWithTheGap = new Instances(pdataSet); for (int i = position; i < position + gapsize; i++) dataSetWithTheGap.instance(i).setMissing(attr); int[] arr = new int[] { attr.index(), dateIdx }; for (final String sss : attrs) { arr = ArraysUtil.concat(arr, new int[] { dataSetWithTheGap.attribute(sss).index() }); }/*from w ww. java 2s .c o m*/ Instances filteredDsWithTheGap = WekaDataProcessingUtil.buildFilteredByAttributesDataSet(dataSetWithTheGap, arr); filteredDsWithTheGap = WekaDataProcessingUtil.buildFilteredDataSet(filteredDsWithTheGap, 0, filteredDsWithTheGap.numAttributes() - 1, Math.max(0, position - GapsUtil.VALUES_BEFORE_AND_AFTER_RATIO * gapsize), Math.min(position + gapsize + GapsUtil.VALUES_BEFORE_AND_AFTER_RATIO * gapsize, filteredDsWithTheGap.numInstances() - 1)); final Instances completedds = gapFiller.fillGaps(filteredDsWithTheGap); final Instances diff = WekaTimeSeriesUtil.buildDiff(filteredDsWithTheGap, completedds); Instances filteredDsWithoutTheGap = WekaDataProcessingUtil.buildFilteredByAttributesDataSet(pdataSet, arr); filteredDsWithoutTheGap = WekaDataProcessingUtil.buildFilteredDataSet(filteredDsWithoutTheGap, 0, filteredDsWithoutTheGap.numAttributes() - 1, Math.max(0, position - GapsUtil.VALUES_BEFORE_AND_AFTER_RATIO * gapsize), Math.min(position + gapsize + GapsUtil.VALUES_BEFORE_AND_AFTER_RATIO * gapsize, filteredDsWithoutTheGap.numInstances() - 1)); diff.insertAttributeAt(new Attribute(attr.name() + "_orig"), diff.numAttributes()); for (int i = 0; i < filteredDsWithoutTheGap.numInstances(); i++) { diff.instance(i).setValue(diff.numAttributes() - 1, filteredDsWithoutTheGap.instance(i).value(filteredDsWithoutTheGap.attribute(attr.name()))); } //System.out.println(attr.name()+"\n"+diff.toSummaryString()); final java.util.List<String> toRemove = new java.util.ArrayList<String>(); for (int j = 0; j < diff.numAttributes(); j++) { final String consideredAttrName = diff.attribute(j).name(); if (!consideredAttrName.contains("timestamp") && !consideredAttrName.contains(attr.name())) toRemove.add(consideredAttrName); } diff.setClassIndex(-1); for (final String ssss : toRemove) diff.deleteAttributeAt(diff.attribute(ssss).index()); //System.out.println(attr.name()+"\n"+diff.toSummaryString()); final ChartPanel cp = TimeSeriesChartUtil.buildChartPanelForAllAttributes(diff, false, WekaDataStatsUtil.getFirstDateAttributeIdx(diff), null); final XYPlot xyp = (XYPlot) cp.getChart().getPlot(); xyp.getDomainAxis().setLabel(""); xyp.getRangeAxis().setLabel(""); final Marker gapBeginMarker = new ValueMarker( dataSetWithTheGap.instance(Math.max(0, position - 1)).value(dateIdx)); gapBeginMarker.setPaint(Color.RED); gapBeginMarker.setLabel("Gap begin"); gapBeginMarker.setLabelAnchor(RectangleAnchor.TOP_LEFT); gapBeginMarker.setLabelTextAnchor(TextAnchor.TOP_RIGHT); cp.getChart().getXYPlot().addDomainMarker(gapBeginMarker); final Marker gapEndMarker = new ValueMarker(dataSetWithTheGap .instance(Math.min(dataSetWithTheGap.numInstances() - 1, position + gapsize)).value(dateIdx)); gapEndMarker.setPaint(Color.RED); gapEndMarker.setLabel("Gap end"); gapEndMarker.setLabelAnchor(RectangleAnchor.TOP_RIGHT); gapEndMarker.setLabelTextAnchor(TextAnchor.TOP_LEFT); cp.getChart().getXYPlot().addDomainMarker(gapEndMarker); addExportPopupMenu(diff, cp); return cp; }
From source file:lu.lippmann.cdb.ext.hydviga.ui.HydroDatasetView.java
License:Open Source License
public HydroDatasetView setDataSet(final Instances pdataSet) { if (pdataSet.classIndex() != -1 && !pdataSet.classAttribute().isNominal()) pdataSet.setClassIndex(-1); if (this.initialDataSet == null) { this.initialDataSet = pdataSet; this.initialCompleteness = new CompletenessComputer(this.initialDataSet); this.dataCompletenessProgressBar.setMaximum(pdataSet.numInstances() * pdataSet.numAttributes()); reinitDataCompleteness();//from w ww . j av a2 s . c o m } this.dataSet = pdataSet; if (!filtered) this.notFilteredDataSet = pdataSet; //updateClassSelectionMenu(); this.supervisedTransformPane.setVisible(pdataSet.classIndex() != -1); for (final TabView tv : tabViews) { tv.update(dataSet); } try { updateFiltersPane(dataSet); } catch (Exception e) { eventPublisher.publish(new ErrorOccuredEvent("Error when updating filters", e)); } updateTooltipShowingDatasetDimensions(); return this; }
From source file:lu.lippmann.cdb.lab.beta.shih.Shih2010.java
License:Open Source License
/** * //from www .j a v a 2s . c o m * @return */ public Instances getModifiedInstances() { //Copy attribute list (and change categorical by numerical) final ArrayList<Attribute> lAttrs = new ArrayList<Attribute>(); for (int i = 0; i < instances.numAttributes(); i++) { Attribute attr = instances.attribute(i); if (attr.isNumeric() || attr.index() == instances.classIndex()) { lAttrs.add(attr); } else { Attribute newAttr = new Attribute(attr.name()); lAttrs.add(newAttr); } } //Build new instance final Instances newInstances = new Instances("Shih instance", lAttrs, instances.numInstances()); newInstances.setClassIndex(instances.classIndex()); for (int i = 0; i < instances.numInstances(); i++) { final Instance instance = instances.instance(i); final Instance cpyInstance = (Instance) instance.copy(); for (int j = 0; j < instance.numAttributes(); j++) { Attribute attribute = instance.attribute(j); int k = 0; if (attribute.index() == instances.classIndex()) { //The class index is nominal cpyInstance.setValue(attribute, instance.stringValue(j)); } else if (!attribute.isNumeric()) { String elt = attribute.value((int) instance.value(j)); cpyInstance.setValue(attribute, F.get(new TupleSI(elt, j))); } else { if (maxNum[k] > 1) { cpyInstance.setValue(attribute, instance.value(j) / maxNum[k]); } k++; } } newInstances.add(cpyInstance); } if (ignoreClass && instances.classIndex() != -1) { newInstances.deleteAttributeAt(instances.classIndex()); } return newInstances; }