-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrandombag_classification.py
More file actions
126 lines (97 loc) · 3.84 KB
/
randombag_classification.py
File metadata and controls
126 lines (97 loc) · 3.84 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
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
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.tree import DecisionTreeClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn import metrics
from time import time
print(f"\n ----- Running: {os.path.basename(__file__)}... ----- \n")
# 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("\n")
print(f"1 - breast cancer dataset -----")
print("\n")
# decision tree
clf = DecisionTreeClassifier(max_depth=2, random_state=123)
fit_obj = ns.RandomBagClassifier(clf, n_hidden_features=2,
direct_link=True,
n_estimators=100,
col_sample=0.9, row_sample=0.9,
dropout=0.3, n_clusters=0, verbose=1)
start = time()
fit_obj.fit(X_train, y_train)
print(f"Elapsed {time() - start}")
print(fit_obj.score(X_test, y_test))
start = time()
preds = fit_obj.predict(X_test)
print(f"Elapsed {time() - start}")
print(metrics.classification_report(preds, y_test))
# dataset no. 2 ----------
wine = load_wine()
Z = wine.data
t = wine.target
np.random.seed(123)
Z_train, Z_test, y_train, y_test = train_test_split(Z, t, test_size=0.2)
print("\n")
print(f"2 - wine dataset -----")
print("\n")
clf = DecisionTreeClassifier(max_depth=2, random_state=123)
fit_obj = ns.RandomBagClassifier(clf, n_hidden_features=5,
direct_link=True,
n_estimators=100,
col_sample=0.5, row_sample=0.5,
dropout=0.1, n_clusters=3,
type_clust="gmm", verbose=1)
start = time()
fit_obj.fit(Z_train, y_train)
print(f"Elapsed {time() - start}")
print(fit_obj.score(Z_test, y_test))
preds = fit_obj.predict(Z_test)
print(metrics.classification_report(preds, y_test))
# dataset no. 3 ----------
iris = load_iris()
Z = iris.data
t = iris.target
np.random.seed(123)
Z_train, Z_test, y_train, y_test = train_test_split(Z, t, test_size=0.2)
print("\n")
print(f"3 - iris dataset -----")
print("\n")
clf = LogisticRegression(solver='liblinear', multi_class = 'ovr',
random_state=123)
fit_obj = ns.RandomBagClassifier(clf, n_hidden_features=5,
direct_link=False,
n_estimators=100,
col_sample=0.5, row_sample=0.5,
dropout=0.1, n_clusters=0, verbose=1)
start = time()
fit_obj.fit(Z_train, y_train)
print(f"Elapsed {time() - start}")
print(fit_obj.score(Z_test, 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,
random_state=351452)
print("\n")
print(f"4 - make_classification dataset -----")
print("\n")
clf = DecisionTreeClassifier(max_depth=1, random_state=123)
fit_obj = ns.RandomBagClassifier(clf, n_hidden_features=5,
direct_link=True,
n_estimators=100,
col_sample=0.5, row_sample=0.5,
dropout=0.1, n_clusters=3,
type_clust="gmm", verbose=1)
start = time()
fit_obj.fit(X_train, y_train)
print(f"Elapsed {time() - start}")
print(fit_obj.score(X_test, y_test))
preds = fit_obj.predict(X_test)
print(metrics.classification_report(preds, y_test))