📄 categoricalattributeoperations.java
字号:
/*
* 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 2 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, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/**
* Title: XELOPES Data Mining Library
* Description: The XELOPES library is an open platform-independent and data-source-independent library for Embedded Data Mining.
* Copyright: Copyright (c) 2002 Prudential Systems Software GmbH
* Company: ZSoft (www.zsoft.ru), Prudsys (www.prudsys.com)
* @author Michael Thess
* @version 1.2
*/
package com.prudsys.pdm.Core;
import com.prudsys.pdm.Utils.IntVector;
/**
* Performs comparisons and basis transformations of categorical attribute. <p>
*
* From PDM CWM extension.
*
* @see CategoricalAttribute
*/
public class CategoricalAttributeOperations extends com.prudsys.pdm.Cwm.Core.Class
{
// -----------------------------------------------------------------------
// Variables declarations
// -----------------------------------------------------------------------
/** Reference to categorical attribute owning this object. */
protected CategoricalAttribute thisCatAtt;
/** Pretransformed categorical attribute. Used for caching. */
private CategoricalAttribute pretransCatAtt = null;
/** Transformed categorical attribute. Used for caching. */
private CategoricalAttribute transCatAtt = null;
/** Indexes of coordinate assignemnt. */
private IntVector indexAssign = new IntVector();
// -----------------------------------------------------------------------
// Protected constructor
// -----------------------------------------------------------------------
/**
* Protected constructor with categorical attribute object.
*
* @param catAtt categorical attribute owning this object
*/
protected CategoricalAttributeOperations(CategoricalAttribute catAtt)
{
this.thisCatAtt = catAtt;
}
// -----------------------------------------------------------------------
// Getter and setter methods
// -----------------------------------------------------------------------
/**
* Returns reference to categorical attribute object owning this object.
*
* @return categorical attribute object owning this object
*/
public CategoricalAttribute getThisCatAtt()
{
return thisCatAtt;
}
// -----------------------------------------------------------------------
// Basis transformation methods
// -----------------------------------------------------------------------
/**
* Transforms categorical attribute depending on the basis transformation type.
*
* Notice that this method can also be applied to a key without passed categorical
* attribute. In this case the last attribute basis transformation is used
* as transformation ressource.
* Such last attribute transformation could have been done either explicitly,
* applying transform to some categorical attribute, or implicitely, when a key
* with attribute was transformed. If none of both cases has ever happened,
* for the key (without attribute) an exception is thrown.
*
* @param catAtt categorical attribute of the key
* @param key key to be transformed with respect to this categorical attribute
* @return transformed key
* @exception MiningException cannot transform key
*/
public double transform(CategoricalAttribute catAtt, double key) throws MiningException {
// Transform categories:
if (catAtt != null) {
// Use caching if possible:
if (catAtt != pretransCatAtt) transform(catAtt);
}
else {
if (transCatAtt == null)
throw new MiningException("No categorical attribute ressource for basis trafo available");
}
// Get key:
int iKey = (int) key;
// Check key:
if (iKey >= indexAssign.size()) {
if (catAtt != null && catAtt.isUnboundedCategories())
transform(catAtt);
else
throw new MiningException("illegal key: " + key + " no category found for attribute: " + catAtt.getName());
}
// Transform key:
int transKey = indexAssign.IntegerAt(iKey);
return transKey;
}
/**
* Transforms categorical attribute depending on the basis transformation type.
*
* @param catAtt categorical attribute to transform
* @return transformed categorical attribute
* @exception MiningException cannot transform attribute
*/
public CategoricalAttribute transform(CategoricalAttribute catAtt) throws MiningException {
// Transformed categorical attribute;
CategoricalAttribute ca = new CategoricalAttribute( catAtt.getName() );
// Init:
int nThisCatAtt = thisCatAtt.getCategoriesNumber();
int nCatAtt = catAtt.getCategoriesNumber();
// Go through thisCatAtt and find thisCatAtt->catAtt mapping:
IntVector invAssign = new IntVector();
invAssign.setSize(nThisCatAtt);
for (int i = 0; i < nThisCatAtt; i++) {
Category cat = thisCatAtt.getCategory(i);
double key = catAtt.getKey(cat);
if ( !Category.isMissingValue(key) ) {
invAssign.setElementAt( (int) key, i);
ca.addCategory( catAtt.getCategory(key) ); // add category of catAtt
}
else {
invAssign.setElementAt( -1, i);
ca.addCategory(cat); // must add category of thisCatAdd for trafo
}
};
// Calculate inverse mapping:
indexAssign.setSize(nCatAtt);
for (int i = 0; i < nCatAtt; i++)
indexAssign.setElementAt(-1, i);
for (int i = 0; i < nThisCatAtt; i++) {
int ind = invAssign.IntegerAt(i);
if (ind > -1)
indexAssign.setElementAt(i, ind);
}
// Go through catAtt and find catAtt->thisCatAtt mapping:
int ind = nThisCatAtt;
for (int i = 0; i < nCatAtt; i++) {
if (indexAssign.IntegerAt(i) == -1) {
ca.addCategory( catAtt.getCategory(i) );
indexAssign.setElementAt(ind, i);
ind = ind + 1;
}
};
// Copy remaining variables of catAtt:
ca.setTaxonomy( catAtt.getTaxonomy() );
ca.setUnboundedCategories( catAtt.isUnboundedCategories() );
ca.setUnstoredCategories( catAtt.isUnstoredCategories() );
transCatAtt = ca;
// For caching:
pretransCatAtt = catAtt;
return transCatAtt;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -