📄 decisiontreedataview.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-27
*
* 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 java.util.ArrayList;
import java.util.Vector;
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.trees.J48;
import weka.core.Instance;
import weka.core.Instances;
import com.prudsys.pdm.Adapters.Weka.WekaCoreAdapter;
import com.prudsys.pdm.Core.CategoricalAttribute;
import com.prudsys.pdm.Core.MiningAttribute;
import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Core.NumericAttribute;
import com.prudsys.pdm.Input.MiningStoredData;
import com.prudsys.pdm.Input.MiningVector;
import eti.bi.alphaminer.operation.result.ResultView;
import eti.bi.alphaminer.operation.result.datamodel.SortingDataGridModel;
import eti.bi.alphaminer.operation.result.export.ExcelExporter;
import eti.bi.alphaminer.operation.result.renderer.DataCellRenderer;
import eti.bi.common.Locale.Resource;
import eti.bi.exception.AppException;
import eti.bi.exception.SysException;
/**
*
* Take J48 classifier, MiningStoredData, MiningAttribute and the target
* MiningAttribute as inputs. Generate the predicted class and the
* probability for each intance in the stored data by using the J48 classifier.
* Output a JPanel (JTable) shown these infor.
*
* @author TWang On Jan 27, 2005.
*
*/
public class DecisionTreeDataView 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;
private J48 m_J48Classifier;
private MiningAttribute[] m_MiningAttributes;
private MiningAttribute m_TargetMiningAttribute;
private MiningStoredData m_MiningStoredData;
private Instances m_Instances;
// The target attribute's classes.
private ArrayList m_ClassValues;
/**
* Might need to check all the input parameters before usage.
* Throw exceptions as necessary.
*
* @param a_Cassifier
* @param a_MiningStoredData
* @param a_MiningAttributes
* @param a_TargetMiningAttribute
* @throws Exception
*/
public DecisionTreeDataView(J48 a_Cassifier, MiningStoredData a_MiningStoredData, MiningAttribute[] a_MiningAttributes, MiningAttribute a_TargetMiningAttribute) throws Exception{
super(Resource.srcStr("DataView"));
m_ViewType = ResultView.TYPE_DATA;
if (a_Cassifier == null) {
throw new SysException("The J48 Classifier in the DecisionOperator is NULL.");
}
m_J48Classifier = a_Cassifier;
if ( !(a_TargetMiningAttribute instanceof CategoricalAttribute) ) {
throw new SysException("The target attribute is not categorical.");
}
m_TargetMiningAttribute = a_TargetMiningAttribute;
m_ClassValues = ((CategoricalAttribute) a_TargetMiningAttribute).getValues();
m_MiningAttributes = a_MiningAttributes;
m_MiningStoredData = a_MiningStoredData;
m_ScrollPane = new JScrollPane();
this.setLayout(new BorderLayout());
this.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
this.add(m_ScrollPane, BorderLayout.CENTER);
createDataTable();
m_ScrollPane.setPreferredSize(new Dimension(200, 70));
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 Exception
*/
@SuppressWarnings("unchecked")
private void createDataTable() throws Exception {
// Three columns more to display index, predicted class, and rule confidence.
int column = m_MiningAttributes.length + 3;
m_DataTableType = new Class[column];
m_DataTableHeader = new String[column];
MiningAttribute attribute = null;
// Create JTable header and JTable class type.
for (int coloumIndex = 0; coloumIndex < column; coloumIndex++) {
if (coloumIndex == 0){
m_DataTableType[coloumIndex] = Integer.class;
m_DataTableHeader[coloumIndex] = Resource.srcStr("DATA_INDEX");
continue;
}
if (coloumIndex == column - 1) {
m_DataTableType[coloumIndex] = Double.class;
m_DataTableHeader[coloumIndex] = Resource.srcStr("DECISIONTREE_CONFIDENCE");
continue;
}
if (coloumIndex == column - 2) {
m_DataTableType[coloumIndex] = String.class;
m_DataTableHeader[coloumIndex] = Resource.srcStr("DATA_PREDICT");
continue;
}
attribute = m_MiningAttributes[coloumIndex-1];
m_DataTableHeader[coloumIndex] = attribute.getName();
m_DataTableType[coloumIndex] = String.class;
if (attribute instanceof NumericAttribute) {
int dataType = ((NumericAttribute) attribute).getDataType();
if (dataType == NumericAttribute.DOUBLE)
m_DataTableType[coloumIndex] = Double.class;
else if (dataType == NumericAttribute.FLOAT)
m_DataTableType[coloumIndex] = Float.class;
else if (dataType == NumericAttribute.INTEGER)
m_DataTableType[coloumIndex] = Integer.class;
else if (dataType == NumericAttribute.BOOLEAN)
m_DataTableType[coloumIndex] = Boolean.class;
}
else if (attribute instanceof CategoricalAttribute) {
int dataType = ((CategoricalAttribute) attribute).getDataType();
if (dataType == CategoricalAttribute.BOOLEAN)
m_DataTableType[coloumIndex] = Boolean.class;
else
m_DataTableType[coloumIndex] = String.class;
}
}
// Fill the JTable content.
if (m_MiningAttributes != null) {
m_Instances = transform(m_MiningStoredData);
// Must set the target attribute before execute the .classifyInstance() method.
m_Instances.setClass(m_Instances.attribute(m_TargetMiningAttribute.getName()));
ArrayList list = m_MiningStoredData.getMiningVectors();
Object[][] content = new Object[list.size()][column];
Vector allVec = null;
MiningVector vec = null;
for (int i = 0; i < list.size(); i++) {
vec = (MiningVector) list.get(i);
// Convert a mining vector into a vector, handle the MISSING value.
// for(int in=0; in<vec.getMetaData().getAttributesArray().length; in++){
// MiningAttribute attr = vec.getMetaData().getMiningAttribute(in);
//
// if (vec.isMissing(in)){
// allVec.add(MissingValue.DISPLAY_VALUE);
// }else if(attr instanceof CategoricalAttribute){
// allVec.add( ((CategoricalAttribute)attr).getCategory(vec.getValue(in)) );
// }else {
// allVec.add( new Double(vec.getValue(in)) );
// }
// }
allVec = vec.toVector();
allVec.insertElementAt(new Integer(i+1), 0);
Instance instance = m_Instances.instance(i);
int classIndex = (int)m_J48Classifier.classifyInstance(instance);
allVec.addElement( m_ClassValues.get(classIndex));
// Must call .getProbabity() only after calling .classifyInstance.
allVec.addElement(new Double(100*m_J48Classifier.getProbability()));
//allVec.addElement( new Double( weka.core.Utils.roundDouble(((Double)m_Distances.elementAt(i)).doubleValue(), 3) ) );
content[i] =allVec.toArray();
}
m_DataTableContent = content;
}
// Create Table
m_DataTable = new JTable();
// m_DataTable.setModel(new DataGridModel(m_DataTableContent, m_DataTableHeader, m_DataTableType));
SortingDataGridModel model = new SortingDataGridModel(m_DataTableContent, m_DataTableHeader, m_DataTableType);
m_DataTable.setModel(model);
m_DataTable.setDefaultRenderer(String.class, new DataCellRenderer(true));
m_DataTable.setDefaultRenderer(Short.class, new DataCellRenderer(true));
m_DataTable.setDefaultRenderer(Long.class, new DataCellRenderer(true));
m_DataTable.setDefaultRenderer(Integer.class, new DataCellRenderer(true));
m_DataTable.setDefaultRenderer(Double.class, new DataCellRenderer(true));
m_DataTable.setDefaultRenderer(Float.class, new DataCellRenderer(true));
model.addMouseListenerToHeader(m_DataTable);
setColumnWidth();
}
/**
* Helper calss to transform Xelopes MingStoredData into WEKA Instances.
*
* @param a_InputMiningStoredData
* @return
* @throws MiningException
*/
public Instances transform(MiningStoredData a_InputMiningStoredData) throws MiningException
{
Instances wekaInstances;
try {
// Reset the cursor of the MiningStoredData set, so the transform starts from
// the first reord. Otherwise, the returned object might be NULL.
// By TWang. Jan 25, 2005.
a_InputMiningStoredData.reset();
wekaInstances = (Instances) WekaCoreAdapter.PDMMiningInputStream2WekaInstances(a_InputMiningStoredData);
} catch (Exception e) {
e.printStackTrace();
throw new MiningException("Can not transform from MiningStoredData to Instances.");
}
return wekaInstances;
}
public void setColumnWidth() {
TableColumnModel tcm = m_DataTable.getColumnModel();
for (int i=1; i<tcm.getColumnCount(); i++)
tcm.getColumn(i).setMinWidth(60);
m_DataTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
tcm.getColumn(0).setPreferredWidth(60);
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 overwirte 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("FileExport"));
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 + -