I have declared a function isPrecisied() in the class GradientDescent and calling it inside another function ( gd () ). I have created an instance of class and then calling a function with it (b.gd()) which contains the isPrecisied() function in the following manner
import numpy as np
import matplotlib.pyplot as plt
class GradientDescent:
def __init__(self,x,y,yhat,alpha=0.01,pre=0.1):
self.x=x # This is the x array
self.y=y # y is the array
self.yhat=yhat # yhat is the array
self.precision=pre
self.alpha=alpha
def isPrecisied(self,e):
if (np.sum(e)/2)>self.precision:
return False
return True
def gd(self):
self.beta=[0.0]*len(self.y)
error=[]
while True:
for j in range(0,len(self.y),1):
temp=self.beta[0]+np.sum(np.multiply(self.beta,self.x))
error.append(np.square(self.y[j]-temp))
t=isPrecisied(error)
print(np.sum(error)/2)
if t==False:
self.beta=np.subtract(self.beta,self.alpha*np.array(self.beta))
else:
return self.beta
#take the average precision by 1/2*summition((y-yhat)^2)
#B0(t+1) = B0(t) – alpha * error(of individual not combined)
a = GradientDescent([1,2,3,4,5],[10,20,30,40,50],[11,21,30,38,48])
b=a.gd()
I got the error:
Traceback (most recent call last):
File "<ipython-input-10-744f30ba0c28>", line 2, in <module>
b=a.gd()
File "<ipython-input-9-13f85a04a1e0>", line 23, in gd
t=isPrecisied(error)
NameError: name 'isPrecisied' is not defined
Please let me know where I am making a mistake.
You should change your
t=isPrecisied(error)
call for
t=self.isPrecisied(error)
Since you must always use self for calling class methods
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.