📄 decisiontreealgorithm.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 Valentine Stepanenko (ValentineStepanenko@zsoft.ru)
* @author Michael Thess
* @version 1.0
*/
package com.prudsys.pdm.Models.Classification.DecisionTree;
import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Core.MiningModel;
import com.prudsys.pdm.Core.MiningSettings;
import com.prudsys.pdm.Input.MiningArrayStream;
import com.prudsys.pdm.Models.Classification.ClassificationAlgorithm;
import com.prudsys.pdm.Transform.MiningTransformationActivity;
import com.prudsys.pdm.Transform.Special.ReplaceMissingValueStream;
import com.prudsys.pdm.Transform.Special.TreatOutlierValueStream;
/**
* Base class for all decision tree algorithms.
*/
public abstract class DecisionTreeAlgorithm extends ClassificationAlgorithm
{
// -----------------------------------------------------------------------
// Variables declarations
// -----------------------------------------------------------------------
/** Replace missing values before tree construction and application. */
protected boolean replaceMissingValues = true;
/** Special outlier treatment before tree application. */
protected boolean specialOutlierTreatment = true;
/** Store score distribution in all nodes of the tree. */
protected boolean storeScoreDistribution = true;
/** Maximum number of surrogate splits per node. */
protected int maxSurrogates;
/** Maximim depth of the tree. */
protected int maxDepth;
/** Maximum number of children of a node. */
protected int maxSplits;
/** Minimum number of vectors per node. */
protected double minNodeSize = 0;
/** Node size unit (count or percentage). */
protected int minNodeSizeUnit;
/** Minimum decrease in impurity per step. */
protected double minDecreauseInImpurity;
// -----------------------------------------------------------------------
// Constructor
// -----------------------------------------------------------------------
/**
* Empty constructor.
*/
public DecisionTreeAlgorithm()
{
}
// -----------------------------------------------------------------------
// Getter and setter methods
// -----------------------------------------------------------------------
/**
* Creates an instance of the decision tree settings class that is required
* to run the algorithm. The mining settings are assigned through the
* setMiningSettings method.
*
* @return new instance of the decision tree settings class of the algorithm
*/
public MiningSettings createMiningSettings() {
return new DecisionTreeSettings();
}
/**
* Set decision tree settings.
*
* @param miningSettings new decision tree settings
* @exception IllegalArgumentException mining settings are not of decision tree type
*/
public void setMiningSettings( MiningSettings miningSettings ) throws IllegalArgumentException
{
if( miningSettings instanceof DecisionTreeSettings )
{
super.setMiningSettings( miningSettings );
DecisionTreeSettings dts = (DecisionTreeSettings) miningSettings;
maxDepth = dts.getMaxDepth();
maxSplits = dts.getMaxSplits();
maxSurrogates = dts.getMaxSurrogates();
minDecreauseInImpurity = dts.getMinDecreaseInImpurity();
minNodeSize = dts.getMinNodeSize();
minNodeSizeUnit = dts.getMinNodeSizeUnit();
}
else
{
throw new IllegalArgumentException( "MiningSettings have to be DecisionTreeSettings." );
}
}
/**
* Replace missing values before tree construction?
* Missing value replacement shuld be used for all decision tree
* algorithms which do not provide a special missing value
* replacement.
*
* @return true if replace, otherwise false
*/
public boolean isReplaceMissingValues()
{
return replaceMissingValues;
}
/**
* Missing value replacement shuld be used for all decision tree
* algorithms which do not provide a special missing value
* replacement.
*
* @param replaceMissingValues set new replacement
*/
public void setReplaceMissingValues(boolean replaceMissingValues)
{
this.replaceMissingValues = replaceMissingValues;
}
/**
* Special outliers treatment should be used for all decision tree
* algorithms which do not provide a special outlier treatment
* in the model application phase. Does not affect the decision
* tree algorithm itself.
*
* @return true if treatment, otherwise false
*/
public boolean isSpecialOutlierTreatment()
{
return specialOutlierTreatment;
}
/**
* Special outliers treatment should be used for all decision tree
* algorithms which do not provide a special outlier treatment
* in the model application phase. Does not affect the decision
* tree algorithm itself.
*
* @param specialOutlierTreatment set new treatment
*/
public void setSpecialOutlierTreatment(boolean specialOutlierTreatment)
{
this.specialOutlierTreatment = specialOutlierTreatment;
}
/**
* Return store score distribution in all nodes.
*
* @return true if store score distribution in all nodes, otherwise false
*/
public boolean isStoreScoreDistribution()
{
return storeScoreDistribution;
}
/**
* Sets store score distribution in all nodes.
*
* @param storeScoreDistribution store score distribution in all nodes
*/
public void setStoreScoreDistribution(boolean storeScoreDistribution)
{
this.storeScoreDistribution = storeScoreDistribution;
}
// -----------------------------------------------------------------------
// Build decision tree model
// -----------------------------------------------------------------------
/**
* Builds mining model by running the decision tree algorithm internally.
*
* @return decision tree mining model generated by the algorithm
* @exception MiningException could not build model
*/
public MiningModel buildModel() throws MiningException
{
long start = ( new java.util.Date() ).getTime();
// Special treatment of outliers in tree application, if desired:
TreatOutlierValueStream tro = null;
if (specialOutlierTreatment) {
tro = new TreatOutlierValueStream(miningInputStream);
tro.createTreatOutlierValueTransformationStep();
};
// Replace missing values by mean and mode values, if desired:
ReplaceMissingValueStream rep = null;
if (replaceMissingValues) {
rep = new ReplaceMissingValueStream(miningInputStream);
miningInputStream = new MiningArrayStream( rep.createReplaceMissingValueStream() );
};
// Run DT algorithm:
runAlgorithm();
// Build DT model:
DecisionTreeMiningModel model = new DecisionTreeMiningModel();
model.setMiningSettings( miningSettings );
model.setInputSpec( applicationInputSpecification );
model.setTarget( applicationInputSpecification.getTargetApplicationAttribute() );
// Outlier treatment and missing values in model, if desired:
if (specialOutlierTreatment || replaceMissingValues) {
// Create inner transformation object:
MiningTransformationActivity mta = new MiningTransformationActivity();
if (specialOutlierTreatment) mta.addTransformationStep( tro.getMts() );
if (replaceMissingValues) mta.addTransformationStep( rep.getMts() );
model.setMiningTransform( mta );
// Outliers and missing values in application input specification:
applicationInputSpecification.setInputSpecFromInnerTrafo(metaData, tro, rep);
};
// Set DT classifier:
model.setClassifier( getClassifier() );
this.miningModel = model;
long end = ( new java.util.Date() ).getTime();
timeSpentToBuildModel = ( end - start ) / 1000.0;
return model;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -