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

📄 logistictableview.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 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.
 */

/*
 * Created on 2005-1-25
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package eti.bi.alphaminer.patch.standard.operation.result.view;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumnModel;

import weka.classifiers.functions.Logistic;

import com.prudsys.pdm.Adapters.Weka.WekaClassifier;
import com.prudsys.pdm.Adapters.Weka.WekaSupervisedMiningModel;
import com.prudsys.pdm.Core.MiningException;

import eti.bi.alphaminer.operation.result.ResultView;
import eti.bi.alphaminer.operation.result.datamodel.DataGridModel;
import eti.bi.alphaminer.operation.result.export.ExcelExporter;
import eti.bi.alphaminer.patch.standard.operation.operator.RegressionOperator;
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;

/**
 * 
 * Take RegressionOperator as input. Output a JPanel (JTable) shown coefficients of 
 * the intercept and all variables being used.  Standard Error column and coefficients
 * for multi-level nominal attribute can be added later if necessary.
 *  
 * @author TWang On Jan 25, 2005.
 * 
 */
public class LogisticTableView extends ResultView {
	 
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// JTable that shows the data
	private JTable m_DataTable;
	private String[] m_DataTableHeader;
	private Object[][] m_DataTableContent;
	private Class[] m_DataTableType; 
	
	// JScrollPane that contains JTable
	private JScrollPane m_ScrollPane;
  
	// Weka and Xelopse data structures
	private RegressionOperator m_ClusteringOperator;
	private weka.classifiers.Classifier m_WekaClassifier; 
	
