|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectweka.core.Instance
Class for handling an instance. All values (numeric, nominal, or string) are internally stored as floating-point numbers. If an attribute is nominal (or a string), the stored value is the index of the corresponding nominal (or string) value in the attribute's definition. We have chosen this approach in favor of a more elegant object-oriented approach because it is much faster.
Typical usage (code from the main() method of this class):
...
// Create empty instance with three attribute values
Instance inst = new Instance(3);
// Set instance's values for the attributes "length", "weight", and "position"
inst.setValue(length, 5.3);
inst.setValue(weight, 300);
inst.setValue(position, "first");
// Set instance's dataset to be the dataset "race"
inst.setDataset(race);
// Print the instance
System.out.println("The instance: " + inst);
...
All methods that change an instance are safe, ie. a change of an instance does not affect any other instances. All methods that change an instance's attribute values clone the attribute value vector before it is changed. If your application heavily modifies instance values, it may be faster to create a new instance from scratch.
Field Summary | |
protected double[] |
m_AttValues
The instance's attribute values. |
protected Instances |
m_Dataset
The dataset the instance has access to. |
protected double |
m_Weight
The instance's weight. |
protected static double |
MISSING_VALUE
Constant representing a missing value. |
Constructor Summary | |
protected |
Instance()
Private constructor for subclasses. |
|
Instance(double weight,
double[] attValues)
Constructor that inititalizes instance variable with given values. |
|
Instance(Instance instance)
Constructor that copies the attribute values and the weight from the given instance. |
|
Instance(int numAttributes)
Constructor of an instance that sets weight to one, all values to be missing, and the reference to the dataset to null. |
Method Summary | |
Attribute |
attribute(int index)
Returns the attribute with the given index. |
Attribute |
attributeSparse(int indexOfIndex)
Returns the attribute with the given index. |
Attribute |
classAttribute()
Returns class attribute. |
int |
classIndex()
Returns the class attribute's index. |
boolean |
classIsMissing()
Tests if an instance's class is missing. |
double |
classValue()
Returns an instance's class value in internal format. |
java.lang.Object |
copy()
Produces a shallow copy of this instance. |
Instances |
dataset()
Returns the dataset this instance has access to. |
void |
deleteAttributeAt(int position)
Deletes an attribute at the given position (0 to numAttributes() - 1). |
java.util.Enumeration |
enumerateAttributes()
Returns an enumeration of all the attributes. |
boolean |
equalHeaders(Instance inst)
Tests if the headers of two instances are equivalent. |
(package private) void |
forceDeleteAttributeAt(int position)
Deletes an attribute at the given position (0 to numAttributes() - 1). |
(package private) void |
forceInsertAttributeAt(int position)
Inserts an attribute at the given position (0 to numAttributes()) and sets its value to be missing. |
private void |
freshAttributeVector()
Clones the attribute vector of the instance and overwrites it with the clone. |
int |
index(int position)
Returns the index of the attribute stored at the given position. |
void |
insertAttributeAt(int position)
Inserts an attribute at the given position (0 to numAttributes()). |
boolean |
isMissing(Attribute att)
Tests if a specific value is "missing". |
boolean |
isMissing(int attIndex)
Tests if a specific value is "missing". |
boolean |
isMissingSparse(int indexOfIndex)
Tests if a specific value is "missing". |
static boolean |
isMissingValue(double val)
Tests if the given value codes "missing". |
static void |
main(java.lang.String[] options)
Main method for testing this class. |
Instance |
mergeInstance(Instance inst)
Merges this instance with the given instance and returns the result. |
static double |
missingValue()
Returns the double that codes "missing". |
int |
numAttributes()
Returns the number of attributes. |
int |
numClasses()
Returns the number of class labels. |
int |
numValues()
Returns the number of values present. |
void |
replaceMissingValues(double[] array)
Replaces all missing values in the instance with the values contained in the given array. |
void |
setClassMissing()
Sets the class value of an instance to be "missing". |
void |
setClassValue(double value)
Sets the class value of an instance to the given value (internal floating-point format). |
void |
setClassValue(java.lang.String value)
Sets the class value of an instance to the given value. |
void |
setDataset(Instances instances)
Sets the reference to the dataset. |
void |
setMissing(Attribute att)
Sets a specific value to be "missing". |
void |
setMissing(int attIndex)
Sets a specific value to be "missing". |
void |
setValue(Attribute att,
double value)
Sets a specific value in the instance to the given value (internal floating-point format). |
void |
setValue(Attribute att,
java.lang.String value)
Sets a value of an nominal or string attribute to the given value. |
void |
setValue(int attIndex,
double value)
Sets a specific value in the instance to the given value (internal floating-point format). |
void |
setValue(int attIndex,
java.lang.String value)
Sets a value of a nominal or string attribute to the given value. |
void |
setValueSparse(int indexOfIndex,
double value)
Sets a specific value in the instance to the given value (internal floating-point format). |
void |
setWeight(double weight)
Sets the weight of an instance. |
java.lang.String |
stringValue(Attribute att)
Returns the string value of a nominal, string, or date attribute for the instance. |
java.lang.String |
stringValue(int attIndex)
Returns the string value of a nominal, string, or date attribute for the instance. |
double[] |
toDoubleArray()
Returns the values of each attribute as an array of doubles. |
java.lang.String |
toString()
Returns the description of one instance. |
java.lang.String |
toString(Attribute att)
Returns the description of one value of the instance as a string. |
java.lang.String |
toString(int attIndex)
Returns the description of one value of the instance as a string. |
double |
value(Attribute att)
Returns an instance's attribute value in internal format. |
double |
value(int attIndex)
Returns an instance's attribute value in internal format. |
double |
valueSparse(int indexOfIndex)
Returns an instance's attribute value in internal format. |
double |
weight()
Returns the instance's weight. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
protected static final double MISSING_VALUE
protected Instances m_Dataset
protected double[] m_AttValues
protected double m_Weight
Constructor Detail |
public Instance(Instance instance)
instance
- the instance from which the attribute
values and the weight are to be copiedpublic Instance(double weight, double[] attValues)
weight
- the instance's weightattValues
- a vector of attribute valuespublic Instance(int numAttributes)
numAttributes
- the size of the instanceprotected Instance()
Method Detail |
public Attribute attribute(int index)
index
- the attribute's index
UnassignedDatasetException
- if instance doesn't have access to a
datasetpublic Attribute attributeSparse(int indexOfIndex)
indexOfIndex
- the index of the attribute's index
UnassignedDatasetException
- if instance doesn't have access to a
datasetpublic Attribute classAttribute()
UnassignedDatasetException
- if the class is not set or the
instance doesn't have access to a datasetpublic int classIndex()
UnassignedDatasetException
- if instance doesn't have access to a datasetpublic boolean classIsMissing()
UnassignedClassException
- if the class is not set or the instance doesn't
have access to a datasetpublic double classValue()
UnassignedClassException
- if the class is not set or the instance doesn't
have access to a datasetpublic java.lang.Object copy()
new Instance(instance)
copy
in interface Copyable
public Instances dataset()
public void deleteAttributeAt(int position)
java.lang.RuntimeException
- if the instance has access to a
datasetpublic java.util.Enumeration enumerateAttributes()
UnassignedDatasetException
- if the instance doesn't
have access to a datasetpublic boolean equalHeaders(Instance inst)
UnassignedDatasetException
- if instance doesn't have access to any
datasetpublic int index(int position)
position
- the position
public void insertAttributeAt(int position)
java.lang.RuntimeException
- if the instance has accesss to a
dataset
java.lang.IllegalArgumentException
- if the position is out of rangepublic boolean isMissing(int attIndex)
attIndex
- the attribute's indexpublic boolean isMissingSparse(int indexOfIndex)
indexOfIndex
- the index of the attribute's indexpublic boolean isMissing(Attribute att)
att
- the attributepublic static boolean isMissingValue(double val)
val
- the value to be tested
public Instance mergeInstance(Instance inst)
inst
- the instance to be merged with this one
public static double missingValue()
public int numAttributes()
public int numClasses()
UnassignedDatasetException
- if instance doesn't have access to any
datasetpublic int numValues()
public void replaceMissingValues(double[] array)
array
- containing the means and modes
java.lang.IllegalArgumentException
- if numbers of attributes are unequalpublic void setClassMissing()
UnassignedClassException
- if the class is not set
UnassignedDatasetException
- if the instance doesn't
have access to a datasetpublic void setClassValue(double value)
value
- the new attribute value (If the corresponding
attribute is nominal (or a string) then this is the new value's
index as a double).
UnassignedClassException
- if the class is not set
UnaddignedDatasetException
- if the instance doesn't
have access to a datasetpublic final void setClassValue(java.lang.String value)
value
- the new class value (If the class
is a string attribute and the value can't be found,
the value is added to the attribute).
UnassignedClassException
- if the class is not set
UnassignedDatasetException
- if the dataset is not set
java.lang.IllegalArgumentException
- if the attribute is not
nominal or a string, or the value couldn't be found for a nominal
attributepublic final void setDataset(Instances instances)
instances
- the reference to the datasetpublic final void setMissing(int attIndex)
attIndex
- the attribute's indexpublic final void setMissing(Attribute att)
att
- the attributepublic void setValue(int attIndex, double value)
attIndex
- the attribute's indexvalue
- the new attribute value (If the corresponding
attribute is nominal (or a string) then this is the new value's
index as a double).public void setValueSparse(int indexOfIndex, double value)
indexOfIndex
- the index of the attribute's indexvalue
- the new attribute value (If the corresponding
attribute is nominal (or a string) then this is the new value's
index as a double).public final void setValue(int attIndex, java.lang.String value)
attIndex
- the attribute's indexvalue
- the new attribute value (If the attribute
is a string attribute and the value can't be found,
the value is added to the attribute).
UnassignedDatasetException
- if the dataset is not set
java.lang.IllegalArgumentException
- if the selected
attribute is not nominal or a string, or the supplied value couldn't
be found for a nominal attributepublic final void setValue(Attribute att, double value)
att
- the attributevalue
- the new attribute value (If the corresponding
attribute is nominal (or a string) then this is the new value's
index as a double).public final void setValue(Attribute att, java.lang.String value)
att
- the attributevalue
- the new attribute value (If the attribute
is a string attribute and the value can't be found,
the value is added to the attribute).
java.lang.IllegalArgumentException
- if the the attribute is not
nominal or a string, or the value couldn't be found for a nominal
attributepublic final void setWeight(double weight)
weight
- the weightpublic final java.lang.String stringValue(int attIndex)
attIndex
- the attribute's index
java.lang.IllegalArgumentException
- if the attribute is not a nominal,
string, or date attribute.
UnassignedDatasetException
- if the instance doesn't belong
to a dataset.public final java.lang.String stringValue(Attribute att)
att
- the attribute
java.lang.IllegalArgumentException
- if the attribute is not a nominal,
string, or date attribute.
UnassignedDatasetException
- if the instance doesn't belong
to a dataset.public double[] toDoubleArray()
public java.lang.String toString()
public final java.lang.String toString(int attIndex)
attIndex
- the attribute's index
public final java.lang.String toString(Attribute att)
att
- the attribute
public double value(int attIndex)
attIndex
- the attribute's index
public double valueSparse(int indexOfIndex)
indexOfIndex
- the index of the attribute's index
public double value(Attribute att)
att
- the attribute
public final double weight()
void forceDeleteAttributeAt(int position)
void forceInsertAttributeAt(int position)
private void freshAttributeVector()
public static void main(java.lang.String[] options)
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |