📄 categmapping.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.1
*/
package com.prudsys.pdm.Transform.OneToOne;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Vector;
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.MapValues;
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.Transform.OneToOneMapping;
/**
* Realization of categorical mapping. In general, not invertable.
*
* Transforms a categorical into another categorical attribute
* by mapping categories onto other ones. Mapping is given by a table.
*/
public class CategMapping extends OneToOneMapping
{
// -----------------------------------------------------------------------
// Variables declarations
// -----------------------------------------------------------------------
/** Hashtable containing assignment of source to target categories. */
private Hashtable map = new Hashtable( 100 );
/** Ensures right order of target categories through source categories. */
private Vector sourceVec = new Vector( 100 );
/** Transformed attribute. */
private CategoricalAttribute transformedAttribute = null;
// -----------------------------------------------------------------------
// Constructor
// -----------------------------------------------------------------------
/**
* Empty constructor.
*/
public CategMapping()
{
}
// -----------------------------------------------------------------------
// Getter and setter methods
// -----------------------------------------------------------------------
/**
* Returns vector of source categories. This vector has the same order
* as the source categories were added by the addCategoryPair method.
*
* @return vector of source categories.
*/
public Vector getSourceVec() {
return sourceVec;
}
/**
* Returns map. Key is category of source attribute, value category of target
* attribute.
*
* @return map
*/
public Hashtable getMap()
{
return map;
}
/**
* Adds new category pair to map. Categories of target attribute will be stored
* in the same order as this method is used.
*
* @param source category of source attribute
* @param target assigned category of target attribute that will replace source category
* @exception MiningException thrown if source category already exists
*/
public void addCategoryPair(Category source, Category target)
throws MiningException {
Category cat = (Category) map.get(source);
if (cat != null)
throw new MiningException("Source category already exists in map");
map.put(source, target);
sourceVec.addElement(source);
}
// -----------------------------------------------------------------------
// Transformation methods
// -----------------------------------------------------------------------
/**
* Transforms the categorical source attribute. The result is the
* new categorical target attribute where the categories 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();
transformedAttribute = new CategoricalAttribute();
transformedAttribute.setName( getTargetNameDynamic() );
ArrayList categs = new ArrayList();
for (int i = 0; i < sourceVec.size(); i++) {
Category value = (Category) map.get( (Category) sourceVec.elementAt(i) );
if (! categs.contains(value))
categs.add(value);
};
try {
transformedAttribute.setValues(categs);
}
catch (MiningException ex) {;};
return transformedAttribute;
}
/**
* Transforms key of a category of source attribute into the
* key of the assigned category 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 dous not exist in source attribute:
if (cat == null)
return Category.MISSING_VALUE;
// value does not exist in map:
Category value = (Category) map.get(cat);
if (value == null)
return Category.MISSING_VALUE;
return transformedAttribute.getKey(value);
}
// -----------------------------------------------------------------------
// Methods of PMML handling
// -----------------------------------------------------------------------
/**
* Creates PMML object DerivedField of this object of MapValues type.
*
* @return DerivedField element
* @exception MiningException could not create PMML object
* @see com.prudsys.pdm.Adapters.PmmlVersion20.MapValues
*/
public Object createPmmlObject() throws MiningException
{
DerivedField field = (DerivedField) super.createPmmlObject();
MapValues mapVal = new MapValues();
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();
ArrayList categs = new ArrayList();
for (int i = 0; i < sourceVec.size(); i++) {
Category key = (Category) sourceVec.elementAt(i);
Category value = (Category) map.get( key );
if (! categs.contains(value))
categs.add(value);
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.setMapValues(mapVal);
return field;
}
/**
* Creates this object from PMML object DerivedField, subobject MapValues.
*
* @param pmml pmml element
* @exception MiningException always thrown
*/
public void parsePmmlObject(Object pmml) throws MiningException
{
super.parsePmmlObject(pmml);
DerivedField field = (DerivedField) pmml;
MapValues mv = field.getMapValues();
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();
transformedAttribute = null;
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();
Category parent = new Category(parval);
Category child = new Category(childval);
addCategoryPair(child, parent);
};
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -