/usr/lib/python3/dist-packages/mdp/test/test_svm_classifier.py is in python3-mdp 3.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | from __future__ import division
from builtins import zip
from builtins import range
from builtins import object
from past.utils import old_div
from ._tools import *
def _randomly_filled_hypercube(widths, num_elem=1000):
"""Fills a hypercube with given widths, centred at the origin.
"""
p = []
for i in range(num_elem):
rand_data = numx_rand.random(len(widths))
rand_data = [w*(d - 0.5) for d, w in zip(rand_data, widths)]
p.append(tuple(rand_data))
return p
def _randomly_filled_hyperball(dim, radius, num_elem=1000):
"""Fills a hyperball with a number of random elements.
"""
r = numx_rand.random(num_elem)
points = numx_rand.random((num_elem, dim))
for i in range(len(points)):
norm = numx.linalg.norm(points[i])
scale = pow(r[i], old_div(1.,dim))
points[i] = points[i] * radius * scale / norm
return points
def _random_clusters(positions, radius=1, num_elem=1000):
"""Puts random clusters with num_elem elements at the given positions.
positions - a list of tuples
"""
data = []
for p in positions:
dim = len(p)
ball = _randomly_filled_hyperball(dim, radius, num_elem)
ball = [numx.array(b) + numx.array(p) for b in ball]
data.append(ball)
return data
def _separable_data(positions, labels, radius=1, num_elem=1000, shuffled=False):
"""
For each position, we create num_elem data points in a certain radius around
that position. If shuffled, we shuffle the output data and labels.
positions -- List of position tuples, e.g. [(1, 1), (-1, -1)]
labels -- List of labels, e.g. [1, -1]
radius -- The maximum distance to the position
num_elem -- The number of elements to be created
shuffled -- Should the output be shuffled.
Returns:
data, labels
"""
assert len(positions) == len(labels)
data = numx.vstack( _random_clusters(positions, radius, num_elem) )
#data = numx.vstack( (numx_rand.random( (num_elem,2) ) - dist,
# numx_rand.random( (num_elem,2) ) + dist) )
a_labels = numx.hstack([[x] * num_elem for x in labels])
if shuffled:
ind = list(range(len(data)))
numx_rand.shuffle(ind)
return data[ind], a_labels[ind]
return data, a_labels
def _sqdist(tuple_a, tuple_b):
return sum( (a-b)**2 for a, b in zip(tuple_a, tuple_b) )
def test_separable_data_is_inside_radius():
positions = [[(1, 1), (-1, -1)],
[(1, 1, 10), (100, -20, 30), (-1, 10, 1000)]]
labels = [[1, -1], [1, 2, 3]]
radii = [0.5, 1, 10]
num_elem = 100
for pos, labs in zip(positions, labels):
for rad in radii:
data, ls = _separable_data(pos, labs, rad, num_elem)
for d,l in zip(data, ls):
idx = labs.index(l)
assert rad**2 > _sqdist(pos[idx], d)
@skip_on_condition(
"not hasattr(mdp.nodes, 'ShogunSVMClassifier')",
"This test requires the 'shogun' module.")
def test_ShogunSVMClassifier():
# TODO: Implement parameter ranges
num_train = 100
num_test = 50
for positions in [((1,), (-1,)),
((1,1), (-1,-1)),
((1,1,1), (-1,-1,1)),
((1,1,1,1), (-1,1,1,1)),
((1,1,1,1), (-1,-1,-1,-1)),
((1,1), (-1,-1), (1, -1), (-1, 1))
]:
radius = 0.3
if len(positions) == 2:
labels = (-1, 1)
elif len(positions) == 3:
labels = (-1, 1, 1)
elif len(positions) == 4:
labels = (-1, -1, 1, 1)
traindata_real, trainlab = _separable_data(positions, labels,
radius, num_train)
testdata_real, testlab = _separable_data(positions, labels,
radius, num_test)
classifiers = ['GMNPSVM', 'GNPPSVM', 'GPBTSVM', #'KernelPerceptron',
'LDA', 'LibSVM', #'LibSVMOneClass', 'MPDSVM',
'Perceptron', 'SVMLin']
kernels = ['PolyKernel', 'LinearKernel', 'SigmoidKernel', 'GaussianKernel']
#kernels = list(mdp.nodes.ShogunSVMClassifier.kernel_parameters.keys())
combinations = {'classifier': classifiers,
'kernel': kernels}
for comb in utils.orthogonal_permutations(combinations):
# this is redundant but makes it clear,
# what has been taken out deliberately
if comb['kernel'] in ['PyramidChi2', 'Chi2Kernel']:
# We don't have good init arguments for these
continue
if comb['classifier'] in ['LaRank', 'LibLinear', 'LibSVMMultiClass',
'MKLClassification', 'MKLMultiClass',
'MKLOneClass', 'MultiClassSVM', 'SVM',
'SVMOcas', 'SVMSGD', 'ScatterSVM',
'SubGradientSVM']:
# We don't have good init arguments for these and/or they work differently
continue
# something does not work here: skipping
if comb['classifier'] == 'GPBTSVM' and comb['kernel'] == 'LinearKernel':
continue
sg_node = mdp.nodes.ShogunSVMClassifier(classifier=comb['classifier'])
if sg_node.classifier.takes_kernel:
sg_node.set_kernel(comb['kernel'])
# train in two chunks to check update mechanism
sg_node.train( traindata_real[:num_train], trainlab[:num_train] )
sg_node.train( traindata_real[num_train:], trainlab[num_train:] )
assert sg_node.input_dim == len(traindata_real.T)
out = sg_node.label(testdata_real)
if sg_node.classifier.takes_kernel:
# check that the kernel has stored all our training vectors
assert sg_node.classifier.kernel.get_num_vec_lhs() == num_train * len(positions)
# check that the kernel has also stored the latest classification vectors in rhs
assert sg_node.classifier.kernel.get_num_vec_rhs() == num_test * len(positions)
# Test also for inverse
worked = numx.all(numx.sign(out) == testlab) or \
numx.all(numx.sign(out) == -testlab)
failed = not worked
should_fail = False
if len(positions) == 2:
if comb['classifier'] in ['LibSVMOneClass',
'GMNPSVM']:
should_fail = True
if comb['classifier'] == 'GPBTSVM' and \
comb['kernel'] in ['LinearKernel']:
should_fail = True
# xor problem
if len(positions) == 4:
if comb['classifier'] in ['LibSVMOneClass', 'SVMLin', 'Perceptron',
'LDA', 'GMNPSVM']:
should_fail = True
if comb['classifier'] == 'LibSVM' and \
comb['kernel'] in ['LinearKernel', 'SigmoidKernel']:
should_fail = True
if comb['classifier'] == 'GPBTSVM' and \
comb['kernel'] in ['LinearKernel', 'SigmoidKernel']:
should_fail = True
if comb['classifier'] == 'GNPPSVM' and \
comb['kernel'] in ['LinearKernel', 'SigmoidKernel']:
should_fail = True
if should_fail:
msg = ("Classification should fail but did not in %s. Positions %s." %
(sg_node.classifier, positions))
else:
msg = ("Classification should not fail but failed in %s. Positions %s." %
(sg_node.classifier, positions))
assert should_fail == failed, msg
class TestLibSVMClassifier(object):
@skip_on_condition("not hasattr(mdp.nodes, 'LibSVMClassifier')",
"This test requires the 'libsvm' module.")
def setup_method(self, method):
self.combinations = {'kernel': mdp.nodes.LibSVMClassifier.kernels,
'classifier': mdp.nodes.LibSVMClassifier.classifiers}
def test_that_parameters_are_correct(self):
import svm as libsvm
for comb in utils.orthogonal_permutations(self.combinations):
C = 1.01
epsilon = 1.1e-5
svm_node = mdp.nodes.LibSVMClassifier(params={"C": C, "eps": epsilon})
svm_node.set_kernel(comb['kernel'])
svm_node.set_classifier(comb['classifier'])
# check that the parameters are correct
assert svm_node.parameter.kernel_type == getattr(libsvm, comb['kernel'])
assert svm_node.parameter.svm_type == getattr(libsvm, comb['classifier'])
assert svm_node.parameter.C == C
assert svm_node.parameter.eps == epsilon
def test_linear_separable_data(self):
num_train = 100
num_test = 50
C = 1.01
epsilon = 1e-5
for positions in [((1,), (-1,)),
((1,1), (-1,-1)),
((1,1,1), (-1,-1,1)),
((1,1,1,1), (-1,1,1,1)),
((1,1,1,1), (-1,-1,-1,-1))]:
radius = 0.3
traindata_real, trainlab = _separable_data(positions, (-1, 1),
radius, num_train, True)
testdata_real, testlab = _separable_data(positions, (-1, 1),
radius, num_test, True)
for comb in utils.orthogonal_permutations(self.combinations):
# Take out non-working cases
if comb['classifier'] in ["ONE_CLASS"]:
continue
if comb['kernel'] in ["SIGMOID", "POLY"]:
continue
if len(positions[0]) == 1 and comb['kernel'] == "RBF":
# RBF won't work in 1d
continue
svm_node = mdp.nodes.LibSVMClassifier(kernel=comb['kernel'],
classifier=comb['classifier'],
probability=True,
params={"C": C, "eps": epsilon})
# train in two chunks to check update mechanism
svm_node.train(traindata_real[:num_train], trainlab[:num_train])
svm_node.train(traindata_real[num_train:], trainlab[num_train:])
assert svm_node.input_dim == len(traindata_real.T)
out = svm_node.label(testdata_real)
testerr = numx.all(numx.sign(out) == testlab)
assert testerr, ('classification error for ', comb)
# we don't have ranks in our regression models
if not comb['classifier'].endswith("SVR"):
pos1_rank = numx.array(svm_node.rank(numx.array([positions[0]])))
pos2_rank = numx.array(svm_node.rank(numx.array([positions[1]])))
assert numx.all(pos1_rank == -pos2_rank)
assert numx.all(abs(pos1_rank) == 1)
assert numx.all(abs(pos2_rank) == 1)
|