python實現牛頓法(newton's method)

import numpy as np
import matplotlib.pyplot as plt
X = np.array([np.ones(100),np.random.rand(100)])
y = np.dot([4, 3], X) + np.random.rand(100)
print('x size is',X.size)
plt.scatter(X[1, :], y)
plt.show()
print(X.shape)
print(y.shape)
print('newton_method start')
num_iter = 20


def newton_method(theta, X, y, num_iter):
    loss_history = np.zeros(num_iter)
    theta_history = np.zeros((num_iter, 2))
    m = len(y)
    for i in range(num_iter):
        y_pred = np.dot(theta, X)
        theta = theta - (np.dot(theta,X)-y).dot(X.T).dot(np.linalg.inv(np.dot(X,X.T)))
        loss = 1/(2*m) * np.sum(np.square(y_pred - y))
        theta_history[i, :] = theta
        loss_history[i] = loss
        if i%5 == 1:
            print('theta is',theta)
            print('current loss is',loss)
    return theta, theta_history, loss_history




theta_ini = np.array([np.random.randn(2)])
theta, theta_history, loss_history = newton_method(theta_ini,X,y, num_iter)

residual = np.zeros((len(y)-1))

print(theta)

plt.plot(loss_history)
plt.show()

同樣是迴歸
在這裏插入圖片描述
收斂很快:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章