# %%
# %%
# %%
import numpy as np
# %%
X = 2*np.random.rand(100, 1)
y = 4 + 3 * X + np.random.rand(100, 1)
# %%
import matplotlib.pyplot as plt
plt.plot(X, y, 'b.')
plt.xlabel('x')
plt.xlabel('y')
plt.legend()
plt.axis([0, 2, 0, 15])
plt.show()
# %%
X_b = np.c_[np.ones((100, 1)), X]
# %%
theta = np.linalg.inv(X_b.T.dot(X_b)).dot(X_b.T).dot(y)
# %%
X_new = np.array([[0], [2]])
X_new_b = np.c_[np.ones((2, 1)), X_new]
y_pre = X_new_b.dot(theta)
y_pre
# %%
plt.plot(X_new, y_pre, 'r--')
plt.plot(X, y, 'b.')
plt.show()
# %%
from sklearn.linear_model import LinearRegression
# %%
linear_reg = LinearRegression()
linear_reg.fit(X, y)
print(linear_reg.coef_)
print(linear_reg.intercept_)
# %%
theta = np.random.randn(2, 1)
m = 100
iterations = 100
rate = 0.01
for ite in range(iterations):
gradit = 2/m * X_b.T.dot(X_b.dot(theta) - y)
theta -= gradit * rate
# %%
X_new_b.dot(theta)
# %%
theta_path_bgd = []
def plot_gradient_descent(theta, eta, theta_path = None):
m = len(X_b)
plt.plot(X, y, 'b.')
n_itera = 1000
for i in range(n_itera):
y_pre = X_new_b.dot(theta)
plt.plot(X_new, y_pre, 'b-')
gradit = 2/m * X_b.T.dot(X_b.dot(theta) - y)
theta -= gradit * rate
if theta_path:
theta_path.append(theta)
plt.xlabel("X_1")
plt.axis([0,2, 0,15])
plt.title('eta = {}'.format(eta))
# %%
theta = np.random.randn(2,1)
plt.figure(figsize=(10, 4))
plt.subplot(131)
plot_gradient_descent(theta, eta = 0.02)
plt.subplot(132)
plot_gradient_descent(theta, eta=0.1)
plt.subplot(133)
plot_gradient_descent(theta, eta = 0.4)
plt.show()
# %%
# %%
n = np.random.randn(5, 1)
b = np.random.randn(1, 5)
# %%
theta = np.random.randn(2,1)
plt.figure(figsize=(10, 4))
plt.subplot(131)
plot_gradient_descent(theta, eta = 0.02)
theta = np.random.randn(2,1)
print(theta)
plt.subplot(132)
plot_gradient_descent(theta, eta=0.1)
theta = np.random.randn(2,1)
print(theta)
plt.subplot(133)
plot_gradient_descent(theta, eta = 0.8)
plt.show()
# %%
X = 2*np.random.rand(100, 1)
y = 4 + 3 * X + np.random.rand(100, 1)
X_new_b = np.c_[np.ones((100, 1)), X]
# %%
def rand_degrit(sample_num, rate, train_num, theta):
for i in range(train_num):
# 随机选择多少个样本数
y_pre = X_new_b.dot(theta)
start = np.random.randint(0, len(X_new_b) - sample_num - 1)
train_x = X_new_b[start: start+ sample_num]
train_y = y[start: start + sample_num]
plt.plot(np.delete(train_x, 0, axis= 1), train_y, 'b.')
plt.plot(X, y_pre, 'r-')
# 计算损失 2/m * X_b.T.dot(X_b.dot(theta) - y)
delta = 1/sample_num* train_x.T.dot(train_x.dot(theta) - train_y)
# 更新
theta = theta - delta * rate
plt.xlabel('x_1')
plt.title('samples{}'.format(sample_num))
theta = np.random.randn(2,1)
plt.figure(figsize=(10, 4))
# plt.subplot(131)
rand_degrit(1, 0.1, 10, theta)
# %%
plt.plot(X_new_b, y, 'b.')
# %%
X_new_b
# %%
11-18
8331
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交