-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSvmReal.py
72 lines (61 loc) · 1.92 KB
/
SvmReal.py
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
from collections import OrderedDict
import copy
import numpy as np
import pandas as pd
from pandas import DataFrame, Series
import sklearn as sk
import sklearn.model_selection as model_selection
from sklearn.model_selection import ShuffleSplit
import sklearn.feature_selection as feature_selection
import sklearn.pipeline as pipeline
import sklearn.svm as svm
import warnings
warnings.filterwarnings("ignore")
import MaclearnUtilities
import RestrictedData
xs = RestrictedData.xs
xnorms = RestrictedData.xnorms
annots = RestrictedData.annots
ys = RestrictedData.ys
ynums = RestrictedData.ynums
cvSchedules = {k : ShuffleSplit(n_splits = 5,
test_size = 0.2,
random_state = 123)
for k in xnorms}
def pandaize(f):
def pandaized(estimator, X, y, **kwargs):
return f(estimator, np.array(X), y, **kwargs)
return pandaized
@pandaize
def cross_val_score_pd(estimator, X, y, **kwargs):
return model_selection.cross_val_score(estimator, X, y, **kwargs)
def fitModelWithNFeat(fitter, n, setname, cv=None):
if cv is None:
cv = cvSchedules[setname]
if n > xnorms[setname].shape[1]:
return None
fsFitter = pipeline.Pipeline([
('featsel', feature_selection.SelectKBest(
feature_selection.f_regression, k=n)),
('classifier', fitter)
])
return np.mean(cross_val_score_pd(estimator = fsFitter,
X = xnorms[setname],
y = ynums[setname],
cv = cv.split(xnorms[setname])))
svmLinAccs = {
s : fitModelWithNFeat(
fitter = svm.SVC(kernel="linear", C=1),
n = 10,
setname = s
)
for s in xnorms
}
svmRadAccs = {
s : fitModelWithNFeat(
fitter = svm.SVC(kernel="rbf", C=1), # use default gamma
n = 10,
setname = s
)
for s in xnorms
}