/usr/share/pyshared/mvpa/clfs/warehouse.py is in python-mvpa 0.4.8-3.
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 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 | # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
#
# See COPYING file distributed along with the PyMVPA package for the
# copyright and license terms.
#
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
"""Collection of classifiers to ease the exploration.
"""
__docformat__ = 'restructuredtext'
import operator
# Define sets of classifiers
from mvpa.clfs.meta import FeatureSelectionClassifier, SplitClassifier, \
MulticlassClassifier
from mvpa.clfs.smlr import SMLR
from mvpa.clfs.knn import kNN
from mvpa.clfs.gnb import GNB
from mvpa.clfs.kernel import KernelLinear, KernelSquaredExponential
# Helpers
from mvpa.base import externals, cfg
from mvpa.measures.anova import OneWayAnova
from mvpa.misc.transformers import Absolute
from mvpa.clfs.smlr import SMLRWeights
from mvpa.featsel.helpers import FractionTailSelector, \
FixedNElementTailSelector, RangeElementSelector
from mvpa.featsel.base import SensitivityBasedFeatureSelection
_KNOWN_INTERNALS = [ 'knn', 'binary', 'svm', 'linear',
'smlr', 'does_feature_selection', 'has_sensitivity',
'multiclass', 'non-linear', 'kernel-based', 'lars',
'regression', 'libsvm', 'sg', 'meta', 'retrainable', 'gpr',
'notrain2predict', 'ridge', 'blr', 'gnpp', 'enet', 'glmnet',
'gnb', 'plr']
class Warehouse(object):
"""Class to keep known instantiated classifiers
Should provide easy ways to select classifiers of needed kind:
clfswh['linear', 'svm'] should return all linear SVMs
clfswh['linear', 'multiclass'] should return all linear classifiers
capable of doing multiclass classification
"""
def __init__(self, known_tags=None, matches=None):
"""Initialize warehouse
:Parameters:
known_tags : list of basestring
List of known tags
matches : dict
Optional dictionary of additional matches. E.g. since any
regression can be used as a binary classifier,
matches={'binary':['regression']}, would allow to provide
regressions also if 'binary' was requested
"""
self._known_tags = set(known_tags)
self.__items = []
self.__keys = set()
if matches is None:
matches = {}
self.__matches = matches
def __getitem__(self, *args):
if isinstance(args[0], tuple):
args = args[0]
# so we explicitely handle [:]
if args == (slice(None),):
args = []
# lets remove optional modifier '!'
dargs = set([str(x).lstrip('!') for x in args]).difference(
self._known_tags)
if len(dargs)>0:
raise ValueError, "Unknown internals %s requested. Known are %s" % \
(list(dargs), list(self._known_tags))
# dummy implementation for now
result = []
# check every known item
for item in self.__items:
good = True
# by default each one counts
for arg in args:
# check for rejection first
if arg.startswith('!'):
if (arg[1:] in item._clf_internals):
good = False
break
else:
continue
# check for inclusion
found = False
for arg in [arg] + self.__matches.get(arg, []):
if (arg in item._clf_internals):
found = True
break
good = found
if not good:
break
if good:
result.append(item)
return result
def __iadd__(self, item):
if operator.isSequenceType(item):
for item_ in item:
self.__iadd__(item_)
else:
if not hasattr(item, '_clf_internals'):
raise ValueError, "Cannot register %s " % item + \
"which has no _clf_internals defined"
if len(item._clf_internals) == 0:
raise ValueError, "Cannot register %s " % item + \
"which has empty _clf_internals"
clf_internals = set(item._clf_internals)
if clf_internals.issubset(self._known_tags):
self.__items.append(item)
self.__keys |= clf_internals
else:
raise ValueError, 'Unknown clf internal(s) %s' % \
clf_internals.difference(self._known_tags)
return self
@property
def internals(self):
"""Known internal tags of the classifiers
"""
return self.__keys
def listing(self):
"""Listing (description + internals) of registered items
"""
return [(x.descr, x._clf_internals) for x in self.__items]
@property
def items(self):
"""Registered items
"""
return self.__items
clfswh = Warehouse(known_tags=_KNOWN_INTERNALS) # classifiers
regrswh = Warehouse(known_tags=_KNOWN_INTERNALS) # regressions
# NB:
# - Nu-classifiers are turned off since for haxby DS default nu
# is an 'infisible' one
# - Python's SMLR is turned off for the duration of development
# since it is slow and results should be the same as of C version
#
clfswh += [ SMLR(lm=0.1, implementation="C", descr="SMLR(lm=0.1)"),
SMLR(lm=1.0, implementation="C", descr="SMLR(lm=1.0)"),
#SMLR(lm=10.0, implementation="C", descr="SMLR(lm=10.0)"),
#SMLR(lm=100.0, implementation="C", descr="SMLR(lm=100.0)"),
#SMLR(implementation="Python", descr="SMLR(Python)")
]
clfswh += \
[ MulticlassClassifier(clfswh['smlr'][0],
descr='Pairs+maxvote multiclass on ' + \
clfswh['smlr'][0].descr) ]
if externals.exists('libsvm'):
from mvpa.clfs import libsvmc as libsvm
clfswh._known_tags.update(libsvm.SVM._KNOWN_IMPLEMENTATIONS.keys())
clfswh += [libsvm.SVM(descr="libsvm.LinSVM(C=def)", probability=1),
libsvm.SVM(
C=-10.0, descr="libsvm.LinSVM(C=10*def)", probability=1),
libsvm.SVM(
C=1.0, descr="libsvm.LinSVM(C=1)", probability=1),
libsvm.SVM(svm_impl='NU_SVC',
descr="libsvm.LinNuSVM(nu=def)", probability=1)
]
clfswh += [libsvm.SVM(kernel_type='RBF', descr="libsvm.RbfSVM()"),
libsvm.SVM(kernel_type='RBF', svm_impl='NU_SVC',
descr="libsvm.RbfNuSVM(nu=def)"),
libsvm.SVM(kernel_type='poly',
descr='libsvm.PolySVM()', probability=1),
#libsvm.svm.SVM(kernel_type='sigmoid',
# svm_impl='C_SVC',
# descr='libsvm.SigmoidSVM()'),
]
# regressions
regrswh._known_tags.update(['EPSILON_SVR', 'NU_SVR'])
regrswh += [libsvm.SVM(svm_impl='EPSILON_SVR', descr='libsvm epsilon-SVR',
regression=True),
libsvm.SVM(svm_impl='NU_SVR', descr='libsvm nu-SVR',
regression=True)]
if externals.exists('shogun'):
from mvpa.clfs import sg
clfswh._known_tags.update(sg.SVM._KNOWN_IMPLEMENTATIONS)
# some classifiers are not yet ready to be used out-of-the-box in
# PyMVPA, thus we don't populate warehouse with their instances
bad_classifiers = [
'mpd', # was segfault, now non-training on testcases, and XOR.
# and was described as "for educational purposes", thus
# shouldn't be used for real data ;-)
# Should be a drop-in replacement for lightsvm
'gpbt', # fails to train for testAnalyzerWithSplitClassifier
# also 'retraining' doesn't work -- fails to generalize
'gmnp', # would fail with 'assertion Cache_Size > 2'
# if shogun < 0.6.3, also refuses to train
'svrlight', # fails to 'generalize' as a binary classifier
# after 'binning'
'krr', # fails to generalize
]
if not externals.exists('sg_fixedcachesize'):
# would fail with 'assertion Cache_Size > 2' if shogun < 0.6.3
bad_classifiers.append('gnpp')
for impl in sg.SVM._KNOWN_IMPLEMENTATIONS:
# Uncomment the ones to disable
if impl in bad_classifiers:
continue
clfswh += [
sg.SVM(
descr="sg.LinSVM(C=def)/%s" % impl, svm_impl=impl),
sg.SVM(
C=-10.0, descr="sg.LinSVM(C=10*def)/%s" % impl, svm_impl=impl),
sg.SVM(
C=1.0, descr="sg.LinSVM(C=1)/%s" % impl, svm_impl=impl),
]
clfswh += [
sg.SVM(kernel_type='RBF',
descr="sg.RbfSVM()/%s" % impl, svm_impl=impl),
# sg.SVM(kernel_type='RBF',
# descr="sg.RbfSVM(gamma=0.1)/%s"
# % impl, svm_impl=impl, gamma=0.1),
# sg.SVM(descr="sg.SigmoidSVM()/%s"
# % impl, svm_impl=impl, kernel_type="sigmoid"),
]
_optional_regressions = []
if externals.exists('shogun.krr'):
_optional_regressions += ['krr']
for impl in ['libsvr'] + _optional_regressions:# \
# XXX svrlight sucks in SG -- dont' have time to figure it out
#+ ([], ['svrlight'])['svrlight' in sg.SVM._KNOWN_IMPLEMENTATIONS]:
regrswh._known_tags.update([impl])
regrswh += [ sg.SVM(svm_impl=impl, descr='sg.LinSVMR()/%s' % impl,
regression=True),
#sg.SVM(svm_impl=impl, kernel_type='RBF',
# descr='sg.RBFSVMR()/%s' % impl,
# regression=True),
]
if len(clfswh['svm', 'linear']) > 0:
# if any SVM implementation is known, import default ones
from mvpa.clfs.svm import *
# lars from R via RPy
if externals.exists('lars'):
import mvpa.clfs.lars as lars
from mvpa.clfs.lars import LARS
for model in lars.known_models:
# XXX create proper repository of classifiers!
lars_clf = LARS(descr="LARS(%s)" % model, model_type=model)
clfswh += lars_clf
# is a regression, too
lars_regr = LARS(descr="_LARS(%s, regression=True)" % model,
regression=True, model_type=model)
regrswh += lars_regr
# clfswh += MulticlassClassifier(lars,
# descr='Multiclass %s' % lars.descr)
## PBS: enet has some weird issue that causes it to fail. GLMNET is
## better anyway, so just use that instead
## # enet from R via RPy
## if externals.exists('elasticnet'):
## from mvpa.clfs.enet import ENET
## clfswh += ENET(descr="ENET()")
## regrswh += ENET(descr="ENET(regression=True)", regression=True)
# glmnet from R via RPy
if externals.exists('glmnet'):
from mvpa.clfs.glmnet import GLMNET_C, GLMNET_R
clfswh += GLMNET_C(descr="GLMNET_C()")
regrswh += GLMNET_R(descr="GLMNET_R()")
# kNN
clfswh += kNN(k=5, descr="kNN(k=5)")
clfswh += kNN(k=5, voting='majority', descr="kNN(k=5, voting='majority')")
clfswh += \
FeatureSelectionClassifier(
kNN(),
SensitivityBasedFeatureSelection(
SMLRWeights(SMLR(lm=1.0, implementation="C")),
RangeElementSelector(mode='select')),
descr="kNN on SMLR(lm=1) non-0")
clfswh += \
FeatureSelectionClassifier(
kNN(),
SensitivityBasedFeatureSelection(
OneWayAnova(),
FractionTailSelector(0.05, mode='select', tail='upper')),
descr="kNN on 5%(ANOVA)")
clfswh += \
FeatureSelectionClassifier(
kNN(),
SensitivityBasedFeatureSelection(
OneWayAnova(),
FixedNElementTailSelector(50, mode='select', tail='upper')),
descr="kNN on 50(ANOVA)")
# GNB
clfswh += GNB(descr="GNB()")
clfswh += GNB(common_variance=True, descr="GNB(common_variance=True)")
clfswh += GNB(prior='uniform', descr="GNB(prior='uniform')")
clfswh += \
FeatureSelectionClassifier(
GNB(),
SensitivityBasedFeatureSelection(
OneWayAnova(),
FractionTailSelector(0.05, mode='select', tail='upper')),
descr="GNB on 5%(ANOVA)")
# GPR
if externals.exists('scipy'):
from mvpa.clfs.gpr import GPR
clfswh += GPR(kernel=KernelLinear(), descr="GPR(kernel='linear')")
clfswh += GPR(kernel=KernelSquaredExponential(),
descr="GPR(kernel='sqexp')")
# BLR
from mvpa.clfs.blr import BLR
clfswh += BLR(descr="BLR()")
#PLR
from mvpa.clfs.plr import PLR
clfswh += PLR(descr="PLR()")
if externals.exists('scipy'):
clfswh += PLR(reduced=0.05, descr="PLR(reduced=0.01)")
# SVM stuff
if len(clfswh['linear', 'svm']) > 0:
linearSVMC = clfswh['linear', 'svm',
cfg.get('svm', 'backend', default='libsvm').lower()
][0]
# "Interesting" classifiers
clfswh += \
FeatureSelectionClassifier(
linearSVMC.clone(),
SensitivityBasedFeatureSelection(
SMLRWeights(SMLR(lm=0.1, implementation="C")),
RangeElementSelector(mode='select')),
descr="LinSVM on SMLR(lm=0.1) non-0")
clfswh += \
FeatureSelectionClassifier(
linearSVMC.clone(),
SensitivityBasedFeatureSelection(
SMLRWeights(SMLR(lm=1.0, implementation="C")),
RangeElementSelector(mode='select')),
descr="LinSVM on SMLR(lm=1) non-0")
# "Interesting" classifiers
clfswh += \
FeatureSelectionClassifier(
RbfCSVMC(),
SensitivityBasedFeatureSelection(
SMLRWeights(SMLR(lm=1.0, implementation="C")),
RangeElementSelector(mode='select')),
descr="RbfSVM on SMLR(lm=1) non-0")
clfswh += \
FeatureSelectionClassifier(
linearSVMC.clone(),
SensitivityBasedFeatureSelection(
OneWayAnova(),
FractionTailSelector(0.05, mode='select', tail='upper')),
descr="LinSVM on 5%(ANOVA)")
clfswh += \
FeatureSelectionClassifier(
linearSVMC.clone(),
SensitivityBasedFeatureSelection(
OneWayAnova(),
FixedNElementTailSelector(50, mode='select', tail='upper')),
descr="LinSVM on 50(ANOVA)")
clfswh += \
FeatureSelectionClassifier(
linearSVMC.clone(),
SensitivityBasedFeatureSelection(
linearSVMC.getSensitivityAnalyzer(transformer=Absolute),
FractionTailSelector(0.05, mode='select', tail='upper')),
descr="LinSVM on 5%(SVM)")
clfswh += \
FeatureSelectionClassifier(
linearSVMC.clone(),
SensitivityBasedFeatureSelection(
linearSVMC.getSensitivityAnalyzer(transformer=Absolute),
FixedNElementTailSelector(50, mode='select', tail='upper')),
descr="LinSVM on 50(SVM)")
### Imports which are specific to RFEs
# from mvpa.datasets.splitters import OddEvenSplitter
# from mvpa.clfs.transerror import TransferError
# from mvpa.featsel.rfe import RFE
# from mvpa.featsel.helpers import FixedErrorThresholdStopCrit
# from mvpa.clfs.transerror import ConfusionBasedError
# SVM with unbiased RFE -- transfer-error to another splits, or in
# other terms leave-1-out error on the same dataset
# Has to be bound outside of the RFE definition since both analyzer and
# error should use the same instance.
rfesvm_split = SplitClassifier(linearSVMC)#clfswh['LinearSVMC'][0])
# "Almost" classical RFE. If this works it would differ only that
# our transfer_error is based on internal splitting and classifier used
# within RFE is a split classifier and its sensitivities per split will get
# averaged
#
#clfswh += \
# FeatureSelectionClassifier(
# clf = LinearCSVMC(), #clfswh['LinearSVMC'][0], # we train LinearSVM
# feature_selection = RFE( # on features selected via RFE
# # based on sensitivity of a clf which does splitting internally
# sensitivity_analyzer=rfesvm_split.getSensitivityAnalyzer(),
# transfer_error=ConfusionBasedError(
# rfesvm_split,
# confusion_state="confusion"),
# # and whose internal error we use
# feature_selector=FractionTailSelector(
# 0.2, mode='discard', tail='lower'),
# # remove 20% of features at each step
# update_sensitivity=True),
# # update sensitivity at each step
# descr='LinSVM+RFE(splits_avg)' )
#
#clfswh += \
# FeatureSelectionClassifier(
# clf = LinearCSVMC(), # we train LinearSVM
# feature_selection = RFE( # on features selected via RFE
# # based on sensitivity of a clf which does splitting internally
# sensitivity_analyzer=rfesvm_split.getSensitivityAnalyzer(),
# transfer_error=ConfusionBasedError(
# rfesvm_split,
# confusion_state="confusion"),
# # and whose internal error we use
# feature_selector=FractionTailSelector(
# 0.2, mode='discard', tail='lower'),
# # remove 20% of features at each step
# update_sensitivity=False),
# # update sensitivity at each step
# descr='LinSVM+RFE(splits_avg,static)' )
rfesvm = LinearCSVMC()
# This classifier will do RFE while taking transfer error to testing
# set of that split. Resultant classifier is voted classifier on top
# of all splits, let see what that would do ;-)
#clfswh += \
# SplitClassifier( # which does splitting internally
# FeatureSelectionClassifier(
# clf = LinearCSVMC(),
# feature_selection = RFE( # on features selected via RFE
# sensitivity_analyzer=\
# rfesvm.getSensitivityAnalyzer(transformer=Absolute),
# transfer_error=TransferError(rfesvm),
# stopping_criterion=FixedErrorThresholdStopCrit(0.05),
# feature_selector=FractionTailSelector(
# 0.2, mode='discard', tail='lower'),
# # remove 20% of features at each step
# update_sensitivity=True)),
# # update sensitivity at each step
# descr='LinSVM+RFE(N-Fold)')
#
#
#clfswh += \
# SplitClassifier( # which does splitting internally
# FeatureSelectionClassifier(
# clf = LinearCSVMC(),
# feature_selection = RFE( # on features selected via RFE
# sensitivity_analyzer=\
# rfesvm.getSensitivityAnalyzer(transformer=Absolute),
# transfer_error=TransferError(rfesvm),
# stopping_criterion=FixedErrorThresholdStopCrit(0.05),
# feature_selector=FractionTailSelector(
# 0.2, mode='discard', tail='lower'),
# # remove 20% of features at each step
# update_sensitivity=True)),
# # update sensitivity at each step
# splitter = OddEvenSplitter(),
# descr='LinSVM+RFE(OddEven)')
|