程序包 weka.core
类 Instances
java.lang.Object
weka.core.Instances
- 所有已实现的接口:
Serializable
,RevisionHandler
- 直接已知子类:
IndividualInstances
,ReferenceInstances
Class for handling an ordered set of weighted instances.
Typical usage:
import weka.core.converters.ConverterUtils.DataSource; ... // Read all the instances in the file (ARFF, CSV, XRFF, ...) DataSource source = new DataSource(filename); Instances instances = source.getDataSet(); // Make the last attribute be the class instances.setClassIndex(instances.numAttributes() - 1); // Print header and instances. System.out.println("\nDataset:\n"); System.out.println(instances); ...
All methods that change a set of instances are safe, ie. a change of a set of instances does not affect any other sets of instances. All methods that change a datasets's attribute information clone the dataset before it is changed.
- 版本:
- $Revision: 10497 $
- 作者:
- Eibe Frank (eibe@cs.waikato.ac.nz), Len Trigg (trigg@cs.waikato.ac.nz), FracPete (fracpete at waikato dot ac dot nz)
- 另请参阅:
-
字段概要
字段修饰符和类型字段说明static final String
The keyword used to denote the start of the arff data sectionstatic final String
The keyword used to denote the start of an arff headerstatic final String
The filename extension that should be used for arff filesstatic final String
The filename extension that should be used for bin. -
构造器概要
构造器构造器说明Reads an ARFF file from a reader, and assigns a weight of one to each instance.已过时。Instances
(String name, FastVector attInfo, int capacity) Creates an empty set of instances.Constructor copying all instances and references to the header information from the given set of instances.Constructor creating an empty set of instances.Creates a new set of instances by copying a subset of another set. -
方法概要
修饰符和类型方法说明void
Adds one instance to the end of the set.attribute
(int index) Returns an attribute.Returns an attribute given its name.attributeStats
(int index) Calculates summary statistics on the values that appear in this set of instances for a specified attribute.double[]
attributeToDoubleArray
(int index) Gets the value of all instances in this dataset for a particular attribute.boolean
checkForAttributeType
(int attType) Checks for attributes of the given type in the datasetboolean
Checks for string attributes in the datasetboolean
checkInstance
(Instance instance) Checks if the given instance is compatible with this dataset.Returns the class attribute.int
Returns the class attribute's index.void
Compactifies the set of instances.void
delete()
Removes all instances from the set.void
delete
(int index) Removes an instance at the given position from the set.void
deleteAttributeAt
(int position) Deletes an attribute at the given position (0 to numAttributes() - 1).void
deleteAttributeType
(int attType) Deletes all attributes of the given type in the dataset.void
Deletes all string attributes in the dataset.void
deleteWithMissing
(int attIndex) Removes all instances with missing values for a particular attribute from the dataset.void
Removes all instances with missing values for a particular attribute from the dataset.void
Removes all instances with a missing class value from the dataset.Returns an enumeration of all the attributes.Returns an enumeration of all instances in the dataset.boolean
equalHeaders
(Instances dataset) Checks if two headers are equivalent.Returns the first instance in the set.getRandomNumberGenerator
(long seed) Returns a random number generator.Returns the revision string.void
insertAttributeAt
(Attribute att, int position) Inserts an attribute at the given position (0 to numAttributes()) and sets all values to be missing.instance
(int index) Returns the instance at the given position.double
kthSmallestValue
(int attIndex, int k) Returns the kth-smallest attribute value of a numeric attribute.double
kthSmallestValue
(Attribute att, int k) Returns the kth-smallest attribute value of a numeric attribute.Returns the last instance in the set.static void
Main method for this class.double
meanOrMode
(int attIndex) Returns the mean (mode) for a numeric (nominal) attribute as a floating-point value.double
meanOrMode
(Attribute att) Returns the mean (mode) for a numeric (nominal) attribute as a floating-point value.static Instances
mergeInstances
(Instances first, Instances second) Merges two sets of Instances together.int
Returns the number of attributes.int
Returns the number of class labels.int
numDistinctValues
(int attIndex) Returns the number of distinct values of a given attribute.int
Returns the number of distinct values of a given attribute.int
Returns the number of instances in the dataset.void
Shuffles the instances in the set so that they are ordered randomly.boolean
readInstance
(Reader reader) 已过时。instead of using this method in conjunction with thereadInstance(Reader)
method, one should use theArffLoader
orDataSource
class instead.Returns the relation's name.void
renameAttribute
(int att, String name) Renames an attribute.void
renameAttribute
(Attribute att, String name) Renames an attribute.void
renameAttributeValue
(int att, int val, String name) Renames the value of a nominal (or string) attribute value.void
renameAttributeValue
(Attribute att, String val, String name) Renames the value of a nominal (or string) attribute value.Creates a new dataset of the same size using random sampling with replacement.resampleWithWeights
(Random random) Creates a new dataset of the same size using random sampling with replacement according to the current instance weights.resampleWithWeights
(Random random, boolean[] sampled) Creates a new dataset of the same size using random sampling with replacement according to the current instance weights.resampleWithWeights
(Random random, double[] weights) Creates a new dataset of the same size using random sampling with replacement according to the given weight vector.resampleWithWeights
(Random random, double[] weights, boolean[] sampled) Creates a new dataset of the same size using random sampling with replacement according to the given weight vector.void
Sets the class attribute.void
setClassIndex
(int classIndex) Sets the class index of the set.void
setRelationName
(String newName) Sets the relation's name.void
sort
(int attIndex) Sorts the instances based on an attribute.void
Sorts the instances based on an attribute.void
stratify
(int numFolds) Stratifies a set of instances according to its class values if the class attribute is nominal (so that afterwards a stratified cross-validation can be performed).Create a copy of the structure if the data has string or relational attributes, "cleanses" string types (i.e.double
Computes the sum of all the instances' weights.void
swap
(int i, int j) Swaps two instances in the set.static void
Method for testing this class.testCV
(int numFolds, int numFold) Creates the test set for one fold of a cross-validation on the dataset.toString()
Returns the dataset as a string in ARFF format.Generates a string summarizing the set of instances.trainCV
(int numFolds, int numFold) Creates the training set for one fold of a cross-validation on the dataset.Creates the training set for one fold of a cross-validation on the dataset.double
variance
(int attIndex) Computes the variance for a numeric attribute.double
Computes the variance for a numeric attribute.
-
字段详细资料
-
FILE_EXTENSION
The filename extension that should be used for arff files- 另请参阅:
-
SERIALIZED_OBJ_FILE_EXTENSION
The filename extension that should be used for bin. serialized instances files- 另请参阅:
-
ARFF_RELATION
The keyword used to denote the start of an arff header- 另请参阅:
-
ARFF_DATA
The keyword used to denote the start of the arff data section- 另请参阅:
-
-
构造器详细资料
-
Instances
Reads an ARFF file from a reader, and assigns a weight of one to each instance. Lets the index of the class attribute be undefined (negative).- 参数:
reader
- the reader- 抛出:
IOException
- if the ARFF file is not read successfully
-
Instances
已过时。instead of using this method in conjunction with thereadInstance(Reader)
method, one should use theArffLoader
orDataSource
class instead.Reads the header of an ARFF file from a reader and reserves space for the given number of instances. Lets the class index be undefined (negative).- 参数:
reader
- the readercapacity
- the capacity- 抛出:
IllegalArgumentException
- if the header is not read successfully or the capacity is negative.IOException
- if there is a problem with the reader.- 另请参阅:
-
Instances
Constructor copying all instances and references to the header information from the given set of instances.- 参数:
dataset
- the set to be copied
-
Instances
Constructor creating an empty set of instances. Copies references to the header information from the given set of instances. Sets the capacity of the set of instances to 0 if its negative.- 参数:
dataset
- the instances from which the header information is to be takencapacity
- the capacity of the new dataset
-
Instances
Creates a new set of instances by copying a subset of another set.- 参数:
source
- the set of instances from which a subset is to be createdfirst
- the index of the first instance to be copiedtoCopy
- the number of instances to be copied- 抛出:
IllegalArgumentException
- if first and toCopy are out of range
-
Instances
Creates an empty set of instances. Uses the given attribute information. Sets the capacity of the set of instances to 0 if its negative. Given attribute information must not be changed after this constructor has been used.- 参数:
name
- the name of the relationattInfo
- the attribute informationcapacity
- the capacity of the set
-
-
方法详细资料
-
stringFreeStructure
Create a copy of the structure if the data has string or relational attributes, "cleanses" string types (i.e. doesn't contain references to the strings seen in the past) and all relational attributes.- 返回:
- a copy of the instance structure.
-
add
Adds one instance to the end of the set. Shallow copies instance before it is added. Increases the size of the dataset if it is not large enough. Does not check if the instance is compatible with the dataset. Note: String or relational values are not transferred.- 参数:
instance
- the instance to be added
-
attribute
Returns an attribute.- 参数:
index
- the attribute's index (index starts with 0)- 返回:
- the attribute at the given position
-
attribute
Returns an attribute given its name. If there is more than one attribute with the same name, it returns the first one. Returns null if the attribute can't be found.- 参数:
name
- the attribute's name- 返回:
- the attribute with the given name, null if the attribute can't be found
-
checkForAttributeType
public boolean checkForAttributeType(int attType) Checks for attributes of the given type in the dataset- 参数:
attType
- the attribute type to look for- 返回:
- true if attributes of the given type are present
-
checkForStringAttributes
public boolean checkForStringAttributes()Checks for string attributes in the dataset- 返回:
- true if string attributes are present, false otherwise
-
checkInstance
Checks if the given instance is compatible with this dataset. Only looks at the size of the instance and the ranges of the values for nominal and string attributes.- 参数:
instance
- the instance to check- 返回:
- true if the instance is compatible with the dataset
-
classAttribute
Returns the class attribute.- 返回:
- the class attribute
- 抛出:
UnassignedClassException
- if the class is not set
-
classIndex
public int classIndex()Returns the class attribute's index. Returns negative number if it's undefined.- 返回:
- the class index as an integer
-
compactify
public void compactify()Compactifies the set of instances. Decreases the capacity of the set so that it matches the number of instances in the set. -
delete
public void delete()Removes all instances from the set. -
delete
public void delete(int index) Removes an instance at the given position from the set.- 参数:
index
- the instance's position (index starts with 0)
-
deleteAttributeAt
public void deleteAttributeAt(int position) Deletes an attribute at the given position (0 to numAttributes() - 1). A deep copy of the attribute information is performed before the attribute is deleted.- 参数:
position
- the attribute's position (position starts with 0)- 抛出:
IllegalArgumentException
- if the given index is out of range or the class attribute is being deleted
-
deleteAttributeType
public void deleteAttributeType(int attType) Deletes all attributes of the given type in the dataset. A deep copy of the attribute information is performed before an attribute is deleted.- 参数:
attType
- the attribute type to delete- 抛出:
IllegalArgumentException
- if attribute couldn't be successfully deleted (probably because it is the class attribute).
-
deleteStringAttributes
public void deleteStringAttributes()Deletes all string attributes in the dataset. A deep copy of the attribute information is performed before an attribute is deleted.- 抛出:
IllegalArgumentException
- if string attribute couldn't be successfully deleted (probably because it is the class attribute).- 另请参阅:
-
deleteWithMissing
public void deleteWithMissing(int attIndex) Removes all instances with missing values for a particular attribute from the dataset.- 参数:
attIndex
- the attribute's index (index starts with 0)
-
deleteWithMissing
Removes all instances with missing values for a particular attribute from the dataset.- 参数:
att
- the attribute
-
deleteWithMissingClass
public void deleteWithMissingClass()Removes all instances with a missing class value from the dataset.- 抛出:
UnassignedClassException
- if class is not set
-
enumerateAttributes
Returns an enumeration of all the attributes. The class attribute (if set) is skipped by this enumeration.- 返回:
- enumeration of all the attributes.
-
enumerateInstances
Returns an enumeration of all instances in the dataset.- 返回:
- enumeration of all instances in the dataset
-
equalHeaders
Checks if two headers are equivalent.- 参数:
dataset
- another dataset- 返回:
- true if the header of the given dataset is equivalent to this header
-
firstInstance
Returns the first instance in the set.- 返回:
- the first instance in the set
-
getRandomNumberGenerator
Returns a random number generator. The initial seed of the random number generator depends on the given seed and the hash code of a string representation of a instances chosen based on the given seed.- 参数:
seed
- the given seed- 返回:
- the random number generator
-
insertAttributeAt
Inserts an attribute at the given position (0 to numAttributes()) and sets all values to be missing. Shallow copies the attribute before it is inserted, and performs a deep copy of the existing attribute information.- 参数:
att
- the attribute to be insertedposition
- the attribute's position (position starts with 0)- 抛出:
IllegalArgumentException
- if the given index is out of range
-
instance
Returns the instance at the given position.- 参数:
index
- the instance's index (index starts with 0)- 返回:
- the instance at the given position
-
kthSmallestValue
Returns the kth-smallest attribute value of a numeric attribute.- 参数:
att
- the Attribute objectk
- the value of k- 返回:
- the kth-smallest value
-
kthSmallestValue
public double kthSmallestValue(int attIndex, int k) Returns the kth-smallest attribute value of a numeric attribute. NOTE CHANGE: Missing values (NaN values) are now treated as Double.MAX_VALUE. Also, the order of the instances in the data is no longer affected.- 参数:
attIndex
- the attribute's indexk
- the value of k- 返回:
- the kth-smallest value
-
lastInstance
Returns the last instance in the set.- 返回:
- the last instance in the set
-
meanOrMode
public double meanOrMode(int attIndex) Returns the mean (mode) for a numeric (nominal) attribute as a floating-point value. Returns 0 if the attribute is neither nominal nor numeric. If all values are missing it returns zero.- 参数:
attIndex
- the attribute's index (index starts with 0)- 返回:
- the mean or the mode
-
meanOrMode
Returns the mean (mode) for a numeric (nominal) attribute as a floating-point value. Returns 0 if the attribute is neither nominal nor numeric. If all values are missing it returns zero.- 参数:
att
- the attribute- 返回:
- the mean or the mode
-
numAttributes
public int numAttributes()Returns the number of attributes.- 返回:
- the number of attributes as an integer
-
numClasses
public int numClasses()Returns the number of class labels.- 返回:
- the number of class labels as an integer if the class attribute is nominal, 1 otherwise.
- 抛出:
UnassignedClassException
- if the class is not set
-
numDistinctValues
public int numDistinctValues(int attIndex) Returns the number of distinct values of a given attribute. Returns the number of instances if the attribute is a string attribute. The value 'missing' is not counted.- 参数:
attIndex
- the attribute (index starts with 0)- 返回:
- the number of distinct values of a given attribute
-
numDistinctValues
Returns the number of distinct values of a given attribute. Returns the number of instances if the attribute is a string attribute. The value 'missing' is not counted.- 参数:
att
- the attribute- 返回:
- the number of distinct values of a given attribute
-
numInstances
public int numInstances()Returns the number of instances in the dataset.- 返回:
- the number of instances in the dataset as an integer
-
randomize
Shuffles the instances in the set so that they are ordered randomly.- 参数:
random
- a random number generator
-
readInstance
已过时。instead of using this method in conjunction with thereadInstance(Reader)
method, one should use theArffLoader
orDataSource
class instead.Reads a single instance from the reader and appends it to the dataset. Automatically expands the dataset if it is not large enough to hold the instance. This method does not check for carriage return at the end of the line.- 参数:
reader
- the reader- 返回:
- false if end of file has been reached
- 抛出:
IOException
- if the information is not read successfully- 另请参阅:
-
relationName
Returns the relation's name.- 返回:
- the relation's name as a string
-
renameAttribute
Renames an attribute. This change only affects this dataset.- 参数:
att
- the attribute's index (index starts with 0)name
- the new name
-
renameAttribute
Renames an attribute. This change only affects this dataset.- 参数:
att
- the attributename
- the new name
-
renameAttributeValue
Renames the value of a nominal (or string) attribute value. This change only affects this dataset.- 参数:
att
- the attribute's index (index starts with 0)val
- the value's index (index starts with 0)name
- the new name
-
renameAttributeValue
Renames the value of a nominal (or string) attribute value. This change only affects this dataset.- 参数:
att
- the attributeval
- the valuename
- the new name
-
resample
Creates a new dataset of the same size using random sampling with replacement.- 参数:
random
- a random number generator- 返回:
- the new dataset
-
resampleWithWeights
Creates a new dataset of the same size using random sampling with replacement according to the current instance weights. The weights of the instances in the new dataset are set to one.- 参数:
random
- a random number generator- 返回:
- the new dataset
-
resampleWithWeights
Creates a new dataset of the same size using random sampling with replacement according to the current instance weights. The weights of the instances in the new dataset are set to one. See also resampleWithWeights(Random, double[], boolean[]).- 参数:
random
- a random number generatorsampled
- an array indicating what has been sampled- 返回:
- the new dataset
-
resampleWithWeights
Creates a new dataset of the same size using random sampling with replacement according to the given weight vector. See also resampleWithWeights(Random, double[], boolean[]).- 参数:
random
- a random number generatorweights
- the weight vector- 返回:
- the new dataset
- 抛出:
IllegalArgumentException
- if the weights array is of the wrong length or contains negative weights.
-
resampleWithWeights
Creates a new dataset of the same size using random sampling with replacement according to the given weight vector. The weights of the instances in the new dataset are set to one. The length of the weight vector has to be the same as the number of instances in the dataset, and all weights have to be positive. Uses Walker's method, see pp. 232 of "Stochastic Simulation" by B.D. Ripley (1987).- 参数:
random
- a random number generatorweights
- the weight vectorsampled
- an array indicating what has been sampled, can be null- 返回:
- the new dataset
- 抛出:
IllegalArgumentException
- if the weights array is of the wrong length or contains negative weights.
-
setClass
Sets the class attribute.- 参数:
att
- attribute to be the class
-
setClassIndex
public void setClassIndex(int classIndex) Sets the class index of the set. If the class index is negative there is assumed to be no class. (ie. it is undefined)- 参数:
classIndex
- the new class index (index starts with 0)- 抛出:
IllegalArgumentException
- if the class index is too big or < 0
-
setRelationName
Sets the relation's name.- 参数:
newName
- the new relation name.
-
sort
public void sort(int attIndex) Sorts the instances based on an attribute. For numeric attributes, instances are sorted in ascending order. For nominal attributes, instances are sorted based on the attribute label ordering specified in the header. Instances with missing values for the attribute are placed at the end of the dataset.- 参数:
attIndex
- the attribute's index (index starts with 0)
-
sort
Sorts the instances based on an attribute. For numeric attributes, instances are sorted into ascending order. For nominal attributes, instances are sorted based on the attribute label ordering specified in the header. Instances with missing values for the attribute are placed at the end of the dataset.- 参数:
att
- the attribute
-
stratify
public void stratify(int numFolds) Stratifies a set of instances according to its class values if the class attribute is nominal (so that afterwards a stratified cross-validation can be performed).- 参数:
numFolds
- the number of folds in the cross-validation- 抛出:
UnassignedClassException
- if the class is not set
-
sumOfWeights
public double sumOfWeights()Computes the sum of all the instances' weights.- 返回:
- the sum of all the instances' weights as a double
-
testCV
Creates the test set for one fold of a cross-validation on the dataset.- 参数:
numFolds
- the number of folds in the cross-validation. Must be greater than 1.numFold
- 0 for the first fold, 1 for the second, ...- 返回:
- the test set as a set of weighted instances
- 抛出:
IllegalArgumentException
- if the number of folds is less than 2 or greater than the number of instances.
-
toString
Returns the dataset as a string in ARFF format. Strings are quoted if they contain whitespace characters, or if they are a question mark. -
trainCV
Creates the training set for one fold of a cross-validation on the dataset.- 参数:
numFolds
- the number of folds in the cross-validation. Must be greater than 1.numFold
- 0 for the first fold, 1 for the second, ...- 返回:
- the training set
- 抛出:
IllegalArgumentException
- if the number of folds is less than 2 or greater than the number of instances.
-
trainCV
Creates the training set for one fold of a cross-validation on the dataset. The data is subsequently randomized based on the given random number generator.- 参数:
numFolds
- the number of folds in the cross-validation. Must be greater than 1.numFold
- 0 for the first fold, 1 for the second, ...random
- the random number generator- 返回:
- the training set
- 抛出:
IllegalArgumentException
- if the number of folds is less than 2 or greater than the number of instances.
-
variance
public double variance(int attIndex) Computes the variance for a numeric attribute.- 参数:
attIndex
- the numeric attribute (index starts with 0)- 返回:
- the variance if the attribute is numeric
- 抛出:
IllegalArgumentException
- if the attribute is not numeric
-
variance
Computes the variance for a numeric attribute.- 参数:
att
- the numeric attribute- 返回:
- the variance if the attribute is numeric
- 抛出:
IllegalArgumentException
- if the attribute is not numeric
-
attributeStats
Calculates summary statistics on the values that appear in this set of instances for a specified attribute.- 参数:
index
- the index of the attribute to summarize (index starts with 0)- 返回:
- an AttributeStats object with it's fields calculated.
-
attributeToDoubleArray
public double[] attributeToDoubleArray(int index) Gets the value of all instances in this dataset for a particular attribute. Useful in conjunction with Utils.sort to allow iterating through the dataset in sorted order for some attribute.- 参数:
index
- the index of the attribute.- 返回:
- an array containing the value of the desired attribute for each instance in the dataset.
-
toSummaryString
Generates a string summarizing the set of instances. Gives a breakdown for each attribute indicating the number of missing/discrete/unique values and other information.- 返回:
- a string summarizing the dataset
-
swap
public void swap(int i, int j) Swaps two instances in the set.- 参数:
i
- the first instance's index (index starts with 0)j
- the second instance's index (index starts with 0)
-
mergeInstances
Merges two sets of Instances together. The resulting set will have all the attributes of the first set plus all the attributes of the second set. The number of instances in both sets must be the same.- 参数:
first
- the first set of Instancessecond
- the second set of Instances- 返回:
- the merged set of Instances
- 抛出:
IllegalArgumentException
- if the datasets are not the same size
-
test
Method for testing this class.- 参数:
argv
- should contain one element: the name of an ARFF file
-
main
Main method for this class. The following calls are possible:-
weka.core.Instances
help
prints a short list of possible commands. -
weka.core.Instances
<filename>
prints a summary of a set of instances. -
weka.core.Instances
merge <filename1> <filename2>
merges the two datasets (must have same number of instances) and outputs the results on stdout. -
weka.core.Instances
append <filename1> <filename2>
appends the second dataset to the first one (must have same headers) and outputs the results on stdout. -
weka.core.Instances
headers <filename1> <filename2>
Compares the headers of the two datasets and prints whether they match or not. -
weka.core.Instances
randomize <seed> <filename>
randomizes the dataset with the given seed and outputs the result on stdout.
- 参数:
args
- the commandline parameters
-
-
getRevision
Returns the revision string.- 指定者:
getRevision
在接口中RevisionHandler
- 返回:
- the revision
-
readInstance(Reader)
method, one should use theArffLoader
orDataSource
class instead.