List of usage examples for weka.core Instance value
public double value(Attribute att);
From source file:milk.classifiers.MINND.java
License:Open Source License
/** * Scale the given exemplar so that the returned exemplar * has the value of 0 to 1 for each dimension * //from www .ja va 2s. c o m * @param before the given exemplar * @return the resultant exemplar after scaling * @exception if given exampler cannot be scaled properly */ private Exemplar scale(Exemplar before) throws Exception { Instances data = before.getInstances(); Exemplar after = new Exemplar(before, 0); for (int i = 0; i < data.numInstances(); i++) { Instance datum = data.instance(i); Instance inst = (Instance) datum.copy(); int k = 0; for (int j = 0; j < data.numAttributes(); j++) { if ((j != before.idIndex()) && (j != before.classIndex())) { if (data.attribute(j).isNumeric()) inst.setValue(j, (datum.value(j) - m_MinArray[k]) / (m_MaxArray[k] - m_MinArray[k])); k++; } } after.add(inst); } return after; }
From source file:milk.core.Exemplar.java
License:Open Source License
/** * Constructor using one instance to form an exemplar * //from w ww. j a v a2s . c o m * @param instance the given instance * @param id the ID index */ public Exemplar(Instance inst, int id) { m_IdIndex = id; m_IdValue = inst.value(id); m_ClassIndex = inst.classIndex(); m_ClassValue = inst.classValue(); m_Instances = new Instances(inst.dataset(), 1); m_Instances.add(inst); }
From source file:milk.core.Exemplar.java
License:Open Source License
/** * Constructor creating an exemplar with the given dataset and the * given ID index//w w w . j a v a 2 s.c o m * * @param instances the instances from which the header * information is to be taken * @param id the index of the ID of the exemplar */ public Exemplar(Instances dataset, int id) throws Exception { m_IdIndex = id; m_ClassIndex = dataset.classIndex(); m_Instances = new Instances(dataset); if (!m_Instances.attribute(m_IdIndex).isNominal()) throw new Exception("The exempler's ID is not nominal!"); double idvalue = (m_Instances.firstInstance()).value(m_IdIndex); double clsvalue = (m_Instances.firstInstance()).classValue(); // The the validity of this exemplar for (int i = 1; i < m_Instances.numInstances(); i++) { Instance inst = m_Instances.instance(i); if ((!Utils.eq(inst.value(m_IdIndex), idvalue)) //|| (!Utils.eq(inst.classValue(), clsvalue)) ) throw new Exception("The Id value and/or class value is not unique!"); } m_IdValue = idvalue; m_ClassValue = clsvalue; }
From source file:milk.core.Exemplar.java
License:Open Source License
/** * Checks if the given instance is compatible with this * Exemplar. //from w w w.j a va 2 s . c o m * * @return true if the instance is compatible with the exemplar */ public final boolean checkInstance(Instance instance) { if (!m_Instances.checkInstance(instance)) return false; if ((!Utils.eq(instance.value(m_IdIndex), m_IdValue)) //|| (!Utils.eq(instance.classValue(), m_ClassValue)) ) return false; return true; }
From source file:milk.core.Exemplar.java
License:Open Source License
/** * Main method for testing this class -- just prints out a set * of Exemplars. Assume the ID index is 0. * * @param argv should contain one element: the name of an ARFF file *///from w w w .j a v a 2 s . co m public static void main(String[] args) { try { Reader r = null; if (args.length > 1) { throw (new Exception("Usage: Instances <filename>")); } else if (args.length == 0) { r = new BufferedReader(new InputStreamReader(System.in)); } else { r = new BufferedReader(new FileReader(args[0])); } Instances i = new Instances(r); i.setClassIndex(i.numAttributes() - 1); Attribute id = i.attribute(0); if (!id.isNominal()) throw new Exception("The first attribute is not nominal"); Exemplar[] egs = new Exemplar[id.numValues()]; for (int j = 0; j < egs.length; j++) egs[j] = null; for (int j = 0; j < i.numInstances(); j++) { Instance ins = i.instance(j); int idv = (int) ins.value(0); if (egs[idv] == null) egs[idv] = new Exemplar(ins, 0); else egs[idv].add(ins); } for (int j = 0; j < egs.length; j++) System.out.println(egs[j].toString()); } catch (Exception ex) { System.err.println(ex.getMessage()); } }
From source file:milk.core.Exemplars.java
License:Open Source License
/** * Adds one instance to one of the exemplars * * @param instance the instance to be added * @exception Exception if the instance cannot be added properly *//*from www .jav a2s . c o m*/ public final void add(Instance instance) { Instance ins = (Instance) instance.copy(); int idv = (int) ins.value(m_IdIndex); int x = 0; for (; x < m_Exemplars.size(); x++) { Exemplar ex = (Exemplar) m_Exemplars.elementAt(x); if (ex != null) { if ((int) (ex.idValue()) == idv) { if (!ex.checkInstance(instance)) throw new IllegalArgumentException("Instance not compatible " + "with the data"); ex.add(ins); break; } } } if (x == m_Exemplars.size()) { Exemplar ex = new Exemplar(ins, m_IdIndex); ex.setWeight(1.0); m_Exemplars.addElement(ex); } }
From source file:milk.visualize.DistributionPanel.java
License:Open Source License
public void setXIndex(int index) { Axis old = xIndex;/*from w w w. ja v a2s . c o m*/ xIndex = new Axis(); xIndex.index = index; if (insts != null) { if (insts.attribute(index).isNominal()) JOptionPane.showMessageDialog(this, "X Attribute cannot be nominal.", "Wrong X Attribute", JOptionPane.ERROR_MESSAGE); else { xIndex.minValue = xIndex.maxValue = insts.firstInstance().value(xIndex.index); for (int y = 1; y < insts.numInstances(); y++) { Instance ins = insts.instance(y); if (ins.value(xIndex.index) > xIndex.maxValue) xIndex.maxValue = ins.value(xIndex.index); else if (ins.value(xIndex.index) < xIndex.minValue) xIndex.minValue = ins.value(xIndex.index); } double var = insts.variance(index); if (var > 0.0) { setStdDev(Math.sqrt(var)); } change.firePropertyChange("XAxis", old, xIndex); } } }
From source file:milk.visualize.GeomPanel.java
License:Open Source License
/** * Set the y axis, then it fire the property change event * to inform (the MIPlot) that the y axis changes * * @param index the attribute index on y axis *//*w ww . j av a2 s . c om*/ public void setYIndex(int index) { Axis old = yIndex; yIndex = new Axis(); yIndex.index = index; if (insts != null) { yIndex.minValue = yIndex.maxValue = insts.firstInstance().value(yIndex.index); for (int y = 1; y < insts.numInstances(); y++) { Instance ins = insts.instance(y); if (ins.value(yIndex.index) > yIndex.maxValue) yIndex.maxValue = ins.value(yIndex.index); else if (ins.value(yIndex.index) < yIndex.minValue) yIndex.minValue = ins.value(yIndex.index); } change.firePropertyChange("YAxis", old, yIndex); } }
From source file:milk.visualize.GeomPanel.java
License:Open Source License
/** * Set the x axis, then it fire the property change event * to inform (the MIPlot) that the x axis changes * * @param index the attribute index on x axis *///from w w w.ja v a 2 s .c o m public void setXIndex(int index) { Axis old = xIndex; xIndex = new Axis(); xIndex.index = index; if (insts != null) { xIndex.minValue = xIndex.maxValue = insts.firstInstance().value(xIndex.index); for (int y = 1; y < insts.numInstances(); y++) { Instance ins = insts.instance(y); if (ins.value(xIndex.index) > xIndex.maxValue) xIndex.maxValue = ins.value(xIndex.index); else if (ins.value(xIndex.index) < xIndex.minValue) xIndex.minValue = ins.value(xIndex.index); } change.firePropertyChange("XAxis", old, xIndex); } }
From source file:milk.visualize.MIPlot2D.java
License:Open Source License
/** * Renders this component//w ww .java2 s . co m * @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 ///////////////////////////////// }