📄 supportvectoralgorithm.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 Michael Thess
* @version 1.1
*/
package com.prudsys.pdm.Models.Regression.SVM;
import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Core.MiningModel;
import com.prudsys.pdm.Core.MiningSettings;
import com.prudsys.pdm.Input.MiningArrayStream;
import com.prudsys.pdm.Input.MiningFilterStream;
import com.prudsys.pdm.Input.MiningInputStream;
import com.prudsys.pdm.Models.Regression.RegressionAlgorithm;
import com.prudsys.pdm.Models.Supervised.Classifier;
import com.prudsys.pdm.Transform.MiningTransformationActivity;
import com.prudsys.pdm.Transform.MiningTransformationStep;
import com.prudsys.pdm.Transform.Special.ReplaceMissingValueStream;
/**
* A class representing a Support Vector Machine algorithm. Each implementation
* should extend this class and override only the methods:
* {@link #runAlgorithm() runAlgorithm()},
* {@link #getClassifier()() getClassifier()}
*/
public abstract class SupportVectorAlgorithm extends RegressionAlgorithm
{
// -----------------------------------------------------------------------
// Variables declarations
// -----------------------------------------------------------------------
// SVM types:
protected int svmType = SupportVectorSettings.SVM_C_SVC;
protected int kernelType = SupportVectorSettings.KERNEL_RBF;
// Kernel parameters:
protected double degree = 3.0;
protected double gamma = 1.0;
protected double coef0 = 0.0;
// Algorithm parameters:
protected double C = 1.0;
protected double nu = 0.5;
protected double lossEpsilon = 0.1;
// -----------------------------------------------------------------------
// Constructor
// -----------------------------------------------------------------------
/**
* Empty constructor.
*/
public SupportVectorAlgorithm()
{
}
// -----------------------------------------------------------------------
// Getter and setter methods
// -----------------------------------------------------------------------
/**
* Creates an instance of the support vector settings class that is required
* to run the algorithm. The mining settings are assigned through the
* setMiningSettings method.
*
* @return new instance of the support vector settings class of the algorithm
*/
public MiningSettings createMiningSettings() {
return new SupportVectorSettings();
}
/**
* Set SVM settings.
*
* @param miningSettings instance of SupportVectorSettings
* @exception IllegalArgumentException mining settings are not of support vector type
*/
public final void setMiningSettings( MiningSettings miningSettings ) throws IllegalArgumentException
{
if ( miningSettings instanceof SupportVectorSettings )
{
super.setMiningSettings( miningSettings );
SupportVectorSettings svs = (SupportVectorSettings) miningSettings;
this.svmType = svs.getSvmType();
this.kernelType = svs.getKernelType();
this.degree = svs.getDegree();
this.gamma = svs.getGamma();
this.coef0 = svs.getCoef0();
this.C = svs.getC();
this.nu = svs.getNu();
this.lossEpsilon = svs.getLossEpsilon();
}
else
{
throw new IllegalArgumentException( "MiningSettings have to be SupportVectorSettings." );
};
}
/**
* Returns SVM classifier.
*
* @return SVM classifier
*/
public abstract Classifier getClassifier();
// -----------------------------------------------------------------------
// Run SVM and build mining model
// -----------------------------------------------------------------------
/**
* Runs SVM algorithm.
*
* @exception MiningException could not run algorithm
*/
protected abstract void runAlgorithm() throws MiningException;
/**
* Builds mining model by running the SVM algorithm internally.
* Before starting the algorithm, missing values are replaced.
*
* @return support vector mining model generated by the algorithm
* @exception MiningException could not build model
*/
public MiningModel buildModel() throws MiningException
{
long start = ( new java.util.Date() ).getTime();
// Create inner trafo object:
MiningTransformationActivity innerTrafo = new MiningTransformationActivity();
// Replace missing values trafo:
ReplaceMissingValueStream rep = new ReplaceMissingValueStream(miningInputStream);
MiningInputStream repStream = rep.createReplaceMissingValueStream();
innerTrafo.addTransformationStep( rep.getMts() );
// Remove supplementary attributes trafo:
MiningInputStream mis = repStream;
MiningTransformationStep removeTrafo = applicationInputSpecification.createRemoveTrafoFromInputSpec();
if (removeTrafo != null) {
mis = new MiningFilterStream(repStream, removeTrafo);
innerTrafo.addTransformationStep(removeTrafo);
}
// Copy stream to array:
miningInputStream = new MiningArrayStream(mis);
// Run SVM algorithm:
runAlgorithm();
// Build SVM model:
SupportVectorMiningModel model = new SupportVectorMiningModel();
model.setMiningSettings( miningSettings );
model.setInputSpec( applicationInputSpecification );
model.setTarget( applicationInputSpecification.getTargetApplicationAttribute() );
model.setMiningTransform( innerTrafo );
// Missing values in application input specification:
applicationInputSpecification.setInputSpecFromInnerTrafo(metaData, null, rep);
// Set SVM parameter:
model.setSvmType(svmType);
model.setKernelType(kernelType);
model.setDegree(degree);
model.setCoef0(coef0);
model.setGamma(gamma);
// Set classifier:
SupportVectorClassifier svmClassifier = (SupportVectorClassifier) getClassifier();
model.setClassifier( svmClassifier );
model.setSupportVectors( svmClassifier.getSupportVectors() );
model.setCoefficients( svmClassifier.getCoefficients() );
model.setAbsoluteCoefficient( svmClassifier.getAbsoluteCoefficient() );
this.miningModel = model;
// Calculate time:
long end = ( new java.util.Date() ).getTime();
timeSpentToBuildModel = ( end - start ) / 1000.0;
return model;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -