<jupyter_start><jupyter_text>https://en.wikipedia.org/wiki/Artificial_neuron ![alt text](https://upload.wikimedia.org/wikipedia/commons/b/b0/Artificial_neuron.png)  Learning from Guvi - Deep Learning course<jupyter_code>import sklearn.datasets
import numpy as np
breast_cancer = sklearn.datasets.load_breast_cancer()

X = breast_cancer.data
Y = breast_cancer.target
print(X.shape,Y.shape)
import pandas as pd

data = pd.DataFrame(data = breast_cancer.data , columns=breast_cancer.feature_names)
data['class'] = breast_cancer.target
data.head()
print(data['class'].value_counts())
breast_cancer.target_names
data.groupby('class').mean()<jupyter_output><empty_output><jupyter_text>Train test split<jupyter_code>from sklearn.model_selection import train_test_split

X = data.drop('class',axis=1)
Y = data['class']

X_train,X_test,Y_train,Y_test = train_test_split(X,Y, test_size=0.1)
print(Y.mean(),Y_train.mean(),Y_test.mean())
X_train,X_test,Y_train,Y_test = train_test_split(X,Y, test_size=0.1,stratify=Y,random_state=1)
print(Y.mean(),Y_train.mean(),Y_test.mean())<jupyter_output>0.6274165202108963 0.626953125 0.631578947368421<jupyter_text>Binarisation<jupyter_code>import matplotlib.pyplot as plt
plt.style.use(['dark_background'])
fig = plt.figure(figsize=(8,6))
plt.plot(X_train,'*')
plt.show()
X_train.head(3)
X_train.T.head(3)
fig = plt.figure(figsize=(12,8))
plt.plot(X_train.T,'*')
plt.xticks(rotation='vertical')
plt.show()
X_binarized_train = X_train.apply(pd.cut,bins=2,labels=[0,1])
fig = plt.figure(figsize=(12,8))
plt.plot(X_binarized_train.T,'*')
plt.xticks(rotation='vertical')
plt.show()
X_binarized_test = X_test.apply(pd.cut,bins=2,labels=[0,1])
X_binarized_train = X_binarized_train.values
X_binarized_test = X_binarized_test.values<jupyter_output><empty_output><jupyter_text>MP Neuron Model<jupyter_code>from random import randint

b = 3
i = randint(0,X_binarized_train.shape[0])

print("For row",i)

if (np.sum(X_binarized_train[i:])> b):
  print("MP Neuron inference is malignant")
else:
  print("MP Neuron inference is benign")

if(Y_train[i] == 1):
  print("Ground truth is malignant")
else:
  print("Ground truth is benign")

b = 3
Y_pred_train =[]
accurate_rows = 0
for x,y in zip(X_binarized_train,Y_train):
  y_pred = (np.sum(x) >=b)
  Y_pred_train.append(y_pred)
  accurate_rows += (y_pred == y)

print(accurate_rows,accurate_rows /X_binarized_train.shape[0] )
for b in range(X_binarized_train.shape[1] + 1):
  Y_pred_train =[]
  accurate_rows = 0
  for x,y in zip(X_binarized_train,Y_train):
    y_pred = (np.sum(x) >=b)
    Y_pred_train.append(y_pred)
    accurate_rows += (y_pred == y)

  print(b,accurate_rows,accurate_rows /X_binarized_train.shape[0] )<jupyter_output>0 321 0.626953125
1 123 0.240234375
2 95 0.185546875
3 77 0.150390625
4 79 0.154296875
5 93 0.181640625
6 110 0.21484375
7 125 0.244140625
8 141 0.275390625
9 153 0.298828125
10 160 0.3125
11 168 0.328125
12 172 0.3359375
13 176 0.34375
14 180 0.3515625
15 183 0.357421875
16 188 0.3671875
17 189 0.369140625
18 191 0.373046875
19 191 0.373046875
20 191 0.373046875
21 191 0.373046875
22 191 0.373046875
23 191 0.373046875
24 191 0.373046875
25 191 0.373046875
26 191 0.373046875
27 191 0.373046875
28 191 0.373046875
29 191 0.373046875
30 191 0.373046875<jupyter_text>As part of binarization ,we splitted the data into two bins as 0,1.For example : radius mean is lower for 1 and higher for 0. But what we did is assigned 1 for higher range and 0 for lower range during binarization techniques. We can swap the binarization labels but it might not applied to all the features we have. But we can try that<jupyter_code>data.groupby('class').mean()<jupyter_output><empty_output><jupyter_text>Reruning binarization<jupyter_code>X_binarized_train = X_train.apply(pd.cut,bins=2,labels=[1,0])
X_binarized_test = X_test.apply(pd.cut,bins=2,labels=[1,0])
X_binarized_train = X_binarized_train.values
X_binarized_test = X_binarized_test.values
for b in range(X_binarized_train.shape[1] + 1):
  Y_pred_train =[]
  accurate_rows = 0
  for x,y in zip(X_binarized_train,Y_train):
    y_pred = (np.sum(x) >=b)
    Y_pred_train.append(y_pred)
    accurate_rows += (y_pred == y)

  print(b,accurate_rows,accurate_rows /X_binarized_train.shape[0] )<jupyter_output>0 321 0.626953125
1 321 0.626953125
2 321 0.626953125
3 321 0.626953125
4 321 0.626953125
5 321 0.626953125
6 321 0.626953125
7 321 0.626953125
8 321 0.626953125
9 321 0.626953125
10 321 0.626953125
11 321 0.626953125
12 321 0.626953125
13 321 0.626953125
14 323 0.630859375
15 324 0.6328125
16 329 0.642578125
17 332 0.6484375
18 336 0.65625
19 340 0.6640625
20 344 0.671875
21 352 0.6875
22 359 0.701171875
23 371 0.724609375
24 387 0.755859375
25 402 0.78515625
26 419 0.818359375
27 433 0.845703125
28 435 0.849609375
29 417 0.814453125
30 389 0.759765625<jupyter_text>You could noticed here that when b=28 , the accuracy is higher<jupyter_code>from sklearn.metrics import accuracy_score
b = 28
Y_pred_test =[]
for x in X_binarized_test:
  y_pred = (np.sum(x) >=b)
  Y_pred_test.append(y_pred)

accuracy = accuracy_score(Y_pred_test,Y_test)

print(b,accuracy)<jupyter_output>28 0.7894736842105263<jupyter_text>**MP Neuron Class**<jupyter_code>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_pred,Y)
    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()
mp_neuron.fit(X_binarized_train,Y_train)
Y_test_pred = mp_neuron.predict(X_binarized_test)
accuracy_test = accuracy_score(Y_test_pred,Y_test)
print(accuracy_test)<jupyter_output>0.7894736842105263
{'fraction_non_alphanumeric': 0.11846282372598162, 'fraction_numerical': 0.16758563074352548, 'mean_word_length': 5.42274678111588, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 34, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '386466', 'license': 'CC0-1.0', 'n_tokens_mistral': 3175, 'n_tokens_neox': 2479, 'n_words': 522, 'path': 'mpneuron/cancer_prediction_using_mpn.ipynb', 'repo_name': 'kannandreams/AI-CodeBook', 'size': '5985'}