📄 regressiontreenode.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.
*/
/**
* Title: XELOPES Data Mining Library
* Description: The XELOPES library is an open platform-independent and data-source-independent library for Embedded Data Mining.
* Copyright: Copyright (c) 2002 Prudential Systems Software GmbH
* Company: ZSoft (www.zsoft.ru), Prudsys (www.prudsys.com)
* @author Victor Borichev
* @author Michael Thess
* @version 1.0
*/
package com.prudsys.pdm.Models.Classification.DecisionTree;
import com.prudsys.pdm.Adapters.PmmlVersion20.Node;
import com.prudsys.pdm.Adapters.PmmlVersion20.SparseGridModel;
import com.prudsys.pdm.Adapters.PmmlVersion20.SupportVectorMachineModel;
import com.prudsys.pdm.Core.Category;
import com.prudsys.pdm.Core.MiningAttribute;
import com.prudsys.pdm.Core.MiningDataSpecification;
import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Core.MiningModel;
import com.prudsys.pdm.Input.MiningVector;
import com.prudsys.pdm.Models.Regression.RegressionMiningModel;
import com.prudsys.pdm.Models.Regression.SVM.SupportVectorMiningModel;
import com.prudsys.pdm.Models.Regression.SVM.SupportVectorSettings;
import com.prudsys.pdm.Models.Regression.SparseGrids.SparseGridsMiningModel;
import com.prudsys.pdm.Models.Regression.SparseGrids.SparseGridsSettings;
/**
* Node of a decision tree that contains a regression model like SVM or
* Sparse Grids.
*/
public class RegressionTreeNode extends DecisionTreeNode {
// -----------------------------------------------------------------------
// Variables declarations
// -----------------------------------------------------------------------
/** Regression model of the node. */
protected RegressionMiningModel model;
/** Virtual attribute name for predicate evaluation. */
protected String virtualAttributeName = "virtual";
// -----------------------------------------------------------------------
// Constructor
// -----------------------------------------------------------------------
/**
* Empty constructor.
*/
public RegressionTreeNode() {
}
// -----------------------------------------------------------------------
// Getter and setter methods
// -----------------------------------------------------------------------
/**
* Returns regression model of node.
*
* @return regression model of node
*/
public RegressionMiningModel getModel() {
return model;
}
/**
* Sets regression model of node.
*
* @param model new regression model of node
*/
public void setModel(RegressionMiningModel model) {
this.model = model;
}
/**
* Returns virtual attribute name for predicate evaluation.
*
* @return virtual attribute name for predicate evaluation
*/
public String getVirtualAttributeName() {
return virtualAttributeName;
}
/**
* Set virtual attribute name for predicate evaluation.
*
* @param virtualAttributeName new virtual attribute name for predicate evaluation
*/
public void setVirtualAttributeName(String virtualAttributeName) {
this.virtualAttributeName = virtualAttributeName;
}
// -----------------------------------------------------------------------
// Apply regression tree node to mining vector
// -----------------------------------------------------------------------
/**
* Applies decision tree recursively to the child nodes which evaluate
* the virtual attribute and returns the reference to scoring node.
*
* @param miningVector vector to be classified, with virtual attribute
* @return node containing score information used for classification
* @throws MiningException could not run this method
*/
public DecisionTreeNode applyForNode(MiningVector miningVector) throws MiningException
{
if (leaf || children == null) return this;
else
{
// Get meta data and virtual attribute:
MiningDataSpecification mds = miningVector.getMetaData();
MiningAttribute virtual = mds.getMiningAttribute(virtualAttributeName);
// Calculate classification value and assign to virtual attribute:
double classVal = model.applyModelFunction(miningVector);
if (Category.isMissingValue( classVal )) {
System.out.println("isMissingValue");
classVal = 0.0;
}
/*
System.out.println("values");
for (int i = 0; i < mds.getAttributesNumber(); i++) {
MiningAttribute ma = mds.getMiningAttribute(i);
String attName = ma.getName();
double value = miningVector.getValue(attName);
String sval = String.valueOf(value);
if (ma instanceof CategoricalAttribute)
sval = ((CategoricalAttribute)ma).getCategory(value).getDisplayValue();
System.out.println( attName + " = " + sval );
}
System.out.println(" =====================> classValue = " + classVal + " ");
*/
miningVector.setValue( mds.getAttributeIndex(virtual), classVal );
// Childs evaluate the virtual attribute:
for (int i = 0; i < children.length; i++)
{
DecisionTreeNode dtn = (DecisionTreeNode) children[i];
if (dtn.predicate.evaluate(miningVector)) return dtn.applyForNode(miningVector);
};
throw new MiningException("bad tree model");
}
}
// -----------------------------------------------------------------------
// Methods of PMML handling
// -----------------------------------------------------------------------
/**
* Extracts regression model from PMML Decision Tree node.
*
* @param dtNode decision tree node containing regression model
* @return regression model containing in DT node, null if no regression model found
* @exception MiningException dtNode not instance of PMML Node
*/
public static Object getPmmlRegressionModel(Object dtNode)
throws MiningException {
Node regNode = null;
if (dtNode instanceof Node)
regNode = (Node) dtNode;
else
throw new MiningException("dtNode is no instance of PMML Node");
Object model = regNode.getSupportVectorMachineModel();
if (model == null) model = regNode.getSparseGridModel();
return model;
}
/**
* Adds regression model to PMML Decision Tree node.
*
* @param model PMML regression model element to add to DT node
* @param dtNode decision tree node
* @exception MiningException dtNode not instance of PMML Node
*/
public static void setPmmlRegressionModel(Object model, Object dtNode)
throws MiningException {
Node regNode = null;
if (dtNode instanceof Node)
regNode = (Node) dtNode;
else
throw new MiningException("dtNode is no instance of PMML Node");
if (model instanceof SupportVectorMachineModel)
regNode.setSupportVectorMachineModel( (SupportVectorMachineModel) model );
else if (model instanceof SparseGridModel)
regNode.setSparseGridModel( (SparseGridModel) model );
}
/**
* Creates regression model contained in this node. Automatically recognizes
* type of model and calls the corresponding model writer.
*
* @return pmmlObject PMML object of regression model
* @exception MiningException could not create PMML object
*/
public Object createPmmlObjectModel() throws MiningException {
Object pmmlModel;
if (model instanceof SupportVectorMiningModel) {
System.out.println("SVM node");
pmmlModel = createPmmlObjectSVM();
}
else if (model instanceof SparseGridsMiningModel) {
System.out.println("SG node");
pmmlModel = createPmmlObjectSG();
}
else
throw new MiningException("unknown regression model type");
return pmmlModel;
}
/**
* Creates SVM model.
*
* @return pmmlObject PMML object of SVM model
* @exception MiningException could not create SVM PMML object
*/
public Object createPmmlObjectSVM() throws MiningException {
SupportVectorMiningModel svmm = (SupportVectorMiningModel) model;
SupportVectorMachineModel svm = (SupportVectorMachineModel) model.createPmmlObject();
return svm;
}
/**
* Creates SG model.
*
* @return pmmlObject PMML object of SG model
* @exception MiningException could not create SG PMML object
*/
public Object createPmmlObjectSG() throws MiningException {
SparseGridsMiningModel sgmm = new SparseGridsMiningModel();
SparseGridModel sgm = (SparseGridModel) model.createPmmlObject();
return sgm;
}
/**
* Parses regression model contained in this node. Automatically recognizes
* type of model and calls the corresponding model parser.
*
* @param model regression model contained in node
* @throws MiningException cannot parse regression model
*/
public void parsePmmlObjectModel(Object model)
throws MiningException {
if (model instanceof SupportVectorMachineModel) {
System.out.println("SVM node");
parsePmmlObjectSVM( (SupportVectorMachineModel) model );
}
else if (model instanceof SparseGridModel) {
System.out.println("SG node");
parsePmmlObjectSG( (SparseGridModel) model );
}
else
throw new MiningException("unknown regression model type");
}
/**
* Parses SVM model contained in this node.
*
* @param svm SVM model contained in node
* @throws MiningException cannot parse SVM model
*/
public void parsePmmlObjectSVM(SupportVectorMachineModel svm)
throws MiningException {
SupportVectorMiningModel svmm = new SupportVectorMiningModel();
SupportVectorSettings svmSettings = new SupportVectorSettings();
svmSettings.setDataSpecification( metaData );
svmm.setMiningSettings( svmSettings );
svmm.setGlobalSupportVectors( decisionTreeModel.getGlobalSupportVectors() );
svmm.parsePmmlObject(svm);
// DISCOVERER-specific ignore inner transformations to save time:
if ( true || getDecisionTreeModel().getApplicationName().equals( MiningModel.APPLICATION_PRUDSYS_DISCOVERER ) )
svmm.setMiningTransform(null);
this.model = svmm;
}
/**
* Parses SG model contained in this node.
*
* @param sgm SG model contained in node
* @throws MiningException cannot parse SG model
*/
public void parsePmmlObjectSG(SparseGridModel sgm)
throws MiningException {
SparseGridsMiningModel sgmm = new SparseGridsMiningModel();
SparseGridsSettings sgSettings = new SparseGridsSettings();
sgSettings.setDataSpecification( metaData );
sgmm.setMiningSettings( sgSettings );
sgmm.parsePmmlObject(sgm);
this.model = sgmm;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -