📄 linearregressiondataview.java
字号:
package eti.bi.alphaminer.patch.standard.operation.result.view;
//@author XiaoguangXu HITSZ-ICE
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.functions.LinearRegression;
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.MiningAttribute;
import com.prudsys.pdm.Core.MiningDataSpecification;
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.alphaminer.patch.standard.operation.operator.LinearRegressionOperator;
import eti.bi.alphaminer.vo.IBIData;
import eti.bi.alphaminer.vo.IBIModel;
import eti.bi.alphaminer.vo.IBIObject;
import eti.bi.exception.AppException;
import eti.bi.exception.SysException;
import eti.bi.common.Locale.Resource;
public class LinearRegressionDataView extends ResultView{
private static final long serialVersionUID = -6642809357689092609L;
// 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 LinearRegressionOperator m_ClusteringOperator;
private weka.classifiers.Classifier m_WekaClassifier;
private MiningAttribute[] m_Attributes;
private MiningStoredData m_MiningStoredData;
private MiningDataSpecification m_MetaData;
private MiningAttribute m_TargetMiningAttribute;
//new adding by XiaoguangXU
private int m_TargetMiningAttributeIndex;
/**
* @param a_ClusteringModel
* @throws Exception
*/
public LinearRegressionDataView(LinearRegressionOperator a_LinearRegressionOperator, MiningAttribute[] a_MingAttributes) throws Exception{
super(Resource.srcStr("DataView"));
m_ViewType = ResultView.TYPE_DATA;
m_ClusteringOperator = a_LinearRegressionOperator;
m_Attributes = a_MingAttributes;
// The condition actually is also judged in the Operator
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();
IBIData aBIData = aBIObject.getBIData();
// Get the weka classifier
WekaSupervisedMiningModel supervisedMiningModel = (WekaSupervisedMiningModel)aBIModel.getMiningModel();
WekaClassifier wekaClassifier = (WekaClassifier) supervisedMiningModel.getClassifier();
m_WekaClassifier = (weka.classifiers.Classifier)wekaClassifier.getWekaClassifier();
if ( ! (m_WekaClassifier instanceof LinearRegression) ){
throw new SysException("The Classifier is not an instance of weka.classifiers.functions.LinearRegression");
}
// Get the MiningStoredData and MetaData
m_MiningStoredData = aBIData.getMiningStoredData();
m_MetaData = aBIData.getMetaData();
// Get the target MiningAttribute
m_TargetMiningAttribute = m_MetaData.getMiningAttribute(supervisedMiningModel.getTarget().getName());
m_TargetMiningAttributeIndex=m_MetaData.getAttributeIndex(m_TargetMiningAttribute);
m_ScrollPane = new JScrollPane();
this.setLayout(new BorderLayout());
this.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
this.add(m_ScrollPane, BorderLayout.CENTER);
createView();
}
public void createView() throws Exception
{
if (m_WekaClassifier == null){
throw new SysException("The Weka Classifier is NULL.");
}
else
{
createDataTable(m_ScrollPane);
m_ScrollPane.setPreferredSize(new Dimension(200, 73));
m_ScrollPane.getViewport().add(m_DataTable);
m_ScrollPane.getViewport().setBackground(Color.WHITE);
}
}
/**
* 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;
}
/**
*
* Create the JTable and attach it in the ScrollPane.
*
* @param a_ScrollPane
* @throws Exception
*/
@SuppressWarnings("unchecked")
private void createDataTable(JScrollPane a_ScrollPane) throws Exception {
// Four columns are added, one is to display index, the others are to display the predict, the actual and the error.
// The Residual Value may be added later.
int column = m_Attributes.length + 4;
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("LINEARREGRESSION_INDEX");
continue;
}
if(coloumIndex==column-3)
{
m_DataTableType[coloumIndex] = String.class;
m_DataTableHeader[coloumIndex] = Resource.srcStr("LINEARREGRESSION_ACTUAL");
continue;
}
if(coloumIndex==column-2)
{
m_DataTableType[coloumIndex] = String.class;
m_DataTableHeader[coloumIndex] = Resource.srcStr("LINEARREGRESSION_PREDICTED");
continue;
}
if (coloumIndex == column - 1) {
m_DataTableType[coloumIndex] = String.class;
m_DataTableHeader[coloumIndex] = Resource.srcStr("LINEARREGRESSION_ERROR");
continue;
}
attribute = m_Attributes[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_MiningStoredData != null) {
ArrayList list = m_MiningStoredData.getMiningVectors();
Instances instances = transform(m_MiningStoredData);
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);
allVec = vec.toVector();
double predicted=m_WekaClassifier.classifyInstance(instances.instance(i));
//double actual=m_WekaClassifier.
// double m_PosP = m_WekaClassifier.distributionForInstance(instances.instance(i));
double actual = (double) vec.getValue(m_TargetMiningAttributeIndex);
double error=predicted-actual;
//String posProb = "";
// Insert the index column
allVec.insertElementAt(new Integer(i+1), 0);
// Add the probability column
allVec.addElement(String.valueOf(actual));
allVec.addElement(String.valueOf(predicted));
allVec.addElement(String.valueOf(error));
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();
}
public void setColumnWidth() {
TableColumnModel tcm = m_DataTable.getColumnModel();
for (int i=1; i<tcm.getColumnCount(); i++){
tcm.getColumn(i).setPreferredWidth(80);
}
m_DataTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
// Make the last column large enough
tcm.getColumn(tcm.getColumnCount() - 1).setMinWidth(400);
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("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();
if (exportFile.exists()) {
int option = JOptionPane
.showConfirmDialog(
(Component) this,
Resource.srcStr("FileMessage")
+ exportFile.getName()
+ "\""
+ Resource.srcStr("ExistsMessage"),//
Resource.srcStr("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);
//ExcelExporter aExporter = new ExcelExporter(m_MiningStoredData, 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);
//ExcelExporter aExporter = new ExcelExporter(m_MiningStoredData, exportFile, true);
aExporter.export();
}
}
//XiaoguangXu 5/6/2006>>
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -