1 | import sklearn.svm as skl_svm |
||
0 ignored issues
–
show
|
|||
2 | |||
3 | from Orange.classification import SklLearner, SklModel |
||
4 | from Orange.base import SklLearner as SklLearnerBase |
||
5 | from Orange.preprocess import Normalize |
||
6 | |||
7 | __all__ = ["SVMLearner", "LinearSVMLearner", "NuSVMLearner", |
||
8 | "OneClassSVMLearner"] |
||
9 | |||
10 | |||
11 | svm_pps = SklLearner.preprocessors + [Normalize()] |
||
12 | |||
13 | |||
14 | class SVMClassifier(SklModel): |
||
15 | |||
16 | def predict(self, X): |
||
17 | value = self.skl_model.predict(X) |
||
18 | if self.skl_model.probability: |
||
19 | prob = self.skl_model.predict_proba(X) |
||
20 | return value, prob |
||
21 | return value |
||
22 | |||
23 | |||
24 | class SVMLearner(SklLearner): |
||
25 | __wraps__ = skl_svm.SVC |
||
26 | __returns__ = SVMClassifier |
||
27 | name = 'svm' |
||
28 | preprocessors = svm_pps |
||
29 | |||
30 | def __init__(self, C=1.0, kernel='rbf', degree=3, gamma=0.0, |
||
0 ignored issues
–
show
|
|||
31 | coef0=0.0, shrinking=True, probability=False, |
||
0 ignored issues
–
show
|
|||
32 | tol=0.001, cache_size=200, max_iter=-1, |
||
0 ignored issues
–
show
|
|||
33 | preprocessors=None): |
||
34 | super().__init__(preprocessors=preprocessors) |
||
35 | self.params = vars() |
||
36 | self.supports_multiclass = True |
||
37 | self.supports_weights = True |
||
38 | |||
39 | |||
40 | class LinearSVMLearner(SklLearner): |
||
41 | __wraps__ = skl_svm.LinearSVC |
||
42 | name = 'linear svm' |
||
43 | preprocessors = svm_pps |
||
44 | |||
45 | def __init__(self, penalty='l2', loss='squared_hinge', dual=True, |
||
0 ignored issues
–
show
|
|||
46 | tol=0.0001, C=1.0, multi_class='ovr', fit_intercept=True, |
||
0 ignored issues
–
show
|
|||
47 | intercept_scaling=True, random_state=None, |
||
0 ignored issues
–
show
|
|||
48 | preprocessors=None): |
||
49 | super().__init__(preprocessors=preprocessors) |
||
50 | self.params = vars() |
||
51 | self.supports_multiclass = True |
||
52 | |||
53 | |||
54 | class NuSVMClassifier(SklModel): |
||
55 | |||
56 | def predict(self, X): |
||
57 | value = self.skl_model.predict(X) |
||
58 | if self.skl_model.probability: |
||
59 | prob = self.skl_model.predict_proba(X) |
||
60 | return value, prob |
||
61 | return value |
||
62 | |||
63 | |||
64 | class NuSVMLearner(SklLearner): |
||
65 | __wraps__ = skl_svm.NuSVC |
||
66 | __returns__ = NuSVMClassifier |
||
67 | name = 'nu svm' |
||
68 | preprocessors = svm_pps |
||
69 | |||
70 | def __init__(self, nu=0.5, kernel='rbf', degree=3, gamma=0.0, coef0=0.0, |
||
0 ignored issues
–
show
|
|||
71 | shrinking=True, probability=False, tol=0.001, cache_size=200, |
||
0 ignored issues
–
show
|
|||
72 | max_iter=-1, preprocessors=None): |
||
0 ignored issues
–
show
|
|||
73 | super().__init__(preprocessors=preprocessors) |
||
74 | self.params = vars() |
||
75 | self.supports_multiclass = True |
||
76 | self.supports_weights = True |
||
77 | |||
78 | |||
79 | class OneClassSVMLearner(SklLearnerBase): |
||
80 | __wraps__ = skl_svm.OneClassSVM |
||
81 | name = 'one class svm' |
||
82 | preprocessors = svm_pps |
||
83 | |||
84 | def __init__(self, kernel='rbf', degree=3, gamma=0.0, coef0=0.0, |
||
0 ignored issues
–
show
|
|||
85 | tol=0.001, nu=0.5, shrinking=True, cache_size=200, |
||
0 ignored issues
–
show
|
|||
86 | max_iter=-1, preprocessors=None): |
||
0 ignored issues
–
show
|
|||
87 | super().__init__(preprocessors=preprocessors) |
||
88 | self.params = vars() |
||
89 | self.supports_weights = True |
||
90 | |||
91 | def fit(self, X, Y=None, W=None): |
||
92 | clf = self.__wraps__(**self.params) |
||
93 | if W is not None: |
||
94 | return self.__returns__(clf.fit(X, W.reshape(-1))) |
||
95 | return self.__returns__(clf.fit(X)) |
||
96 | |||
97 | |||
98 | if __name__ == '__main__': |
||
99 | import Orange |
||
100 | |||
101 | data = Orange.data.Table('iris') |
||
102 | learners = [SVMLearner(), NuSVMLearner(), LinearSVMLearner()] |
||
103 | res = Orange.evaluation.CrossValidation(data, learners) |
||
104 | for l, ca in zip(learners, Orange.evaluation.CA(res)): |
||
105 | print("learner: {}\nCA: {}\n".format(l, ca)) |
||
106 |
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.py
files in your module folders. Make sure that you place one file in each sub-folder.