Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
The Waikato Environment for Knowledge Analysis (WEKA), a machine
learning workbench. This is the stable version. Apart from bugfixes, this version
does not receive any other updates.
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License
* along with this program. If not, see .
*/
/*
* Attribute.java
* Copyright (C) 1999-2012 University of Waikato, Hamilton, New Zealand
*
*/
package weka.core;
import java.io.Serializable;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import java.util.Properties;
/**
* Class for handling an attribute. Once an attribute has been created, it can't
* be changed.
*
*
* The following attribute types are supported:
*
*
numeric:
* This type of attribute represents a floating-point number.
*
nominal:
* This type of attribute represents a fixed set of nominal values.
*
string:
* This type of attribute represents a dynamically expanding set of nominal
* values. Usually used in text classification.
*
date:
* This type of attribute represents a date, internally represented as
* floating-point number storing the milliseconds since January 1, 1970,
* 00:00:00 GMT. The string representation of the date must be ISO-8601 compliant, the default is
* yyyy-MM-dd'T'HH:mm:ss.
*
relational:
* This type of attribute can contain other attributes and is, e.g., used for
* representing Multi-Instance data. (Multi-Instance data consists of a nominal
* attribute containing the bag-id, then a relational attribute with all the
* attributes of the bag, and finally the class attribute.)
*
*
* Typical usage (code from the main() method of this class):
*
*
*
* ...
*
* // Create numeric attributes "length" and "weight"
* Attribute length = new Attribute("length");
* Attribute weight = new Attribute("weight");
*
* // Create list to hold nominal values "first", "second", "third"
* List my_nominal_values = new ArrayList(3);
* my_nominal_values.add("first");
* my_nominal_values.add("second");
* my_nominal_values.add("third");
*
* // Create nominal attribute "position"
* Attribute position = new Attribute("position", my_nominal_values);
*
* ...
*
*
*
* @author Eibe Frank ([email protected])
* @version $Revision: 12593 $
*/
public class Attribute implements Copyable, Serializable, RevisionHandler {
/** for serialization */
static final long serialVersionUID = -742180568732916383L;
/** Constant set for numeric attributes. */
public static final int NUMERIC = 0;
/** Constant set for nominal attributes. */
public static final int NOMINAL = 1;
/** Constant set for attributes with string values. */
public static final int STRING = 2;
/** Constant set for attributes with date values. */
public static final int DATE = 3;
/** Constant set for relation-valued attributes. */
public static final int RELATIONAL = 4;
/** Constant set for symbolic attributes. */
public static final int ORDERING_SYMBOLIC = 0;
/** Constant set for ordered attributes. */
public static final int ORDERING_ORDERED = 1;
/** Constant set for modulo-ordered attributes. */
public static final int ORDERING_MODULO = 2;
/** The keyword used to denote the start of an arff attribute declaration */
public final static String ARFF_ATTRIBUTE = "@attribute";
/** A keyword used to denote a numeric attribute */
public final static String ARFF_ATTRIBUTE_INTEGER = "integer";
/** A keyword used to denote a numeric attribute */
public final static String ARFF_ATTRIBUTE_REAL = "real";
/** A keyword used to denote a numeric attribute */
public final static String ARFF_ATTRIBUTE_NUMERIC = "numeric";
/** The keyword used to denote a string attribute */
public final static String ARFF_ATTRIBUTE_STRING = "string";
/** The keyword used to denote a date attribute */
public final static String ARFF_ATTRIBUTE_DATE = "date";
/** The keyword used to denote a relation-valued attribute */
public final static String ARFF_ATTRIBUTE_RELATIONAL = "relational";
/** The keyword used to denote the end of the declaration of a subrelation */
public final static String ARFF_END_SUBRELATION = "@end";
/** Strings longer than this will be stored compressed. */
protected static final int STRING_COMPRESS_THRESHOLD = 200;
/** The attribute's name. */
protected final/* @ spec_public non_null @ */String m_Name;
/** The attribute's type. */
protected/* @ spec_public @ */int m_Type;
/*
* @ invariant m_Type == NUMERIC || m_Type == DATE || m_Type == STRING ||
* m_Type == NOMINAL || m_Type == RELATIONAL;
*/
/** The attribute info (null for numeric attributes) */
protected AttributeInfo m_AttributeInfo;
/** The attribute's index. */
protected/* @ spec_public @ */int m_Index = -1;
/** The attribute's weight. */
protected double m_Weight = 1.0;
/** The meta data for the attribute. */
protected AttributeMetaInfo m_AttributeMetaInfo;
/**
* Constructor for a numeric attribute.
*
* @param attributeName the name for the attribute
*/
// @ requires attributeName != null;
// @ ensures m_Name == attributeName;
public Attribute(String attributeName) {
this(attributeName, (ProtectedProperties)null);
}
/**
* Constructor for a numeric attribute, where metadata is supplied.
*
* @param attributeName the name for the attribute
* @param metadata the attribute's properties
*/
// @ requires attributeName != null;
// @ requires metadata != null;
// @ ensures m_Name == attributeName;
public Attribute(String attributeName, ProtectedProperties metadata) {
m_Name = attributeName;
if (metadata != null) {
m_AttributeMetaInfo = new AttributeMetaInfo(metadata, this);
}
}
/**
* Constructor for a date attribute.
*
* @param attributeName the name for the attribute
* @param dateFormat a string suitable for use with SimpleDateFormatter for
* parsing dates.
*/
// @ requires attributeName != null;
// @ requires dateFormat != null;
// @ ensures m_Name == attributeName;
public Attribute(String attributeName, String dateFormat) {
this(attributeName, dateFormat, (ProtectedProperties)null);
}
/**
* Constructor for a date attribute, where metadata is supplied.
*
* @param attributeName the name for the attribute
* @param dateFormat a string suitable for use with SimpleDateFormatter for
* parsing dates.
* @param metadata the attribute's properties
*/
// @ requires attributeName != null;
// @ requires dateFormat != null;
// @ requires metadata != null;
// @ ensures m_Name == attributeName;
public Attribute(String attributeName, String dateFormat,
ProtectedProperties metadata) {
m_Name = attributeName;
m_Type = DATE;
m_AttributeInfo = new DateAttributeInfo(dateFormat);
if (metadata != null) {
m_AttributeMetaInfo = new AttributeMetaInfo(metadata, this);
}
}
/**
* Constructor for nominal attributes and string attributes. If a null vector
* of attribute values is passed to the method, the attribute is assumed to be
* a string.
*
* @param attributeName the name for the attribute
* @param attributeValues a vector of strings denoting the attribute values.
* Null if the attribute is a string attribute.
*/
// @ requires attributeName != null;
// @ ensures m_Name == attributeName;
public Attribute(String attributeName, List attributeValues) {
this(attributeName, attributeValues, (ProtectedProperties)null);
}
/**
* Constructor for nominal attributes and string attributes, where metadata is
* supplied. If a null vector of attribute values is passed to the method, the
* attribute is assumed to be a string.
*
* @param attributeName the name for the attribute
* @param attributeValues a vector of strings denoting the attribute values.
* Null if the attribute is a string attribute.
* @param metadata the attribute's properties
*/
// @ requires attributeName != null;
// @ requires metadata != null;
/*
* @ ensures m_Name == attributeName; ensures m_Index == -1; ensures
* attributeValues == null && m_Type == STRING || attributeValues != null &&
* m_Type == NOMINAL && m_Values.size() == attributeValues.size(); signals
* (IllegalArgumentException ex) (* if duplicate strings in attributeValues
* *);
*/
public Attribute(String attributeName, List attributeValues,
ProtectedProperties metadata) {
m_Name = attributeName;
m_AttributeInfo = new NominalAttributeInfo(attributeValues, attributeName);
if (attributeValues == null) {
m_Type = STRING;
} else {
m_Type = NOMINAL;
}
if (metadata != null) {
m_AttributeMetaInfo = new AttributeMetaInfo(metadata, this);
}
}
/**
* Constructor for relation-valued attributes.
*
* @param attributeName the name for the attribute
* @param header an Instances object specifying the header of the relation.
*/
public Attribute(String attributeName, Instances header) {
this(attributeName, header, (ProtectedProperties)null);
}
/**
* Constructor for relation-valued attributes.
*
* @param attributeName the name for the attribute
* @param header an Instances object specifying the header of the relation.
* @param metadata the attribute's properties
*/
public Attribute(String attributeName, Instances header,
ProtectedProperties metadata) {
if (header.numInstances() > 0) {
throw new IllegalArgumentException("Header for relation-valued "
+ "attribute should not contain " + "any instances");
}
m_Name = attributeName;
m_Type = RELATIONAL;
m_AttributeInfo = new RelationalAttributeInfo(header);
if (metadata != null) {
m_AttributeMetaInfo = new AttributeMetaInfo(metadata, this);
}
}
/**
* Produces a shallow copy of this attribute.
*
* @return a copy of this attribute with the same index
*/
// @ also ensures \result instanceof Attribute;
@Override
public/* @ pure non_null @ */Object copy() {
return copy(m_Name);
}
/**
* Returns an enumeration of all the attribute's values if the attribute is
* nominal, string, or relation-valued, null otherwise.
*
* @return enumeration of all the attribute's values
*/
public final/* @ pure @ */Enumeration