forked from DeepSleepUCDenver/sleep_models
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadaboost_test.py
More file actions
69 lines (60 loc) · 2.25 KB
/
adaboost_test.py
File metadata and controls
69 lines (60 loc) · 2.25 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
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.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.ensemble import AdaBoostClassifier
#from sklearn.semi_supervised import label_propagation
from sklearn.semi_supervised import LabelSpreading
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)]
# keep = np.load('sfs_features.npy')
# x_obs = x_obs[:,keep]
# x_nuls= x_nuls[:,keep]
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_adi_trees(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)
mod = AdaBoostClassifier(n_estimators=200, random_state=0)
mod.fit(x_tr, y_tr)
print(mod.score(x_te, y_te))
def test_adi_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)
mod = AdaBoostClassifier(base_estimator=SVC(probability=True, kernel='rbf'), n_estimators=100, random_state=0)
mod.fit(x_tr, y_tr)
print(mod.score(x_te, y_te))
test_adi_trees(x_obs, y_obs)
test_adi_trees(x_all, y_all)
# this sucks trust me
#test_adi_svm(x_obs, y_obs)
#test_adi_svm(x_all, y_all)