📄 instance.java
字号:
} catch (Exception e) {
return true;
}
return false;
}
/**
* Tests if a specific value is "missing". Does the same thing as
* isMissing() if applied to an Instance.
*
* @param indexOfIndex
* the index of the attribute's index
*/
public boolean isMissingSparse(int indexOfIndex) {
if (Double.isNaN(m_AttValues[indexOfIndex])) {
return true;
}
return false;
}
/**
* Tests if a specific value is "missing". The given attribute has to belong
* to a dataset.
*
* @param att
* the attribute
*/
public boolean isMissing(Attribute att) {
return isMissing(att.index());
}
/**
* Tests if the given value codes "missing".
*
* @param val
* the value to be tested
* @return true if val codes "missing"
*/
public static boolean isMissingValue(double val) {
return Double.isNaN(val);
}
/**
* Merges this instance with the given instance and returns the result.
* Dataset is set to null.
*
* @param inst
* the instance to be merged with this one
* @return the merged instances
*/
public Instance mergeInstance(Instance inst) {
int m = 0;
double[] newVals = new double[numAttributes() + inst.numAttributes()];
for (int j = 0; j < numAttributes(); j++, m++) {
newVals[m] = value(j);
}
for (int j = 0; j < inst.numAttributes(); j++, m++) {
newVals[m] = inst.value(j);
}
return new Instance(1.0, newVals);
}
/**
* Returns the double that codes "missing".
*
* @return the double that codes "missing"
*/
public static double missingValue() {
return MISSING_VALUE;
}
/**
* Returns the number of attributes.
*
* @return the number of attributes as an integer
*/
public int numAttributes() {
return m_AttValues.length;
}
/**
* Returns the number of class labels.
*
* @return the number of class labels as an integer if the class attribute
* is nominal, 1 otherwise.
* @exception UnassignedDatasetException
* if instance doesn't have access to any dataset
*/
public int numClasses() {
if (m_Dataset == null) {
throw new UnassignedDatasetException("Instance doesn't have access to a dataset!");
}
return m_Dataset.numClasses();
}
/**
* Returns the number of values present. Always the same as numAttributes().
*
* @return the number of values
*/
public int numValues() {
return m_AttValues.length;
}
/**
* Replaces all missing values in the instance with the values contained in
* the given array. A deep copy of the vector of attribute values is
* performed before the values are replaced.
*
* @param array
* containing the means and modes
* @exception IllegalArgumentException
* if numbers of attributes are unequal
*/
public void replaceMissingValues(double[] array) {
if ((array == null) || (array.length != m_AttValues.length)) {
throw new IllegalArgumentException("Unequal number of attributes!");
}
freshAttributeVector();
for (int i = 0; i < m_AttValues.length; i++) {
if (isMissing(i)) {
m_AttValues[i] = array[i];
}
}
}
/**
* 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.
*
* @exception UnassignedClassException
* if the class is not set
* @exception UnassignedDatasetException
* if the instance doesn't have access to a dataset
*/
public void setClassMissing() {
if (classIndex() < 0) {
throw new UnassignedClassException("Class is not set!");
}
setMissing(classIndex());
}
/**
* 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.
*
* @param 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).
* @exception UnassignedClassException
* if the class is not set
* @exception UnaddignedDatasetException
* if the instance doesn't have access to a dataset
*/
public void setClassValue(double value) {
if (classIndex() < 0) {
throw new UnassignedClassException("Class is not set!");
}
setValue(classIndex(), 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.
*
* @param 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).
* @exception UnassignedClassException
* if the class is not set
* @exception UnassignedDatasetException
* if the dataset is not set
* @exception IllegalArgumentException
* if the attribute is not nominal or a string, or the value
* couldn't be found for a nominal attribute
*/
public final void setClassValue(String value) {
if (classIndex() < 0) {
throw new UnassignedClassException("Class is not set!");
}
setValue(classIndex(), value);
}
/**
* 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.
*
* @param instances
* the reference to the dataset
*/
public final void setDataset(Instances instances) {
m_Dataset = instances;
}
/**
* 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.
*
* @param attIndex
* the attribute's index
*/
public final void setMissing(int attIndex) {
setValue(attIndex, MISSING_VALUE);
}
/**
* 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.
*
* @param att
* the attribute
*/
public final void setMissing(Attribute att) {
setMissing(att.index());
}
/**
* 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.
*
* @param attIndex
* the attribute's index
* @param 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).
*/
public void setValue(int attIndex, double value) {
freshAttributeVector();
m_AttValues[attIndex] = 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. Does exactly the same thing as
* setValue().
*
* @param indexOfIndex
* the index of the attribute's index
* @param 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).
*/
public void setValueSparse(int indexOfIndex, double value) {
freshAttributeVector();
m_AttValues[indexOfIndex] = 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.
*
* @param attIndex
* the attribute's index
* @param value
* 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).
* @exception UnassignedDatasetException
* if the dataset is not set
* @exception IllegalArgumentException
* if the selected attribute is not nominal or a string, or
* the supplied value couldn't be found for a nominal
* attribute
*/
public final void setValue(int attIndex, String value) {
int valIndex;
if (m_Dataset == null) {
throw new UnassignedDatasetException("Instance doesn't have access to a dataset!");
}
if (!attribute(attIndex).isNominal() && !attribute(attIndex).isString()) {
throw new IllegalArgumentException("Attribute neither nominal nor string!");
}
valIndex = attribute(attIndex).indexOfValue(value);
if (valIndex == -1) {
if (attribute(attIndex).isNominal()) {
throw new IllegalArgumentException("Value not defined for given nominal attribute!");
} else {
attribute(attIndex).forceAddValue(value);
valIndex = attribute(attIndex).indexOfValue(value);
}
}
setValue(attIndex, (double) valIndex);
}
/**
* 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.
*
* @param att
* the attribute
* @param 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).
*/
public final void setValue(Attribute att, double value) {
setValue(att.index(), 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.
*
* @param att
* the attribute
* @param value
* 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).
* @exception IllegalArgumentException
* if the the attribute is not nominal or a string, or the
* value couldn't be found for a nominal attribute
*/
public final void setValue(Attribute att, String value) {
if (!att.isNominal() && !att.isString()) {
throw new IllegalArgumentException("Attribute neither nominal nor string!");
}
int valIndex = att.indexOfValue(value);
if (valIndex == -1) {
if (att.isNominal()) {
throw new IllegalArgumentException("Value not defined for given nominal attribute!");
} else {
att.forceAddValue(value);
valIndex = att.indexOfValue(value);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -