📄 linearnormal.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.0
*/
package com.prudsys.pdm.Transform.OneToOne;
import com.prudsys.pdm.Adapters.PmmlVersion20.DerivedField;
import com.prudsys.pdm.Adapters.PmmlVersion20.LinearNorm;
import com.prudsys.pdm.Adapters.PmmlVersion20.NormContinuous;
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.Models.Statistics.Group;
import com.prudsys.pdm.Transform.OneToOneMapping;
/**
* Realization of linear normalization. By default, the
* (0,1) normalization is used.
*
* The value of minimum and maximum can be set via the
* corresponding setter methods. If a statistics model
* object is passed, they are taken from this.
*
* Missing values are transformed into missing values,
* outliers can be handled on three different ways:
* 1. as is, 2. as missing values, 3. as extreme values
* with projection.
*/
public class LinearNormal extends OneToOneMapping
{
// -----------------------------------------------------------------------
// Constants of types of outlier treatment
// -----------------------------------------------------------------------
/** Treatmeant of outliers as is. */
public static final String OUTLIER_TREATMENT_METHOD_asIs = "asIs";
/** Treatment of outliers as missing values. */
public static final String OUTLIER_TREATMENT_METHOD_asMissingValues = "asMissingValues";
/** Treatment of values as extreme values, i.e. by projection to bounds. */
public static final String OUTLIER_TREATMENT_METHOD_asExtremeValues = "asExtremeValues";
/** Minimum span to be treated as positiv value. */
public static final double IDENTITY_EPSILON = 0.00000000001;
// -----------------------------------------------------------------------
// Variables declarations
// -----------------------------------------------------------------------
/** Minimum value of attribute. */
private double min = 0.0;
/** Maximum value of attribute. */
private double max = 0.0;
/** Lower bound of attribute. */
private double lowerBound = 0.0;
/** Upper bound of attribute. */
private double upperBound = 1.0;
/** Treatment of outliers, values outside interval [min, max]. */
private String outliers = OUTLIER_TREATMENT_METHOD_asIs;
// -----------------------------------------------------------------------
// Constructor
// -----------------------------------------------------------------------
/**
* Empty constructor.
*/
public LinearNormal()
{
}
// -----------------------------------------------------------------------
// Getter and setter methods
// -----------------------------------------------------------------------
/**
* Sets minimum value.
*
* @param min new minimum value
*/
public void setMin(double min)
{
this.min = min;
}
/**
* Returns minimum value.
*
* @return minimum value
*/
public double getMin()
{
return min;
}
/**
* Sets maximum value.
*
* @param max new maximum value
*/
public void setMax(double max)
{
this.max = max;
}
/**
* Returns maximum value.
*
* @return maximum value
*/
public double getMax()
{
return max;
}
/**
* Sets lower bound of normalized values (default 0).
*
* @param lowerBound new lower bound of normalized values
*/
public void setLowerBound(double lowerBound)
{
this.lowerBound = lowerBound;
}
/**
* Returns lower bound of normalized values (default 0).
*
* @return lower bound of normalized values
*/
public double getLowerBound()
{
return lowerBound;
}
/**
* Sets upper bound of normalized values (default 1).
*
* @param upperBound new upper bound of normalized values
*/
public void setUpperBound(double upperBound)
{
this.upperBound = upperBound;
}
/**
* Returns upper bound of normalized values (default 1).
*
* @return upper bound of normalized values
*/
public double getUpperBound()
{
return upperBound;
}
/**
* Sets treatment type of outliers.
*
* @param outliers new treatment type of outliers
*/
public void setOutliers(String outliers)
{
this.outliers = outliers;
}
/**
* Returns treatment type of outliers.
*
* @return treatment type of outliers
*/
public String getOutliers()
{
return outliers;
}
// -----------------------------------------------------------------------
// Transformation methods
// -----------------------------------------------------------------------
/**
* Transforms the source attribute. The result is the target attribute.
*
* @return transformed (normalized) 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 NumericAttribute))
throw new MiningException("Source attribute '" + sourceName + "' must be numeric");
NumericAttribute sourceAttribute = (NumericAttribute) getSourceAttribute();
NumericAttribute transformedAttribute = (NumericAttribute) sourceAttribute.clone();
transformedAttribute.setName( getTargetNameDynamic() );
if (outliers == OUTLIER_TREATMENT_METHOD_asIs)
{
// For "As Is" the upper/lower bound is un-predictable, so set infinity
transformedAttribute.setLowerBound( Double.NEGATIVE_INFINITY );
transformedAttribute.setUpperBound( Double.POSITIVE_INFINITY );
}else
{
transformedAttribute.setLowerBound( lowerBound );
transformedAttribute.setUpperBound( upperBound );
}
if (statisticsMiningModel != null) {
Group group = statisticsMiningModel.getRootGroup();
min = group.getMin();
max = group.getMax();
};
return transformedAttribute;
}
/**
* Transforms attribute value. The result is also a value.
*
* @param attributeValue value of attribute to be transformed
* @return transformed (normalized) value
* @exception MiningException could not transform attribute value
*/
public double transformAttributeValue( double attributeValue ) throws MiningException
{
// Missing value:
if (Category.isMissingValue(attributeValue))
return attributeValue;
// Outlier:
if ( !outliers.equals(OUTLIER_TREATMENT_METHOD_asIs) ) {
if (attributeValue < min || attributeValue > max) {
if ( outliers.equals(OUTLIER_TREATMENT_METHOD_asMissingValues) )
return Category.MISSING_VALUE;
// Treat as extreme values:
else {
if (attributeValue < min)
return lowerBound; // projection to lower bound
else
return upperBound; // projection to upper bound
}
}
}
// Normalization:
if (Math.abs(max - min) < IDENTITY_EPSILON)
return lowerBound;
double transformedValue = lowerBound +
(upperBound - lowerBound)*(attributeValue - min)/(max - min);
return transformedValue;
}
// -----------------------------------------------------------------------
// Methods of PMML handling
// -----------------------------------------------------------------------
/**
* Creates PMML object DerivedField of this object of NormContinuous type.
*
* @return DerivedField element
* @see com.prudsys.pdm.Adapters.PmmlVersion20.NormContinuous
* @exception MiningException could not create PMML object
*/
public Object createPmmlObject() throws MiningException
{
DerivedField field = (DerivedField) super.createPmmlObject();
NormContinuous normContinuous = new NormContinuous();
normContinuous.setField( sourceName );
LinearNorm linearNorm[] = new LinearNorm[2];
linearNorm[0] = new LinearNorm();
linearNorm[0].setOrig( "" + min );
linearNorm[0].setNorm( String.valueOf(lowerBound) );
linearNorm[1] = new LinearNorm();
linearNorm[1].setOrig( "" + max );
linearNorm[1].setNorm( String.valueOf(upperBound) );
normContinuous.setLinearNorm( linearNorm );
field.setNormContinuous( normContinuous );
return field;
}
/**
* Creates this object from PMML object DerivedField, subobject NormContinuous.
*
* @param pmml DerivedField element
* @see com.prudsys.pdm.Adapters.PmmlVersion20.NormContinuous
* @exception MiningException could not parse PMML object
*/
public void parsePmmlObject(Object pmml) throws MiningException
{
super.parsePmmlObject(pmml);
DerivedField field = (DerivedField) pmml;
com.prudsys.pdm.Adapters.PmmlVersion20.NormContinuous norm = field.getNormContinuous();
sourceName = norm.getField();
LinearNorm linearNorm[] = norm.getLinearNorm();
min = Double.parseDouble( linearNorm[0].getOrig() );
lowerBound = Double.parseDouble( linearNorm[0].getNorm() );
max = Double.parseDouble( linearNorm[1].getOrig() );
upperBound = Double.parseDouble( linearNorm[1].getNorm() );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -