📄 categoricalattribute.java
字号:
* Returns category for a given key.
*
* @param key key for accessing category
* @return Category object for given key, null if no category for key found
*/
public Category getCategory( double key )
{
if ( Category.isMissingValue(key) )
return null;
Category value = null;
int index = (int) key;
if ( !Double.isInfinite(key) && index >= 0 && index < values.size() )
{
value = values.get(index);
}
return value;
}
/**
* Returns taxonomy for the categories of this categorical attribute.
*
* @return taxonomy for the categories of the attribute
*/
public CategoryHierarchy getTaxonomy()
{
return taxonomy;
}
/**
* Sets taxonomy for the categories of this categorical attribute.
*
* @param taxonomy taxonomy for the categories of the attribute
*/
public void setTaxonomy(CategoryHierarchy taxonomy)
{
this.taxonomy = taxonomy;
}
/**
* Number of categories is not known apriori fixed (default: false).
*
* @return true if number of categories is not fixed, otherwise false
*/
public boolean isUnboundedCategories()
{
return unboundedCategories;
}
/**
* Sets number of categories fixed (default: false).
*
* @param unboundedCategories number of categories not fixed
*/
public void setUnboundedCategories(boolean unboundedCategories)
{
this.unboundedCategories = unboundedCategories;
if (!unboundedCategories) {
unstoredCategories = false;
}
}
/**
* Number of categories is not apriori fixed and only the current
* category is stored (default: false). This is used when the categories
* of the attribute should not be stored in order to save memory.
* Examples are transaction identifiers.
*
* Unstored categories are automatically unbounded categories.
*
* @return true if categories not stored, otherwise false
*/
public boolean isUnstoredCategories() {
return unstoredCategories;
}
/**
* Set number of categories is not apriori fixed and only the current
* category is stored (default: false). This should be used when the
* categories of the attribute should not be stored in order to save memory.
* Examples are transaction identifiers.
*
* Attention: If set to true, also the variable unboundedCategories
* is automatically set to true and category list is cleared.
*
* @param unstoredCategories true if categories not stored, otherwise false
*/
public void setUnstoredCategories(boolean unstoredCategories) {
this.unstoredCategories = unstoredCategories;
if (unstoredCategories) {
this.unboundedCategories = true;
removeValues();
};
}
/**
* Returns reference to categorical attribute operations object owned by
* this categorical attribute.
*
* @return associated categorical attribute operations object
*/
public CategoricalAttributeOperations getCatAttOp()
{
return catAttOp;
}
// -----------------------------------------------------------------------
// Methods of PMML handling
// -----------------------------------------------------------------------
/**
* Create PMML object DataField for use in PMML documents.
*
* @return PMMLs DataField object
* @throws MiningException if can't create pmml representation for this attribute
* @see com.prudsys.pdm.Adapters.PmmlVersion20.DataField
*/
public Object createPmmlObject() throws MiningException
{
DataField dataField = new DataField();
dataField.setName( getName() );
dataField.setDisplayName( getName() );
dataField.setIsCyclic( "0" );
dataField.setOptype( "categorical" );
if (dataType == STRING) dataField.setDataType("string");
else if (dataType == DOUBLE) dataField.setDataType("double");
else if (dataType == FLOAT) dataField.setDataType("float");
else if (dataType == INTEGER) dataField.setDataType("integer");
else if (dataType == BOOLEAN) dataField.setDataType("boolean");
else if (dataType == DATETIME_PRUDSYS) dataField.setDataType("datePrudsys");
else if (dataType == DATETIME_UNIX) dataField.setDataType("dateUnix");
else if (dataType == USER_SPECIFIC) dataField.setDataType("userSpecific");
if (!unboundedCategories) {
int size = values.size();
Value[] value = new Value[ size ];
for (int j = 0; j < size; j++)
{
value[j] = new Value();
value[j].setValue( "" + values.get( j ) );
value[j].setDisplayValue( "" + values.get( j ) );
value[j].setProperty( "valid" );
}
dataField.setValue( value );
};
if (taxonomy != null) {
dataField.addTaxonomy( (Taxonomy) taxonomy.createPmmlObject() );
dataField.setTaxonomyRefName( taxonomy.getName() );
};
return dataField;
}
/**
* Reads from PMML object DataField.
*
* @param pmml PMMLs DataField object
* @see com.prudsys.pdm.Adapters.PmmlVersion20.DataField
* @exception MiningException cannot parse PMML object
*/
public void parsePmmlObject( Object pmml ) throws MiningException
{
DataField dataField = (DataField) pmml;
setName( dataField.getName() );
values = new ArrayList<Category>( 100 );
values2indexes = new Hashtable<Category, Double>( 100 );
category = null;
taxonomy = null;
unboundedCategories = false;
unstoredCategories = false;
catAttOp = new CategoricalAttributeOperations(this);
String dt = dataField.getDataType();
if (dt != null) {
if ( dt.equals("string") ) dataType = STRING;
else if ( dt.equals("double") ) dataType = DOUBLE;
else if ( dt.equals("float") ) dataType = FLOAT;
else if ( dt.equals("integer") ) dataType = INTEGER;
else if ( dt.equals("boolean") ) dataType = BOOLEAN;
else if ( dt.equals("datePrudsys") ) dataType = DATETIME_PRUDSYS;
else if ( dt.equals("dateUnix") ) dataType = DATETIME_UNIX;
else if ( dt.equals("userSpecific") ) dataType = USER_SPECIFIC;
}
Value[] value = dataField.getValue();
if (value.length == 0) unboundedCategories = true;
for (int j = 0; j < value.length; j++)
{
String attributeValue = value[j].getValue();
if (attributeValue == null) attributeValue = "";
addCategory( new Category(attributeValue) );
};
Taxonomy[] tax = dataField.getTaxonomy();
if (tax != null & tax.length > 0) {
CategoryHierarchy cah = new CategoryHierarchy();
cah.parsePmmlObject(tax[0]);
taxonomy = cah;
};
}
// -----------------------------------------------------------------------
// Other export methods
// Frank Xu, 01/12/2004
// Add single quotation mark to the attribute and the data,
// when converting data from SQL data source into ARFF format.
// -----------------------------------------------------------------------
/**
* Returns attribute as ARFF format description of the popular
* WEKA library.
*
* @return attribute information as ARFF description
*/
public String createArffDescription()
{
String arff = "@attribute" + " '" + name + "' ";
if (! unboundedCategories) {
arff = arff + "{";
int size = values.size();
for (int j = 0; j < size; j++) {
arff = arff + StringUtils.quote("" + values.get(j)) + ", ";
}
int i = arff.lastIndexOf(",");
if (i != -1) {
arff = arff.substring(0, i);
}
arff = arff + "}";
}
else {
arff = arff + "string";
};
return arff;
}
// -----------------------------------------------------------------------
// java.lang.Object methods
// -----------------------------------------------------------------------
/**
* Copies categorical attribute. Shallow copy.
*
* @return copy of categorical attribute
*/
public Object clone() {
CategoricalAttribute catAtt = new CategoricalAttribute();
catAtt.setName( this.getName() );
catAtt.dataType = this.dataType;
catAtt.values = this.values;
catAtt.values2indexes = this.values2indexes;
catAtt.category = this.category;
catAtt.taxonomy = this.taxonomy;
catAtt.unboundedCategories = this.unboundedCategories;
catAtt.unstoredCategories = this.unstoredCategories;
return catAtt;
}
/**
* Returns attribute as string.
*
* @return attribute information as string
*/
public String toString()
{
String description = name + ", ";
description = description + "type: categorical, " + "categories: " + "{";
int size = values.size();
for( int j = 0; j < size; j++ )
{
description = description + StringUtils.quote( "" + values.get( j ) ) + ", ";
}
int i = description.lastIndexOf( "," );
if( size > 0 )
{
description = description.substring( 0, i );
}
description = description + "}";
if (taxonomy != null)
description = description + ", taxonomy";
if (unboundedCategories)
description = description + ", unboundedCategories";
if (unstoredCategories)
description = description + ", unstoredCategories";
if (dataType != MiningAttribute.UNDEFINED) {
description = description + ", dataType: ";
if (dataType == STRING) description = description + "string";
else if (dataType == DOUBLE) description = description + "double";
else if (dataType == FLOAT) description = description + "float";
else if (dataType == INTEGER) description = description + "integer";
else if (dataType == BOOLEAN) description = description + "boolean";
else if (dataType == DATETIME_PRUDSYS) description = description + "datePrudsys";
else if (dataType == DATETIME_UNIX) description = description + "dateUnix";
else if (dataType == USER_SPECIFIC) description = description + "userSpecific";
}
return description;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -