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:
numeric:
This type of attribute represents a floating-point number.
-
nominal:
This type of attribute represents a fixed set of nominal values.
-
string:
This type of attribute represents a dynamically expanding set of
nominal values. String attributes are not used by the learning
schemes in Weka. They can be used, for example, to store an
identifier with each instance in a dataset.
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);
Attribute
(java.lang.String attributeName,
FastVector
attributeValues,
ProtectedProperties
metadata)
Constructor for nominal attributes and string attributes, where
metadata is supplied.
(package private)
Attribute
(java.lang.String attributeName,
int index)
Constructor for a numeric attribute with a particular index.
Attribute
(java.lang.String attributeName,
java.lang.String dateFormat,
ProtectedProperties
metadata)
Constructor for a date attribute, where metadata is supplied.
private static final int
STRING_COMPRESS_THRESHOLD
-
Strings longer than this will be stored compressed.
-
See Also:
-
Constant Field Values
ProtectedProperties
metadata)
-
Constructor for a numeric attribute, where metadata is supplied.
-
Parameters:
-
attributeName
- the name for the attribute
-
metadata
- the attribute's properties
Attribute
public
Attribute
(java.lang.String attributeName,
java.lang.String dateFormat)
-
Constructor for a date attribute.
-
Parameters:
-
attributeName
- the name for the attribute
-
dateFormat
- a string suitable for use with
SimpleDateFormatter for parsing dates.
Attribute
public
Attribute
(java.lang.String attributeName,
java.lang.String dateFormat,
ProtectedProperties
metadata)
-
Constructor for a date attribute, where metadata is supplied.
-
Parameters:
-
attributeName
- the name for the attribute
-
dateFormat
- a string suitable for use with
SimpleDateFormatter for parsing dates.
-
metadata
- the attribute's properties
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.
Attribute
public
Attribute
(java.lang.String attributeName,
FastVector
attributeValues,
ProtectedProperties
metadata)
-
Constructor for nominal attributes and string attributes, where
metadata is supplied. 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.
-
metadata
- the attribute's properties
Attribute
Attribute
(java.lang.String attributeName,
int index)
-
Constructor for a numeric attribute with a particular index.
-
Parameters:
-
attributeName
- the name for the attribute
-
index
- the attribute's index
Attribute
Attribute
(java.lang.String attributeName,
java.lang.String dateFormat,
int index)
-
Constructor for date attributes with a particular index.
-
Parameters:
-
attributeName
- the name for the attribute
-
dateFormat
- a string suitable for use with
SimpleDateFormatter for parsing dates. Null for a default format
string.
-
index
- the attribute's index
Attribute
Attribute
(java.lang.String attributeName,
FastVector
attributeValues,
int index)
-
Constructor for nominal attributes and string attributes with
a particular index.
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.
-
index
- the attribute's index
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.
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
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.
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.
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
-
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.
-
Returns:
-
the index assigned to the string, or -1 if the attribute is not
of type Attribute.STRING
final void
addValue
(java.lang.String value)
-
Adds an attribute value. Creates a fresh list of attribute
values before adding it.
-
Parameters:
-
index
- the value's index
-
string
- the value
-
Throws:
-
java.lang.IllegalArgumentException
- if the attribute is not nominal or
string.
-
Returns the ordering of the attribute. One of the following:
ORDERING_SYMBOLIC - attribute values should be treated as symbols.
ORDERING_ORDERED - attribute values have a global ordering.
ORDERING_MODULO - attribute values have an ordering which wraps.
public final boolean
hasZeropoint
()
-
Returns whether the attribute has a zeropoint and may be
added meaningfully.
private void
setMetadata
(
ProtectedProperties
metadata)
-
Sets the metadata for the attribute. Processes the strings stored in the
metadata of the attribute so that the properties can be set up for the
easy-access metadata methods. Any strings sought that are omitted will
cause default values to be set.
The following properties are recognised:
ordering, averageable, zeropoint, regular, weight, and range.
All other properties can be queried and handled appropriately by classes
calling the getMetadata() method.
private void
setNumericRange
(java.lang.String rangeString)
-
Sets the numeric range based on a string. If the string is null the range
will default to [-inf,+inf]. A square brace represents a closed interval, a
curved brace represents an open interval, and 'inf' represents infinity.
Examples of valid range strings: "[-inf,20)","(-13.5,-5.2)","(5,inf]"