(转)Polynomial interpolation 多项式插值
This example demonstrates how to approximate a function with a polynomial of degree n_degree by using ridge regression. Concretely, from n_samples 1d points, it suffices to build the Vandermonde matrix, which is n_samples x n_degree+1 and has the following form:
这个例子演示了如何用岭回归去近似一个函数用n_degree级的多项式级数。具体而言,从n_samples 1d个点,这满足去建立一个范德蒙德矩阵,有n_samples行,n_degree+1列。有如下的形式:
[[1,x1,x1∗∗2,x1∗∗3,…],[1,x2,x2∗∗2,x2∗∗3,…],…][[1,x1,x1∗∗2,x1∗∗3,…],[1,x2,x2∗∗2,x2∗∗3,…],…] [[1, x_1, x_1 ** 2, x_1 ** 3, …], \\ [1, x_2, x_2 ** 2, x_2 ** 3, …], …] [[1,x1,x1∗∗2,x1∗∗3,…],[1,x2,x2∗∗2,x2∗∗3,…],…]
Intuitively, this matrix can be interpreted as a matrix of pseudo features (the points raised to some power). The matrix is akin to (but different from) the matrix induced by a polynomial kernel.
直觉上来说,这个矩阵可以被理解为伪特征的矩阵。这些点提升到能量。
这个矩阵类似于(但不同于) 多项式核生成的矩阵。
This example shows that you can do non-linear regression with a linear model, using a pipeline to add non-linear features. Kernel methods extend this idea and can induce very high (even infinite) dimensional feature spaces.
这个例子显示,你可以用线性模型做非线性回归,通过管道来添加非线性特征。核方法扩展了这个想法,可以导出非常高维的(甚至是无限维)的特征空间。
实验过程
- 注意:
- 这里是用高阶多项式来进行岭回归
- 本质上不是我们日常所说的插值,因为插值点其实并不在对应的位置上。
- 这里说的特征,在其实在fit之前只有一个多项式作为了特征。后面说到了这里采用的是岭回归。


# %pylab inline
# 如果是jupyter notebook就把上面一行注释去掉~
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import Ridge
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
def f(x):
""" function to approximate by polynomial interpolation"""
return x * np.sin(x)
# generate points used to plot
x_plot = np.linspace(0, 10, 100)
# 随机获取20个插值点
# generate points and keep a subset of them
x = np.linspace(0, 10, 100) # 生成100个数据
rng = np.random.RandomState(0)
rng.shuffle(x) # 随机打乱这个数据
x = np.sort(x[:20]) # 将前20个数据排序
y = f(x) # 放入f之中,获取散点的值
# create matrix versions of these arrays
X = x[:, np.newaxis] # 将该数据提高一个维度
X_plot = x_plot[:, np.newaxis]
colors = ['teal', 'yellowgreen', 'gold'] # 颜色集合
lw = 2 # 线宽度
# 真实数据所构成的曲线
plt.plot(x_plot, f(x_plot), color='cornflowerblue', linewidth=lw,
label="ground truth")
# 散点图,描出插值点
plt.scatter(x, y, color='navy', s=30, marker='o', label="training points")
for count, degree in enumerate([3, 4, 5]):
# 用degree的多项式特征结合上岭回归放入到管道之中构建模型
model = make_pipeline(PolynomialFeatures(degree), Ridge())
# print(model)
model.fit(X, y) # 训练模型
y_plot = model.predict(X_plot) # 做出预测
# 描绘出插值曲线
plt.plot(x_plot, y_plot, color=colors[count], linewidth=lw,
label="degree %d" % degree)
plt.legend(loc='lower left')
plt.show()
将for循环部分改成这样子:
for count, degree in enumerate([3]):
# 用degree的多项式特征结合上岭回归放入到管道之中构建模型
model = make_pipeline(PolynomialFeatures(degree), Ridge())
model.fit(X, y) # 训练模型
y_plot = model.predict(X_plot) # 做出预测
# 描绘出插值曲线
plt.plot(x_plot, y_plot, color=colors[count], linewidth=lw,
label="degree %d-Ridge" % degree)
model = make_pipeline(PolynomialFeatures(degree), LinearRegression())
model.fit(X, y) # 训练模型
y_plot = model.predict(X_plot) # 做出预测
# 描绘出插值曲线
plt.plot(x_plot, y_plot, color=colors[len(colors) - 1 - count], linewidth=lw,
label="degree %d-linear" % degree)
输出的图像为:(只需要将数值从3改成其他degree就可以生成其他图片了)



