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

📄 attribute.java

📁 一个很好的LIBSVM的JAVA源码。对于要研究和改进SVM算法的学者。可以参考。来自数据挖掘工具YALE工具包。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 *  YALE - Yet Another Learning Environment
 *  Copyright (C) 2001-2004
 *      Simon Fischer, Ralf Klinkenberg, Ingo Mierswa, 
 *          Katharina Morik, Oliver Ritthoff
 *      Artificial Intelligence Unit
 *      Computer Science Department
 *      University of Dortmund
 *      44221 Dortmund,  Germany
 *  email: yale-team@lists.sourceforge.net
 *  web:   http://yale.cs.uni-dortmund.de/
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License as 
 *  published by the Free Software Foundation; either version 2 of the
 *  License, or (at your option) any later version. 
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 *  USA.
 */
package edu.udo.cs.yale.example;

import edu.udo.cs.yale.tools.Ontology;
import edu.udo.cs.yale.tools.LogService;

import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Iterator;
import java.io.Serializable;
import java.io.File;
import java.io.IOException;
import java.io.DataOutput;
import java.io.DataInput;

/** This class holds all information on a single attribute.
 *  <ul>
 *    <li>the name
 *    <li>the index, i.e. its column in the {@link ExampleTable} and {@link DataRow}
 *    <li>the value type (nominal, numerical, ...)
 *    <li>the block type (single value, time series...)
 *    <li>a number indicating the block it belongs to (important for time series only)
 *    <li>minimum, maximum and average of the values of the attribute
 *    <li>information about how the attribute was generated
 *  </ul>
 *
 *  @see edu.udo.cs.yale.tools.Ontology#ATTRIBUTE_VALUE_TYPE
 *  @see edu.udo.cs.yale.tools.Ontology#ATTRIBUTE_BLOCK_TYPE
 * 
 *  @author simon, ingo
 *  @version $Id: Attribute.java,v 2.42 2004/09/04 20:07:02 ingomierswa Exp $
 */
public class Attribute implements Cloneable, Serializable {

    /** Names for SI-Units. */
    private static final String  UNIT_NAME[] = {"m", "s","kg", "A", "K", "cd", "mol"};

    public static final int      UNDEFINED_ATTRIBUTE_INDEX = -1;

    /** The index that will be assigned to the first class. */
    public static final int FIRST_CLASS_INDEX = 0;

    /** The map between symbolic values and their indices. */
    private Map symbolToIndexMap = new HashMap();
    private Map indexToSymbolMap = new LinkedHashMap();
    private Map symbolToCounterMap = new HashMap();

    private int nextFreeSymbolMapIndex = FIRST_CLASS_INDEX;

    /** The current highest id for generated symbols. Is increased each time a new symbol is generated. */
    private static int genSym = 1;

    private static final String GENSYM_PREFIX = "gensym";

    /** block number: the block number has not been set properly. */
    public final static int UNDEFINED_BLOCK_NR = -1;

    /** optionally contains the name of the attribute. */
    private String name;

    /** Name of the function if this attribute was generated. */
    private String generatingFunctionName; 

    /** If this attribute was generated, this array of attributes holds
     *  the input arguments of the generation. */
    private Attribute[] generatingFunctionArguments;

    /** This flag is true if the generating function should be displayed as infix operator. */
    private boolean generatingFunctionInfix;

    /** All attributes belonging to the same block should have a common 
     *  blockNr. Single values should have a unique block number. */
    private int blockNr = UNDEFINED_BLOCK_NR;

    /** An int indicating the value type in terms of the Ontology.ATTRIBUTE_VALUE_TYPE. */
    private int valueType = Ontology.ATTRIBUTE_VALUE;

    /** An int indicating the block type in terms of the Ontology.ATTRIBUTE_BLOCK_TYPE. */
    private int blockType = Ontology.ATTRIBUTE_BLOCK;

    /** Index of this attribute in its ExampleTable. */
    private int index = UNDEFINED_ATTRIBUTE_INDEX;

    /** Maximum of all attribute values. */
    private double maximum;
    /** Minimum of all attribute values. */
    private double minimum;
    /** Average of all attribute values. */
    private double average;    
    /** Variance of all attribute values. */
    private double variance;

    /** Unit exponents. */
    private int[] unit;

    /** Creates an undefined attribute with most general types and generated name. */
    public Attribute() {
	name = createName();

	this.generatingFunctionName = this.name;
	this.generatingFunctionArguments = null;
	this.generatingFunctionInfix = false;
    }

    /** Use this constructor only to create a reference attribute that specifies
     *  the return value of a generator. */
    public Attribute(int valueType, int blockType) {
	this (null, valueType, blockType, UNDEFINED_BLOCK_NR, null);
    }

    /** Creates a simple, not generated attribute. */
    public Attribute(String name, int valueType, int blockType, int blockNr, String units) {
	this.name = (name != null) ? name : createName();
	setValueType(valueType);
	this.blockType = blockType;
	this.blockNr = blockNr;
	
	this.index = -1;
	this.unit = new int[UNIT_NAME.length];
	parseUnits(units);

	this.generatingFunctionName = this.name;
	this.generatingFunctionArguments = null;
	this.generatingFunctionInfix = false;
    }


    /** Creates a simple, not generated attribute with most general types. */
    public Attribute(String name) {
	this(name, Ontology.ATTRIBUTE_VALUE, Ontology.ATTRIBUTE_BLOCK, UNDEFINED_BLOCK_NR, null);
    }

    /** Creates a new generated attribute. */
    public Attribute(int valueType, int blockType,
		     String functionName, Attribute[] arguments, boolean infix, 
		     int blockNumber, int[] unit) {
	this.name = createName();
	setValueType(valueType);
	this.blockType = blockType;
	if (unit == null) {
	    this.unit = new int[UNIT_NAME.length];
	} else {
	    this.unit = unit;
	}

	this.generatingFunctionName      = functionName;
	this.generatingFunctionArguments = arguments;
	this.generatingFunctionInfix     = infix && (arguments.length == 2);

	this.blockNr = blockNumber;
    }

    /** Creates a new attribute which is still to generate. */
    public Attribute(String functionName, Attribute[] arguments) {
	this(Ontology.ATTRIBUTE_VALUE, Ontology.ATTRIBUTE_BLOCK, 
	     functionName, arguments, false, UNDEFINED_BLOCK_NR,null);
    }

    /** Creates a new attribute that has the same properties as argument. */
    public Attribute(Attribute argument, String functionName) {
	this(argument);
	this.name                        = functionName+"("+argument.name+")";
	this.generatingFunctionName      = functionName;
	this.generatingFunctionArguments = new Attribute[] { argument };
	this.generatingFunctionInfix     = false;
	this.index = -1;
    }

    /** Private clone constructor. */
    private Attribute(Attribute a) {
	this.valueType               = a.valueType;
	this.blockType               = a.blockType;
	this.name                    = new String(a.name);
	this.blockNr                 = a.blockNr;
	this.index                   = a.index;

	if (a.symbolToIndexMap != null) {
	    this.symbolToIndexMap = new HashMap();
	    this.symbolToIndexMap.putAll(a.symbolToIndexMap);
	} else {
	    this.symbolToIndexMap = null;
	}
	if (a.indexToSymbolMap != null) {
	    this.indexToSymbolMap = new LinkedHashMap();
	    this.indexToSymbolMap.putAll(a.indexToSymbolMap);
	} else {
	    this.indexToSymbolMap = null;
	}
	if (a.symbolToCounterMap != null) {
	    this.symbolToCounterMap = new HashMap();
	    this.symbolToCounterMap.putAll(a.symbolToCounterMap);
	} else {
	    this.symbolToCounterMap = null;
	}
	
	this.generatingFunctionName      = a.generatingFunctionName;
	this.generatingFunctionArguments = a.generatingFunctionArguments;
	this.generatingFunctionInfix     = a.generatingFunctionInfix;

	this.unit = new int[a.unit.length];
	System.arraycopy(a.unit, 0, this.unit, 0, a.unit.length);

	this.maximum                 = a.maximum;
	this.minimum                 = a.minimum;
	this.average                 = a.average;
    }
    
    /** Clones this attribute. */
    public Object clone() {
	return new Attribute(this);
    }
    
    /** Returns the name of the attribute. */
    public String getName() { return name; }
    /** Sets the name of the attribtue. */
    public void setName(String  v) { this.name = v; }
    
    
    /** Returns the minimum value of all attribute values in the example table. */
    public double getMinimum() { return minimum; }
    /** Sets the minimum value of all attribute values in the example table. */
    public void setMinimum(double  v) { this.minimum = v; }
    
