📄 smooperatorresult.java
字号:
package eti.bi.alphaminer.patch.standard.operation.result;
//@author XiaoguangXu HITSZ-ICE
import java.util.ArrayList;
import javax.swing.JTable;
import javax.swing.table.TableColumnModel;
import weka.classifiers.Classifier;
import weka.classifiers.CostMatrix;
import weka.classifiers.Evaluation;
import weka.classifiers.trees.J48;
import com.prudsys.pdm.Core.CategoricalAttribute;
import com.prudsys.pdm.Core.Category;
import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Input.MiningStoredData;
import com.prudsys.pdm.Models.Supervised.SupervisedMiningSettings;
import eti.bi.alphaminer.core.handler.ICaseHandler;
import eti.bi.alphaminer.operation.operator.INodeInfo;
import eti.bi.alphaminer.operation.result.OperatorResult;
import eti.bi.alphaminer.operation.result.ResultView;
import eti.bi.alphaminer.operation.result.view.DataView;
import eti.bi.alphaminer.operation.result.view.PmmlView;
import eti.bi.alphaminer.patch.standard.operation.operator.EvaluationMatrix;
import eti.bi.alphaminer.patch.standard.operation.operator.PredictionAssessmentOperator;
import eti.bi.alphaminer.patch.standard.operation.operator.SMOOperator;
import eti.bi.alphaminer.patch.standard.operation.result.view.AssessmentOperatorConfusionMatrixView;
import eti.bi.alphaminer.vo.IBIModel;
import eti.bi.alphaminer.vo.IBIObject;
import eti.bi.common.Locale.Resource;
import eti.bi.exception.AppException;
import eti.bi.exception.SysException;
import weka.core.Attribute;
import weka.core.Instances;
import com.prudsys.pdm.Adapters.Weka.WekaClassifier;
import com.prudsys.pdm.Adapters.Weka.WekaCoreAdapter;
import com.prudsys.pdm.Adapters.Weka.WekaSupervisedMiningModel;
public class SMOOperatorResult extends OperatorResult{
private SMOOperator m_SMOOperator;
private IBIModel SMOModel;
private MiningStoredData m_MiningStoredData;
private String m_ModelPMMLPath;
/**
*
*/
private static final long serialVersionUID = -4526894341818615447L;
public SMOOperatorResult(String a_CaseID, String a_NodeID, String a_Name, INodeInfo a_NodeInfo, ICaseHandler a_CaseHandler) throws Exception{
super(a_Name + Resource.srcStr("Result"), a_CaseID, a_NodeID, a_NodeInfo, a_CaseHandler);
}
@Override
protected void init() throws Exception {
}
protected void getContent()throws SysException{
m_SMOOperator = (SMOOperator) m_Operator;
IBIObject aBIObject = m_SMOOperator.getOutputBIObject();
if (aBIObject == null || aBIObject.getBIModel() == null || aBIObject.getBIData() == null) {
throw new SysException("The input BIObject is NULL");
}
/*get model and data*/
SMOModel = aBIObject.getBIModel();
m_MiningStoredData = aBIObject.getBIData().getMiningStoredData();
try {
// to create pmml view
m_Operator.writeTempModelFile();
m_ModelPMMLPath = SMOModel.getTempBIModelPath();
} catch (SysException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void createResult() throws MiningException, AppException, SysException{
DataView dataview = new DataView(m_SMOOperator.getOutputBIObject().getBIData(),this);
dataview.showData();
PmmlView pmmlView = new PmmlView(m_ModelPMMLPath, this);
ResultView ConfusionMatricesView = CreateConfusionMatricesView();
addView(ConfusionMatricesView);
try {
pmmlView.preparePmml();
pmmlView.showPmml();
addView(pmmlView);
} catch (SysException e) {
e.printStackTrace();
m_SystemMessageHandler.appendMessage(e.getMessage());
}
addView(dataview);
this.setClosable(true);
this.setMaximizable(true);
this.setResizable(true);
this.setSize(700, 500);
}
public ResultView CreateConfusionMatricesView() throws MiningException, AppException, SysException{
EvaluationMatrix evaluationMatrix = CreateConfusionMatrices(1);
ArrayList<EvaluationMatrix> evaluationMatrixLists = new ArrayList<EvaluationMatrix>(1);
evaluationMatrixLists.add(evaluationMatrix);
AssessmentOperatorConfusionMatrixView ConfusionMatrixView = new AssessmentOperatorConfusionMatrixView(evaluationMatrixLists);
return ConfusionMatrixView;
}
private EvaluationMatrix CreateConfusionMatrices(double dbThreshold) throws MiningException, AppException, SysException
{
EvaluationMatrix evaluationMatrix = null;
String [] classNames = null;
ArrayList valueList = getTargetAttribute().getValues();
if(valueList.size() > 0){
classNames = new String[valueList.size()];
for(int k = 0; k < valueList.size(); k++){
classNames[k] = ((Category)valueList.get(k)).getDisplayValue();
}
}
//tranform the orginal data set accordingly.
//MiningStoredData transformedMiningStoredData = transformData(m_SMOOperator, m_MiningStoredData);
evaluationMatrix = runMatricesCalculation(SMOModel, m_MiningStoredData, dbThreshold);
evaluationMatrix.setM_ModelName(SMOModel.getModelName());
evaluationMatrix.setM_DataName(m_SMOOperator.getNodeID());
evaluationMatrix.setM_ClassNames(classNames);
// set the statistics names in the runMatricesCalculation() method.
//evaluationMatrix.setM_StatisticsNames(statisticsName);
return evaluationMatrix;
}
private CategoricalAttribute getTargetAttribute( )
{
SupervisedMiningSettings miningSettings = (SupervisedMiningSettings)SMOModel.getMiningSettings();
CategoricalAttribute miningAttribute = (CategoricalAttribute)miningSettings.getTarget();
return miningAttribute;
}
private EvaluationMatrix runMatricesCalculation(IBIModel inputModel,
MiningStoredData assessmentData,
double dbThreshold)
throws MiningException, AppException, SysException
{
String [] statisticsName = null;
EvaluationMatrix evaluationMatrix = null;
WekaSupervisedMiningModel miningModel = (WekaSupervisedMiningModel)inputModel.getMiningModel();
String targetAttributeName = miningModel.getTarget().getName();
WekaClassifier wekaClassifier = (WekaClassifier)miningModel.getClassifier();
Object classifer = wekaClassifier.getWekaClassifier();
// Get Weka instances from mining stream:
Instances wekaInstances = null;
try {
assessmentData.reset();
wekaInstances = (Instances) WekaCoreAdapter.PDMMiningInputStream2WekaInstances(assessmentData);
}catch (Exception e) {
e.printStackTrace();
throw new MiningException( "Could not call weka's model valuation module correctly.");
};
// Set the target class in WekaInstances
Attribute targetAtt = wekaInstances.attribute(targetAttributeName);
if(targetAtt != null){
wekaInstances.setClass(targetAtt);
}else{
throw new MiningException( "Invalid model assessment data.");
}
CostMatrix costMatrix = null;
Evaluation wekaEvaluation = null;
try{
wekaEvaluation = new Evaluation(wekaInstances, costMatrix);
if(wekaEvaluation != null){
//Call the modified interface for model evaluation.
wekaEvaluation.eti_evaluateModel((Classifier)classifer, wekaInstances, dbThreshold, 0.90);
evaluationMatrix = new EvaluationMatrix();
if(evaluationMatrix != null)
evaluationMatrix.setM_ConfusionMatrixElements(wekaEvaluation.confusionMatrix());
int nNumClasses = wekaInstances.numClasses();
double[][] dbStatisticalElement = null;
// Set the statistics names/columns for different classifers
if(classifer instanceof J48){
dbStatisticalElement = new double[nNumClasses][6];
statisticsName = PredictionAssessmentOperator.TREE_CLASSIFIERSTATISTICS_NAMES;
}
else {
dbStatisticalElement = new double[nNumClasses][5];
statisticsName = PredictionAssessmentOperator.REGRESSION_CLASSIFIERSTATISTICS_NAMES;
}
evaluationMatrix.setM_StatisticsNames(statisticsName);
evaluationMatrix.setM_OverallPrecision(wekaEvaluation.totalPrecision());
for(int i = 0; i < nNumClasses; i++) {
dbStatisticalElement[i][0] = wekaEvaluation.truePositiveRate(i);
dbStatisticalElement[i][1] = wekaEvaluation.falsePositiveRate(i);
dbStatisticalElement[i][2] = wekaEvaluation.precision(i);
dbStatisticalElement[i][3] = wekaEvaluation.fMeasure(i);
dbStatisticalElement[i][4] = 1-wekaEvaluation.precision(i);
if(classifer instanceof J48){
dbStatisticalElement[i][5] = wekaEvaluation.eti_truePositiveConfidence(i);
}
}
if(dbStatisticalElement != null)
evaluationMatrix.setM_StatisticsMatrixElements(dbStatisticalElement);
}
}catch (Exception e) {
e.printStackTrace();
throw new MiningException( "Could not call weka's model valuation module correctly.");
};
return evaluationMatrix;
}
public void setColumnWidth(JTable a_JTable) {
TableColumnModel tcm = a_JTable.getColumnModel();
if (tcm.getColumn(0).getWidth()<60)
{
for (int i=1; i<tcm.getColumnCount(); i++){
tcm.getColumn(i).setMinWidth(80);
}
a_JTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
}
tcm.getColumn(0).setMaxWidth(80);
tcm.getColumn(0).setCellRenderer(a_JTable.getTableHeader().getDefaultRenderer());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -