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

📄 schema.java

📁 决策树分类中经典算法的ID3和C4.5代码公共包!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package shared;
import java.util.*;
import java.lang.*;
import java.io.*;

/** Schema defines the attribute information. Most effort was directed at labelled
 * instances for supervised learning, and on discrete attributes.
 * @author James Louis Java Implementation.
 * @author Dan Sommerfield 2/21/97 Added loss functions.
 * @author Robert Allen Added project(). 1/27/95
 * @author Chia-Hsin Li Added display_names(). 9/29/94
 * @author Yeogirl Yun Merged LabelledInstanceInfo and InstanceInfo into Schema
 * and made it reference-count class. 6/12/94
 * @author Richard Long 5/27/94 AttrInfo stored as Array instead of linked list.
 * @author Richard Long 1/21/94 Added weighted option.
 * @author Svetlozar Nestorov 1/8/94 Added copy constructors.
 * @author Ronny Kohavi 8/18/93 Change equal to non-virtual, eliminated
 * combinations of LabelledInstanceInfo and InstanceInfo for equal and operator=.
 * See coding standards for explanations.
 * @author Richard Long 7/14/93 Initial revision (.c)
 * @author Ronny Kohavi 7/13/93 Initial revision (.h)
 *
 */
public class Schema
{
    /** Array of information on attributes.
     */    
   AttrInfo[] attr;
   /** Information on labels.
    */   
   AttrInfo labelInfo;
   /** The number of references to this Schema.
    */   
   int refCount;
   /** Loss matrix for this schema.
    */   
   double[][] lossMatrix;
   /** TRUE if the Lossmatrix should be transposed with the attribute information.
    */   
   boolean transposeLossMatrix = false;
   /** Constructor.
    * @param attrInfos	The information about each attribute to be
    * stored in this Schema.
    */
   public Schema(LinkedList attrInfos)
   {
      attr = new AttrInfo[attrInfos.size()];
      lossMatrix = null;
      refCount = 1;
  
       
      labelInfo = null;
      ListIterator pix = attrInfos.listIterator(0);
      for (int i=0; i < attrInfos.size(); i++){
         if(pix.hasNext())attr[i] = (AttrInfo)pix.next();  
         if(attr[i] == null)
            Error.err("Schema::Schema:attribute info "
               + i + "is NULL --> fatal_error");
      }
      attrInfos = null;
      OK(); 
   }
 
   /** Constructor.
    * @param attrInfos	The information about the each attribute to be
    * stored in this Schema.
    * @param lInfo		The information about the labels for each
    * catagory.
    */
   public Schema(LinkedList attrInfos,AttrInfo lInfo)
   {  
      attr = new AttrInfo[attrInfos.size()];
      lossMatrix = null;
      refCount = 1;
   
      if(lInfo == null)
         Error.err("Error-->Schema::Schema: Null labelInfo not"
            +" not allowed.  Use the constructor which does not require"
            +" a labelInfo argument, fatal_error");

      labelInfo = lInfo;
      ListIterator pix = attrInfos.listIterator(0); //(1) CHANGE
      for(int i=0; i< attrInfos.size();i++) {
         attr[i] = (AttrInfo)pix.next();
         if(attr[i] == null)
            Error.err("Schema::Schema: attribute info "
               +i+"is Null, fatal_error");
      }

      attrInfos = null;
      lInfo = null;
      
      OK(); 
   }

   /** Copy Constructor.
    * @param source The Schema object to be copied.
    * @throws CloneNotSupportedException if the cloning process for the new Schema experiences an Exception.
    */
   public Schema(Schema source) throws CloneNotSupportedException 
   {
      attr = new AttrInfo[source.num_attr()];
      //instanceOp = null;
      lossMatrix = null;
      refCount = 1;

      for(int i=0;i < source.attr.length; i++) {
         AttrInfo nextAttr = (AttrInfo)source.attr_info(i).clone(); //clone();
	 attr[i] = nextAttr;
      }
      
      if(source.is_labelled())
         labelInfo = (AttrInfo)source.labelInfo.clone(); //clone();
      else
         labelInfo = null;
   
      if(source.lossMatrix != null)
         lossMatrix = source.lossMatrix;

      //copy instance operation, if there is one
      //if(source.instanceOp)
      //   instanceOp = source.instanceOp; //clone();
	 
      //DBG(OK());
   }

   /** Checks if this Schema object has a loss matrix.
    * @return TRUE if there is a loss matrix, FALSE otherwise.
    */
   public boolean has_loss_matrix(){return lossMatrix != null;}
   
   /** Returns the loss matrix for this Schema object.
    * @return The loss matrix.
    */
   public double[][] get_loss_matrix()
   {
      return lossMatrix;
   }
   
