多项式回归_搜索参数

多项式回归与交叉验证
# -*- coding: utf-8 -*-

import numpy as np
import matplotlib.pyplot as plt

from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split

X = np.arange(100).reshape(100, 1)
y = X**4 + X**3 + X + 1

x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

rmses = []
degrees = np.arange(1, 10)
min_rmse, min_deg = 1e10, 0

for deg in degrees:

    # Train features
    poly_features = PolynomialFeatures(degree=deg, include_bias=False)
    x_poly_train = poly_features.fit_transform(x_train)

    # Linear regression
    poly_reg = LinearRegression()
    poly_reg.fit(x_poly_train, y_train)

    # Compare with test data
    x_poly_test = poly_features.fit_transform(x_test)
    poly_predict = poly_reg.predict(x_poly_test)
    poly_mse = mean_squared_error(y_test, poly_predict)
    poly_rmse = np.sqrt(poly_mse)
    rmses.append(poly_rmse)

    # Cross-validation of degree
    if min_rmse > poly_rmse:
        min_rmse = poly_rmse
        min_deg = deg

# Plot and present results
print('Best degree {} with RMSE {}'.format(min_deg, min_rmse))

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(degrees, rmses)
ax.set_yscale('log')
ax.set_xlabel('Degree')
ax.set_ylabel('RMSE')
plt.show()

 

### 关于多项式回归的PPT演示文稿 对于寻找有关多项式回归(Polynomial Regression)的PPT演示文稿,通常这类资源会涵盖模型的基本概念、实现方法及其应用场景等内容。虽然具体提及的文档未直接涉及此主题[^1],但可以根据常见的教育资料结构推测这些幻灯片可能包括以下几个方面: #### 多项式回归简介 介绍线性关系无法充分捕捉数据模式时的情况,以及为什么需要更复杂的函数形式来拟合曲线。 #### 数学基础 解释如何通过增加输入特征的幂次扩展简单的一元或多元线性回归,形成更高维度的空间表示。例如,在二阶情况下\[y = \beta_0 +\beta_1x+\beta_2x^2+\epsilon\],其中\(x\)是原始自变量,而\(x^2\)则作为新的二次项加入到模型之中。 ```python import numpy as np from sklearn.preprocessing import PolynomialFeatures X = np.array([[1], [2], [3]]) poly = PolynomialFeatures(degree=2, include_bias=False) X_poly = poly.fit_transform(X) print(X_poly) ``` #### 参数估计与评估指标 讨论最小二乘法用于求解最佳参数组合的方式,并讲解均方误差(MSE),决定系数(R²)等性能度量标准的意义和用途。 #### 实际案例分析 展示实际应用中的例子,比如房价预测或者时间序列建模等领域内的实例研究,说明如何利用Python库如`scikit-learn`快速构建并训练多项式回归模型。 为了获取具体的PPT文件,建议访问学术机构网站、在线课程平台或是专业会议记录页面进行搜索。此外,Google Scholar也是一个很好的起点,能够找到许多高质量的教学材料链接。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值