@leona1992
2022-08-03T15:35:46.000000Z
字数 852
阅读 188
python openCV
网上找的code
def gradient_descent(X_b, y, initial_theta, eta, n_iters=1e4, epsilon=1e-8):theta = initial_thetai_iter = 0xb = (np.array(X_b)[:, 1])while i_iter < n_iters:plt.ion()gradient = dJ(theta, X_b, y)last_theta = thetatheta = theta - eta * gradient#while (i_iter % 1000) == 1:while (i_iter < 150) & ((i_iter % 5) == 1):print(i_iter)xq = [0, 2]yq = theta[0] * np.array(xq) + theta[1] * np.ones(np.shape(theta))fig = plt.figure()fig = plt.scatter((np.array(X_b)[:, 1]), y)fig = plt.plot(xq, yq, linewidth=4, color='darkgoldenrod')fig = plt.legend(['data', 'simulate'], loc='upper left')fig = plt.title('gradient')fig = plt.xlabel(i_iter)plt.savefig(r"C:\\Users\\xiaoy\\Pictures\\s\\" + format(i_iter+100) +'.png')plt.show()print(theta)plt.close()i_iter = i_iter + 1if (abs(J(theta, X_b, y) - J(last_theta, X_b, y)) < epsilon):breaki_iter = i_iter + 1return thetatheta = gradient_descent(X_b, y, initial_theta, eta)print("荣耀不朽")