	// All the variables being used. Non-target nominal attribute
	// should have been Binaried.
 	private String[] m_VariableName;
	private double[][] m_coefficients; 
 	
 	
	/**
	 * @param a_ClusteringModel
	 * @throws SysException
	 * @throws MiningException
	 */
	public LogisticTableView(RegressionOperator a_RegressionOperator) throws SysException, MiningException{
		
		super(Resource.srcStr("TableView"));
		m_ViewType = ResultView.TYPE_DATA;
		
		m_ClusteringOperator = a_RegressionOperator;   
	 
		IBIObject aBIObject = m_ClusteringOperator.getOutputBIObject();
		if (aBIObject == null || aBIObject.getBIModel() == null
				|| aBIObject.getBIData() == null) {
			
			throw new SysException("The OutputBIObject in the ClusteringOperator is NULL");

		}		
		IBIModel aBIModel = aBIObject.getBIModel();
		// Get the WekaClassifier
		WekaSupervisedMiningModel supervisedMiningModel = (WekaSupervisedMiningModel)aBIModel.getMiningModel();
    	WekaClassifier testClassifier = (WekaClassifier) supervisedMiningModel.getClassifier();     
    	m_WekaClassifier = (weka.classifiers.Classifier)testClassifier.getWekaClassifier();
	 
    	if ( ! (m_WekaClassifier instanceof Logistic) ){
    		throw new SysException("The Classifier is not an instance of weka.classifiers.functions.Logistic");
    	}
    	
    	// Get the coefficients and variable name from the logistic model
    	m_coefficients = ((Logistic)m_WekaClassifier).getCoefficient();
    	m_VariableName = ((Logistic)m_WekaClassifier).getVariableNames();
    	
    	if ( m_coefficients == null || m_VariableName == null){
    		throw new SysException("Can not get the cofficients/variableName for classifier: weka.classifiers.functions.Logistic");
    	}  
    	if (m_VariableName.length != m_coefficients.length  ){
			throw new SysException("The size of cofficients array is not correct.");
		}
		 
		m_ScrollPane = new JScrollPane(); 
		
		this.setLayout(new BorderLayout());
		this.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0)); 
		this.add(m_ScrollPane, BorderLayout.CENTER);
 		createView();
 	} 
	 
	/**
	 * Create the view. 
	 * @throws SysException
	 * @throws MiningException
	 */
	public void createView() throws SysException, MiningException
	{
		if (m_WekaClassifier == null){
			throw new SysException("The Weka Classifier is NULL.");
		}
		else
		{   
			createDataTable(m_ScrollPane); 
				
			m_ScrollPane.setPreferredSize(new Dimension(100, 60));
			m_ScrollPane.getViewport().add(m_DataTable);
			m_ScrollPane.getViewport().setBackground(Color.WHITE);  
		}
	}
	
	/**
	 * 
	 * Create the JTable and attach it in the ScrollPane.   
	 *  
	 * @param a_ScrollPane
	 * @throws SysException
	 */
	private void createDataTable(JScrollPane a_ScrollPane) throws SysException {
	 
		// The target attribute row is replaced by the intercept row.
		int row = m_VariableName.length;
		
		// Variable column and coefficient estimate column
		int column = 2;
		
		m_DataTableType = new Class[column];
		m_DataTableHeader = new String[column];  
 
		// Create JTable header and JTable class type.
	  	m_DataTableType[0] = String.class;
		m_DataTableHeader[0] = Resource.srcStr("REGRESSION_VARIABLE");		
		m_DataTableType[1] = Double.class;
		m_DataTableHeader[1] = Resource.srcStr("REGRESSION_COEFFICIENT");
		 
		// Fill the JTable content.
		m_DataTableContent = new Object[row][column];	 
		
		for (int i=0; i<row; i++) {  
			
			// The first element is the intercept value.
			m_DataTableContent[i][0] = m_VariableName[i];				
			
			// Since we only support target variable with two levels, only one coefficient is needed.
			m_DataTableContent[i][1] = new Double(m_coefficients[i][0]); 
		} 

		// Create Table			
		m_DataTable = new JTable();
		m_DataTable.setModel(new DataGridModel(m_DataTableContent, m_DataTableHeader, m_DataTableType));
		setColumnWidth();
	}
	
	
	public void setColumnWidth() {
			TableColumnModel tcm = m_DataTable.getColumnModel();
			
			for (int i=1; i<tcm.getColumnCount(); i++){
					tcm.getColumn(i).setPreferredWidth(150);
			}
			
			tcm.getColumn(0).setMinWidth(100);
			m_DataTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 
			tcm.getColumn(0).setCellRenderer(m_DataTable.getTableHeader().getDefaultRenderer()); 
	}
	
	/**
	 * Exort the data table into an excel file.
	 * 
	 * Called by the OperatorResult class. The subclass of OperatorResult must 
	 * call m_SelectedView.export() explictly if it overwirtes the export() function 
	 * of OperatorResult.
	 */
	public void export() throws AppException, SysException {
		// Use user home directory 
		File directory = new File(System.getProperty("user.dir"));

		// Create and initialize file chooser for excel
		JFileChooser chooser = new JFileChooser(directory);
		chooser.setDialogTitle(Resource.srcStr("ExportTable"));
		chooser.setFileFilter(ExcelExporter.FILTER);
		chooser.setSelectedFile(ExcelExporter.DEFAULT_FILE);

  		// pop up the file chooser dialog and return the file value
		int returnVal = chooser.showSaveDialog(this);	  
		if (returnVal == JFileChooser.APPROVE_OPTION) 
		{
			File exportFile = chooser.getSelectedFile();
			//<<tyleung 20/4/2005
			if (exportFile.exists()) {
				int option = JOptionPane
						.showConfirmDialog(
								(Component) this,
								"The file  \""
										+ exportFile.getName()
										+ "\""
										+ " already exists. Do you want to replace the existing file?",//						
								"AlphaMiner", JOptionPane.YES_NO_OPTION,
								JOptionPane.QUESTION_MESSAGE);
				if (option != JOptionPane.CANCEL_OPTION) {
					if (option == JOptionPane.YES_OPTION) {
//					  Create the excel exporter with the excel file extension enforced to be .xls
					  	ExcelExporter aExporter = new ExcelExporter(m_DataTable, exportFile, true);
						aExporter.export();
						
					}else{
						returnVal = chooser.showSaveDialog(this);
					}
				}
			}
			else {

				// Create the excel exporter with the excel file extension enforced to be .xls
			  	ExcelExporter aExporter = new ExcelExporter(m_DataTable, exportFile, true);
				aExporter.export();
			}
		}
		//tyleung 20/4/2005>>
	} 

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -