📄 linearnormalstream.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.Special;
import com.prudsys.pdm.Core.MiningAttribute;
import com.prudsys.pdm.Core.MiningDataSpecification;
import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Core.NumericAttribute;
import com.prudsys.pdm.Input.MiningInputStream;
import com.prudsys.pdm.Models.Statistics.SimpleStats;
import com.prudsys.pdm.Transform.MiningTransformationFactory;
import com.prudsys.pdm.Transform.MiningTransformationStep;
import com.prudsys.pdm.Transform.OneToOne.Identity;
import com.prudsys.pdm.Transform.OneToOne.LinearNormal;
/**
* Realization of normalization for a given mining input
* stream. Only numeric attributes are normalized. The parameters
* lowerBound and upperBound of the normalization range apply
* to all numeric attributes.
*/
public class LinearNormalStream extends VectorTransformationStream
{
// -----------------------------------------------------------------------
// Variables declarations
// -----------------------------------------------------------------------
/** Array of minimum values of all attributes. */
private double[] minValues = null;
/** Array of maximum values of all attributes. */
private double[] maxValues = null;
/** Lower bound of normalization valus (default = 0). */
private double lowerBound = 0.0;
/** Upper bound of normalization valus (default = 1). */
private double upperBound = 1.0;
// -----------------------------------------------------------------------
// Constructors
// -----------------------------------------------------------------------
/**
* Empty constructor.
*/
public LinearNormalStream()
{
}
/**
* Constructor for given stream.
*
* @param inputStream mining input stream for normalization
*/
public LinearNormalStream(MiningInputStream inputStream) {
this.inputStream = inputStream;
}
// -----------------------------------------------------------------------
// Getter and setter methods
// -----------------------------------------------------------------------
/**
* 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;
}
/**
* Returns array of minimum values.
*
* @return array of minimum values
*/
public double[] getMinValues()
{
return minValues;
}
/**
* Returns array of maximum values.
*
* @return array of maximum values
*/
public double[] getMaxValues()
{
return maxValues;
}
// -----------------------------------------------------------------------
// Transformation methods
// -----------------------------------------------------------------------
/**
* Calculates minimum and maximum values for numeric attributes.
*
* @exception MiningException error while calculating minmax values
*/
private void calcMinMaxValues() throws MiningException {
// Calculate simple statistics:
SimpleStats sist = new SimpleStats();
sist.setInputStream(inputStream);
sist.runCalculation();
// Fill arrays of minimum and maximum values:
MiningDataSpecification metaData = inputStream.getMetaData();
int nAtt = metaData.getAttributesNumber();
minValues = new double[nAtt];
maxValues = new double[nAtt];
for (int i = 0; i < nAtt; i++) {
MiningAttribute att = metaData.getMiningAttribute(i);
if (att instanceof NumericAttribute) {
minValues[i] = sist.getCalculatedValue(att, SimpleStats.STAT_MIN);
maxValues[i] = sist.getCalculatedValue(att, SimpleStats.STAT_MAX);
};
};
}
/**
* Creates mining transformation step for linearization of numeric attributs.
*
* @return mining transformation step
* @exception MiningException no input stream defined
*/
public MiningTransformationStep createMiningTransformationStep() throws MiningException {
// No mining input stream defined => exception:
if (inputStream == null)
throw new MiningException("No mining input stream defined");
// Get minimum and maximum values of all numericattributes:
calcMinMaxValues();
// Mining transformation factory:
MiningTransformationFactory mtf = new MiningTransformationFactory();
boolean notrans = true;
MiningDataSpecification metaData = inputStream.getMetaData();
for (int i = 0; i < metaData.getAttributesNumber(); i++) {
// Get attribute and name:
MiningAttribute mAtt = metaData.getMiningAttribute(i);
String attName = mAtt.getName();
// Don't use excluded attributes, if defined:
if ( excludedAttributeNames != null && excludedAttributeNames.indexOf(attName) > -1)
continue;
// Add linear normalization if numeric attribute:
if (mAtt instanceof NumericAttribute) {
LinearNormal ln = new LinearNormal();
ln.setSourceName( attName );
ln.setTargetName( "n_" + attName );
ln.setMin( minValues[i] );
ln.setMax( maxValues[i] );
ln.setLowerBound( lowerBound );
ln.setUpperBound( upperBound );
mtf.addOneToOneMapping(ln);
notrans = false;
};
};
// No transformations at all => just 1 required, use first attribute:
if (notrans) {
MiningAttribute mAtt = metaData.getMiningAttribute(0);
Identity id = new Identity();
id.setSourceName( mAtt.getName() );
mtf.addOneToOneMapping(id);
};
// Create transformation step for normalization:
mts = mtf.createMiningTransformationStep();
return mts;
}
// -----------------------------------------------------------------------
// Other methods
// -----------------------------------------------------------------------
/**
* Returns normalization description.
*
* @returns description of normalization
*/
public String toString() {
String mess = "Linear normalization stream. Transform in following interval:";
mess = mess + " LowerBound = " + lowerBound + " UpperBound = " + upperBound;
return mess;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -