⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 userclassifier.java

📁 :<<数据挖掘--实用机器学习技术及java实现>>一书的配套源程序
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
     * This function gets called to set the node to use a linear regression     * and attribute filter.     * @exception If can't set a default linear egression model.     */    private void setLinear() throws Exception {      //then set default behaviour for node.      //set linear regression combined with attribute filter            //find the attributes used for splitting.      boolean[] attributeList = new boolean[m_training.numAttributes()];      for (int noa = 0; noa < m_training.numAttributes(); noa++) {	attributeList[noa] = false;      }            TreeClass temp = this;      attributeList[m_training.classIndex()] = true;      while (temp != null) {	attributeList[temp.m_attrib1] = true;	attributeList[temp.m_attrib2] = true;	temp = temp.m_parent;      }      int classind = 0;                  //find the new class index      for (int noa = 0; noa < m_training.classIndex(); noa++) {	if (attributeList[noa]) {	  classind++;	}      }      //count how many attribs were used      int count = 0;      for (int noa = 0; noa < m_training.numAttributes(); noa++) {	if (attributeList[noa]) {	  count++;	}      }                  //fill an int array with the numbers of those attribs      int[] attributeList2 = new int[count];      count = 0;      for (int noa = 0; noa < m_training.numAttributes(); noa++) {	if (attributeList[noa]) {	  attributeList2[count] = noa;	  count++;	}      }            m_filter = new AttributeFilter();      ((AttributeFilter)m_filter).setInvertSelection(true);      ((AttributeFilter)m_filter).setAttributeIndicesArray(attributeList2);      m_filter.setInputFormat(m_training);            Instances temp2 = Filter.useFilter(m_training, m_filter);      temp2.setClassIndex(classind);      m_classObject = new LinearRegression();      m_classObject.buildClassifier(temp2);                      }            /**     * Call to find out if an instance is in a polyline.     * @param ob The polyline to check.     * @param x The value of attribute1 to check.     * @param y The value of attribute2 to check.     * @return True if inside, false if not.     */    private boolean inPolyline(FastVector ob, double x, double y) {      //this works similar to the inPoly below except that      //the first and last lines are treated as extending infinite       //in one direction and       //then infinitly in the x dirction their is a line that will       //normaly be infinite but      //can be finite in one or both directions            int countx = 0;      double vecx, vecy;      double change;      double x1, y1, x2, y2;            for (int noa = 1; noa < ob.size() - 4; noa+= 2) {	y1 = ((Double)ob.elementAt(noa+1)).doubleValue();	y2 = ((Double)ob.elementAt(noa+3)).doubleValue();	x1 = ((Double)ob.elementAt(noa)).doubleValue();	x2 = ((Double)ob.elementAt(noa+2)).doubleValue();	vecy = y2 - y1;	vecx = x2 - x1;	if (noa == 1 && noa == ob.size() - 6) {	  //then do special test first and last edge	  if (vecy != 0) {	    change = (y - y1) / vecy;	    if (vecx * change + x1 >= x) {	      //then intersection	      countx++;	    }	  }	  	  	}	else if (noa == 1) {	  if ((y < y2 && vecy > 0) || (y > y2 && vecy < 0)) {	    //now just determine intersection or not	    change = (y - y1) / vecy;	    if (vecx * change + x1 >= x) {	      //then intersection on horiz	      countx++;	    }	  }	}	else if (noa == ob.size() - 6) {	  //then do special test on last edge	  if ((y <= y1 && vecy < 0) || (y >= y1 && vecy > 0)) {	    change = (y - y1) / vecy;	    if (vecx * change + x1 >= x) {	      countx++;	    }	  }	  	}	else if ((y1 <= y && y < y2) || (y2 < y && y <= y1)) {	  //then continue tests.	  if (vecy == 0) {	    //then lines are parallel stop tests in 	    //ofcourse it should never make it this far	  }	  else {	    change = (y - y1) / vecy;	    if (vecx * change + x1 >= x) {	      //then intersects on horiz	      countx++;	    }	  }	}	      }            //now check for intersection with the infinity line      y1 = ((Double)ob.elementAt(ob.size() - 2)).doubleValue();      y2 = ((Double)ob.elementAt(ob.size() - 1)).doubleValue();            if (y1 > y2) {	//then normal line	if (y1 >= y && y > y2) {	  countx++;	}      }      else {	//then the line segment is inverted	if (y1 >= y || y > y2) {	  countx++;	}      }                  if ((countx % 2) == 1) {	return true;      }      else {	return false;      }                }            /**      * Call this to determine if an instance is in a polygon.     * @param ob The polygon.     * @param x The value of attribute 1.     * @param y The value of attribute 2.     * @return True if in polygon, false if not.     */    private boolean inPoly(FastVector ob, double x, double y) {      int count = 0;      double vecx, vecy;      double change;      double x1, y1, x2, y2;      for (int noa = 1; noa < ob.size() - 2; noa += 2) {	y1 = ((Double)ob.elementAt(noa+1)).doubleValue();	y2 = ((Double)ob.elementAt(noa+3)).doubleValue();	if ((y1 <= y && y < y2) || (y2 < y && y <= y1)) {	  //then continue tests.	  vecy = y2 - y1;	  if (vecy == 0) {	    //then lines are parallel stop tests for this line	  }	  else {	    x1 = ((Double)ob.elementAt(noa)).doubleValue();	    x2 = ((Double)ob.elementAt(noa+2)).doubleValue();	    vecx = x2 - x1;	    change = (y - y1) / vecy;	    if (vecx * change + x1 >= x) {	      //then add to count as an intersected line	      count++;	    }	  }	  	}      }      if ((count % 2) == 1) {	//then lies inside polygon	//System.out.println("in");	return true;      }      else {	//System.out.println("out");	return false;      }      //System.out.println("WHAT?!?!?!?!!?!??!?!");      //return false;    }        /**     * Goes through the tree structure recursively and returns the node that     * has the id.     * @param id The node to find.     * @return The node that matches the id.     */    public TreeClass getNode(String id) {      //returns the treeclass object with the particular ident      if (id.equals(m_identity)) {	return this;      }            if (m_set1 != null) {	TreeClass tmp = m_set1.getNode(id);	if (tmp != null) {	  return tmp;	}      }      if (m_set2 != null) {	TreeClass tmp = m_set2.getNode(id);	if (tmp != null) {	  return tmp;	}      }      return null;    }            /**     * Returns a string containing a bit of information about this node, in      * alternate form.     * @param s The string buffer to fill.     * @exception Exception if can't create label.     */    public void getAlternateLabel(StringBuffer s) throws Exception {            //StringBuffer s = new StringBuffer();            FastVector tmp = (FastVector)m_ranges.elementAt(0);            if (m_classObject != null && m_training.classAttribute().isNominal()) {	s.append("Classified by " + m_classObject.getClass().getName());      }      else if (((Double)tmp.elementAt(0)).intValue() == LEAF) {	if (m_training.classAttribute().isNominal()) {	  double high = -1000;	  int num = 0;	  double count = 0;	  for (int noa = 0; noa < m_training.classAttribute().numValues();	       noa++) {	    if (((Double)tmp.elementAt(noa + 1)).doubleValue() > high) {	      high = ((Double)tmp.elementAt(noa + 1)).doubleValue();	      num  = noa + 1;	    }	    count += ((Double)tmp.elementAt(noa + 1)).doubleValue();	  }	  s.append(m_training.classAttribute().value(num-1) + "(" + count);	  if (count > high) {	    s.append("/" + (count - high));	  }	  s.append(")");	}	else {	  if (m_classObject == null 	      && ((Double)tmp.elementAt(0)).intValue() == LEAF) {	    setLinear();	  }	  s.append("Standard Deviation = " 		   + Utils.doubleToString(((Double)tmp.elementAt(1))					  .doubleValue(), 6));	  	}      }      else {	s.append("Split on ");	s.append(m_training.attribute(m_attrib1).name() + " AND ");	s.append(m_training.attribute(m_attrib2).name());		      }            //return s.toString();    }            /**     * Returns a string containing a bit of information about this node.     * @param s The stringbuffer to fill.     * @exception Exception if can't create label.     */    public void getLabel(StringBuffer s) throws Exception {      //for now just return identity      //StringBuffer s = new StringBuffer();            FastVector tmp = (FastVector)m_ranges.elementAt(0);                  if (m_classObject != null && m_training.classAttribute().isNominal()) {	s.append("Classified by\\n" + m_classObject.getClass().getName());      }      else if (((Double)tmp.elementAt(0)).intValue() == LEAF) {		if (m_training.classAttribute().isNominal()) {	  boolean first = true;	  for (int noa = 0; noa < m_training.classAttribute().numValues(); 	       noa++) {	    if (((Double)tmp.elementAt(noa + 1)).doubleValue() > 0) {	      if (first)		{		  s.append("[" + m_training.classAttribute().value(noa));		  first = false;		}	      else		{		  s.append("\\n[" + m_training.classAttribute().value(noa));		}	      s.append(", " + ((Double)tmp.elementAt(noa + 1)).doubleValue() 		       + "]");	    }      	  }	}	else {	  if (m_classObject == null 	      && ((Double)tmp.elementAt(0)).intValue() == LEAF) {	    setLinear();	  }	  s.append("Standard Deviation = " 		   + Utils.doubleToString(((Double)tmp.elementAt(1))		   .doubleValue(), 6));	}      }      else {	s.append("Split on\\n");	s.append(m_training.attribute(m_attrib1).name() + " AND\\n");	s.append(m_training.attribute(m_attrib2).name());      }      //return s.toString();    }    /**     * Converts The tree structure to a dotty string.     * @param t The stringbuffer to fill with the dotty structure.     * @exception Exception if can't convert structure to dotty.     */    public void toDotty(StringBuffer t) throws Exception {      //this will recursively create all the dotty info for the structure      t.append(m_identity + " [label=\"");      getLabel(t);      t.append("\" ");      //System.out.println(((Double)((FastVector)ranges.elementAt(0)).      //elementAt(0)).intValue() + " A num ");      if (((Double)((FastVector)m_ranges.elementAt(0)).elementAt(0)).intValue()	  == LEAF) {	t.append("shape=box ");      }      else {	t.append("shape=ellipse ");      }      t.append("style=filled color=gray95]\n");            if (m_set1 != null) {	t.append(m_identity + "->");	t.append(m_set1.m_identity + " [label=\"True\"]\n");//the edge for 	//the left	m_set1.toDotty(t);      }      if (m_set2 != null) {	t.append(m_identity + "->");	t.append(m_set2.m_identity + " [label=\"False\"]\n"); //the edge for 	//the 	//right	m_set2.toDotty(t);      }          }        /**     * This will append the class Object in the tree to the string buffer.     * @param t The stringbuffer.     */    public void objectStrings(StringBuffer t) {            if (m_classObject != null) {	t.append("\n\n" + m_identity +" {\n" + m_classObject.toString()+"\n}");      }      if (m_set1 != null) {	m_set1.objectStrings(t);      }      if (m_set2 != null) {	m_set2.objectStrings(t);      }    }        /**     * Converts the tree structure to a string. for people to read.     * @param l How deep this node is in the tree.     * @param t The stringbuffer to fill with the string.     * @exception Exception if can't convert th string.     */    public void toString(int l, StringBuffer t) throws Exception {                  if (((Double)((FastVector)m_ranges.elementAt(0)).elementAt(0)).intValue()	  == LEAF) {	t.append(": " + m_identity + " ");	getAlternateLabel(t);      }      if (m_set1 != null) {	t.append("\n");	for (int noa = 0; noa < l; noa++) {	  t.append("|   ");	  	}	getAlternateLabel(t);	t.append(" (In Set)");	m_set1.toString(l+1, t);      }      if (m_set2 != null) {	t.append("\n");	for (int noa = 0; noa < l; noa++) {	  t.append("|   ");	}	getAlternateLabel(t);	t.append(" (Not in Set)");	m_set2.toString(l+1, t);      }      //return t.toString();    }      }      }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -