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

📄 instance.java

📁 java数据挖掘算法
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package shared;
import java.lang.*;
import java.io.*;

/** The class Instance provides instances with or without label and
 * support for describing instances, iterating through values, and looking at
 * the instance label. Most effort was directed at categorization of labelled
 * instances for supervised learning. Has been extended to tree-structured
 * attributes.
 */

public class Instance {
    
    /** The Schema for the data stored in this Instance. **/
    private Schema schema;
    /** The values for each Attribute stored in this Instance. **/
    public AttrValue[] values;
    /** The weight assigned to this Instance of data. **/
    private double weight;
    /** Count of the references to this Instance object.
     */
    private int refCount;
    /** The label this Instance is categorized as. **/
    private AttrValue labelValue;
    
    /** A string used for displaying purposes. **/
    private static final String INSTANCE_WRAP_INDENT = "   ";
    /** A string used for displaying purposes. **/
    private static final char END_OF_INSTANCE_LINE = '.';
    
    /** Cloning function for the Instance class. Provides a deep copy.
     * @return A copy of the Object.
     * @throws CloneNotSupportedException if the cloning process for contained data members encounters a
     * CloneNotSupportedException.
     */
    public Object clone() throws CloneNotSupportedException {
        Instance newClone = new Instance(schema);
        newClone.set_weight(weight);
        newClone.set_label((AttrValue)labelValue.clone());
        for(int i=0;i<schema.num_attr();i++)
            newClone.values[i] = (AttrValue)get_value(i).clone();
        
        return newClone;
    }
    
    /** Constructor for the Instance class.
     * @param newSchema	The Schema for the data stored in this Instance.
     */
    public Instance(Schema newSchema) {
        values = new AttrValue[newSchema.num_attr()];
        for(int i = 0; i < values.length; values[i++] = new AttrValue());
        refCount = 1;
        weight = 1.0; //default value
        schema = newSchema;
        //DBG(init_values())
        //DBG(OK())
    }
    
    /** Copy constructor for the Instance class.
     * @param source	The original Instance to be copied.
     */
    public Instance(Instance source) {
        schema = source.schema;
        values = source.values;
        labelValue = source.labelValue;
        refCount = 1;
        
        weight = source.weight;
        //      DBG(OK());
    }
    
    
    /** Returns the Schema of attributes for this Instance.
     * @return The Schema for this Instance.
     */
    public Schema get_schema(){return schema;}
    
    /** Returns the number of attributes for data in this Instance.
     * @return The number of attributes in this Instance.
     */
    public int num_attr(){return schema.num_attr();}
    
    /** Returns the label this Instance is categorized as.
     * @return The labelValue for this Instance.
     */
    public AttrValue get_label(){return labelValue;}
    
    /** Returns a copy with only the attributes included that are are indicated
     * by the mask. Schema.project() should be called to create the new Schema
     * for the Instance.
     * @return The new Instance with the attributes specified.
     * @param shortSchema	The Schema for the new Instance created.
     * @param attrMask		An array of boolean values equal in length to the
     * original number of attributes. Each element
     * relates to an attribute of the original
     * instance. If the element is set TRUE, the
     * corresponding attribute is included in the new
     * Instance.
     */
    public Instance project(Schema shortSchema, boolean[] attrMask) {
        //DBG(OK());
        //ASSERT(attrMask.size() == num_attr());
        
        if(get_schema().is_labelled() != shortSchema.is_labelled())
            Error.err("Instance.project: short schema "
            +" has different labelled status from this schema-->"
            +"fatal_error");
        
        Instance newInst = new Instance(shortSchema);
        int newnum = 0;
        for(int i=0;i<num_attr();i++) {
            if(attrMask[i]) {
                newInst.values[newnum] = values[i];
                newnum++;
            }
        }
        
        if(get_schema().is_labelled())
            newInst.set_label(get_label());
        
        //set the weight for this instance
        newInst.set_weight(get_weight());
        
        return newInst;
    }
    
    // reimplementation of overrided [] method
    /** Returns the attribute value at the specified attribute.
     * @return The attribute value stored for the attribute at that index.
     * @param index	The index number for the specified attribute.
     */
    public AttrValue get_value(int index) {
        return values[index];
    }
    
    /** Sets the Schema for this Instance.
     * @param schemaRC	The new Schema for this Instance.
     */
    public void set_schema(Schema schemaRC) //SchemaRC
    {
        if(schemaRC.num_attr() != schema.num_attr())
            Error.err("Instance::set_schema(): attribute "
            +"count does not match -->fatal_error");
        schema = schemaRC;
    }
    
    /** Sets the weight for this Instance.
     * @param wt	The new weight for this Instance.
     */
    public void set_weight(double wt) {
        weight = wt;
    }
    
    /** Sets the label for this Instance.
     * @param lvalue	The new label value for this Instance.
     */
    public void set_label(AttrValue lvalue) {
        if(!schema.is_labelled())
            System.out.println("Instance::set_label():labelInfo is not set ->fe");
        schema.label_info().check_in_range(lvalue);
        labelValue = lvalue;
    }
    
    /** Returns the weight for this Instance.
     * @return The weight for this Instance.
     */
    public double get_weight(){return weight;}
    
    /** Displays the Instance with labels.
     * @param displayWeight	TRUE if the weight for this Instance should be shown,
     * FALSE otherwise.
     * @param normalizeReals TRUE if the attribute values should be normalized,
     * FALSE otherwise.
     */
    public void display(boolean displayWeight, boolean normalizeReals) {
        display_unlabelled(displayWeight, normalizeReals);
        if(is_labelled()){
            if(schema.num_attr() > 0)
                System.out.print(", ");
            AttrInfo ai = schema.label_info();
            String data = ai.attrValue_to_string(labelValue);
            //if(protectChars)
            //   data = protect_chars(data);
            System.out.print(data);
        }
        System.out.println();
        
    }
    
    /** Displays the Instance without labels.
     * @param normalizeReal TRUE if the attribute values should be normalized,
     * FALSE otherwise.
     * @param displayWeight TRUE if the weight for this Instance should be shown,
     * FALSE otherwise.
     */
    public void display_unlabelled(boolean displayWeight, boolean normalizeReal) {
        String separator =  new String(", ");
        if(displayWeight)
            System.out.println(weight+separator);
        for(int attrNum = 0;attrNum<schema.num_attr();attrNum++){
            AttrInfo ai = schema.attr_info(attrNum);
            if(normalizeReal && ai.can_cast_to_real()){
                RealAttrInfo rai = ai.cast_to_real();
                if(rai.is_unknown(values[attrNum]))
                    System.out.print(AttrInfo.UNKNOWN_VAL_STR);
                else
                    System.out.print(rai.normalized_value(values[attrNum]));
                if(attrNum < schema.num_attr() -1)
                    System.out.print(separator);
            }
            else{
                //System.out.println("Values.length = " + values.length);
                //System.out.println("attrNum = "+attrNum);
                if(values[attrNum] == null)System.out.println("Error");
                String data = ai.attrValue_to_string(values[attrNum]);
                if(ai.can_cast_to_nominal())// && protectChars)
                    ;// data = protect_chars(data);
                System.out.print(data);
                if((attrNum < (schema.num_attr()-1))&&(!(data.equals(""))))
                    System.out.print(separator);
                
            }
        }
    }
    
    /** Checks if this Instance is labelled.
     * @returns TRUE if the Instance is labelled, FALSE otherwise.
     * @return TRUE if the Instance contains a label value, FALSE otherwise.
     */
    public boolean is_labelled() {
        boolean fatalOnFalse = false;
        return schema.is_labelled();
    }
    
    /** Checks if this Instance is labelled.
     * @return TRUE if the Instance is labelled, FALSE otherwise.
     * @param fatalOnFalse TRUE if an Error message should be displayed if there
     * is no label for this Instance, FALSE otherwise.
     */
    public boolean is_labelled(boolean fatalOnFalse) {
        return schema.is_labelled(fatalOnFalse);
    }
    
    
    /** Returns the information about the label this Instance is categorized as.
     * @return The information about the label value.
     */
    public AttrInfo label_info() {
        if (!schema.is_labelled())
            Error.fatalErr("Instance::label_info(): label is not set ");
        return schema.label_info();
    }
    
    /** Transfers this Instance to a String value.
     * @return A String with representations of the data stored in this Instance.
     */
    public String toString() {
        boolean displayWeight = false;
        boolean normalizeReals = false;
        String rtrn = "";
        String separator =  new String(", ");
        if(displayWeight)
            rtrn = rtrn + weight + separator;
        for(int attrNum = 0;attrNum<schema.num_attr();attrNum++){
            AttrInfo ai = schema.attr_info(attrNum);
            if(normalizeReals && ai.can_cast_to_real()){
                RealAttrInfo rai = ai.cast_to_real();
                if(rai.is_unknown(values[attrNum]))
                    rtrn = rtrn + "?";//Globals.UNKNOWN_VAL_STR;
                else
                    rtrn = rtrn + rai.normalized_value(values[attrNum]);
                if(attrNum < schema.num_attr() -1)

⌨️ 快捷键说明

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