📄 naivebayesoperatorresult.java
字号:
package eti.bi.alphaminer.patch.standard.operation.result;
import java.util.ArrayList;
import java.util.Vector;
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 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;
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.core.transform.DataTransformAction;
import eti.bi.alphaminer.operation.operator.INodeInfo;
import eti.bi.alphaminer.operation.operator.Operator;
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.NaiveBayesOperator;
import eti.bi.alphaminer.patch.standard.operation.operator.PredictionAssessmentOperator;
import eti.bi.alphaminer.patch.standard.operation.result.view.AssessmentOperatorConfusionMatrixView;
import eti.bi.alphaminer.vo.IBIData;
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;
public class NaiveBayesOperatorResult extends OperatorResult {
private NaiveBayesOperator m_NaiveBayesOperator;
private IBIModel NaiveBayesModel;
private MiningStoredData m_MiningStoredData;
private String m_ModelPMMLPath;
/**
*
*/
private static final long serialVersionUID = -4452620630505615447L;
public NaiveBayesOperatorResult(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);
}
protected void createResult() throws MiningException, AppException, SysException{
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());
}
DataView dataview = new DataView(m_NaiveBayesOperator.getOutputBIObject().getBIData(),this);
dataview.showData();
addView(dataview);
this.setClosable(true);
this.setMaximizable(true);
this.setResizable(true);
this.setSize(700, 500);
}
@Override
protected void init() throws Exception {
// TODO Auto-generated method stub
}
protected void getContent()throws SysException{
m_NaiveBayesOperator = (NaiveBayesOperator) m_Operator;
IBIObject aBIObject = m_NaiveBayesOperator.getOutputBIObject();
if (aBIObject == null || aBIObject.getBIModel() == null || aBIObject.getBIData() == null) {
throw new SysException("The input BIObject is NULL");
}
/*get model and data*/
NaiveBayesModel = aBIObject.getBIModel();
m_MiningStoredData = aBIObject.getBIData().getMiningStoredData();
try {
// to create pmml view
m_Operator.writeTempModelFile();
m_ModelPMMLPath = NaiveBayesModel.getTempBIModelPath();
} catch (SysException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private 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_NaiveBayesOperator, m_MiningStoredData);
evaluationMatrix = runMatricesCalculation(NaiveBayesModel, transformedMiningStoredData, dbThreshold);
evaluationMatrix.setM_ModelName(NaiveBayesModel.getModelName());
evaluationMatrix.setM_DataName(m_NaiveBayesOperator.getNodeID());
evaluationMatrix.setM_ClassNames(classNames);
// set the statistics names in the runMatricesCalculation() method.
//evaluationMatrix.setM_StatisticsNames(statisticsName);
return evaluationMatrix;
}
private CategoricalAttribute getTargetAttribute( )
{
//Since only supervised models are shown in the assement property dialog for
//user to choose, delcare the variable (SupervisedMiningSettings) directly, and get
//the demanding information from it.
SupervisedMiningSettings miningSettings = (SupervisedMiningSettings)NaiveBayesModel.getMiningSettings();
//Only categorical attributes can be set as target variables in
//J48, and Logistic supervised models, delcare the variable (CategoricalAttribute)
//directly, and get the demanding information from it.
CategoricalAttribute miningAttribute = (CategoricalAttribute)miningSettings.getTarget();
return miningAttribute;
}
/**
* Transform a MiningStoredData instance by what has been done on an Operator's input data set.
* Feb. 22, 2005.
* @param operator
* @param originalMiningStoredData
* @return
* @throws SysException
*/
private MiningStoredData transformData(Operator operator, MiningStoredData originalMiningStoredData) throws SysException{
IBIData transformedData = operator.getInputBIObject().getBIData();
Vector transformHistory = null;
if (transformedData != null){
transformHistory = transformedData.getTransformActionHistory();
}
if (transformHistory == null){
throw new SysException("Transform action of the training is lost");
}
if (originalMiningStoredData == null)
{
throw new SysException("Input data source has no data.");
}
MiningStoredData transformedMiningStoredData = originalMiningStoredData;
MiningStoredData fromData = null;
// Loop the transformHistory, and apply the same transform actions to the scoring data source
for (int t=0;t<transformHistory.size();t++)
{
Object obj = transformHistory.get(t);
if (obj instanceof DataTransformAction)
{
DataTransformAction action = (DataTransformAction)obj;
fromData = transformedMiningStoredData;
try {
transformedMiningStoredData = action.transform(fromData);
} catch (Exception e) {
e.printStackTrace();
throw new SysException("Unable to transform Assessment Data.");
}
}
}
return transformedMiningStoredData;
}
private EvaluationMatrix runMatricesCalculation(IBIModel inputModel,
MiningStoredData assessmentData,
double dbThreshold)
throws MiningException, AppException, SysException
{
String [] statisticsName = null;
EvaluationMatrix evaluationMatrix = null;
//Assume that only weka's classifiers are used in KBBI Platform training version.
//J48, and Logistic Regression.
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 {
// Reset the cursor of the MiningStoredData set, so the transform starts from
// the first reord. Otherwise, the returned object might be NULL. TWang.
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.");
}
//Call Weka's evaluation interfaces, for KBBI platform, no cost-matrix is
//supplied in the training version.
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 + -