📄 treatoutlierattributevalue.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.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;
/**
* Treatment of outliers. By default, the
* 'asIs' treatment is used which does nothing.
*
* For identifying whether the value is an outlier,
* an assessment attribute must be defined which is
* of the same type like the attribute to be transfomed.
* For numeric attributes the value is tested for the
* interval (lowerBound, upperBound), for categorical
* attributes it must correspond to one of its categories.
*
* Use identical attribute transformation; hence target
* name of attribute is identical to source name and
* therefore ignored.
*
* No PMML convertation implemented because this is usually done
* via the PMML element MiningSchema.
*/
public class TreatOutlierAttributeValue extends OneToOneMapping
{
// -----------------------------------------------------------------------
// Constants of types of outliers trealment
// -----------------------------------------------------------------------
public static final String OUTLIER_TREATMENT_METHOD_asIs = "asIs";
public static final String OUTLIER_TREATMENT_METHOD_asMissingValues = "asMissingValues";
public static final String OUTLIER_TREATMENT_METHOD_asExtremeValues = "asExtremeValues";
// -----------------------------------------------------------------------
// Variables declarations
// -----------------------------------------------------------------------
/** Treatment type of outliers. */
private String outliers = OUTLIER_TREATMENT_METHOD_asIs;
/** Replacement value for lower projection for 'asExtremeValues'. */
private double lowValue = 0.0;
/** Replacement value for upper projection for 'asExtremeValues'. */
private double highValue = 0.0;
/** Attribute for evaluating if value is an outlier. */
private MiningAttribute assessmentAttribute = null;
// -----------------------------------------------------------------------
// Constructor
// -----------------------------------------------------------------------
/**
* Empty constructor.
*/
public TreatOutlierAttributeValue()
{
}
// -----------------------------------------------------------------------
// Getter and setter methods
// -----------------------------------------------------------------------
/**
* Returns type of outlier treatment.
*
* @return type of outlier treatment
*/
public String getOutliers()
{
return outliers;
}
/**
* Sets type of outlier treatment.
*
* @param outliers new type of outlier treatment
*/
public void setOutliers(String outliers)
{
this.outliers = outliers;
}
/**
* Returns replacement value for lower projection for type 'asExtremeValues'.
*
* @return lower replacement value
*/
public double getLowValue()
{
return lowValue;
}
/**
* Sets replacement value for lower projection for type 'asExtremeValues'.
*
* @param lowValue new low replacement value
*/
public void setLowValue(double lowValue)
{
this.lowValue = lowValue;
}
/**
* Returns replacement value for upper projection for type 'asExtremeValues'.
*
* @return upper replacement value
*/
public double getHighValue()
{
return highValue;
}
/**
* Sets replacement value for upper projection for type 'asExtremeValues'.
*
* @param highValue new upper replacement value
*/
public void setHighValue(double highValue)
{
this.highValue = highValue;
}
/**
* Returns attribute used for outlier testing.
*
* @return attribute used for outlier test
*/
public MiningAttribute getAssessmentAttribute()
{
return assessmentAttribute;
}
/**
* Sets attribute used for outlier test.
*
* @param assessmentAttribute new attribute used for outlier test
*/
public void setAssessmentAttribute(MiningAttribute assessmentAttribute)
{
this.assessmentAttribute = assessmentAttribute;
}
// -----------------------------------------------------------------------
// Transformation methods
// -----------------------------------------------------------------------
/**
* Transforms the source attribute. The result is the target attribute.
* Identity operator.
*
* @return transformed attribute
* @exception MiningException could not transform attribute
*/
public MiningAttribute transformAttribute() throws MiningException
{
MiningAttribute sourceAttribute = getSourceAttribute();
if (sourceAttribute == null)
throw new MiningException("Could not find source attribute: " + sourceName);
try
{
MiningAttribute att = (MiningAttribute)sourceAttribute.clone();
return att;
}catch (Exception e)
{
throw new MiningException(e.getMessage());
}
}
/**
* 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
{
double transformedValue = attributeValue;
// Treatment 'asIs' => return:
if (outliers.equals(OUTLIER_TREATMENT_METHOD_asIs))
return transformedValue;
// Is missing value or no assessment attribute => return:
if (Category.isMissingValue( attributeValue ) || assessmentAttribute == null)
return transformedValue;
// Test for outlier:
int outlierType = 0;
MiningAttribute att = getSourceAttribute();
if (att instanceof CategoricalAttribute) {
if (assessmentAttribute instanceof NumericAttribute)
throw new MiningException("Assessment attribute '" + assessmentAttribute + "' must be categorical");
Category categ = ((CategoricalAttribute) att).getCategory(attributeValue);
double key = ((CategoricalAttribute) assessmentAttribute).getKey(categ);
if (Category.isMissingValue(key))
outlierType = 3;
}
else {
if (assessmentAttribute instanceof CategoricalAttribute)
throw new MiningException("Assessment attribute '" + assessmentAttribute + "' must be numeric");
NumericAttribute numAtt = (NumericAttribute) assessmentAttribute;
if (attributeValue < numAtt.getLowerBound())
outlierType = 1;
if (attributeValue > numAtt.getUpperBound())
outlierType = 2;
};
// Treatment of outlier:
if (outlierType > 0) {
if ( outliers.equals(OUTLIER_TREATMENT_METHOD_asMissingValues) )
transformedValue = Category.MISSING_VALUE;
else {
if (outlierType == 1) transformedValue = lowValue;
if (outlierType == 2) transformedValue = highValue;
if (outlierType == 3) ; // meaningless treatment definition
}
};
return transformedValue;
}
// -----------------------------------------------------------------------
// Methods of PMML handling
// -----------------------------------------------------------------------
/**
* Creates PMML object DerivedField of this object.
*
* @return DerivedField element
* @see com.prudsys.pdm.Adapters.PmmlVersion20.TransformationDictionary
* @exception MiningException could not create PMML object
*/
public Object createPmmlObject() throws MiningException
{
/**@todo Implement this com.prudsys.pdm.DataMining.PmmlPresentable method*/
throw new java.lang.UnsupportedOperationException("Method createPmmlObject() not yet implemented.");
}
/**
* Creates this object from PMML.
* Currently not supported.
*
* @param pmml pmml element
* @exception MiningException always thrown
*/
public void parsePmmlObject(Object pmml) throws MiningException
{
/**@todo Implement this com.prudsys.pdm.DataMining.PmmlPresentable method*/
throw new java.lang.UnsupportedOperationException("Method parsePmmlObject() not yet implemented.");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -