[英]Polynomial Regression without scikitlearn
尝试进行多项式回归。 然而,对于 3 以外的任何 n 值,误差显着增加,x vs y_hat 图实际上开始向下。 日志已被用来去除异常值
import random
import numpy as np
import matplotlib.pyplot as plt
import math
x = np.array([math.log10(1), math.log10(9), math.log10(22), math.log10(24), math.log10(25), math.log10(26), math.log10(27), math.log10(28), math.log10(29), math.log10(30), math.log10(31), math.log10(32), math.log10(33), math.log10(34), math.log10(35)])
y = np.array([math.log10(8), math.log10(9), math.log10(51), math.log10(115), math.log10(164), math.log10(209),math.log10(278), math.log10(321), math.log10(382),math.log10(456), math.log10(596), math.log10(798),math.log10(1140), math.log10(1174), math.log10(1543)])
c = random.random()
plt.scatter(x, y)
n = 3
m=[]
x_real = []
alpha = 0.0001
y_hat = []
for i in range(1, n+1):
x_real.append(x**i)
m.append(random.random())
x_real = np.array(x_real)
m = np.array(m)
x_real = np.transpose(x_real)
y_hat = np.matmul(x_real, m)+c
error = 0.5*(np.sum((y-y_hat)**2))
print(error)
sum = np.sum(y_hat-y)
for epochs in range(101):
for items in range(n):
m[items] = m[items] - (alpha*(sum*x[items]))
c = c - (alpha*sum)
y_hat = (np.matmul(x_real, m))+c
error = 0.5*(np.sum((y-y_hat)**2))
print(error)
plt.plot(x, y_hat)
您需要更新每个时期的 sum 值:
prev = 0
for epochs in range(101):
sum = np.sum(y_hat-y)
for items in range(n):
m[items] = m[items] - (alpha*(sum*x[items]))
c = c - (alpha*sum)
y_hat = (np.matmul(x_real, m))+c
error = 0.5*(np.sum((y-y_hat)**2))
if error == prev:
break
print(error)
plt.plot(x, y_hat)
只是一个小错误,我假设!
您也可以在错误太接近时中断纪元循环,或者在您的情况下,当它们在连续纪元中相等时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.