ZeroDivisionError: division by zero error in gradient descent.py
ZeroDivisionError: division by zero error in gradient descent.py
def computeCost(X,y,theta,lam):
tobesummed = np.power(((X.dot(theta.T))-y),2)+lam*np.sum(np.power(theta,2))
return np.sum(tobesummed)/(2 * len(X))
def denormalise_price(price):
global mean
global stddev
ret = price * stddev + mean
return ret
def gradientDescent(X,y,theta,iters,alpha, lam):
lam_matrix = lam * np.ones(theta.shape)
lam_matrix[0][0] = 0
for i in range(iters):
theta = theta*(1- lam_matrix / len(X)) - (alpha/len(X)) * np.sum(X * (X @ theta.T - y), axis=0)
return theta
def computeError(predicted, actual):
error = 0
for i in range(len(predicted)):
error += abs(actual[i] - predicted[i]) / actual[i]
error /= len(actual)
return error[0]
def plotGraph(x,y,labelX='X',labelY='Y',title='X vs Y'):
fig, ax = plt.subplots()
ax.plot(x, y, 'r')
ax.set_xlabel(labelX)
ax.set_ylabel(labelY)
ax.set_title(title)
theta = np.zeros([1,X.shape[1]])
alpha = .1 #learning rate
iters = 500 #epoch
error_matrix = []
lam_range = 600 #maximam lamda
for lam in range(lam_range):
g = gradientDescent(X_train,Y_train,theta,iters,alpha, lam)
Cost = computeCost(X_train,Y_train,g,lam)
Y_pred = X_test.dot(g.T)
error = computeError(denormalise_price(Y_pred),denormalise_price(Y_test))
error_matrix.append(error*100)
optimal_lambda = 0
min_error = 9999
for i in range(len(error_matrix)):
if error_matrix[i] < min_error:
optimal_lambda = i
min_error = error_matrix[i]
print("min Error : ", (min_error),'%')
print("Optimal Lambda : ", optimal_lambda)
plotGraph(np.arange(lam_range),error_matrix,'lambda','error','lambda vs error')
我收到错误:
ZeroDivisionError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_18012/2755139905.py in <module>
9
10 Y_pred = X_test.dot(g.T)
---> 11 error = computeError(denormalise_price(Y_pred),denormalise_price(Y_test))
12 error_matrix.append(error*100)
13 optimal_lambda = 0
~\AppData\Local\Temp/ipykernel_18012/1683987217.py in computeError(predicted, actual)
13 for i in range(len(predicted)):
14 error += abs(actual[i] - predicted[i]) / actual[i]
---> 15 error /= len(actual)
16 return error[0]
17
ZeroDivisionError: division by zero
您在 error /= len(actual)
处除以零。如果 actual
没有元素,那么 len(actual)
将为 return 0。您可能需要添加一个检查来处理这种情况:
if len(actual) == 0:
return 0
def computeCost(X,y,theta,lam):
tobesummed = np.power(((X.dot(theta.T))-y),2)+lam*np.sum(np.power(theta,2))
return np.sum(tobesummed)/(2 * len(X))
def denormalise_price(price):
global mean
global stddev
ret = price * stddev + mean
return ret
def gradientDescent(X,y,theta,iters,alpha, lam):
lam_matrix = lam * np.ones(theta.shape)
lam_matrix[0][0] = 0
for i in range(iters):
theta = theta*(1- lam_matrix / len(X)) - (alpha/len(X)) * np.sum(X * (X @ theta.T - y), axis=0)
return theta
def computeError(predicted, actual):
error = 0
for i in range(len(predicted)):
error += abs(actual[i] - predicted[i]) / actual[i]
error /= len(actual)
return error[0]
def plotGraph(x,y,labelX='X',labelY='Y',title='X vs Y'):
fig, ax = plt.subplots()
ax.plot(x, y, 'r')
ax.set_xlabel(labelX)
ax.set_ylabel(labelY)
ax.set_title(title)
theta = np.zeros([1,X.shape[1]])
alpha = .1 #learning rate
iters = 500 #epoch
error_matrix = []
lam_range = 600 #maximam lamda
for lam in range(lam_range):
g = gradientDescent(X_train,Y_train,theta,iters,alpha, lam)
Cost = computeCost(X_train,Y_train,g,lam)
Y_pred = X_test.dot(g.T)
error = computeError(denormalise_price(Y_pred),denormalise_price(Y_test))
error_matrix.append(error*100)
optimal_lambda = 0
min_error = 9999
for i in range(len(error_matrix)):
if error_matrix[i] < min_error:
optimal_lambda = i
min_error = error_matrix[i]
print("min Error : ", (min_error),'%')
print("Optimal Lambda : ", optimal_lambda)
plotGraph(np.arange(lam_range),error_matrix,'lambda','error','lambda vs error')
我收到错误:
ZeroDivisionError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_18012/2755139905.py in <module>
9
10 Y_pred = X_test.dot(g.T)
---> 11 error = computeError(denormalise_price(Y_pred),denormalise_price(Y_test))
12 error_matrix.append(error*100)
13 optimal_lambda = 0
~\AppData\Local\Temp/ipykernel_18012/1683987217.py in computeError(predicted, actual)
13 for i in range(len(predicted)):
14 error += abs(actual[i] - predicted[i]) / actual[i]
---> 15 error /= len(actual)
16 return error[0]
17
ZeroDivisionError: division by zero
您在 error /= len(actual)
处除以零。如果 actual
没有元素,那么 len(actual)
将为 return 0。您可能需要添加一个检查来处理这种情况:
if len(actual) == 0:
return 0