Examples

Regression

The objective of this Use Case is to create a SVM Regression algorithm in order to create a metamodel.

Otsvm enables :
  • to set lists of tradeoff factors and kernel parameter with the methods setTradeoffFactor, setKernelParameter.

  • to select the kernel type in this list : Linear Kernel, Polynomial Kernel, Sigmoid Kernel, RBF kernel.

  • to compute the algorithm on an input and output samples.

  • to compute the algorithm on an experiment plane and a function.

  • to compute the algorithm on an input and output samples and an isoprobabilistic distribution.

We recommend for users to use the RBF Kernel ( the gaussian kernel ). Moreover, it is important to understand that the selection of parameters ( kernel parameter and tradeoff factor ) is primary. If you don’t know what to take as parameters, you must take a wide range values, for example tradeoff \in \{10^-5,10^-3,10^-1...10^3 \} kernel\ parameter \in \{10^-15, 10^-13...,10^3 \}. Usually, the algorithm always converges, but this can take a long while, mostly if you have a lot of parameters to test.

import openturns as ot
import otsvm

# create a function, here we create the Sobol function
dimension = 3
meanTh = 1.0
a = ot.Point(dimension)
inputVariables = ot.Description(dimension)
outputVariables = ot.Description(1)
outputVariables[0] = "y"
formula = "1.0"
covTh = 1.0
for i in range(dimension):
    a[i] = 0.5 * i
    covTh = covTh * (1.0 + 1.0 / (3.0 * (1.0 + a[i]) ** 2))
    inputVariables[i] = "xi" + str(i)
    formula += (
        " * ((abs(4.0 * xi"
        + str(i)
        + " -2.0) + "
        + str(a[i])
        + ") / (1.0 + "
        + str(a[i])
        + "))"
    )
covTh = covTh - 1.0
model = ot.Function(inputVariables, outputVariables, ot.Description(1, formula))

# create the input distribution
ot.RandomGenerator.SetSeed(0)
marginals = ot.DistributionCollection(dimension)
for i in range(dimension):
    marginals[i] = ot.Uniform(0.0, 1.0)
distribution = ot.ComposedDistribution(marginals)

# create lists of kernel parameters and tradeoff factors
tradeoff = [0.01, 0.1, 1, 10, 100, 1000]
kernel = [0.001, 0.01, 0.1, 1, 10, 100]

# first example : create the problem with an input and output samples:
# first, we create samples
dataIn = distribution.getSample(250)
dataOut = model(dataIn)
# second, we create our svm regression object, we must select the third parameter
# in an enumerate in the list { NormalRBF, Linear, Sigmoid, Polynomial }
algo = otsvm.SVMRegression(dataIn, dataOut, otsvm.LibSVM.NormalRBF)
# third, we set kernel parameter and tradeoff factor
algo.setTradeoffFactor(tradeoff)
algo.setKernelParameter(kernel)
# Perform the algorithm
algo.run()
# Stream out the results
result = algo.getResult()
# get the residual error
residual = result.getResiduals()
# get the relative error
relativeError = result.getRelativeErrors()

# second example : create the problem with an experiment plane:
# first, we create the plane
myExperiment = ot.MonteCarloExperiment(distribution, 250)
# second, we create our svm regression object, the first parameter is the
# function
algo2 = otsvm.SVMRegression(model, myExperiment, otsvm.LibSVM.Linear)
# third, we set kernel parameter and tradeoff factor
algo2.setTradeoffFactor(tradeoff)
algo2.setKernelParameter(kernel)
# Perform the algorithm
algo2.run()
# Stream out the results
result = algo2.getResult()
# get the residual error
residual = result.getResiduals()
# get the relative error
relativeError = result.getRelativeErrors()

# third example : create the problem with an isoprobabilistic distribution
# first, we create our distribution
marginals = ot.DistributionCollection(dimension)
for i in range(dimension):
    marginals[i] = ot.Uniform(0.0, 1.0)
distribution = ot.ComposedDistribution(marginals)
# second, we create input and output samples
dataIn = distribution.getSample(250)
dataOut = model(dataIn)
# third, we create our svm regression
algo3 = otsvm.SVMRegression(dataIn, dataOut, distribution, otsvm.LibSVM.Polynomial)
# and to finish, we set kernel parameter and tradeoff factor
algo3.setTradeoffFactor(tradeoff)
algo3.setKernelParameter(kernel)
# Perform the algorithm
algo3.run()
# Stream out the results
result = algo3.getResult()
# get the residual error
residual = result.getResiduals()
# get the relative error
relativeError = result.getRelativeErrors()

# fourth example is here to present you the SVMResourceMap class.
# Users can fix others parameters like the degree and the constant of the
# Polynomial Kernel,the cacheSize, the number of folds or the epsilon
# first, we create samples
dataIn = distribution.getSample(250)
dataOut = model(dataIn)
# second, we create our svm regression object
# here, we select the Polynomial Kernel but by default his degree is 3. We want a
# degree of 2
ot.ResourceMap.Set("LibSVM-DegreePolynomialKernel", "2")
# now the degree of the Polynomial kernel is 2
algo = otsvm.SVMRegression(dataIn, dataOut, otsvm.LibSVM.Polynomial)
# third, we set kernel parameter and tradeoff factor
algo.setTradeoffFactor(tradeoff)
algo.setKernelParameter(kernel)
# Perform the algorithm
algo.run()
# Stream out the results
result = algo.getResult()
# get the residual error
residual = result.getResiduals()
# get the relative error
relativeError = result.getRelativeErrors()

Classification

The objective of this Use Case is to create a SVM Classification algorithm in order to build a metamodel that separates datas in 2 classes.

Otsvm enables to :
  • to set lists of tradeoff factors and kernel parameter with the methods setTradeoffFactor, setKernelParameter.

  • to select the kernel type in this list : Linear Kernel, Polynomial Kernel, Sigmoid Kernel, RBF kernel.

  • to compute the algorithm on an input and output samples.

import openturns as ot
import otsvm
import os

# this example uses a csv file with the datas for the classification
# we retrieve the sample from the file sample.csv
path = os.path.abspath(os.path.dirname(__file__))
dataInOut = ot.Sample.ImportFromCSVFile(path + "/sample.csv")

# we create dataIn and dataOut
dataIn = ot.Sample(861, 2)
dataOut = ot.Sample(861, 1)

# we build the input Sample and the output Sample because we must separate
# dataInOut
for i in range(861):
    a = dataInOut[i]
    b = ot.Point(2)
    b[0] = a[1]
    b[1] = a[2]
    dataIn[i] = b
    dataOut[i] = int(a[0])

# list of C parameter
cp = [0.000001, 0.00001, 0.0001, 0.001, 0.01, 0.1, 1, 10, 100]
# list of gamma parameter in kernel function
gamma = [0.000001, 0.00001, 0.0001, 0.001, 0.01, 0.1, 1, 10, 100]

# create the Classification Problem
algo = otsvm.LibSVMClassification(dataIn, dataOut)
algo.setKernelType(otsvm.LibSVM.NormalRbf)
algo.setTradeoffFactor(cp)
algo.setKernelParameter(gamma)

# compute the classification
algo.run()
print("#######################")
print("Results with Samples I/O")
print("Accuracy(p.c.)=", algo.getAccuracy())