weka.core
Class Attribute

java.lang.Object
  extended by weka.core.Attribute
All Implemented Interfaces:
java.io.Serializable, Copyable

public class Attribute
extends java.lang.Object
implements Copyable, java.io.Serializable

Class for handling an attribute. Once an attribute has been created, it can't be changed.

Three attribute types are supported:

Typical usage (code from the main() method of this class):

...
// Create numeric attributes "length" and "weight"
Attribute length = new Attribute("length");
Attribute weight = new Attribute("weight");

// Create vector to hold nominal values "first", "second", "third"
FastVector my_nominal_values = new FastVector(3);
my_nominal_values.addElement("first");
my_nominal_values.addElement("second");
my_nominal_values.addElement("third");

// Create nominal attribute "position"
Attribute position = new Attribute("position", my_nominal_values);
...

Version:
$Revision: 1.1 $
Author:
Eibe Frank ([email protected])
See Also:
Serialized Form

Field Summary
static int NOMINAL
          Constant set for nominal attributes.
static int NUMERIC
          Constant set for numeric attributes.
static int STRING
          Constant set for attributes with string values.
 
Constructor Summary
Attribute(java.lang.String attributeName)
          Constructor for a numeric attribute.
Attribute(java.lang.String attributeName, FastVector attributeValues)
          Constructor for nominal attributes and string attributes.
 
Method Summary
 int addStringValue(Attribute src, int index)
          Adds a string value to the list of valid strings for attributes of type STRING and returns the index of the string.
 int addStringValue(java.lang.String value)
          Adds a string value to the list of valid strings for attributes of type STRING and returns the index of the string.
 java.lang.Object copy()
          Produces a shallow copy of this attribute.
 java.util.Enumeration enumerateValues()
          Returns an enumeration of all the attribute's values if the attribute is nominal or a string, null otherwise.
 boolean equals(java.lang.Object other)
          Tests if given attribute is equal to this attribute.
 int index()
          Returns the index of this attribute.
 int indexOfValue(java.lang.String value)
          Returns the index of a given attribute value.
 boolean isNominal()
          Test if the attribute is nominal.
 boolean isNumeric()
          Tests if the attribute is numeric.
 boolean isString()
          Tests if the attribute is a string.
static void main(java.lang.String[] ops)
          Simple main method for testing this class.
 java.lang.String name()
          Returns the attribute's name.
 int numValues()
          Returns the number of attribute values.
 java.lang.String toString()
          Returns a description of this attribute in ARFF format.
 int type()
          Returns the attribute's type as an integer.
 java.lang.String value(int valIndex)
          Returns a value of a nominal or string attribute.
 
Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

NUMERIC

public static final int NUMERIC
Constant set for numeric attributes.

See Also:
Constant Field Values

NOMINAL

public static final int NOMINAL
Constant set for nominal attributes.

See Also:
Constant Field Values

STRING

public static final int STRING
Constant set for attributes with string values.

See Also:
Constant Field Values
Constructor Detail

Attribute

public Attribute(java.lang.String attributeName)
Constructor for a numeric attribute.

Parameters:
attributeName - the name for the attribute

Attribute

public Attribute(java.lang.String attributeName,
                 FastVector attributeValues)
Constructor for nominal attributes and string attributes. If a null vector of attribute values is passed to the method, the attribute is assumed to be a string.

Parameters:
attributeName - the name for the attribute
attributeValues - a vector of strings denoting the attribute values. Null if the attribute is a string attribute.
Method Detail

copy

public java.lang.Object copy()
Produces a shallow copy of this attribute.

Specified by:
copy in interface Copyable
Returns:
a copy of this attribute with the same index

enumerateValues

public final java.util.Enumeration enumerateValues()
Returns an enumeration of all the attribute's values if the attribute is nominal or a string, null otherwise.

Returns:
enumeration of all the attribute's values

equals

public final boolean equals(java.lang.Object other)
Tests if given attribute is equal to this attribute.

Overrides:
equals in class java.lang.Object
Parameters:
other - the Object to be compared to this attribute
Returns:
true if the given attribute is equal to this attribute

index

public final int index()
Returns the index of this attribute.

Returns:
the index of this attribute

indexOfValue

public final int indexOfValue(java.lang.String value)
Returns the index of a given attribute value. (The index of the first occurence of this value.)

Parameters:
value - the value for which the index is to be returned
Returns:
the index of the given attribute value if attribute is nominal or a string, -1 if it is numeric or the value can't be found

isNominal

public final boolean isNominal()
Test if the attribute is nominal.

Returns:
true if the attribute is nominal

isNumeric

public final boolean isNumeric()
Tests if the attribute is numeric.

Returns:
true if the attribute is numeric

isString

public final boolean isString()
Tests if the attribute is a string.

Returns:
true if the attribute is a string

name

public final java.lang.String name()
Returns the attribute's name.

Returns:
the attribute's name as a string

numValues

public final int numValues()
Returns the number of attribute values. Returns 0 for numeric attributes.

Returns:
the number of attribute values

toString

public final java.lang.String toString()
Returns a description of this attribute in ARFF format. Quotes strings if they contain whitespace characters, or if they are a question mark.

Overrides:
toString in class java.lang.Object
Returns:
a description of this attribute as a string

type

public final int type()
Returns the attribute's type as an integer.

Returns:
the attribute's type.

value

public final java.lang.String value(int valIndex)
Returns a value of a nominal or string attribute. Returns an empty string if the attribute is neither nominal nor a string attribute.

Parameters:
valIndex - the value's index
Returns:
the attribute's value as a string

addStringValue

public int addStringValue(java.lang.String value)
Adds a string value to the list of valid strings for attributes of type STRING and returns the index of the string.

Parameters:
value - The string value to add
Returns:
the index assigned to the string, or -1 if the attribute is not of type Attribute.STRING

addStringValue

public int addStringValue(Attribute src,
                          int index)
Adds a string value to the list of valid strings for attributes of type STRING and returns the index of the string. This method is more efficient than addStringValue(String) for long strings.

Parameters:
src - The Attribute containing the string value to add.
index - the index of the string value in the source attribute.
Returns:
the index assigned to the string, or -1 if the attribute is not of type Attribute.STRING

main

public static void main(java.lang.String[] ops)
Simple main method for testing this class.