多项式回归的实现
// 多项式回归
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(0)
x0 = np.random.random(50)
y = 2 * x0 ** 3 + 1.2 * x0 ** 2 + 1.5
def degreed(x0):
degreed = 3
onex = np.c_[np.ones(len(x0))]
for i in range(1, degreed+1):
onex = np.c_[onex, x0**i]
return onex
onex = degreed(x0)
y = np.c_[y]
m, n = onex.shape
#前向传播
def model(theta):
h = onex.dot(theta)
return h
#损失函数
def computeCost(h):
cost = 0.5*np.mean((h-y)**2)
return cost
#梯度下降
def gradtheta(h):
d_theta = (1/m)*np.dot(onex.T, h-y)
return d_theta
#训练模型
def trainModel():
alpha = 1
iters = 10000
Jcost = []
theta = np.zeros((n, 1))
for i in range(iters):
h = model(theta)
jcost = computeCost(h)
d_theta = gradtheta(h)
theta -= alpha*d_theta
if i % 100 == 0:
Jcost.append(jcost)
return theta, Jcost
theta, Jcost = trainModel()
print(theta)
plt.plot(x0, y, 'rx')
x0 = np.sort(x0)
# y0 = theta[0] + theta[1] * x0 + theta[2] * x0 ** 2 + theta[3] * x0 ** 3
onex = degreed(x0)
y0 = model(theta)
plt.plot(x0, y0)
plt.show()
学习讨论群请加QQ群:521364338,扫码进群领取人工智能学习资料,转载标明出处,侵权必究!