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()

同样是回归
python实现牛顿法(newton's method)_第1张图片
收敛很快:
python实现牛顿法(newton's method)_第2张图片

你可能感兴趣的:(Python实现优化算法)