public class Instance extends java.lang.Object implements Copyable, java.io.Serializable, RevisionHandler
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.
Constructor and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
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.
|
java.lang.String |
getRevision()
Returns the revision string.
|
boolean |
hasMissingValue()
Tests whether an instance has a missing value.
|
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.
|
Instances |
relationalValue(Attribute att)
Returns the relational value of a relational attribute.
|
Instances |
relationalValue(int attIndex)
Returns the relational value of a relational attribute.
|
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 value of a nominal, string, date, or relational attribute
for the instance as a string.
|
java.lang.String |
stringValue(int attIndex)
Returns the value of a nominal, string, date, or relational attribute
for the instance as a string.
|
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.
|
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 instancepublic Attribute attribute(int index)
index
- the attribute's indexUnassignedDatasetException
- if instance doesn't have access to a
datasetpublic Attribute attributeSparse(int indexOfIndex)
indexOfIndex
- the index of the attribute's indexUnassignedDatasetException
- 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)
public Instances dataset()
public void deleteAttributeAt(int position)
position
- the attribute's positionjava.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)
inst
- another instanceUnassignedDatasetException
- if instance doesn't have access to any
datasetpublic boolean hasMissingValue()
UnassignedDatasetException
- if instance doesn't have access to any
datasetpublic int index(int position)
position
- the positionpublic void insertAttributeAt(int position)
position
- the attribute's positionjava.lang.RuntimeException
- if the instance has accesss to a
datasetjava.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 testedpublic Instance mergeInstance(Instance inst)
inst
- the instance to be merged with this onepublic 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 modesjava.lang.IllegalArgumentException
- if numbers of attributes are unequalpublic void setClassMissing()
UnassignedClassException
- if the class is not setUnassignedDatasetException
- 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 setUnaddignedDatasetException
- 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 setUnassignedDatasetException
- if the dataset is not setjava.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 setjava.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 Instances relationalValue(int attIndex)
attIndex
- the attribute's indexjava.lang.IllegalArgumentException
- if the attribute is not a
relation-valued attributeUnassignedDatasetException
- if the instance doesn't belong
to a dataset.public final Instances relationalValue(Attribute att)
att
- the attributejava.lang.IllegalArgumentException
- if the attribute is not a
relation-valued attributeUnassignedDatasetException
- if the instance doesn't belong
to a dataset.public final java.lang.String stringValue(int attIndex)
attIndex
- the attribute's indexjava.lang.IllegalArgumentException
- if the attribute is not a nominal,
string, date, or relation-valued attribute.UnassignedDatasetException
- if the instance doesn't belong
to a dataset.public final java.lang.String stringValue(Attribute att)
att
- the attributejava.lang.IllegalArgumentException
- if the attribute is not a nominal,
string, date, or relation-valued attribute.UnassignedDatasetException
- if the instance doesn't belong
to a dataset.public double[] toDoubleArray()
public java.lang.String toString()
toString
in class java.lang.Object
public final java.lang.String toString(int attIndex)
attIndex
- the attribute's indexpublic final java.lang.String toString(Attribute att)
att
- the attributepublic double value(int attIndex)
attIndex
- the attribute's indexpublic double valueSparse(int indexOfIndex)
indexOfIndex
- the index of the attribute's indexpublic double value(Attribute att)
att
- the attributepublic final double weight()
public static void main(java.lang.String[] options)
options
- the commandline options - ignoredpublic java.lang.String getRevision()
getRevision
in interface RevisionHandler