【问题标题】:Polynomial Regression without scikitlearn没有 scikit 学习的多项式回归
【发布时间】:2020-07-04 01:28:55
【问题描述】:

尝试进行多项式回归。但是,对于除 3 之外的任何 n 值,误差都会显着增加,x 与 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)

【问题讨论】:

  • 嗨萨特维克。你有什么问题?请把它放在你的帖子的顶部。

标签: python numpy machine-learning regression


【解决方案1】:

你需要为每个 epoch 更新 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)

我认为只是一个小错误!

一旦错误太接近,或者在您的情况下,当它们在连续的时期相等时,您也可以打破时期循环。

【讨论】:

    猜你喜欢
    • 2018-10-12
    • 2013-03-09
    • 2021-05-25
    • 2019-07-18
    • 2015-08-05
    • 2016-03-26
    • 2018-03-16
    • 2017-12-11
    • 2018-08-17
    相关资源
    最近更新 更多