(转)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)求出简单 ...
随机推荐
- 天天动听API
本次分析的是天天动听API,天天动听有一点比较好,就是搜索返回直接有歌曲播放的地址了,并且有无损的音频 搜索歌曲API:http://so.ard.iyyin.com/s/song_with_out? ...
- 《Java基础知识》Java super关键字
super可以理解为是指向自己超(父)类对象的一个指针,而这个超类指的是离自己最近的一个父类. super也有三种用法: 1.普通的直接引用 与this类似,super相当于是指向当前对象的父类,这样 ...
- tomcat session共享
1.版本 redis3.2 nginx tomcat8.5 2.下载jar包 tomcat-cluster-redis-session-manager 解压后大家可以看看 readMe.tx ...
- NET Framework项目移植到NET Core上遇到的一系列坑(2)
目录 获取请求的参数 获取完整的请求路径 获取域名 编码 文件上传的保存方法 获取物理路径 返回Json属性大小写问题 webconfig的配置移植到appsettings.json 设置区域块MVC ...
- Cesium案例解析(一)——HelloWorld
目录 1. 概述 2. 实例 2.1. HelloWorld.html 2.2. HelloWorld.js 3. 结果 1. 概述 感觉网上已经有不少关于cesium的教程了,但是学习一个框架最快的 ...
- 「STM32 」IIC通讯原理及其实验
I2C两线式串行总线通讯协议,它是由飞利浦开发的,主要用于连接微控制器及其外围设备之间,它是由数据线SDA和信号线SCL构成的,可发送和接收数据即在MUC和I2C设备之间,I2C和I2C之间进行全双工 ...
- Centos7脚本一键优化
我把优化centos7的脚本分享给大家,建议刚安装完服务器的朋友执行如下优化脚本 [root@test2 yum.repos.d]# cat centos7.sh #!/bin/bash #autho ...
- (办公)记事本_Linux常用的文件操作命令
参考尚硅谷的谷粒学院的linux教程:http://www.gulixueyuan.com/course/300/task/7080/show 好吧,其实一个命令他要是讲超过20分钟,我就去看菜鸟教程 ...
- hadoop集群搭建教程
1. 相关软件准备: VMware-workstation-full-15.0.4-12990004.exe CentOS-7-x86_64-DVD-1810.iso jdk-8u231-linux- ...
- IT兄弟连 HTML5教程 CSS3属性特效 弹性盒模型
CSS3引入了新的盒模型——弹性盒模型,该模型决定一个盒子在其他盒子中的分布方式以及如何处理可用的空间.使用该模型,可以很轻松的创建自适应浏览器窗口的流动布局或自适应字体大小的弹性布局.弹性盒模型看起 ...