-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource code.py
More file actions
367 lines (253 loc) · 11 KB
/
source code.py
File metadata and controls
367 lines (253 loc) · 11 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 12 15:14:31 2024
@author: SATHVIK
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout
from sklearn.model_selection import train_test_split
from imblearn.over_sampling import SMOTE
from sklearn.preprocessing import StandardScaler, OrdinalEncoder, LabelEncoder
from sklearn.metrics import classification_report
from sklearn.model_selection import RandomizedSearchCV
from sklearn.ensemble import RandomForestClassifier
from xgboost import XGBClassifier
from sklearn.svm import SVC
import seaborn as sns
import pickle
import warnings
warnings.filterwarnings("ignore", category=UserWarning)
data = pd.read_csv(r'C:\Users\SATHVIK\OneDrive\Desktop\smartinternz\Thyroid\Data\thyroidDF.csv')
data
data.head()
data.isnull()
data.isnull().sum()
# Drop redundant attributes and modify the original dataframe
data.drop(['TSH_measured', 'T3_measured', 'TT4_measured', 'T4U_measured', 'FTI_measured', 'TBG_measured', 'referral_source', 'patient_id'], axis=1, inplace=True)
# Remapping target values to diagnostic group
diagnoses = {
'A': 'hyperthyroid conditions', 'B': 'hyperthyroid conditions',
'C': 'hyperthyroid conditions', 'D': 'hyperthyroid conditions',
'E': 'hypothyroid conditions', 'F': 'hypothyroid conditions',
'G': 'hypothyroid conditions', 'H': 'hypothyroid conditions',
'I': 'binding protein', 'J': 'binding protein',
'K': 'general health', 'L': 'replacement therapy',
'M': 'replacement therapy', 'N': 'replacement therapy',
'O': 'antithyroid treatment', 'P': 'antithyroid treatment',
'Q': 'antithyroid treatment', 'R': 'miscellaneous',
'S': 'miscellaneous', 'T': 'miscellaneous'
}
data['target'] = data['target'].map(diagnoses)
data.dropna(subset=['target'], inplace=True)
data['target'].value_counts()
data.describe()
data.info()
print(data[data.age > 100])
#changing age of observation with(age>100) to null
data['age']=np.where((data.age>100), np.nan, data.age)
data
# Split the data
x = data.iloc[:, 0:-1]
y = data.iloc[:, -1]
x
y
x['sex'].unique()
x['sex'].replace(np.nan, 'F', inplace=True)
x['sex'].value_counts()
# Converting the data
x['age'] = x['age'].astype('float')
x['TSH'] = x['TSH'].astype('float')
x['T3'] = x['T3'].astype('float')
x['TT4'] = x['TT4'].astype('float')
x['T4U'] = x['T4U'].astype('float')
x['FTI'] = x['FTI'].astype('float')
x['TBG'] = x['TBG'].astype('float')
x.info()
# Ordinal encoding for categorical features
ordinal_encoder = OrdinalEncoder(dtype='int64')
x[x.columns[1:16]] = ordinal_encoder.fit_transform(x.iloc[:, 1:16])
x.fillna(0, inplace=True)
x
# Label encoding for the target variable
label_encoder = LabelEncoder()
y = pd.DataFrame(label_encoder.fit_transform(y), columns=['target'])
y
# checking correlation using Heatmap
import seaborn as sns
corrmat = x.corr()
f, ax = plt.subplots (figsize = (9, 8))
sns.heatmap (corrmat, ax = ax, cmap = "YlGnBu", linewidths = 0.1)
# Split the data into train and test sets
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.20, random_state=0)
# Scale the data
sc = StandardScaler()
x_train = sc.fit_transform(x_train)
x_test = sc.transform(x_test)
# Handling imbalanced data
from imblearn.over_sampling import SMOTE
os = SMOTE(random_state=0, k_neighbors=1)
x_bal, y_bal = os.fit_resample(x_train, y_train)
x_test_bal, y_test_bal = os.fit_resample(x_test, y_test)
print(y_train.value_counts())
x_bal
# Convert arrays to dataframes
columns = ['age', 'sex', 'on_thyroxine', 'query_on_thyroxine', 'on_antithyroid_meds', 'sick', 'pregnant', 'thyroid_surgery', 'I131_treatment', 'query_hypothyroid', 'query_hyperthyroid', 'lithium', 'goitre', 'tumor', 'hypopituitary', 'psych', 'TSH', 'T3', 'TT4', 'T4U', 'FTI', 'TBG']
x_train_bal = pd.DataFrame(x_bal, columns=columns)
y_train_bal = pd.DataFrame(y_bal, columns=['target'])
x_test_bal = pd.DataFrame(x_test_bal, columns=columns)
y_test_bal = pd.DataFrame(y_test_bal, columns=['target'])
# Random Forest Classifier Model
rfr = RandomForestClassifier().fit(x_bal, y_bal.values.ravel())
y_pred = rfr.predict(x_test_bal)
print(classification_report(y_test_bal, y_pred))
print(x_bal.shape, y_bal.shape, x_test_bal.shape, y_test_bal.shape)
from sklearn.metrics import accuracy_score
test_score=accuracy_score(y_test_bal,y_pred)
test_score
train_score = accuracy_score(y_bal,rfr.predict(x_bal))
train_score
# Feature importance
from sklearn.inspection import permutation_importance
results = permutation_importance(rfr, x_bal, y_bal, scoring='accuracy')
feature_importance = ['age', 'sex', 'on_thyroxine', 'query_on_thyroxine', 'on_antithyroid_meds', 'sick', 'pregnant', 'thyroid_surgery', 'I131_treatment', 'query_hypothyroid', 'query_hyperthyroid', 'lithium', 'goitre', 'tumor', 'hypopituitary', 'psych', 'TSH', 'T3', 'TT4', 'T4U', 'FTI', 'TBG']
importance = results.importances_mean
importance = np.sort(importance)
for i, v in enumerate(importance):
i = feature_importance[i]
print('Feature: {:<20} Score: {}'.format(i, v))
plt.figure(figsize=(10, 10))
plt.bar(feature_importance, importance.astype(float))
plt.xticks(rotation=30, ha='right')
plt.show()
x.head()
# Convert x_bal back to a DataFrame if necessary
if isinstance(x_bal, np.ndarray):
x_bal = pd.DataFrame(x_bal, columns=columns) # Assuming 'columns' contains the column names
# Now you can drop the columns
x_bal.drop(['age', 'sex', 'on_thyroxine', 'query_on_thyroxine', 'on_antithyroid_meds', 'sick', 'pregnant', 'thyroid_surgery', 'I131_treatment'], axis=1, inplace=True)
# Drop the specified columns from the dataframe (assuming x_test_bal is still a DataFrame)
x_test_bal.drop(['age', 'sex', 'on_thyroxine', 'query_on_thyroxine', 'on_antithyroid_meds', 'sick', 'pregnant', 'thyroid_surgery', 'I131_treatment'], axis=1, inplace=True)
x_bal.head()
x_bal_filtered = x_bal[['goitre', 'tumor' , 'hypopituitary', 'psych', 'TSH', 'T3', 'TT4', 'T4U', 'FTI', 'TBG']]
x_test_bal_filtered = x_test_bal[['goitre', 'tumor' , 'hypopituitary', 'psych', 'TSH', 'T3', 'TT4', 'T4U', 'FTI', 'TBG']]
x_bal_filtered.head()
from sklearn.ensemble import RandomForestClassifier
RFclassifier = RandomForestClassifier(max_leaf_nodes=30)
RFclassifier.fit(x_train, y_train)
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report, confusion_matrix # Import confusion_matrix
RFclassifier = RandomForestClassifier(max_leaf_nodes=30)
RFclassifier.fit(x_train, y_train)
y_pred = RFclassifier.predict(x_test)
print(classification_report(y_test, y_pred))
print(confusion_matrix(y_test, y_pred))
# HyperParameter Tuning for RF
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV
rf = {
'n_estimators': [100, 200, 300],
'max_depth': [3, 5, 7, 10, None]
}
grid_rf = GridSearchCV(RandomForestClassifier(random_state=42), rf, cv=5)
grid_rf.fit(x_train, y_train)
print("Best parameters for Random Forest:", grid_rf.best_params_)
from sklearn.model_selection import GridSearchCV
rf = {
'n_estimators': [100, 200, 300],
'max_depth': [3, 5, 7, 10, None]
}
RFclassifier = RandomForestClassifier(random_state=42)
grid_rf = GridSearchCV(RFclassifier, rf, cv=5)
grid_rf.fit(x_train, y_train)
print("Best parameters for Random Forest:", grid_rf.best_params_)
y_pred = grid_rf.predict(x_test)
print(classification_report(y_test, y_pred))
print(confusion_matrix(y_test, y_pred))
RFAcc = accuracy_score(y_pred,y_test)
print('Random Forest accuracy is: {:.2f}%'.format(RFAcc*100))
from xgboost import XGBClassifier
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
y_train_encoded = le.fit_transform(y_train)
xgb = XGBClassifier()
xgb.fit(x_train, y_train_encoded)
y_test_encoded = le.transform(y_test)
y_pred = xgb.predict(x_test)
print(classification_report(y_test_encoded, y_pred))
from sklearn.model_selection import GridSearchCV
param_grid = {
'learning_rate': [0.01, 0.1, 0.2, 0.3],
'max_depth': [3, 5, 7, 10],
'n_estimators': [100, 150, 200],
}
grid_search = GridSearchCV(XGBClassifier(), param_grid, cv=5)
grid_search.fit(x_train, y_train_encoded)
best_params = grid_search.best_params_
print("Best parameters:", best_params)
best_model = grid_search.best_estimator_
y_pred = best_model.predict(x_test)
accuracy = accuracy_score(y_test_encoded, y_pred)
print("Accuracy:", accuracy)
XGBAcc = accuracy_score(y_test_encoded, y_pred)
print('XGB accuracy is: {:.2f}%'.format(XGBAcc*100))
from sklearn.svm import SVC
SVCclassifier = SVC(kernel='linear', max_iter=251)
SVCclassifier.fit(x_train, y_train)
# Hyperparametric Tuning for SVC model
svc_params = {
'kernel': ['linear', 'poly', 'rbf', 'sigmoid'],
'C': [1, 10, 100],
'gamma': ['scale', 'auto']
}
grid_svc = GridSearchCV(SVC(), svc_params, cv=5)
grid_svc.fit(x_train, y_train)
print("Best parameters for SVC:", grid_svc.best_params_)
y_pred = SVCclassifier.predict(x_test)
print(classification_report(y_test, y_pred))
print(confusion_matrix(y_test, y_pred))
#Evaluating Performance Of The svc Model Using GridSearch CV
from sklearn.model_selection import GridSearchCV
param_grid = {
'kernel': ['linear', 'poly', 'rbf', 'sigmoid'],
'C': [1, 10, 100],
'gamma': ['scale', 'auto']
}
grid_search = GridSearchCV(SVC(), param_grid, cv=5)
grid_search.fit(x_train, y_train)
best_params = grid_search.best_params_
print("Best parameters:", best_params)
best_model = grid_search.best_estimator_
y_pred = best_model.predict(x_test)
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
SVCAcc = accuracy_score(y_pred,y_test)
print('SVC accuracy is: {:.2f}%'.format(SVCAcc*100))
models = pd.DataFrame({
'Model' : ['Random Forest Classifier','SVC Model','XGB classifier'],
'Score' : [RFAcc,SVCAcc,XGBAcc]
})
models.sort_values(by = 'Score', ascending = False)
models = pd.DataFrame({
'Model': ['Random Forest Classifier', 'SVC Model', 'XGB classifier'],
'Score': [RFAcc, SVCAcc, XGBAcc]
})
models_sorted = models.sort_values(by='Score', ascending=False)
models_sorted
best_model_name = models_sorted.iloc[0, 0]
if best_model_name == 'Random Forest Classifier':
best_model = grid_rf.best_estimator_
elif best_model_name == 'SVC Model':
best_model = grid_svc.best_estimator_
elif best_model_name == 'XGB classifier':
best_model = grid_search.best_estimator_
features = np.array([[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.000000,0.0,0.0,1.00,0.0,40.0]])
print(label_encoder.inverse_transform(xgb.predict(features)))
data['target'].unique()
y['target'].unique()
import pickle
pickle.dump(best_model, open('thyroid_1_model.pkl', 'wb'))
pickle.dump(le, open('label_encoder.pkl', 'wb'))