📄 svmtimeseriesalgorithm.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
* @author Victor Borichev
* @author Valentine Stepanenko (valentine.stepanenko@zsoft.ru)
* @version 1.0
*/
package com.prudsys.pdm.Models.TimeSeriesPredict.Algorithms.Multidimensional;
import com.prudsys.pdm.Core.MiningAlgorithm;
import com.prudsys.pdm.Core.MiningAlgorithmSpecification;
import com.prudsys.pdm.Core.MiningAttribute;
import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Input.MiningStoredData;
import com.prudsys.pdm.Models.Regression.RegressionDeviationAssessment;
import com.prudsys.pdm.Models.Regression.SVM.SupportVectorMiningModel;
import com.prudsys.pdm.Models.Regression.SVM.SupportVectorSettings;
import com.prudsys.pdm.Models.Supervised.SupervisedMiningModel;
import com.prudsys.pdm.Models.TimeSeriesPredict.TimeSeriesMiningAlgorithm;
import com.prudsys.pdm.Transform.Special.BlockVectorStream;
import com.prudsys.pdm.Utils.GeneralUtils;
/**
* Implementation of multidimensional SVM-based time series prognosis
* using regression methods.
*/
public class SVMTimeSeriesAlgorithm extends TimeSeriesMiningAlgorithm {
SupportVectorMiningModel[] svmm;
String regressionMethod;
/**
* Empty constructor.
*/
public SVMTimeSeriesAlgorithm() {
}
/**
* Run algorithm.
*
* @exception MiningException could not create time series models
*/
protected void runAlgorithm() throws MiningException {
System.out.print("Building classifiers...");
BlockVectorStream bvStream = new BlockVectorStream();
bvStream.setBlockLength( embeddingDimension );
bvStream.setStepSize( stepSize );
int nAtt = miningInputStream.getMetaData().getAttributesNumber();
svmm = new SupportVectorMiningModel[nAtt];
for (int i = 0; i < nAtt; i++) {
// Create mining input stream for training i-th classifie:
MiningStoredData msd = new MiningStoredData();
MiningAttribute tarAtt = miningInputStream.getMetaData().getMiningAttribute(i);
miningInputStream.reset();
bvStream.transformTarget(miningInputStream, tarAtt, msd);
// Display transformed stream:
System.out.println("Transformed: ");
System.out.println("nAtt = "+ msd.getMetaData().getAttributesNumber() +
" nVec = " + msd.getVectorsNumber() );
System.out.println(msd);
// Run regression method:
SupportVectorSettings svmSettings = new SupportVectorSettings();
svmSettings.setDataSpecification( msd.getMetaData() );
// Assign settings:
int iTar = msd.getMetaData().getAttributesNumber()-1;
svmSettings.setTarget( msd.getMetaData().getMiningAttribute(iTar) );
svmSettings.setSvmType( SupportVectorSettings.SVM_EPSILON_SVR );
svmSettings.setKernelType( SupportVectorSettings.KERNEL_POLY);
svmSettings.setC(1.0);
svmSettings.setGamma(1.0);
svmSettings.setDegree(2.0);
svmSettings.setLossEpsilon(0.1);
svmSettings.verifySettings();
// Get default mining algorithm specification from 'algorithms.xml':
MiningAlgorithmSpecification miningAlgorithmSpecification =
MiningAlgorithmSpecification.getMiningAlgorithmSpecification( regressionMethod, null );
if( miningAlgorithmSpecification == null )
throw new MiningException( "Can't find application " + regressionMethod );
// Get class name from algorithms specification:
String className = miningAlgorithmSpecification.getClassname();
if( className == null )
throw new MiningException( "className attribute of regressionMethod expected." );
// Create algorithm object with default values:
MiningAlgorithm svmAlgo = GeneralUtils.createMiningAlgorithmInstance(className);
// Put it all together:
svmAlgo.setMiningInputStream( msd );
svmAlgo.setMiningSettings( svmSettings );
svmAlgo.setMiningAlgorithmSpecification( miningAlgorithmSpecification );
svmAlgo.verify();
// Build the mining model:
msd.reset();
svmm[i] = (SupportVectorMiningModel) svmAlgo.buildModel();
RegressionDeviationAssessment rda = new RegressionDeviationAssessment();
rda.setAssessmentData(msd);
rda.setMiningModel(svmm[i]);
msd.reset();
double dev = rda.calculateAssessment();
System.out.println("dev = " + dev);
};
}
/**
* Returns approximators for all attributes.
*
* @return array of approximators for all time series
*/
protected SupervisedMiningModel[] getApproximator() {
return svmm;
}
/**
* Returns name of regression method for MiningAlgorithmSpecification.
*
* @return name of regression method
*/
public String getRegressionMethod()
{
return regressionMethod;
}
/**
* Sets new name of regression method for MiningAlgorithmSpecification.
*
* @param regressionMethod new name of regression method
*/
public void setRegressionMethod(String regressionMethod)
{
this.regressionMethod = regressionMethod;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -