📄 regressionoperator.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.
*/
/*
* $Author$
* $Date$
* $Revision$
*/
package eti.bi.alphaminer.patch.standard.operation.operator;
import java.util.Vector;
import com.prudsys.pdm.Core.CategoricalAttribute;
import com.prudsys.pdm.Core.MiningAlgorithm;
import com.prudsys.pdm.Core.MiningAlgorithmSpecification;
import com.prudsys.pdm.Core.MiningAttribute;
import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Core.MiningModel;
import com.prudsys.pdm.Models.Supervised.SupervisedMiningSettings;
import com.prudsys.pdm.Utils.GeneralUtils;
import eti.bi.alphaminer.core.handler.ICaseHandler;
import eti.bi.alphaminer.operation.operator.INodeInfo;
import eti.bi.alphaminer.operation.operator.ModelOperator;
import eti.bi.alphaminer.operation.operator.Operator;
import eti.bi.common.Locale.Resource;
import eti.bi.alphaminer.vo.BIData;
import eti.bi.alphaminer.vo.BIModel;
import eti.bi.alphaminer.vo.BIObject;
import eti.bi.alphaminer.vo.IBIData;
import eti.bi.alphaminer.vo.IBIModel;
import eti.bi.alphaminer.vo.IOperatorNode;
import eti.bi.exception.AppException;
import eti.bi.exception.SysException;
/**
* RegressionOperator is a kind of Operator
*/
public class RegressionOperator extends ModelOperator {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* @param a_CaseID
* @param a_CaseWindow
* @param aOperatorInfo
*/
public RegressionOperator(String a_CaseID, INodeInfo aNodeInfo, ICaseHandler aCaseHandler) {
super(a_CaseID, aNodeInfo, aCaseHandler);
//2006/07/29 Xiaojun Chen
PredictionAssessmentOperator.registerParentsDefinitionID(aNodeInfo.getDefinitionID());
ScoreOperator.registerParentsDefinitionID(aNodeInfo.getDefinitionID());
}
/* Parameter name for Regression Operator in BIML */
public static String ITERATION_NUMBER = "Iteration number";
public static String RIDGE = "Ridge";
/* Default parameter value for Regression Operator */
public static String DEFAULT_ITERATION_NUMBER = "-1";
public static String DEFAULT_RIDGE = "0.00000001";
/* Parameter name for MiningSettingSpecification and MiningAlgorithm */
private static String ALGORITHM_NAME = "Logistic (Weka)";
private static String MAP_WEKA_CLASS_PARAMETERS = "wekaClassParameters";
/**
* Set node id and update operator text of the DecisionTreeOperator at the same time.
* @param a_NodeID ID of the node
*/
public void setNodeID(String a_NodeID) {
setLabel(getDescription() + " [" + a_NodeID + "]");
setDefaultModelName(Resource.srcStr("LogisticRegression")+"_" + a_NodeID);
super.setNodeID(a_NodeID);
}
/**
* Set node id and update operator text of the DecisionTreeOperator at the same time.
* @param a_NodeID ID of the node
*/
public void setDescription(String a_Description) {
m_Description = a_Description;
setLabel(m_Description + " [" + m_NodeID + "]");
setDefaultModelName(Resource.srcStr("LogisticRegression")+"_" + m_NodeID);
}
/**
* Test if the Regression Operator contains any results.
* @return true if Regression Operator has result; false otherwise.
*/
public boolean hasResult() {
if (m_OutputBIObject != null)
{
return (m_OutputBIObject.hasResult(BIObject.DATA) &&
m_OutputBIObject.hasResult(BIObject.MODEL));
}else
{
return false;
}
}
/**
* Build logistic regression model for this Regression Operator.
* @param a_OperatorNode Operator Node represented by this Regression Operator.
* @param a_Parents a Vector storing node IDs of parent nodes of this Regression Operator.
*/
public void execute(IOperatorNode a_OperatorNode, Vector a_Parents)
throws MiningException, AppException, SysException{
/* Get parameter from user input */
String ridge = (String) a_OperatorNode.getParameterValue(RIDGE);
if (ridge==null)
ridge = DEFAULT_RIDGE;
String iteration = (String) a_OperatorNode.getParameterValue(ITERATION_NUMBER);
if (iteration==null)
iteration = DEFAULT_ITERATION_NUMBER;
String logisticParameters = "-R " + ridge + " -M " + iteration;
/* Get input bi object from parent node */
Operator parentOp = (Operator)a_Parents.elementAt(0);
setInputBIObject(parentOp.getOutputBIObject());
IBIData aInputBIData = getInputBIObject().getBIData();
aInputBIData.getMiningStoredData().reset();
/* Prepare output data model */
BIData aOutputBIData = new BIData(getCaseID(), getNodeID());
aOutputBIData.setTargetAttribute(aInputBIData.getTargetAttribute());
aOutputBIData.setTransformActionHistory(aInputBIData.getTransformActionHistory());
aOutputBIData.setTargetAttribute(aInputBIData.getTargetAttribute());
aOutputBIData.setMiningStoredData(aInputBIData.getMiningStoredData());
BIModel aOutputBIModel = new BIModel(getCaseID(), getNodeID(), IBIModel.TYPE_REGRESSION);
/* Execute model building */
MiningAttribute targetAttribute = aInputBIData.getTargetAttribute();
aOutputBIModel.setTargetAttribute(targetAttribute);
if (targetAttribute==null)
{
m_SystemMessageHandler.appendMessage("Categorical Target attribute is missing. Please add target attribute by using Data Set Attribute Node.");
throw new AppException("Categorical Target attribute is missing. Please add target attribute by using Data Set Attribute Node.");
}else if (!(targetAttribute instanceof CategoricalAttribute))
{
m_SystemMessageHandler.appendMessage("Attribute \""+targetAttribute.getName() + "\" is not Categorical.");
throw new AppException("Attribute \""+targetAttribute.getName() + "\" is not Categorical.");
}
//Only process bounded target variable. For unbounded categorical attribute, the
//space complexity of the Regression algorithm is O(NC*NA), where NC is the number of values
//in the target attribute and NA is the number of attributes. This can easily lead to "out of
//memory" error. TWang. Mar 23, 2005.
if(((CategoricalAttribute)targetAttribute).isUnboundedCategories()){
m_SystemMessageHandler.appendMessage("Categorical Target attribute must be bounded.");
throw new AppException("Attribute \""+targetAttribute.getName() + "\" should be a bounded Categorical attribute.");
}
//>> END Twang.
/* Create MiningSettings object and assign metadata */
SupervisedMiningSettings miningSettings = new SupervisedMiningSettings();
miningSettings.setDataSpecification(aInputBIData.getMetaData());
/* Assign settings */
miningSettings.setTarget(targetAttribute);
try {
miningSettings.verifySettings();
} catch (Exception e)
{
m_SystemMessageHandler.appendMessage("Invalid parameters in building the Regression model.");
throw new AppException("Invalid parameters in building the Regression model.");
}
/* Set MiningSettings */
aOutputBIModel.setMiningSettings(miningSettings);
/* Get default mining algorithm specification from 'algorithms.xml' */
MiningAlgorithmSpecification miningAlgorithmSpecification =
MiningAlgorithmSpecification.getMiningAlgorithmSpecification( ALGORITHM_NAME, getNodeInfo());
if( miningAlgorithmSpecification == null )
throw new MiningException( "Can't find weka classification method." );
/* Get class name from algorithms specification */
String className = miningAlgorithmSpecification.getClassname();
if( className == null )
throw new MiningException( "className attribute expected." );
/* Set MiningAlgorithmSpecification */
miningAlgorithmSpecification.setMAPValue(MAP_WEKA_CLASS_PARAMETERS, logisticParameters);
aOutputBIModel.setMiningAlgorithmSpecification(miningAlgorithmSpecification);
displayMiningAlgSpecParameters(miningAlgorithmSpecification);
/* Set and display mining parameters */
GeneralUtils.displayMiningAlgSpecParameters(miningAlgorithmSpecification);
/* Create algorithm object with default values */
MiningAlgorithm algorithm = GeneralUtils.createMiningAlgorithmInstance(className, this.getClass().getClassLoader());
algorithm.setMiningInputStream(aInputBIData.getMiningStoredData());
algorithm.setMiningSettings(miningSettings);
algorithm.setMiningAlgorithmSpecification(miningAlgorithmSpecification);
try
{
algorithm.verify();
} catch(IllegalArgumentException e)
{
throw new MiningException(e.getMessage());
}
MiningModel model = algorithm.buildModel();
m_SystemMessageHandler.appendMessage(Resource.srcStr("calculationtime")+" [s]: " + algorithm.getTimeSpentToBuildModel()+Resource.srcStr("ms"));
m_SystemMessageHandler.nextLine();
m_SystemMessageHandler.nextLine();
/* set output mining data and model to the output mining object */
aOutputBIModel.setMiningModel(model);
// aOutputBIModel.setModelName("Logistic Regression_"+a_OperatorNode.getNodeID());
aOutputBIModel.setModelName(getDefaultModelName());
m_OutputBIObject.setBIData(aOutputBIData);
m_OutputBIObject.setBIModel(aOutputBIModel);
/* set run time parameter value to the node object (It needs to be stored in the BIML) */
//a_OperatorNode.setParameterValue("Temporary model", aOutputBIModel.getTempBIModelPath());
//aOutputBIModel.writeTempBIModel();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -