-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
230 lines (179 loc) · 9.1 KB
/
main.py
File metadata and controls
230 lines (179 loc) · 9.1 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import pandas as pd
import seaborn as sn
import matplotlib.pyplot as plt
def data_processor(data, k, scaler_type='standard', regress_model='decision_tree', regress_parameter=None, is_stratified=False):
# First, split target and data
if regress_parameter is None:
# The regress_model should be decision_tree_g or decision_tree_e
# so, check the regress_model if it is decision_tree_g or decision_tree_e
# else, raise ValueError
if regress_model != 'decision_tree_g' and regress_model != 'decision_tree_e':
raise ValueError('regress_parameter must be a list of parameters')
X, y = data[:, :-1], data[:, -1]
y = y.astype('int')
# Second, scale the data using data_scaler
X = data_scaler(X, scaler_type=scaler_type)
# Third, split the data using KFold, or StratifiedKFold
X_train, X_test, y_train, y_test = split_k_fold(X, y, n_splits=k, is_stratified=is_stratified)
# Lastly, regress the data using data_regression
trained_model = data_regression(X_train, y_train, method=regress_model, regress_parameter=regress_parameter)
y_pred = trained_model.predict(X_test)
# Use Score mthos to get the accuracy of model
score = trained_model.score(X_test, y_test)
return y_pred, score, [scaler_type, regress_model, regress_parameter, is_stratified, k]
# I: split the data using sklearn KFold, stratifiedKFold
# return: the train and test data
def split_k_fold(data, target, n_splits=5, random_state=0, is_stratified=False):
from sklearn.model_selection import KFold, StratifiedKFold
x_train, x_test, y_train, y_test = None, None, None, None
if is_stratified:
skf = StratifiedKFold(n_splits=n_splits, random_state=random_state, shuffle=True)
for train_index, test_index in skf.split(data, target):
x_train, x_test = data[train_index], data[test_index]
y_train, y_test = target[train_index], target[test_index]
else:
kf = KFold(n_splits=n_splits, random_state=random_state, shuffle=True)
for train_index, test_index in kf.split(data):
x_train, x_test = data[train_index], data[test_index]
y_train, y_test = target[train_index], target[test_index]
assert x_train is not None and x_test is not None \
and y_train is not None and y_test is not None
return x_train, x_test, y_train, y_test
# II: data scaler using sklearn StandardScaler, MinMaxScaler and RobustScaler
# return: the scaled data
def data_scaler(data, scaler_type='standard'):
from sklearn.preprocessing import StandardScaler, MinMaxScaler, RobustScaler
if scaler_type == 'standard':
scaler = StandardScaler()
elif scaler_type == 'minmax':
scaler = MinMaxScaler()
elif scaler_type == 'robust':
scaler = RobustScaler()
else:
raise ValueError('scaler_type must be standard, minmax or robust')
return scaler.fit_transform(data)
# III: data regression methods function using sklearn DecisionTreeRegressor with entropy,
# DecisionTreeRegressor with Gini, LogisticRegression and SVM
# return: trained_model
def data_regression(data, target, method='decision_tree_g', regress_parameter=None):
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVR
if method == 'decision_tree_g':
model = DecisionTreeClassifier(max_depth=5, criterion='gini', random_state=0)
elif method == 'decision_tree_e':
model = DecisionTreeClassifier(max_depth=5, criterion='entropy', random_state=0)
elif method == 'logistic':
solver = regress_parameter[0]
model = LogisticRegression(solver=solver, random_state=0)
elif method == 'svm':
kernel = regress_parameter[0]
C_value = regress_parameter[1]
model = SVR(kernel=kernel, C=C_value)
else:
raise ValueError('method must be decision_tree, logistic or svm')
model.fit(data, target)
return model
# main
if __name__ == '__main__':
# load dataset
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data'
df = pd.read_csv(url)
# since it has some random numerical values, rename the dataset's columns
df.columns = ['Sample code number', 'Clump Thickness', 'Uniformity of Cell Size', 'Uniformity of Cell Shape',
'Marginal Adhesion', 'Single Epithelial Cell Size',
'Bare Nuclei', 'Bland Chromatin', 'Normal Nucleoli', 'Mitoses', 'Class']
# check if there's a feature that has null values
df.isnull().sum()
# check if there are any dirty data which cannot be converted to float, drop it
df = df[df['Bare Nuclei'] != '?']
# since it has no correlation with class variable, drop 'Sample code number' column
df.drop(df.columns[0], axis=1, inplace=True)
# create a correlation matrix and show it
corrMatrix = df.corr()
plt.figure(figsize=(9, 7))
sn.heatmap(corrMatrix, annot=True)
plt.show()
# Corr Matrix says 'Mistoses' is least correlated with class varibale, drop the 'Mistoses' column as well.
df.drop(df.columns[8], axis=1, inplace=True)
df.head(10)
# Let's find the top 5 components that has the highest accuracy,
# using all possible combinations by brute force.
scalers = ['standard', 'minmax', 'robust']
regressors = ['decision_tree_e', 'decision_tree_g', 'svm', 'logistic']
# lots of parameters, so I'll use multiprocessing to speed up the process
logistic_solver = ['lbfgs', 'liblinear', 'sag', 'saga']
svm_kernels = ['linear', 'poly', 'rbf', 'sigmoid']
svm_C = [0.1, 1, 10, 100]
is_stratified = [True, False]
k_lst = [2, 3, 4, 5, 6, 7]
combinations = [] # total combinations: 3 * 4 * 2 * 6 * (5 + 25) = 1'800
for scaler in scalers: # 3
for regressor in regressors: # 4
for strat in is_stratified: # 2
for k in k_lst: # 6
if regressor == 'logistic': # 5
for solver in logistic_solver:
_, accuracy, combination = data_processor(df.values, k, scaler_type=scaler,
regress_model=regressor,
regress_parameter=[solver], is_stratified=strat)
combinations.append((accuracy, combination))
elif regressor == 'svm': # 20
for kernel in svm_kernels:
for c in svm_C:
_, accuracy, combination = data_processor(df.values, k, scaler_type=scaler,
regress_model=regressor,
regress_parameter=[kernel, c],
is_stratified=strat)
combinations.append((accuracy, combination))
else: # 4
_, accuracy, combination = data_processor(df.values, k, scaler_type=scaler,
regress_model=regressor,
is_stratified=strat)
combinations.append((accuracy, combination))
# sort the combinations by accuracy
combinations.sort(key=lambda x: x[0], reverse=True)
combinations[:100]
# plot result
import matplotlib.pyplot as plt
plt.rc('font', size=20)
plt.rc('axes', labelsize=15)
plt.rc('xtick', labelsize=10)
plt.rc('ytick', labelsize=10)
plt.rc('figure', titlesize=30)
# print Top 5 Combinations list
print("[Top 5 Combinations]")
print()
# plot top 5 combinations
fig = plt.figure(figsize=(9, 7))
ax = fig.add_subplot(111)
plt.title("Top 5 Combinations")
for i in range(5):
scaler_type = combinations[i][1][0]
regress_model = combinations[i][1][1]
if (combinations[i][1][3]):
is_stratified = 'Stratified KFold'
else:
is_stratified = 'KFold'
k = combinations[i][1][4]
print("Top", i + 1)
print("Scaling method :", scaler_type)
# print used regression model with its parameters
if regress_model == 'logistic':
regress_parameter = combinations[i][1][2]
print("Regression model : Logistic Regression")
print("Solver :", regress_parameter[0])
elif regress_model == 'svm':
regress_parameter = combinations[i][1][2]
print("Regression model : SVM")
print("Kernel :", regress_parameter[0])
print("C :", regress_parameter[1])
else:
print("Regression model :", regress_model)
print("Using", is_stratified, ", k = ", k)
print()
ax.scatter(k, combinations[i][0], c='b', marker='o')
ax.text(k, combinations[i][0], scaler_type + ' ' + regress_model + ' ' + is_stratified)
ax.set_xlabel('k')
ax.set_ylabel('accuracy')
plt.show()