-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
glm_classification.py
175 lines (126 loc) · 4.48 KB
/
glm_classification.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import os
import nnetsauce as ns
import numpy as np
from sklearn.datasets import load_breast_cancer, load_wine, load_iris, make_classification
from sklearn.model_selection import train_test_split
from sklearn import metrics
from time import time
import matplotlib.pyplot as plt
print(f"\n ----- Running: {os.path.basename(__file__)}... ----- \n")
print(f"\n method = 'momentum' ----------")
# dataset no. 1 ----------
breast_cancer = load_breast_cancer()
Z = breast_cancer.data
t = breast_cancer.target
np.random.seed(123)
X_train, X_test, y_train, y_test = train_test_split(Z, t, test_size=0.2)
print(f"\n 1 - breast_cancer dataset ----------")
fit_obj = ns.GLMClassifier(n_hidden_features=5,
n_clusters=2, type_clust="gmm")
start = time()
fit_obj.fit(X_train, y_train, verbose=0)
print(time() - start)
# plt.plot(fit_obj.optimizer.results[2])
print(fit_obj.score(X_test, y_test))
print(fit_obj.score(X_test, y_test, scoring="roc_auc"))
start = time()
preds = fit_obj.predict(X_test)
print(time() - start)
print(metrics.classification_report(preds, y_test))
# dataset no. 2 ----------
wine = load_wine()
Z = wine.data
t = wine.target
np.random.seed(123575)
X_train, X_test, y_train, y_test = train_test_split(Z, t, test_size=0.2)
print(f"\n 2 - wine dataset ----------")
fit_obj = ns.GLMClassifier(n_hidden_features=3,
n_clusters=2, type_clust="gmm")
start = time()
fit_obj.fit(X_train, y_train, verbose=0)
print(time() - start)
# plt.plot(fit_obj.optimizer.results[2])
print(fit_obj.score(X_test, y_test))
start = time()
preds = fit_obj.predict(X_test)
print(time() - start)
print(metrics.classification_report(preds, y_test))
print(f"\n method = 'exp' ----------")
# dataset no. 1 ----------
breast_cancer = load_breast_cancer()
Z = breast_cancer.data
t = breast_cancer.target
X_train, X_test, y_train, y_test = train_test_split(Z, t, test_size=0.2, random_state=123)
print(f"\n 1 - breast_cancer dataset ----------")
opt = ns.Optimizer()
opt.learning_method = "exp"
fit_obj = ns.GLMClassifier(optimizer=opt)
fit_obj.lambda1=1e-5
fit_obj.lambda2=100
fit_obj.optimizer.type_optim = "scd"
fit_obj.optimizer.verbose=0
fit_obj.optimizer.learning_rate=0.01
fit_obj.optimizer.batch_prop=0.5
start = time()
fit_obj.fit(X_train, y_train)
print(time() - start)
# plt.plot(fit_obj.optimizer.results[2])
print(fit_obj.score(X_test, y_test))
print(fit_obj.score(X_test, y_test, scoring="roc_auc"))
start = time()
preds = fit_obj.predict(X_test)
print(time() - start)
print(metrics.classification_report(preds, y_test))
print(f"\n method = 'poly' ----------")
# dataset no. 1 ----------
print(f"\n 1 - breast_cancer dataset ----------")
opt = ns.Optimizer()
opt.learning_method = "poly"
fit_obj = ns.GLMClassifier(optimizer=opt)
fit_obj.lambda1=1
fit_obj.lambda2=1
fit_obj.optimizer.type_optim = "scd"
fit_obj.optimizer.verbose=0
fit_obj.optimizer.learning_rate=0.001
fit_obj.optimizer.batch_prop=0.5
start = time()
fit_obj.fit(X_train, y_train)
print(time() - start)
# plt.plot(fit_obj.optimizer.results[2])
print(fit_obj.score(X_test, y_test))
print(fit_obj.score(X_test, y_test, scoring="roc_auc"))
start = time()
preds = fit_obj.predict(X_test)
print(time() - start)
print(metrics.classification_report(preds, y_test))
# dataset no. 3 ----------
iris = load_iris()
Z = iris.data
t = iris.target
X_train, X_test, y_train, y_test = train_test_split(Z, t, test_size=0.2, stratify=t, random_state=123)
print(f"\n 3 - iris dataset ----------")
fit_obj = ns.GLMClassifier(n_hidden_features=3,
n_clusters=3, type_clust="gmm")
start = time()
fit_obj.fit(X_train, y_train)
print(time() - start)
# plt.plot(fit_obj.optimizer.results[2])
print(fit_obj.score(X_test, y_test))
start = time()
preds = fit_obj.predict(X_test)
print(time() - start)
print(metrics.classification_report(preds, y_test))
# dataset no. 4 ----------
X, y = make_classification(n_samples=2500, n_features=20,
random_state=783451)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, stratify=y,
random_state=35145)
print(f"\n 4 - make_classification dataset ----------")
fit_obj = ns.GLMClassifier(n_hidden_features=5,
dropout=0.1, n_clusters=0)
start = time()
fit_obj.fit(X_train, y_train)
print(time() - start)
print(fit_obj.score(X_test, y_test))
preds = fit_obj.predict(X_test)
print(metrics.classification_report(preds, y_test))