-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbreast_cancer_model .py
More file actions
199 lines (133 loc) · 3.91 KB
/
breast_cancer_model .py
File metadata and controls
199 lines (133 loc) · 3.91 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
# -*- coding: utf-8 -*-
"""Breast_cancer_detection.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1ymE_oVeuCkH3y3PcbQTWIg1p5ztuDtvt
"""
import sklearn.datasets
import numpy as np
"""
#Data Loading
"""
breast_cancer = sklearn.datasets.load_breast_cancer()
X = breast_cancer.data
Y = breast_cancer.target
print(X)
print(Y)
print(X.shape,Y.shape)
import pandas as pd
data=pd.DataFrame(X , columns=breast_cancer.feature_names)
data['class']=Y
data.head(5)
print(data['class'].value_counts())
print("Mean of the data : ",data['class'].mean())
print(breast_cancer.target_names)
"""The above shows 1 is malignant and 2 benign i.e with cancer there are more samples than without"""
data.groupby('class').mean()
"""#Train - Test split"""
from sklearn.model_selection import train_test_split
X = data.drop('class',axis=1)
Y=data['class']
"""x and y are now dataframes and not numpy"""
type(X)
X_train, X_test, Y_train,Y_test = train_test_split(X,Y,test_size = 0.1,stratify = Y, random_state = 42)
print(X_train.shape,Y_train.shape)
print(X_test.shape,Y_test.shape)
print(Y_train.mean(),Y_test.mean())
"""#Data Visualization"""
import matplotlib.pyplot as plt
plt.plot(X_train.T,'*')
plt.xticks(rotation = 'vertical')
plt.show()
"""#Binarizing the data"""
X_binarised_train = X_train.apply(pd.cut, bins = 2, labels =[1,0])
plt.plot(X_binarised_train.T,'*')
plt.xticks(rotation = 'vertical')
plt.show()
X_binarised_test = X_test.apply(pd.cut,bins=2,labels = [1,0])
type(X_binarised_test)
X_binarised_test = X_binarised_test.values
X_binarised_train = X_binarised_train.values
type(X_binarised_train)
"""#MP neuron"""
X_binarised_train.shape
for b in range(X_binarised_train.shape[1]+1):
Y_pred_train = []
accurate_rows = 0
for x,y in zip(X_binarised_train,Y_train):
Y_pred = (np.sum(x)>=b)
Y_pred_train.append(Y_pred)
accurate_rows+=(y == Y_pred)
print(b,accurate_rows/X_binarised_train.shape[0])
"""accuracy is max at b=27
#Evaluation
"""
from sklearn.metrics import accuracy_score
Y_test.shape
b=27
Y_pred_test = []
for x in X_binarised_test:
Y_pred = (np.sum(x)>=b)
Y_pred_test.append(Y_pred)
accuracy = accuracy_score(Y_pred_test, Y_test)
print(accuracy)
"""#MP neuron Class"""
class MpNeuron:
def __init__(self):
self.b = None
def model(self, x):
return (sum(x) >= self.b)
def predict(self, X):
Y = []
for x in X:
result = self.model(x)
Y.append(result)
return np.array(Y)
def fit(self, X, Y):
accuracy = {}
for b in range(X.shape[1] + 1):
self.b = b
Y_pred = self.predict(X)
accuracy[b] = accuracy_score(Y, Y_pred)
best_b = max(accuracy, key=accuracy.get)
self.b = best_b
print('Optimal value of b is ', best_b)
print('Highest accuracy is ', accuracy[best_b])
mp_neuron=MpNeuron()
Y_train.shape
mp_neuron.fit(X_binarised_train,Y_train)
mp_neuron.fit(X_binarised_test,Y_test)
"""#Perceptron model
"""
class Perceptron:
def __init__(self):
self.w = None
self.b = None
def model(self, x):
return 1 if (np.dot(self.w, x) >= self.b) else 0
def predict(self, X):
Y = []
for x in X:
result = self.model(x)
Y.append(result)
return np.array(Y)
def fit(self, X, Y,epochs,lr):
self.w = np.ones(X.shape[1])
self.b = 0
for i in range(epochs):
for x, y in zip(X, Y):
y_pred = self.model(x)
if y == 1 and y_pred == 0:
self.w += lr*x
self.b += lr*1
elif y == 0 and y_pred == 1:
self.w -= lr*x
self.b -= lr*1
perceptron = Perceptron()
perceptron.fit(X_train,Y_train,2,1)
Y_predicted = perceptron.predict(X_train)
accuracy = accuracy_score(Y_predicted,Y_train)
print(accuracy)
Y_predicted = perceptron.predict(X_test)
accuracy = accuracy_score(Y_predicted,Y_test)
print(accuracy)