📄 zetnormal.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 zeta normalization. Technically, this is also
* a linear normalization.
*
* The value of mean and deviation 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.
*/
public class ZetNormal extends OneToOneMapping
{
// -----------------------------------------------------------------------
// Constant to define deviation epsion
// -----------------------------------------------------------------------
/** Lower deviation bound for division. */
public static final double DEVIATION_EPSILON = 0.0000000001;
// -----------------------------------------------------------------------
// Variables declarations
// -----------------------------------------------------------------------
/** Mean value of attribute. */
private double mean;
/** Deviation of attribute. */
private double deviation;
// -----------------------------------------------------------------------
// Constructor
// -----------------------------------------------------------------------
/**
* Empty constructor.
*/
public ZetNormal()
{
}
// -----------------------------------------------------------------------
// Getter and setter methods
// -----------------------------------------------------------------------
/**
* Sets mean value.
*
* @param mean new mean value
*/
public void setMean(double mean)
{
this.mean = mean;
}
/**
* Returns mean value.
*
* @return mean value
*/
public double getMean()
{
return mean;
}
/**
* Sets deviation value.
*
* @param deviation new deviation value
*/
public void setDeviation(double deviation)
{
this.deviation = deviation;
}
/**
* Returns deviation value.
*
* @return deviation value
*/
public double getDeviation()
{
return deviation;
}
// -----------------------------------------------------------------------
// 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() );
transformedAttribute.setLowerBound( Double.NEGATIVE_INFINITY );
transformedAttribute.setUpperBound( Double.POSITIVE_INFINITY );
if (statisticsMiningModel != null) {
Group group = statisticsMiningModel.getRootGroup();
mean = group.getMean();
deviation = group.getStandart();
};
return transformedAttribute;
}
/**
* Transforms attribute value. The result is also a value.
*
* @param attributeValue value of attribute to be transformed
* @return tranformed (normalized) value
* @exception MiningException could not transform attribute value
*/
public double transformAttributeValue( double attributeValue ) throws MiningException
{
// Missing value:
if (Category.isMissingValue(attributeValue))
return attributeValue;
// Normalization:
if (deviation < DEVIATION_EPSILON)
return 0.0;
double transformedValue = ( attributeValue - mean ) / deviation;
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( "" + mean );
linearNorm[0].setNorm( "0" );
linearNorm[1] = new LinearNorm();
linearNorm[1].setOrig( "" + ( deviation + mean ) );
linearNorm[1].setNorm( "1" );
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();
mean = Double.parseDouble( linearNorm[0].getOrig() );
deviation = Double.parseDouble( linearNorm[1].getOrig() ) - mean;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -