Package weka.core
Class AbstractInstance
java.lang.Object
weka.core.AbstractInstance
- All Implemented Interfaces:
Serializable
,Copyable
,Instance
,RevisionHandler
- Direct Known Subclasses:
DenseInstance
,SparseInstance
public abstract class AbstractInstance
extends Object
implements Instance, 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:
-
Field Summary
Modifier and TypeFieldDescriptionstatic int
Default max number of digits after the decimal point for numeric values -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionattribute
(int index) Returns the attribute with the given index.attributeSparse
(int indexOfIndex) Returns the attribute with the given index in the sparse representation.Returns class attribute.int
Returns the class attribute's index.boolean
Tests if an instance's class is missing.double
Returns an instance's class value in internal format.dataset()
Returns the dataset this instance has access to.void
deleteAttributeAt
(int position) Deletes an attribute at the given position (0 to numAttributes() - 1).Returns an enumeration of all the attributes.boolean
equalHeaders
(Instance inst) Tests if the headers of two instances are equivalent.equalHeadersMsg
(Instance inst) Checks if the headers of two instances are equivalent.Returns the revision string.boolean
Tests whether an instance has a missing value.void
insertAttributeAt
(int position) Inserts an attribute at the given position (0 to numAttributes()).boolean
isMissing
(int attIndex) Tests if a specific value is "missing".boolean
Tests if a specific value is "missing".boolean
isMissingSparse
(int indexOfIndex) Tests if a specific value is "missing", given an index in the sparse representation.int
Returns the number of class labels.final Instances
relationalValue
(int attIndex) Returns the relational value of a relational attribute.final Instances
relationalValue
(Attribute att) Returns the relational value of a relational attribute.void
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).final void
setClassValue
(String value) Sets the class value of an instance to the given value.final void
setDataset
(Instances instances) Sets the reference to the dataset.final void
setMissing
(int attIndex) Sets a specific value to be "missing".final void
setMissing
(Attribute att) Sets a specific value to be "missing".final void
Sets a value of a nominal or string attribute to the given value.final void
Sets a specific value in the instance to the given value (internal floating-point format).final void
Sets a value of an nominal or string attribute to the given value.final void
setWeight
(double weight) Sets the weight of an instance.final String
stringValue
(int attIndex) Returns the value of a nominal, string, date, or relational attribute for the instance as a string.final String
stringValue
(Attribute att) Returns the value of a nominal, string, date, or relational attribute for the instance as a string.toString()
Returns the description of one instance.final String
toString
(int attIndex) Returns the description of one value of the instance as a string.final String
toString
(int attIndex, int afterDecimalPoint) Returns the description of one value of the instance as a string.final String
Returns the description of one value of the instance as a string.final String
Returns the description of one value of the instance as a string.final String
toStringMaxDecimalDigits
(int afterDecimalPoint) Returns the description of one instance with any numeric values printed at the supplied maximum number of decimal places.double
Returns an instance's attribute value in internal format.double
valueSparse
(int indexOfIndex) Returns an instance's attribute value in internal format, given an index in the sparse representation.final double
weight()
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
-
Field Details
-
s_numericAfterDecimalPoint
public static int s_numericAfterDecimalPointDefault max number of digits after the decimal point for numeric values
-
-
Constructor Details
-
AbstractInstance
public AbstractInstance()
-
-
Method Details
-
attribute
Returns the attribute with the given index.- Specified by:
attribute
in 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
Returns the attribute with the given index in the sparse representation.- Specified by:
attributeSparse
in 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
Returns class attribute.- Specified by:
classAttribute
in 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:
classIndex
in 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:
classIsMissing
in 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:
classValue
in 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
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:
deleteAttributeAt
in interfaceInstance
- Parameters:
position
- the attribute's position- Throws:
RuntimeException
- if the instance has access to a dataset
-
enumerateAttributes
Returns an enumeration of all the attributes.- Specified by:
enumerateAttributes
in interfaceInstance
- Returns:
- enumeration of all the attributes
- Throws:
UnassignedDatasetException
- if the instance doesn't have access to a dataset
-
equalHeaders
Tests if the headers of two instances are equivalent.- Specified by:
equalHeaders
in 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
Checks if the headers of two instances are equivalent. If not, then returns a message why they differ.- Specified by:
equalHeadersMsg
in 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:
hasMissingValue
in 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:
insertAttributeAt
in interfaceInstance
- Parameters:
position
- the attribute's position- Throws:
RuntimeException
- if the instance has accesss to a datasetIllegalArgumentException
- 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:
isMissingSparse
in interfaceInstance
- Parameters:
indexOfIndex
- the index of the attribute's index- Returns:
- true if the value is "missing"
-
isMissing
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:
numClasses
in 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:
setClassMissing
in 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:
setClassValue
in 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
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:
setClassValue
in 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 setIllegalArgumentException
- if the attribute is not nominal or a string, or the value couldn't be found for a nominal attribute
-
setDataset
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:
setDataset
in 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:
setMissing
in interfaceInstance
- Parameters:
attIndex
- the attribute's index
-
setMissing
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:
setMissing
in interfaceInstance
- Parameters:
att
- the attribute
-
setValue
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:
setValue
in 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 setIllegalArgumentException
- if the selected attribute is not nominal or a string, or the supplied value couldn't be found for a nominal attribute
-
setValue
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
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:
setValue
in 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:
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
Returns the relational value of a relational attribute.- Specified by:
relationalValue
in interfaceInstance
- Parameters:
attIndex
- the attribute's index- Returns:
- the corresponding relation as an Instances object
- Throws:
IllegalArgumentException
- if the attribute is not a relation-valued attributeUnassignedDatasetException
- if the instance doesn't belong to a dataset.
-
relationalValue
Returns the relational value of a relational attribute.- Specified by:
relationalValue
in interfaceInstance
- Parameters:
att
- the attribute- Returns:
- the corresponding relation as an Instances object, null if missing
- Throws:
IllegalArgumentException
- if the attribute is not a relation-valued attributeUnassignedDatasetException
- if the instance doesn't belong to a dataset.
-
stringValue
Returns the value of a nominal, string, date, or relational attribute for the instance as a string.- Specified by:
stringValue
in interfaceInstance
- Parameters:
attIndex
- the attribute's index- Returns:
- the value as a string
- Throws:
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
Returns the value of a nominal, string, date, or relational attribute for the instance as a string.- Specified by:
stringValue
in interfaceInstance
- Parameters:
att
- the attribute- Returns:
- the value as a string
- Throws:
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
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:
toStringMaxDecimalDigits
in 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
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. -
toString
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
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
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
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
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:
valueSparse
in 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
Returns the revision string.- Specified by:
getRevision
in interfaceRevisionHandler
- Returns:
- the revision
-