思考
- 观察:会发现岭回归的结果会线性回归的结果稍显波动小些。
- 回答:岭回归多加了一个L2的范数 约束了多项式特征的w的大小。
(转)Polynomial interpolation 多项式插值的更多相关文章
- 多项式函数插值:全域多项式插值(一)单项式基插值、拉格朗日插值、牛顿插值 [MATLAB]
全域多项式插值指的是在整个插值区域内形成一个多项式函数作为插值函数.关于多项式插值的基本知识,见“计算基本理论”. 在单项式基插值和牛顿插值形成的表达式中,求该表达式在某一点处的值使用的Horner嵌 ...
- 【转载】interpolation(插值)和 extrapolation(外推)的区别
根据已有数据以及模型(函数)预测未知区域的函数值,预测的点在已有数据范围内就是interpolation(插值), 范围外就是extrapolation(外推). The Difference Bet ...
- codeforces 955F Cowmpany Cowmpensation 树上DP+多项式插值
给一个树,每个点的权值为正整数,且不能超过自己的父节点,根节点的最高权值不超过D 问一共有多少种分配工资的方式? 题解: A immediate simple observation is that ...
- 【Codechef】Chef and Bike(二维多项式插值)
something wrong with my new blog! I can't type matrixs so I come back. qwq 题目:https://www.codechef.c ...
- 整数拆分 [dp+多项式插值]
题意 $1 \leq n \leq 10^{18}$ $2 \leq m \leq 10^{18}$ $1 \leq k \leq 20$ 思路 n,m较小 首先考虑朴素的$k=1$问题: $f[i] ...
- 数值计算方法实验之newton多项式插值 (Python 代码)
一.实验目的 在己知f(x),x∈[a,b]的表达式,但函数值不便计算或不知f(x),x∈[a,b]而又需要给出其在[a,b]上的值时,按插值原则f(xi)=yi (i=0,1,……, n)求出简单函 ...
- 数值计算方法实验之Hermite 多项式插值 (Python 代码)
一.实验目的 在已知f(x),x∈[a,b]的表达式,但函数值不便计算,或不知f(x),x∈[a,b]而又需要给出其在[a,b]上的值时,按插值原则f(xi)= yi(i= 0,1…….,n)求出简单 ...
- 数值计算方法实验之Newton 多项式插值(MATLAB代码)
一.实验目的 在己知f(x),x∈[a,b]的表达式,但函数值不便计算或不知f(x),x∈[a,b]而又需要给出其在[a,b]上的值时,按插值原则f(xi)=yi (i=0,1,……, n)求出简单函 ...
- 数值计算方法实验之Lagrange 多项式插值 (Python 代码)
一.实验目的 在已知f(x),x∈[a,b]的表达式,但函数值不便计算,或不知f(x),x∈[a,b]而又需要给出其在[a,b]上的值时,按插值原则f(xi)= yi(i= 0,1…….,n)求出简单 ...
随机推荐
- 前端day01
目录 软件开发架构 web服务的本质 HTTP协议 HTML的注释 HTML的文档结构 标签的分类 标签的分类 列表标签 表格标签 软件开发架构 c/s b/s b/s本质也是c/s ...
- 微信小程序视图层介绍及用法
一. 视图层 WXML(WeiXin Markup Language)是框架设计的一套标签语言,结合基础组件.事件系统,可以构建出页面的结构. 1.1. 数据绑定 1.1.1. 普通写法 <vi ...
- 《.Net 最佳实践》 - 学习笔记
<.Net 最佳实践> ========== ========== ==========[作者] (美) Stephen Ritchie[译者] (中) 黄灯桥 黄浩宇 李永[出版] 机械 ...
- 在项目中在线使用Iconfont图标
Iconfont真的很强大,图标数量惊人,基本任意的关键词都能搜索到你想要的结果.而且是国产的,网速会比较快,还可以改变图标颜色. 它提供svg.png.ai三种格式下载,之前我一直都是乖乖的一个个下 ...
- 微信小程序—支付宝身份验证(支付宝小程序)
查看应用:https://open.alipay.com/platform/keyManage.htm 这里找到您调用接口的应用 支付宝身份验证快速接入:https://docs.open.alip ...
- JavaScript图形实例:四瓣花型图案
设有坐标计算公式如下: X=L*(1+SIN(4α))*COS(α) Y=L*(1+SIN(4α))*SIN(α) 用循环依次取α值为0~2π,计算出X和Y,在canvas画布中对坐标位置(X,Y)描 ...
- 二分查找(Java)
题目: 编写程序,完成以下功能: (1)输入5个整数到数组中; (2)使用冒泡法对5个数按从小到大排序,输出排序后的数组; (3)输入一个整数X,在数组中用二分法查找X,找到输出X在数组中的下标,找不 ...
- Angular 彻底解决 Dropdown 在 Safari 上无法自动关闭的问题
之前在 Safari 上用 focus 事件来实现 Dropdown 下拉菜单,结果在 iOS 上不兼容. 尝试了 MDN 和 stack over flow 上各种奇技淫巧,然而在 iOS 上全都败 ...
- Spring Boot 项目维护全局json数据
1:概述 过去 我们在每一个方法中处理前端发过来的请求,需要自己构造请求数据,然后通过spring 提供的@ResponseBody 强制转为JSON数据吗,实际上出现了很多重复的代码,我么亦可以通过 ...
- Apollo 分布式配置中心(补充)
1. Namespace 1.1. 什么是Namespace Namespace是配置项的集合,类似于一个配置文件的概念. Apollo在创建项目的时候,都会默认创建一个“application ...