    /** Returns the average value of all attribute values in the example table. */
    public double getAverage() { return average; }
    /** Sets the average value of all attribute values in the example table. */
    public void setAverage(double  v) { this.average = v; }

    /** Returns the variance of all attribute values in the example table. */
    public double getVariance() { return variance; }
    /** Sets the variance of all attribute values in the example table. */
    public void setVariance(double v) { this.variance = v; }

    /** Returns the maximum value of all attribute values in the example table. */
    public double getMaximum() { return maximum; }
    /** Sets the maximum value of all attribute values in the example table. */
    public void setMaximum(double  v) { this.maximum = v; }

    /** Returns a string that describes how this attribute was generated 
     *  from other attributes. */
    public String getConstructionDescription() {
	return getConstructionDescription(true);
    }

    /** Returns the block type of this attribute.
     *  @see edu.udo.cs.yale.tools.Ontology#ATTRIBUTE_BLOCK_TYPE */
    public int getBlockType() { return blockType; }
    /** Sets the block type of this attribute.
     *  @see edu.udo.cs.yale.tools.Ontology#ATTRIBUTE_BLOCK_TYPE */
    public void setBlockType(int  b) { this.blockType = b; }    

    /** Returns the value type of this attribute. 
     *  @see edu.udo.cs.yale.tools.Ontology#ATTRIBUTE_VALUE_TYPE */
    public int getValueType() { return valueType; }
    
    /** Sets the value type of this attribute. Creates empty maps for 
     *  the classes in case the value type is nominal.
     *  @see edu.udo.cs.yale.tools.Ontology#ATTRIBUTE_VALUE_TYPE */
    public void setValueType(int v) {
  	this.valueType = v;
	if (Ontology.ATTRIBUTE_VALUE_TYPE.isA(valueType, Ontology.NOMINAL)) {
	    if (symbolToIndexMap == null) {
		this.symbolToIndexMap   = new HashMap();
		this.indexToSymbolMap   = new LinkedHashMap();
	    }
	}
    }    
    
    /** Get the value of blockNr.
     *  @return value of blockNr. */
    public int getBlockNr() {
	return blockNr;
    }

    /** Clears the construction description. */
    public void clearConstructionDescription() {
	this.generatingFunctionName = this.name;
	this.generatingFunctionArguments = null;
	this.generatingFunctionInfix = false;
    }

    /** Returns a string that describes how this attribute was generated 
     *  from other attributes.
     *  @param useInfix Whether or not to use infix notation for binary generators */
    public String getConstructionDescription(boolean useInfix) {
	if (generatingFunctionArguments == null) return generatingFunctionName;
	else if ((generatingFunctionInfix) && (useInfix)) {
	    return "("+
		generatingFunctionArguments[0].getConstructionDescription(useInfix) + 
		generatingFunctionName + 
		generatingFunctionArguments[1].getConstructionDescription(useInfix)+")";
	} else {
	    String cd = generatingFunctionName + "(";
	    for (int i = 0; i < generatingFunctionArguments.length; i++)
		cd += (i==0 ? "":", ") + generatingFunctionArguments[i].getConstructionDescription(useInfix);
	    return cd + ")";
	}
    }

    public int getConstructionDepth() {
	if (!isGenerated()) return 0;
	else {
	    int max = -1;
	    for (int i = 0; i < generatingFunctionArguments.length; i++) {
		max = Math.max(max, generatingFunctionArguments[i].getConstructionDepth());
	    }
	    return max + 1;
	}
    }

    /** Returns true if the attribute was constructed the same way o was constructed. */
    public boolean equals(Object o) {
	if (!(o instanceof Attribute)) return false;
	Attribute a = (Attribute)o;
	if (!generatingFunctionName.equals(a.generatingFunctionName)) return false;
	if ((generatingFunctionArguments == null) && (a.generatingFunctionArguments == null)) return true;
	if ((generatingFunctionArguments != null) && (a.generatingFunctionArguments != null)) {
	    if (generatingFunctionArguments.length != a.generatingFunctionArguments.length) return false;
	    for (int i = 0; i < generatingFunctionArguments.length; i++)

⌨️ 快捷键说明

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