问题描述
我认为错误可能在梯度下降
def gradient_descent(X,y,theta,alpha,iterations,do_plot):
fig,ax1 = plt.subplots()
if do_plot == True:
plot_hypothesis(X,ax1)
m = X.shape[0] # the number of training samples is the number of rows of array X
cost_vector = np.array([],dtype=np.float32) # empty array to store the cost for every iteration
# Gradient Descent loop
for it in range(iterations):
# initialize temporary theta,as a copy of the existing theta array
theta_temp = theta.copy()
sigma = np.zeros((len(theta)))
for i in range(m):
hypothesis = calculate_hypothesis(X,i)
output = y[i]
sigma = sigma + (hypothesis - output)
theta_temp = theta_temp - (alpha / m) * sigma
theta = theta_temp.copy()
print(m)
# append current iteration's cost to cost_vector
iteration_cost = compute_cost(X,theta)
cost_vector = np.append(cost_vector,iteration_cost)
if do_plot == True:
plot_hypothesis(X,ax1)
plot_hypothesis(X,ax1)
# save the predictions as a figure
plot_filename = os.path.join(os.getcwd(),'figures','predictions.png')
plt.savefig(plot_filename)
print('Gradient descent finished.')
# Plot the cost for all iterations
plot_cost(cost_vector)
min_cost = np.min(cost_vector)
argmin_cost = np.argmin(cost_vector)
print('Minimum cost: {:.5f},on iteration #{}'.format(min_cost,argmin_cost + 1))
return theta
最终,我可能弄错了这个假设,该假设应该接受足够数量的值
def calculate_hypothesis(X,i):
"""
:param X : 2D array of our dataset
:param theta : 1D array of the trainable parameters
:param i : scalar,index of current training sample's row
"""
print(X[1])
hypothesis = np.dot(X[1],theta)
return hypothesis
你们中的任何人都能发现错误吗?
只是想像一下,我将向您展示剧情 plot
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)