⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rbfnetworkoperatorresult.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 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 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.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.RBFNetworkOperator;
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;
public class RBFNetworkOperatorResult extends OperatorResult{

	
	private RBFNetworkOperator m_RBFNetworkOperator;
	private IBIModel RBFNetworkModel;
	private MiningStoredData m_MiningStoredData;
	private String m_ModelPMMLPath;
	
	private static final long serialVersionUID = -4526943418689615447L;

	
	public RBFNetworkOperatorResult(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{
		DataView dataview = new DataView(m_RBFNetworkOperator.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); 
	}
	
	@Override
	protected void init() throws Exception {
		// TODO Auto-generated method stub
		
	}
	
	protected void getContent()throws SysException{
		m_RBFNetworkOperator = (RBFNetworkOperator) m_Operator;
		IBIObject aBIObject = m_RBFNetworkOperator.getOutputBIObject();
		
		if (aBIObject == null || aBIObject.getBIModel() == null	|| aBIObject.getBIData() == null) {
			throw new SysException("The input BIObject is NULL");
		}
		
		/*get model and data*/
		RBFNetworkModel  = aBIObject.getBIModel();
		m_MiningStoredData = aBIObject.getBIData().getMiningStoredData();
		
    	try {
			// to create pmml view
    		m_Operator.writeTempModelFile();
			m_ModelPMMLPath = RBFNetworkModel.getTempBIModelPath();
		} catch (SysException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	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_RBFNetworkOperator, m_MiningStoredData);
       
		evaluationMatrix = runMatricesCalculation(RBFNetworkModel, m_MiningStoredData, dbThreshold);
		evaluationMatrix.setM_ModelName(RBFNetworkModel.getModelName());
		evaluationMatrix.setM_DataName(m_RBFNetworkOperator.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)RBFNetworkModel.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;
	}
	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 + -