forked from singhmayank980/Hactoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Breast Cancer Detection System
334 lines (147 loc) · 3.88 KB
/
Breast Cancer Detection System
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
# In[1]:
import pandas as pd
import numpy as np
from sklearn.datasets import load_breast_cancer
from sklearn.preprocessing import StandardScaler
# In[2]:
data=load_breast_cancer()
# In[3]:
data
# In[4]:
data.keys()
# In[5]:
data['DESCR']
# In[6]:
print(data['DESCR'])
#569 tumors each having 30 attributes
# In[7]:
print(data['frame'])
# In[8]:
print(data['target_names'])
# In[9]:
print(data['target'])
# 0=malignant (cancerous)
# 1=benign (non - cancerous)
# In[10]:
print(data['data'])
# In[11]:
print(data['data'][0]) #values of first tumor
# In[12]:
data['data'].shape
# 569 tumors each having 30 features.
# Each tumor has a target , either 0 or 1. Means if any tumor has target=0,it is malignant and if any tumor has target=1,it is benign
# In[13]:
print(data['feature_names'])
print(len(data['feature_names']))
# In[14]:
j = 0
for i in data['feature_names']:
print(i,":",data['data'][568][j])
j+=1
#values and features of 568th tumor
# In[15]:
feature=data['data']
feature
#attributes of each tumor
# In[16]:
label=data['target']
label
# target or label = malignant or benign
# In[17]:
feature.shape
# In[18]:
label.shape
# In[19]:
print(data['DESCR'])
# here if we observe that min and max values are very much varied, some have small range and some have a very wide range
# As for area attribute, range is very high , but if we look for concave points , range is very small
# So we need to rearrange all the values in a particular range, so that every row is having only a particular range
# In[20]:
# Standardising the data
scale = StandardScaler()
feature = scale.fit_transform(feature)
print(feature)
# In[21]:
# Comparing before and after standardisation
# For first tumor
j = 0
for i in data['feature_names']:
print(i,":",data['data'][568][j])
j+=1
print('------------------------AFTER-----------------------------------------')
j = 0
for i in data['feature_names']:
print(i,":",feature[0][j])
j+=1
# In[22]:
# We will pass these features to the neural network and will get '0' or '1' as output
# In[23]:
print(feature[568])
print(data['target_names'][label[568]],label[568])
# In[24]:
df= pd.DataFrame(feature,columns=data['feature_names'])
# In[25]:
df
# In[26]:
df_features= pd.DataFrame(feature , columns = data['feature_names'])
df_label = pd.DataFrame(label , columns = ['label'])
df = pd.concat([df_features, df_label], axis=1)
df
# In[27]:
#500 Training
X_train = feature[:500]
y_train = label[:500]
#35 Validation
X_val = feature[500:535]
y_val = label[500:535]
#34 Testing
X_test = feature[535:]
y_test = label[535:]
# In[28]:
# neural network
from keras.models import Sequential
from keras.layers import Dense
# In[49]:
model=Sequential()
model.add(Dense(15, activation='relu',input_dim=30))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
# In[53]:
model.fit(X_train,y_train,batch_size=1,epochs=5,validation_data=(X_val,y_val))
# In[54]:
model.evaluate(X_test,y_test)
# In[55]:
#Predictions
sample=X_test[0]
# In[56]:
sample
# In[58]:
sample.shape
# In[62]:
import numpy as np
sample=np.reshape(sample, (1,30))
sample.shape
# In[68]:
model.predict(sample)[0][0]
# In[69]:
if model.predict(sample)[0][0]>0.5:
print("Benign")
else:
print("Malignant")
# In[76]:
print("-------------Predicted vs actual value---------------")
for i in range(100):
sample=X_test[i]
sample=np.reshape(sample, (1,30))
if model.predict(sample)[0][0]>0.5:
print("-Benign")
else:
print("-Malignant")
if y_test[i]==0:
print("*Malignant")
else:
print("*Benign")
print("----------------------------")
# In[ ]:
#df=df.sample(frac=1) to shuffle the dataset