📄 instance.java
字号:
/**
*
* AgentAcademy - an open source Data Mining framework for
* training intelligent agents
*
* Copyright (C) 2001-2003 AA Consortium.
*
* This library is open source software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General
* Public License as published by the Free Software Foundation;
* either version 2.0 of the License, or (at your option) any later
* version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
*/
package org.agentacademy.modules.dataminer.core;
/**
* <p>Title: The Data Miner prototype</p>
* <p>Description: A prototype for the DataMiner (DM), the Agent Academy (AA) module responsible for performing data mining on the contents of the Agent Use Repository (AUR). The extracted knowledge is to be sent back to the AUR in the form of a PMML document.</p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: CERTH</p>
* @author asymeon
* @version 0.3
*/
import java.util.*;
import java.io.*;
import org.apache.log4j.Logger;
/**
* Class for handling an instance. All values (numeric, nominal, or
* string) are internally stored as floating-point numbers. If an
* attribute is nominal (or a string), the stored value is the index
* of the corresponding nominal (or string) value in the attribute's
* definition. We have chosen this approach in favor of a more elegant
* object-oriented approach because it is much faster. <p>
*
* Typical usage (code from the main() method of this class): <p>
*
* <code>
* ... <br>
*
* // Create empty instance with three attribute values <br>
* Instance inst = new Instance(3); <br><br>
*
* // Set instance's values for the attributes "length", "weight", and "position"<br>
* inst.setValue(length, 5.3); <br>
* inst.setValue(weight, 300); <br>
* inst.setValue(position, "first"); <br><br>
*
* // Set instance's dataset to be the dataset "race" <br>
* inst.setDataset(race); <br><br>
*
* // Print the instance <br>
* System.out.println("The instance: " + inst); <br>
*
* ... <br>
* </code><p>
*
* 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.
*
*/
public class Instance implements Copyable, Serializable {
public static Logger log = Logger.getLogger(Instance.class);
/** Constant representing a missing value. */
protected final static double MISSING_VALUE = Double.NaN;
/**
* The dataset the instance has access to. Null if the instance
* doesn't have access to any dataset. Only if an instance has
* access to a dataset, it knows about the actual attribute types.
*/
protected Instances m_Dataset;
/** The instance's attribute values. */
protected double[] m_AttValues;
/** The instance's weight. */
protected double m_Weight;
/**
* Constructor that copies the attribute values and the weight from
* the given instance. Reference to the dataset is set to null.
* (ie. the instance doesn't have access to information about the
* attribute types)
*
* @param instance the instance from which the attribute
* values and the weight are to be copied
*/
public Instance(Instance instance) {
m_AttValues = instance.m_AttValues;
m_Weight = instance.m_Weight;
m_Dataset = null;
}
/**
* Constructor that inititalizes instance variable with given
* values. Reference to the dataset is set to null. (ie. the instance
* doesn't have access to information about the attribute types)
*
* @param weight the instance's weight
* @param attValues a vector of attribute values
*/
public Instance(double weight, double[] attValues){
m_AttValues = attValues;
m_Weight = weight;
m_Dataset = null;
}
/**
* Constructor of an instance that sets weight to one, all values to
* be missing, and the reference to the dataset to null. (ie. the instance
* doesn't have access to information about the attribute types)
*
* @param numAttributes the size of the instance
*/
public Instance(int numAttributes) {
m_AttValues = new double[numAttributes];
for (int i = 0; i < m_AttValues.length; i++) {
m_AttValues[i] = MISSING_VALUE;
}
m_Weight = 1;
m_Dataset = null;
}
/**
* Returns the attribute with the given index.
*
* @param index the attribute's index
* @return the attribute at the given position
* @exception UnassignedDatasetException if instance doesn't have access to a
* dataset
*/
public Attribute attribute(int index) {
if (m_Dataset == null) {
throw new UnassignedDatasetException("Instance doesn't have access to a dataset!");
}
return m_Dataset.attribute(index);
}
/**
* Returns the attribute with the given index. Does the same
* thing as attribute().
*
* @param indexOfIndex the index of the attribute's index
* @return the attribute at the given position
* @exception UnassignedDatasetException if instance doesn't have access to a
* dataset
*/
public Attribute attributeSparse(int indexOfIndex) {
if (m_Dataset == null) {
throw new UnassignedDatasetException("Instance doesn't have access to a dataset!");
}
return m_Dataset.attribute(indexOfIndex);
}
/**
* Returns class attribute.
*
* @return the class attribute
* @exception UnassignedDatasetException if the class is not set or the
* instance doesn't have access to a dataset
*/
public Attribute classAttribute() throws Exception{
if (m_Dataset == null) {
throw new UnassignedDatasetException("Instance doesn't have access to a dataset!");
}
return m_Dataset.classAttribute();
}
/**
* Returns the class attribute's index.
*
* @return the class index as an integer
* @exception UnassignedDatasetException if instance doesn't have access to a dataset
*/
public int classIndex() {
if (m_Dataset == null) {
throw new UnassignedDatasetException("Instance doesn't have access to a dataset!");
}
return m_Dataset.classIndex();
}
/**
* Tests if an instance's class is missing.
*
* @return true if the instance's class is missing
* @exception UnassignedClassException if the class is not set or the instance doesn't
* have access to a dataset
*/
public boolean classIsMissing() {
if (classIndex() < 0) {
throw new UnassignedClassException("Class is not set!");
}
return isMissing(classIndex());
}
/**
* Returns an instance's class value in internal format. (ie. as a
* floating-point number)
*
* @return 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).
* @exception UnassignedClassException if the class is not set or the instance doesn't
* have access to a dataset
*/
public double classValue() {
if (classIndex() < 0) {
throw new UnassignedClassException("Class is not set!");
}
return value(classIndex());
}
/**
* Produces a shallow copy of this instance. The copy has
* access to the same dataset. (if you want to make a copy
* that doesn't have access to the dataset, use
* <code>new Instance(instance)</code>
*
* @return the shallow copy
*/
public Object copy() {
Instance result = new Instance(this);
result.m_Dataset = m_Dataset;
return result;
}
/**
* 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.
*
* @return the dataset the instance has accesss to
*/
public Instances dataset() {
return m_Dataset;
}
/**
* 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.
*
* @param pos the attribute's position
* @exception RuntimeException if the instance has access to a
* dataset
*/
public void deleteAttributeAt(int position) {
if (m_Dataset != null) {
throw new RuntimeException("Instance has access to a dataset!");
}
forceDeleteAttributeAt(position);
}
/**
* Returns an enumeration of all the attributes.
*
* @return enumeration of all the attributes
* @exception UnassignedDatasetException if the instance doesn't
* have access to a dataset
*/
public Enumeration enumerateAttributes() {
if (m_Dataset == null) {
throw new UnassignedDatasetException("Instance doesn't have access to a dataset!");
}
return m_Dataset.enumerateAttributes();
}
/**
* Tests if the headers of two instances are equivalent.
*
* @param instance another instance
* @return true if the header of the given instance is
* equivalent to this instance's header
* @exception UnassignedDatasetException if instance doesn't have access to any
* dataset
*/
public boolean equalHeaders(Instance inst) {
if (m_Dataset == null) {
throw new UnassignedDatasetException("Instance doesn't have access to a dataset!");
}
return m_Dataset.equalHeaders(inst.m_Dataset);
}
/**
* Returns the index of the attribute stored at the given position.
* Just returns the given value.
*
* @param position the position
* @return the index of the attribute stored at the given position
*/
public int index(int position) {
return 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.
*
* @param pos the attribute's position
* @exception RuntimeException if the instance has accesss to a
* dataset
* @exception IllegalArgumentException if the position is out of range
*/
public void insertAttributeAt(int position) {
if (m_Dataset != null) {
throw new RuntimeException("Instance has accesss to a dataset!");
}
if ((position < 0) ||
(position > numAttributes())) {
throw new IllegalArgumentException("Can't insert attribute: index out "+
"of range");
}
forceInsertAttributeAt(position);
}
/**
* Tests if a specific value is "missing".
*
* @param attIndex the attribute's index
*/
public boolean isMissing(int attIndex) {
if (Double.isNaN(m_AttValues[attIndex])) {
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -