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
FieldsModifier and TypeFieldDescriptionstatic intDefault max number of digits after the decimal point for numeric values -
Constructor Summary
Constructors -
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.intReturns the class attribute's index.booleanTests if an instance's class is missing.doubleReturns an instance's class value in internal format.dataset()Returns the dataset this instance has access to.voiddeleteAttributeAt(int position) Deletes an attribute at the given position (0 to numAttributes() - 1).Returns an enumeration of all the attributes.booleanequalHeaders(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.booleanTests 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".booleanTests if a specific value is "missing".booleanisMissingSparse(int indexOfIndex) Tests if a specific value is "missing", given an index in the sparse representation.intReturns the number of class labels.final InstancesrelationalValue(int attIndex) Returns the relational value of a relational attribute.final InstancesrelationalValue(Attribute att) Returns the relational value of a relational attribute.voidSets 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).final voidsetClassValue(String value) Sets the class value of an instance to the given value.final voidsetDataset(Instances instances) Sets the reference to the dataset.final voidsetMissing(int attIndex) Sets a specific value to be "missing".final voidsetMissing(Attribute att) Sets a specific value to be "missing".final voidSets a value of a nominal or string attribute to the given value.final voidSets a specific value in the instance to the given value (internal floating-point format).final voidSets a value of an nominal or string attribute to the given value.final voidsetWeight(double weight) Sets the weight of an instance.final StringstringValue(int attIndex) Returns the value of a nominal, string, date, or relational attribute for the instance as a string.final StringstringValue(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 StringtoString(int attIndex) Returns the description of one value of the instance as a string.final StringtoString(int attIndex, int afterDecimalPoint) Returns the description of one value of the instance as a string.final StringReturns the description of one value of the instance as a string.final StringReturns the description of one value of the instance as a string.final StringtoStringMaxDecimalDigits(int afterDecimalPoint) Returns the description of one instance with any numeric values printed at the supplied maximum number of decimal places.doubleReturns 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.final doubleweight()Returns the instance's weight.Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods 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:
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
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
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
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:
RuntimeException- if the instance has access to a dataset
-
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
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
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:
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:
isMissingSparsein 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:
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
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 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:
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
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
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 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:
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:
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:
relationalValuein 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:
relationalValuein 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:
stringValuein 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:
stringValuein 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:
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
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:
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
Returns the revision string.- Specified by:
getRevisionin interfaceRevisionHandler- Returns:
- the revision
-