Changeset 3672:0398fd6b4821 in orange
 Timestamp:
 05/21/07 11:14:53 (7 years ago)
 Branch:
 default
 Convert:
 a85530f14d7c75d839e5881475d78d9457ead50e
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

orange/orngSVM.py
r3604 r3672 22 22 return l 23 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 24 30 class SVMLearnerClass: 25 31 def __init__(self, **kwds): 26 self.learner=orange.SVMLearner()27 32 self.svm_type=0 28 33 self.kernel_type=2 … … 41 46 self.learner=orange.SVMLearner(**kwds) 42 47 43 def __setattr__(self, name, value):48 """def __setattr__(self, name, value): 44 49 if name in ["svm_type", "kernel_type", "kernelFunc", "C", "nu", "p", "gamma", "degree", 45 50 "coef0", "shrinking", "probability", "cache_size", "eps"]: 46 51 self.learner.__dict__[name]=value 47 self.__dict__[name]=value 52 self.__dict__[name]=value""" 48 53 49 54 def __call__(self, examples, weight=0): … … 55 60 raise AttributeError, "Custom kernel function not supplied" 56 61 ################################################## 57 if self.kernel_type==4: #There is a bug in svm 62 if self.kernel_type==4: #There is a bug in svm. For some unknown reason only the probability model works with custom kernels 58 63 self.probability=True 59 64 ################################################## … … 66 71 def learnClassifier(self, examples): 67 72 return self.learner(examples) 68 73 74 class SVMLearnerSparseClass(SVMLearnerClass): 75 def __init__(self, **kwds): 76 SVMLearnerClass.__init__(self, **kwds) 77 self.learner=orange.SVMLearnerSparse(**kwds) 69 78 70 79 def parameter_selection(learner, data, folds=4, parameters={}, best={}, callback=None): … … 125 134 return l 126 135 136 def SVMLearnerSparseEasy(examples=None, weightID=0, **kwds): 137 l=apply(SVMLearnerSparseClassEasy, (), kwds) 138 if examples: 139 l=l(examples) 140 return l 141 127 142 128 143 class SVMLearnerClassEasy(SVMLearnerClass): … … 154 169 return SVMClassifierClassEasyWrapper(self.learner(newexamples), newdomain) 155 170 171 class SVMLearnerSparseClassEasy(SVMLearnerClassEasy, SVMLearnerSparseClass): 172 def __init__(self, **kwds): 173 SVMLearnerSparseClass.__init__(self, **kwds) 174 156 175 class SVMClassifierClassEasyWrapper: 157 176 def __init__(self, classifier, domain=None): … … 164 183 return getattr(self.classifier,name) 165 184 185 def getLinearSVMWeights(classifier): 186 """returns list of weights of linear class vs. class classifiers for the linear multiclass svm classifier. The list is in the order of 1vs2, 1vs3 ... 1vsN, 2vs3 ...""" 187 def updateWeights(w, key, val, mul): 188 if key in w: 189 w[key]+=mul*val 190 else: 191 w[key]=mul*val 192 193 SVs=classifier.supportVectors 194 weights=[] 195 classes=classifier.supportVectors.domain.classVar.values 196 classSV=dict([(value, filter(lambda sv: sv.getclass()==value, classifier.supportVectors)) for value in classes]) 197 svRanges=[(0, classifier.nSV[0])] 198 for n in classifier.nSV[1:]: 199 svRanges.append((svRanges[1][1], svRanges[1][1]+n)) 200 for i in range(len(classes)1): 201 for j in range(i+1, len(classes)): 202 w={} 203 print i,j, j1, i 204 coefInd=j1 205 for svInd in apply(range, svRanges[i]): 206 for attr in SVs.domain.attributes+SVs[svInd].getmetas(orange.Variable).keys(): 207 if attr.varType==orange.VarTypes.Continuous: 208 updateWeights(w, attr, float(SVs[svInd][attr]), classifier.coef[coefInd][svInd]) 209 coefInd=i 210 for svInd in apply(range, svRanges[j]): 211 for attr in SVs.domain.attributes+SVs[svInd].getmetas(orange.Variable).keys(): 212 if attr.varType==orange.VarTypes.Continuous: 213 updateWeights(w, attr, float(SVs[svInd][attr]), classifier.coef[coefInd][svInd]) 214 weights.append(w) 215 return weights 216 166 217 import math 167 218 class KernelWrapper:
Note: See TracChangeset
for help on using the changeset viewer.