Perceptron class showing None type

import sklearn.datasets

import numpy as np

import pandas as pd

from sklearn.model_selection import train_test_split

breast_cancer=sklearn.datasets.load_breast_cancer()

X=breast_cancer.data

Y=breast_cancer.target

data1=pd.DataFrame(breast_cancer.data,columns=breast_cancer.feature_names)

data=data1.copy()

data.describe()

data[‘class’]=breast_cancer.target

data[‘faltu’]=data[‘mean radius’]

col = data.columns

sub=[‘faltu’]

final=list(set(col)-set(sub))

new_data=data[final]

new_data.columns

X=new_data.drop([‘class’],axis=1)

Y=new_data[‘class’]

x_train,x_test,y_train,y_test=train_test_split(X,Y,test_size=0.2,stratify=Y)

#perceptron model

#making class of perceptron

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):

self.w =np.ones(X.shape\[1\])                      #vfghhjhjhj

self.b=0

for x,y in zip(X,Y):

  y\_pred = self.model(x)

  if y == 1 and y\_pred == 0:

    self.w = self.w + x

    self.b = self.b + 1

  elif y==0 and y\_pred==1:

    self.w = self.w - x

    self.b = self.b - 1

please help me out with this …class perceptron showing None type

Hi Shubham,
What’s the error you’re getting?
Can you share a screenshot/link to your notebook(with public access) please?

thanks ishvinder for promp reply,

why it is showing

AttributeError: ‘NoneType’ object has no attribute ‘predict’

when i am running class function?

where “perceptron” is a class and “percep” is some variable

percep = perceptron()

cell number 26 ,

when i did this the code just ran fine.

perceptron = perceptron()

perceptron.fit(x_train , y_train)

instead of

perceptron = perceptron()

perceptron=perceptron.fit(x_train , y_train)

In cell 26, you call ‘percep.fit()’ and assign it to variable perceptron.

But fit function is not returning anything (equivalent to returning None) and hence the return type of perceptron is ‘None’

Besides, instead of using the variable name ‘perceptron’ (which is same as class name), use a different variable name.

thankyou sanjay ,

this is really helfull