   /** Sets the loss matrix to the matrix specified.
    * @param aLossMatrix	The new matrix to be stored in this Schema
    * object.
    */
   public void set_loss_matrix(double[][] aLossMatrix)
   {
      establish_loss_matrix();
      lossMatrix = aLossMatrix;

      //some error checking here !
   }

   /** Allocates the loss matrix for this Schema object. The Schema must have
    * label information or an error message will be displayed.
    */
   public void establish_loss_matrix()
   {
      if(!is_labelled())
         Error.err("Schema::establish_loss_matrix: the"
	    +" data must be labelled -->fatal_error");

      //The dimensions of the lossmatrix in MLC++ are [-1..num_label_values]
      //[0..num_label_values + 1].  So we will make these [0..nlv][0..nlv+1]
      if(lossMatrix == null)
         lossMatrix = new double[num_label_values()][num_label_values()+1];
   
      //left out some stuff about dimensions of 2D array.  See Schema.c 
   }

   /** Creates a new Schema object which contains only the specified attributes.
    * @return The new Schema created.
    * @param attrMask A boolean array where each element represents an attribute
    * in the Schema. It should have have the same number of
    * elements as attributes. Elements set to true will result in
    * the corresponding attribute being included in the new
    * Schema.
    * @throws CloneNotSupportedException if the cloning process for the new Schema experiences an Exception.
    */
   public Schema project(boolean[] attrMask) throws CloneNotSupportedException
   {
      //DBG(OK());
      //ASSERT(attrMask.size() == num_attr());
      LinkedList attrInfos = new LinkedList();
      int newnum = 0;
      for(int i=0;i<num_attr();i++) {
         if(attrMask[i]) {
	    AttrInfo ai = (AttrInfo)attr_info(i).clone();  //clone()
	    newnum++;
	    attrInfos.add(ai);
	 }
      }

      if(is_labelled()) {
         AttrInfo labelInfo = label_info(); //clone()
	 Schema newSchema = new Schema(attrInfos, labelInfo);

	 //if the previous schema had a loss matrix, set it inside the new
	 //shema too
	 if(has_loss_matrix())
	    newSchema.set_loss_matrix(get_loss_matrix());

	 //newSchema gets ownership
	 //ASSERT(attrInfos == null); ASSERT(labelInfo == null);
	 return newSchema;
      }
      else{
         Schema newSchema = new Schema(attrInfos);
	 //ASSERT(attrInfos == null);
	 return newSchema;
      }
   }

   /** Casts the specified attribute to a nominal attribute.
    * @return The NominalAttrInfo object of the specified attribute.
    * @param attrNum	The index number of the specified attribute.
    */
   public NominalAttrInfo nominal_attr_info(int attrNum)
   {
      return attr_info(attrNum).cast_to_nominal();
   }

   /** Validate assertions.
    */
   private void OK()
   {
      int level = 0; 
      // ASSERT refCount >= 0
      check_duplicate_attrs();

      if (level <1) {
         for(int i=0; i< attr.length; i++)
            if (attr[i]==null)
               Error.err("Shema::OK: attribute info "
                  + i + "is NULL --> fatal_error");
      }

      if(lossMatrix != null){
         //ASSERT is_labelled() == true 
         int numLabelVals = num_label_values();
         //ASSERT(lossMatrix->start_row() == FIRST_CATEGORY_VAL);
         //ASSERT(lossMatrix->start_col() == UNKNOWN_CATEGORY_VAL);
         //ASSERT(lossMatrix->num_rows() == numLabelVals);
         //ASSERT(lossMatrix->num_cols() == numLabelVals + 1);
      } 
   }

   /** Returns the number of values possible for the specified attribute.
    * @return The number of values.
    * @param attrNum	The index number of the specified attribute.
    */
   public int num_attr_values(int attrNum)
   {
      return nominal_attr_info(attrNum).num_values();
   }


   /** Returns the number of classification labels possible for this Schema.
    * @return The number of labels.
    */
   public int num_label_values()
   {
      return nominal_label_info().num_values();
   }
   
   /** Casts the label information to a nominal attribute.
    * @return The NominalAttrInfo containing the label information.
    */
   public NominalAttrInfo nominal_label_info()
   {
      return label_info().cast_to_nominal();
   }

   /** Returns the label information.
    * @return The AttrInfo containing the label information.
    */
   public AttrInfo label_info()
   {
      if (!is_labelled())
         Error.err("Schema::label_info() : labelInfo is "
            + "NULL -->fatal_error");
      return labelInfo; 
   }
   
   /** Checks if this Schema has label information.
    * @return TRUE if label information is present, FALSE otherwise.
    */
   public boolean is_labelled()
   {
      return is_labelled(false);
   }

   /** Checks if this Schema has label information.
    * @return TRUE if label information is present, FALSE otherwise.
    * @param fatalOnFalse	If set to TRUE, an error message will be displayed if
    * there is no label information in this Schema.

⌨️ 快捷键说明

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