forked from DeepSleepUCDenver/sleep_models
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsvm_label_spread.py
More file actions
85 lines (75 loc) · 2.2 KB
/
svm_label_spread.py
File metadata and controls
85 lines (75 loc) · 2.2 KB
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
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score
from sklearn.metrics import multilabel_confusion_matrix
from sklearn.preprocessing import scale, normalize
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
from sklearn import svm
from sklearn.semi_supervised import label_propagation
from sklearn.semi_supervised import LabelSpreading
from mlxtend.feature_selection import SequentialFeatureSelector as sfs
from mlxtend.plotting import plot_sequential_feature_selection as plot_sfs
import matplotlib.pyplot as plt
# Results
# linear
# 0.255573681348559
# poly
# 0.26318651441000546
# rbf
# 0.26373028820010874
# sigmoid
# 0.2561174551386623
# linear
# 0.2792511700468019
# poly
# 0.31461258450338014
# rbf
# 0.3078523140925637
# sigmoid
# 0.29017160686427457
# Read am partition the matrix
data = pd.read_feather('./feature_stage_data_all.ftr')
x = data[data.columns[3:]]
y = data['stage']
x = x.values
x = normalize(x)
y = y.values
nnl = lambda a: np.invert(np.isnan(a))
nul = lambda a: np.isnan(a)
x_obs = x[nnl(y)]
y_obs = y[nnl(y)]
# apply LabelSpreading
x_nuls = x[nul(y)]
label_spread = LabelSpreading(kernel='knn', alpha=0.8)
label_spread.fit(x_obs, y_obs)
x_all = np.concatenate([x_obs, x_nuls], axis=0)
y_all = np.concatenate([y_obs, label_spread.predict(x_nuls)], axis=0)
def test_svm(x, y):
x, y = shuffle(x, y, random_state=42)
smpnum = min([sum(y==i) for i in range(1,6)])
y_btr = y[y == 1][:smpnum]
x_btr = x[y == 1][:smpnum]
for i in range(2,6):
x_btr = np.concatenate([x_btr, x[y == i][:smpnum]])
y_btr = np.concatenate([y_btr, y[y == i][:smpnum]])
x_tr, x_te, y_tr, y_te = train_test_split(x_btr, y_btr, test_size = 0.20)
kerns = [
'linear',
'poly',
'rbf',
'sigmoid'
]
kern_svms = [
svm.SVC(kernel='linear'),
svm.SVC(kernel='poly'),
svm.SVC(kernel='rbf'),
svm.SVC(kernel='sigmoid')
]
for kern, mod in zip(kerns, kern_svms):
print(kern)
mod.fit(x_tr, y_tr)
print(mod.score(x_te, y_te))
test_svm(x_obs, y_obs)
test_svm(x_all, y_all)