Changeset 5047:b10cd272be47 in orange
 Timestamp:
 08/01/08 10:54:06 (6 years ago)
 Branch:
 default
 Convert:
 a6a38b29d6198239b5d260c3debd2e50a32a60fd
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

orange/orngSVM.py
r5028 r5047 16 16 pass 17 17 18 def SVMLearner(examples=None, weightID=0, **kwds): 19 l=apply(SVMLearnerClass, (), kwds) 20 if examples: 21 l=l(examples) 22 return l 23 24 def SVMLearnerSparse(examples=None, weightID=0, **kwds): 25 l=apply(SVMLearnerSparseClass, (), kwds) 26 if examples: 27 l=l(examples) 28 return l 29 30 class SVMLearnerClass: 31 def __init__(self, **kwds): 32 self.svm_type=0 18 class SVMLearner(orange.Learner): 19 def __new__(cls, examples=None, weightID=0, **kwargs): 20 self = orange.Learner.__new__(cls, **kwargs) 21 if examples: 22 self.__init__(**kwargs) 23 return self.__call__(examples, weightID) 24 else: 25 return self 26 27 def __init__(self, **kwargs): 28 self.svm_type=orange.SVMLearner.Nu_SVC 33 29 self.kernel_type=2 34 30 self.kernelFunc=None … … 43 39 self.cache_size=100 44 40 self.eps=0.001 45 self.__dict__.update(kwds) 46 self.learner=orange.SVMLearner(**kwds) 47 48 """def __setattr__(self, name, value): 49 if name in ["svm_type", "kernel_type", "kernelFunc", "C", "nu", "p", "gamma", "degree", 50 "coef0", "shrinking", "probability", "cache_size", "eps"]: 51 self.learner.__dict__[name]=value 52 self.__dict__[name]=value""" 41 self.__dict__.update(kwargs) 42 self.learner=orange.SVMLearner(**kwargs) 53 43 54 44 def __call__(self, examples, weight=0): … … 74 64 return self.learner(examples) 75 65 76 class SVMLearnerSparse Class(SVMLearnerClass):66 class SVMLearnerSparse(SVMLearner): 77 67 def __init__(self, **kwds): 78 SVMLearner Class.__init__(self, **kwds)68 SVMLearner.__init__(self, **kwds) 79 69 self.learner=orange.SVMLearnerSparse(**kwds) 80 70 … … 130 120 return best 131 121 132 def SVMLearnerEasy(examples=None, weightID=0, **kwds):133 l=apply(SVMLearnerClassEasy, (), kwds)134 if examples:135 l=l(examples)136 return l137 138 def SVMLearnerSparseEasy(examples=None, weightID=0, **kwds):139 l=apply(SVMLearnerSparseClassEasy, (), kwds)140 if examples:141 l=l(examples)142 return l143 144 122 145 class SVMLearnerClassEasy(SVMLearnerClass): 146 folds=5 123 class SVMLearnerEasy(SVMLearner): 147 124 def __init__(self, **kwds): 148 SVMLearnerClass.__init__(self, **kwds) 125 self.folds=5 126 SVMLearner.__init__(self, **kwds) 149 127 150 128 def learnClassifier(self, examples): … … 171 149 return SVMClassifierClassEasyWrapper(self.learner(newexamples), newdomain, examples) 172 150 173 class SVMLearnerSparseClassEasy(SVMLearner ClassEasy, SVMLearnerSparseClass):151 class SVMLearnerSparseClassEasy(SVMLearnerEasy, SVMLearnerSparse): 174 152 def __init__(self, **kwds): 175 SVMLearnerSparse Class.__init__(self, **kwds)153 SVMLearnerSparse.__init__(self, **kwds) 176 154 177 155 class SVMClassifierClassEasyWrapper: … … 236 214 237 215 import math 238 class KernelWrapper :216 class KernelWrapper(object): 239 217 def __init__(self, wrapped): 240 218 self.wrapped=wrapped … … 276 254 return self.l*self.wrapped1(example1, example2) + (1self.l)*self.wrapped2(example1,example2) 277 255 278 from sets import Set 279 class SparseLinKernel: 256 class SparseLinKernel(object): 280 257 """Computes a linear kernel function using the examples meta attributes (need to be floats)""" 281 258 def __call__(self, example1, example2): 282 s= Set(example1.getmetas().keys()+example2.getmetas().keys())259 s=set(example1.getmetas().keys()+example2.getmetas().keys()) 283 260 sum=0 284 261 getmeta=lambda e: e.hasmeta(key) and float(e[key]) or 0.0 … … 287 264 return pow(sum, 0.5) 288 265 289 class BagOfWords :266 class BagOfWords(object): 290 267 """Computes a BOW kernel function (sum_i(example1[i]*example2[i])) using the examples meta attributes (need to be floats)""" 291 268 def __call__(self, example1, example2):
Note: See TracChangeset
for help on using the changeset viewer.