📄 categnummapping.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 Rolf Rossius
* @author Michael Thess
* @version 1.0
*/
package com.prudsys.pdm.Transform.OneToOne;
import java.util.Enumeration;
import java.util.Hashtable;
import com.prudsys.pdm.Adapters.PmmlVersion20.DerivedField;
import com.prudsys.pdm.Adapters.PmmlVersion20.FieldColumnPair;
import com.prudsys.pdm.Adapters.PmmlVersion20.InlineTable;
import com.prudsys.pdm.Adapters.PmmlVersion20.MapValuesNum;
import com.prudsys.pdm.Adapters.PmmlVersion20.Member;
import com.prudsys.pdm.Adapters.PmmlVersion20.Pargroup;
import com.prudsys.pdm.Adapters.PmmlVersion20.Row;
import com.prudsys.pdm.Core.CategoricalAttribute;
import com.prudsys.pdm.Core.Category;
import com.prudsys.pdm.Core.MiningAttribute;
import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Core.NumericAttribute;
import com.prudsys.pdm.Transform.OneToOneMapping;
/**
* Realization of categorical to numerical mapping. In general, not invertable.
*
* Transforms a categorical into a numeric attribute
* by mapping categories onto numeric values. Mapping is given by a table.
*
* In the simple case of replacing categories by their keys, the Numerization
* transformation can be used instead of this class.
*
* @see Numerization
*/
public class CategNumMapping extends OneToOneMapping
{
// -----------------------------------------------------------------------
// Variables declarations
// -----------------------------------------------------------------------
/** Hashtable containing assignment of source to target values. */
private Hashtable map = new Hashtable( 100 );
// -----------------------------------------------------------------------
// Constructor
// -----------------------------------------------------------------------
/**
* Empty constructor.
*/
public CategNumMapping()
{
}
// -----------------------------------------------------------------------
// Getter and setter methods
// -----------------------------------------------------------------------
/**
* Returns map. Key is category of source attribute, value double value
* of target attribute.
*
* @return map
*/
public Hashtable getMap()
{
return map;
}
/**
* Sets map. Key is category of source attribute, value double object of
* target attribute.
*
* @param map new map
*/
public void setMap(Hashtable map)
{
this.map = map;
}
/**
* Adds new category-value pair to map.
*
* @param source category of source attribute
* @param target assigned double object of target attribute that will replace source category
* @exception MiningException thrown if source category already exists
*/
public void addCategoryPair(Category source, Double target)
throws MiningException {
Double val = (Double) map.get(source);
if (val != null)
throw new MiningException("Source category already exists in map");
map.put(source, target);
}
// -----------------------------------------------------------------------
// Transformation methods
// -----------------------------------------------------------------------
/**
* Transforms the categorical source attribute. The result is the
* new numeric target attribute where the double values are
* obtained by the map.
*
* @return transformed (mapped) attribute
* @exception MiningException could not transform attribute
*/
public MiningAttribute transformAttribute() throws MiningException
{
if (getSourceAttribute() == null)
throw new MiningException("Could not find source attribute: " + sourceName);
if (! (getSourceAttribute() instanceof CategoricalAttribute))
throw new MiningException("Source attribute '" + sourceName + "' must be categorical");
CategoricalAttribute sourceAttribute = (CategoricalAttribute) getSourceAttribute();
NumericAttribute transformedAttribute = new NumericAttribute();
transformedAttribute.setName( getTargetNameDynamic() );
return transformedAttribute;
}
/**
* Transforms key of a category of source attribute into the
* assigned value of the target attribute.
*
* @param attributeValue key of category to be mapped
* @return key of mapped category, missing value if no category for key found
* @exception MiningException could not transform attribute value
*/
public double transformAttributeValue( double attributeValue ) throws MiningException
{
CategoricalAttribute sourceAttribute = (CategoricalAttribute) getSourceAttribute();
Category cat = sourceAttribute.getCategory(attributeValue);
// attributeValue does not exist in source attribute:
if (cat == null)
return Category.MISSING_VALUE;
// value does not exist in map:
Double value = (Double) map.get(cat);
if (value == null)
return Category.MISSING_VALUE;
return value.doubleValue();
}
// -----------------------------------------------------------------------
// Methods of PMML handling
// -----------------------------------------------------------------------
/**
* Creates PMML object DerivedField of this object of MapValuesNum type.
*
* @return DerivedField element
* @exception MiningException could not create PMML object
* @see com.prudsys.pdm.Adapters.PmmlVersion20.MapValuesNum
*/
public Object createPmmlObject() throws MiningException
{
DerivedField field = (DerivedField) super.createPmmlObject();
MapValuesNum mapVal = new MapValuesNum();
mapVal.setOutputColumn("pargroup");
FieldColumnPair[] fcp = new FieldColumnPair[1];
fcp[0] = new FieldColumnPair();
fcp[0].setField(sourceName);
fcp[0].setColumn("member");
mapVal.setFieldColumnPair(fcp);
InlineTable table = new InlineTable();
Enumeration keys = map.keys();
while (keys.hasMoreElements()) {
Category key = (Category) keys.nextElement();
Double value = (Double) map.get( key );
Row row = new Row();
Member memb = new Member();
memb.setName( key.toString() );
row.setMember( memb );
Pargroup pgroup = new Pargroup();
pgroup.setName( value.toString() );
row.setPargroup( pgroup );
table.addRow(row);
};
mapVal.setInlineTable(table);
field.setMapValuesNum(mapVal);
return field;
}
/**
* Creates this object from PMML object DerivedField, subobject MapValuesNum.
*
* @param pmml pmml element
* @exception MiningException cannot parse PMML object
*/
public void parsePmmlObject(Object pmml) throws MiningException
{
super.parsePmmlObject(pmml);
DerivedField field = (DerivedField) pmml;
MapValuesNum mv = field.getMapValuesNum();
FieldColumnPair[] fcp = mv.getFieldColumnPair();
sourceName = fcp[0].getField();
InlineTable it = mv.getInlineTable();
Row[] rows = it.getRow();
if (rows == null) return;
// Read inline table and build graph:
map.clear();
for (int i = 0; i < rows.length; i++) {
Member memb = rows[i].getMember();
String childval = memb.getName();
Pargroup pgroup = rows[i].getPargroup();
String parval = pgroup.getName();
Double parent = new Double(parval);
Category child = new Category(childval);
addCategoryPair(child, parent);
};
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -