python 多项式回归以及可视化

简介

多项式回归:回归函数是回归变量多项式的回归1
x k x_k xk为自变量, y y y为因变量: y = f ( x 0 , . . . , x k ) y = f(x_0,...,x_k) y=f(x0,...,xk) 本文主要介绍两种一元二元

一、一元N次多项式回归

已知数据 x x x y y y,求 b b b 和系数 k k k
公式: y = b + k 1 ∗ x 1 + k 2 ∗ x 2 + . . . + k n ∗ x n y=b + k_1 * x^1 + k_2 * x^2 + ... + k_n * x^n y=b+k1x1+k2x2+...+knxn
令方程为: y = 1 + x − 2 ∗ x 2 + 3 ∗ x n y=1 + x -2 * x^2 + 3 * x^n y=1+x2x2+3xn (使用np.random.rand造一些数据,测试多项式回归)

1.1 可视化

在这里插入图片描述

1.2 代码

代码实现:

import numpy as np
import matplotlib.pyplot as plt
from numpy.linalg import lstsq
plt.rcParams["font.sans-serif"] = ["SimHei"]
plt.rcParams["axes.unicode_minus"] = False

# 1. 造50对数据
np.random.seed(0)
x_t = np.random.rand(50).reshape(50, -1)
y_t = 1 + x_t + -2 * x_t ** 2 + 3 * x_t ** 3 # + np.random.rand(50).reshape(50, -1) / 3

# 2. 尝试求解 以及 可视化
fig = plt.figure(figsize=(18, 6))
for n in [1, 2, 3]:                  # 最高到3次
    # 求解
    x_tmp = x_t.copy()
    for i in range(2, n+1):
        x_tmp = np.concatenate((x_tmp, x_t ** i), axis=1)
    m = np.ones(x_t.shape)
    m = np.concatenate((m, x_tmp), axis=1)
    k = lstsq(m, y_t, rcond=None)[0].reshape(-1)
    print(k)
    # 可视化
    ax = fig.add_subplot(1, 3, n)
    ax.scatter(x_t.reshape(-1), y_t.reshape(-1), c='red', s=20, label='标签')
    x = np.linspace(0, 1, 100)
    y = k[0] + k[1] * x
    for i in range(2, n+1):
        y += k[i] * (x ** i)
    ax.plot(x, y, label='函数')
    ax.set_title('一元' + str(n) + '次')
    ax.legend()

plt.legend()
#plt.savefig('cs.png', dpi=300)
plt.show()

二、二元二次多项式回归

已知数据 x 1 x_1 x1 x 2 x_2 x2 y y y,求 b b b 和系数 k k k
公式: y = b + k 1 ∗ x 1 + k 2 ∗ x 1 2 + k 3 ∗ x 2 + k 4 ∗ x 2 2 + k 5 ∗ x 1 ∗ x 2 y=b + k_1 * x_1 + k_2 * x_1^2 + k_3 * x_2 + k_4 * x_2^2 + k_5 * x_1 * x_2 y=b+k1x1+k2x12+k3x2+k4x22+k5x1x2
令方程为: y = 1 + x 1 − 2 ∗ x 1 2 + x 2 − x 2 2 + x 1 ∗ x 2 y=1 + x_1 -2 * x_1^2 + x_2 -x_2^2 + x_1*x_2 y=1+x12x12+x2x22+x1x2 (使用np.random.rand造一些数据,测试多项式回归)

2.1 可视化

在这里插入图片描述

2.2 代码

代码实现:2

import numpy as np
import matplotlib.pyplot as plt
from numpy.linalg import lstsq
plt.rcParams["font.sans-serif"] = ["SimHei"]
plt.rcParams["axes.unicode_minus"] = False

# 1. 造50对数据
np.random.seed(0)
x_1 = np.random.rand(50).reshape(50, -1)
x_2 = np.random.rand(50).reshape(50, -1)
y_t = 1 + x_1 + -2 * x_1 ** 2 + x_2 + -1 * x_2 ** 2 + x_1*x_2  # + np.random.rand(50).reshape(50, -1) / 3

# 2. 尝试求解 以及 可视化
fig = plt.figure(figsize=(10, 5))
m = np.ones(x_1.shape)
m = np.hstack((m, x_1, x_1 ** 2, x_2, x_2 ** 2, x_1*x_2))
k = lstsq(m, y_t, rcond=None)[0].reshape(-1)
print(k)
# 可视化
ax = fig.add_subplot(1, 2, 2, projection='3d')
ax.scatter(x_1.reshape(-1), x_2.reshape(-1), y_t.reshape(-1), c='red', s=20, label='标签')
x1 = np.linspace(0, 1, 100)
x2 = np.linspace(0, 1, 100)
x, y =np.meshgrid(x1, x2)
z = k[0] + k[1] * x + k[2] * x ** 2 + k[3] * y + k[4] * y**2 + k[5] * x * y
ax.plot_surface(x, y, z,rstride=4,cstride=4,alpha=0.6)
ax.legend()
ax.set_title('二元二次多项式归回')
ax.view_init(12,-78)

# 3. 增加一个对比 二元线性回归
ax = fig.add_subplot(1, 2, 1, projection='3d')
ax.scatter(x_1.reshape(-1), x_2.reshape(-1), y_t.reshape(-1), c='red', s=20, label='标签')
m = np.ones(x_1.shape)
m = np.hstack((m, x_1, x_2))
k = lstsq(m, y_t, rcond=None)[0].reshape(-1)
print(k)
x1 = np.linspace(0, 1, 100)
x2 = np.linspace(0, 1, 100)
x, y =np.meshgrid(x1, x2)
z = k[0] + k[1] * x + k[2] * y
ax.plot_surface(x, y, z,rstride=4,cstride=4,alpha=0.6)
ax.legend()
ax.set_title('二元线性归回')
ax.view_init(12,-78)

plt.show()

  1. 多项式回归-百度百科 ↩︎

  2. python绘制三维图添加文本标注—已解决 ↩︎

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大米粥哥哥

感谢认可!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值