Package weka.core
Class AbstractInstance
- java.lang.Object
-
- weka.core.AbstractInstance
-
- All Implemented Interfaces:
java.io.Serializable,Copyable,Instance,RevisionHandler
- Direct Known Subclasses:
DenseInstance,SparseInstance
public abstract class AbstractInstance extends java.lang.Object implements Instance, java.io.Serializable, RevisionHandler
Abstract class providing common functionality for the original instance implementations.- Version:
- $Revision: 10649 $
- Author:
- Eibe Frank (eibe@cs.waikato.ac.nz)
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static ints_numericAfterDecimalPointDefault max number of digits after the decimal point for numeric values
-
Constructor Summary
Constructors Constructor Description AbstractInstance()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Attributeattribute(int index)Returns the attribute with the given index.AttributeattributeSparse(int indexOfIndex)Returns the attribute with the given index in the sparse representation.AttributeclassAttribute()Returns class attribute.intclassIndex()Returns the class attribute's index.booleanclassIsMissing()Tests if an instance's class is missing.doubleclassValue()Returns an instance's class value in internal format.Instancesdataset()Returns the dataset this instance has access to.voiddeleteAttributeAt(int position)Deletes an attribute at the given position (0 to numAttributes() - 1).java.util.Enumeration<Attribute>enumerateAttributes()Returns an enumeration of all the attributes.booleanequalHeaders(Instance inst)Tests if the headers of two instances are equivalent.java.lang.StringequalHeadersMsg(Instance inst)Checks if the headers of two instances are equivalent.java.lang.StringgetRevision()Returns the revision string.booleanhasMissingValue()Tests whether an instance has a missing value.voidinsertAttributeAt(int position)Inserts an attribute at the given position (0 to numAttributes()).booleanisMissing(int attIndex)Tests if a specific value is "missing".booleanisMissing(Attribute att)Tests if a specific value is "missing".booleanisMissingSparse(int indexOfIndex)Tests if a specific value is "missing", given an index in the sparse representation.intnumClasses()Returns the number of class labels.InstancesrelationalValue(int attIndex)Returns the relational value of a relational attribute.InstancesrelationalValue(Attribute att)Returns the relational value of a relational attribute.voidsetClassMissing()Sets the class value of an instance to be "missing".voidsetClassValue(double value)Sets the class value of an instance to the given value (internal floating-point format).voidsetClassValue(java.lang.String value)Sets the class value of an instance to the given value.voidsetDataset(Instances instances)Sets the reference to the dataset.voidsetMissing(int attIndex)Sets a specific value to be "missing".voidsetMissing(Attribute att)Sets a specific value to be "missing".voidsetValue(int attIndex, java.lang.String value)Sets a value of a nominal or string attribute to the given value.voidsetValue(Attribute att, double value)Sets a specific value in the instance to the given value (internal floating-point format).voidsetValue(Attribute att, java.lang.String value)Sets a value of an nominal or string attribute to the given value.voidsetWeight(double weight)Sets the weight of an instance.java.lang.StringstringValue(int attIndex)Returns the value of a nominal, string, date, or relational attribute for the instance as a string.java.lang.StringstringValue(Attribute att)Returns the value of a nominal, string, date, or relational attribute for the instance as a string.java.lang.StringtoString()Returns the description of one instance.java.lang.StringtoString(int attIndex)Returns the description of one value of the instance as a string.java.lang.StringtoString(int attIndex, int afterDecimalPoint)Returns the description of one value of the instance as a string.java.lang.StringtoString(Attribute att)Returns the description of one value of the instance as a string.java.lang.StringtoString(Attribute att, int afterDecimalPoint)Returns the description of one value of the instance as a string.java.lang.StringtoStringMaxDecimalDigits(int afterDecimalPoint)Returns the description of one instance with any numeric values printed at the supplied maximum number of decimal places.doublevalue(Attribute att)Returns an instance's attribute value in internal format.doublevalueSparse(int indexOfIndex)Returns an instance's attribute value in internal format, given an index in the sparse representation.doubleweight()Returns the instance's weight.-
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface weka.core.Instance
copy, index, mergeInstance, numAttributes, numValues, replaceMissingValues, setValue, setValueSparse, toDoubleArray, toStringNoWeight, toStringNoWeight, value
-
-
-
-
Method Detail
-
attribute
public Attribute attribute(int index)
Returns the attribute with the given index.- Specified by:
attributein interfaceInstance- Parameters:
index- the attribute's index- Returns:
- the attribute at the given position
- Throws:
UnassignedDatasetException- if instance doesn't have access to a dataset
-
attributeSparse
public Attribute attributeSparse(int indexOfIndex)
Returns the attribute with the given index in the sparse representation.- Specified by:
attributeSparsein interfaceInstance- Parameters:
indexOfIndex- the index of the attribute's index- Returns:
- the attribute at the given position
- Throws:
UnassignedDatasetException- if instance doesn't have access to a dataset
-
classAttribute
public Attribute classAttribute()
Returns class attribute.- Specified by:
classAttributein interfaceInstance- Returns:
- the class attribute
- Throws:
UnassignedDatasetException- if the class is not set or the instance doesn't have access to a dataset
-
classIndex
public int classIndex()
Returns the class attribute's index.- Specified by:
classIndexin interfaceInstance- Returns:
- the class index as an integer
- Throws:
UnassignedDatasetException- if instance doesn't have access to a dataset
-
classIsMissing
public boolean classIsMissing()
Tests if an instance's class is missing.- Specified by:
classIsMissingin interfaceInstance- Returns:
- true if the instance's class is missing
- Throws:
UnassignedClassException- if the class is not set or the instance doesn't have access to a dataset
-
classValue
public double classValue()
Returns an instance's class value in internal format. (ie. as a floating-point number)- Specified by:
classValuein interfaceInstance- Returns:
- the corresponding value as a double (If the corresponding attribute is nominal (or a string) then it returns the value's index as a double).
- Throws:
UnassignedClassException- if the class is not set or the instance doesn't have access to a dataset
-
dataset
public Instances dataset()
Returns the dataset this instance has access to. (ie. obtains information about attribute types from) Null if the instance doesn't have access to a dataset.
-
deleteAttributeAt
public void deleteAttributeAt(int position)
Deletes an attribute at the given position (0 to numAttributes() - 1). Only succeeds if the instance does not have access to any dataset because otherwise inconsistencies could be introduced.- Specified by:
deleteAttributeAtin interfaceInstance- Parameters:
position- the attribute's position- Throws:
java.lang.RuntimeException- if the instance has access to a dataset
-
enumerateAttributes
public java.util.Enumeration<Attribute> enumerateAttributes()
Returns an enumeration of all the attributes.- Specified by:
enumerateAttributesin interfaceInstance- Returns:
- enumeration of all the attributes
- Throws:
UnassignedDatasetException- if the instance doesn't have access to a dataset
-
equalHeaders
public boolean equalHeaders(Instance inst)
Tests if the headers of two instances are equivalent.- Specified by:
equalHeadersin interfaceInstance- Parameters:
inst- another instance- Returns:
- true if the header of the given instance is equivalent to this instance's header
- Throws:
UnassignedDatasetException- if instance doesn't have access to any dataset
-
equalHeadersMsg
public java.lang.String equalHeadersMsg(Instance inst)
Checks if the headers of two instances are equivalent. If not, then returns a message why they differ.- Specified by:
equalHeadersMsgin interfaceInstance- Parameters:
dataset- another instance- Returns:
- null if the header of the given instance is equivalent to this instance's header, otherwise a message with details on why they differ
-
hasMissingValue
public boolean hasMissingValue()
Tests whether an instance has a missing value. Skips the class attribute if set.- Specified by:
hasMissingValuein interfaceInstance- Returns:
- true if instance has a missing value.
- Throws:
UnassignedDatasetException- if instance doesn't have access to any dataset
-
insertAttributeAt
public void insertAttributeAt(int position)
Inserts an attribute at the given position (0 to numAttributes()). Only succeeds if the instance does not have access to any dataset because otherwise inconsistencies could be introduced.- Specified by:
insertAttributeAtin interfaceInstance- Parameters:
position- the attribute's position- Throws:
java.lang.RuntimeException- if the instance has accesss to a datasetjava.lang.IllegalArgumentException- if the position is out of range
-
isMissing
public boolean isMissing(int attIndex)
Tests if a specific value is "missing".
-
isMissingSparse
public boolean isMissingSparse(int indexOfIndex)
Tests if a specific value is "missing", given an index in the sparse representation.- Specified by:
isMissingSparsein interfaceInstance- Parameters:
indexOfIndex- the index of the attribute's index- Returns:
- true if the value is "missing"
-
isMissing
public boolean isMissing(Attribute att)
Tests if a specific value is "missing". The given attribute has to belong to a dataset.
-
numClasses
public int numClasses()
Returns the number of class labels.- Specified by:
numClassesin interfaceInstance- Returns:
- the number of class labels as an integer if the class attribute is nominal, 1 otherwise.
- Throws:
UnassignedDatasetException- if instance doesn't have access to any dataset
-
setClassMissing
public void setClassMissing()
Sets the class value of an instance to be "missing". A deep copy of the vector of attribute values is performed before the value is set to be missing.- Specified by:
setClassMissingin interfaceInstance- Throws:
UnassignedClassException- if the class is not setUnassignedDatasetException- if the instance doesn't have access to a dataset
-
setClassValue
public void setClassValue(double value)
Sets the class value of an instance to the given value (internal floating-point format). A deep copy of the vector of attribute values is performed before the value is set.- Specified by:
setClassValuein interfaceInstance- Parameters:
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).- Throws:
UnassignedClassException- if the class is not setUnaddignedDatasetException- if the instance doesn't have access to a dataset
-
setClassValue
public final void setClassValue(java.lang.String value)
Sets the class value of an instance to the given value. A deep copy of the vector of attribute values is performed before the value is set.- Specified by:
setClassValuein interfaceInstance- Parameters:
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).- Throws:
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 attribute
-
setDataset
public final void setDataset(Instances instances)
Sets the reference to the dataset. Does not check if the instance is compatible with the dataset. Note: the dataset does not know about this instance. If the structure of the dataset's header gets changed, this instance will not be adjusted automatically.- Specified by:
setDatasetin interfaceInstance- Parameters:
instances- the reference to the dataset
-
setMissing
public final void setMissing(int attIndex)
Sets a specific value to be "missing". Performs a deep copy of the vector of attribute values before the value is set to be missing.- Specified by:
setMissingin interfaceInstance- Parameters:
attIndex- the attribute's index
-
setMissing
public final void setMissing(Attribute att)
Sets a specific value to be "missing". Performs a deep copy of the vector of attribute values before the value is set to be missing. The given attribute has to belong to a dataset.- Specified by:
setMissingin interfaceInstance- Parameters:
att- the attribute
-
setValue
public final void setValue(int attIndex, java.lang.String value)Sets a value of a nominal or string attribute to the given value. Performs a deep copy of the vector of attribute values before the value is set.- Specified by:
setValuein interfaceInstance- Parameters:
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).- Throws:
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 attribute
-
setValue
public final void setValue(Attribute att, double value)
Sets a specific value in the instance to the given value (internal floating-point format). Performs a deep copy of the vector of attribute values before the value is set, so if you are planning on calling setValue many times it may be faster to create a new instance using toDoubleArray. The given attribute has to belong to a dataset.
-
setValue
public final void setValue(Attribute att, java.lang.String value)
Sets a value of an nominal or string attribute to the given value. Performs a deep copy of the vector of attribute values before the value is set, so if you are planning on calling setValue many times it may be faster to create a new instance using toDoubleArray. The given attribute has to belong to a dataset.- Specified by:
setValuein interfaceInstance- Parameters:
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).- Throws:
java.lang.IllegalArgumentException- if the the attribute is not nominal or a string, or the value couldn't be found for a nominal attribute
-
setWeight
public final void setWeight(double weight)
Sets the weight of an instance.
-
relationalValue
public final Instances relationalValue(int attIndex)
Returns the relational value of a relational attribute.- Specified by:
relationalValuein interfaceInstance- Parameters:
attIndex- the attribute's index- Returns:
- the corresponding relation as an Instances object
- Throws:
java.lang.IllegalArgumentException- if the attribute is not a relation-valued attributeUnassignedDatasetException- if the instance doesn't belong to a dataset.
-
relationalValue
public final Instances relationalValue(Attribute att)
Returns the relational value of a relational attribute.- Specified by:
relationalValuein interfaceInstance- Parameters:
att- the attribute- Returns:
- the corresponding relation as an Instances object, null if missing
- Throws:
java.lang.IllegalArgumentException- if the attribute is not a relation-valued attributeUnassignedDatasetException- if the instance doesn't belong to a dataset.
-
stringValue
public final java.lang.String stringValue(int attIndex)
Returns the value of a nominal, string, date, or relational attribute for the instance as a string.- Specified by:
stringValuein interfaceInstance- Parameters:
attIndex- the attribute's index- Returns:
- the value as a string
- Throws:
java.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.
-
stringValue
public final java.lang.String stringValue(Attribute att)
Returns the value of a nominal, string, date, or relational attribute for the instance as a string.- Specified by:
stringValuein interfaceInstance- Parameters:
att- the attribute- Returns:
- the value as a string
- Throws:
java.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.
-
toStringMaxDecimalDigits
public final java.lang.String toStringMaxDecimalDigits(int afterDecimalPoint)
Returns the description of one instance with any numeric values printed at the supplied maximum number of decimal places. If the instance doesn't have access to a dataset, it returns the internal floating-point values. Quotes string values that contain whitespace characters.- Specified by:
toStringMaxDecimalDigitsin interfaceInstance- Parameters:
afterDecimalPoint- the maximum number of digits permitted after the decimal point for a numeric value- Returns:
- the instance's description as a string
-
toString
public java.lang.String toString()
Returns the description of one instance. If the instance doesn't have access to a dataset, it returns the internal floating-point values. Quotes string values that contain whitespace characters.- Overrides:
toStringin classjava.lang.Object- Returns:
- the instance's description as a string
-
toString
public final java.lang.String toString(int attIndex)
Returns the description of one value of the instance as a string. If the instance doesn't have access to a dataset, it returns the internal floating-point value. Quotes string values that contain whitespace characters, or if they are a question mark.
-
toString
public final java.lang.String toString(int attIndex, int afterDecimalPoint)Returns the description of one value of the instance as a string. If the instance doesn't have access to a dataset, it returns the internal floating-point value. Quotes string values that contain whitespace characters, or if they are a question mark.
-
toString
public final java.lang.String toString(Attribute att)
Returns the description of one value of the instance as a string. If the instance doesn't have access to a dataset it returns the internal floating-point value. Quotes string values that contain whitespace characters, or if they are a question mark. The given attribute has to belong to a dataset.
-
toString
public final java.lang.String toString(Attribute att, int afterDecimalPoint)
Returns the description of one value of the instance as a string. If the instance doesn't have access to a dataset it returns the internal floating-point value. Quotes string values that contain whitespace characters, or if they are a question mark. The given attribute has to belong to a dataset.
-
value
public double value(Attribute att)
Returns an instance's attribute value in internal format. The given attribute has to belong to a dataset.
-
valueSparse
public double valueSparse(int indexOfIndex)
Returns an instance's attribute value in internal format, given an index in the sparse representation.- Specified by:
valueSparsein interfaceInstance- Parameters:
indexOfIndex- the index of the attribute's index- Returns:
- the specified value as a double (If the corresponding attribute is nominal (or a string) then it returns the value's index as a double).
-
weight
public final double weight()
Returns the instance's weight.
-
getRevision
public java.lang.String getRevision()
Returns the revision string.- Specified by:
getRevisionin interfaceRevisionHandler- Returns:
- the revision
-
-