scipy.interpolate 插值子模块件

scipy.interpolate 插值子模块件

首页冒险解谜Sans的怪物逼近更新时间:2024-04-26

菜鸟教程(scipy): https://www.runoob.com/scipy/scipy-module.html scipy.interpolate: https://docs.scipy.org/doc/scipy/tutorial/interpolate.html

## Scipy(02):scipy.interpolate 插值子模块

### 1 一维插值

#### 1.1 interp1d()

# 1.线性插值

# 2.临近点插值

# 3.前点插值

# 4.后点插值

# 5.零阶样条插值

# 6.一阶样条插值

# 7.三阶样条插值

# 8.五阶样条插值

### 2 二维插值

#### 2.1 interp2d()

# 1.线性插值

# 2.三阶样条插值

# 3.五阶样条插值

### 3 离散数据插值到网格

#### 3.1 griddata()

# 1.nearest 临近点插值

# 2.linear 线性插值

# 3.cubic 三阶样条插值

### 1 一维插值

已知离散点数据集,构造一个解析函数,使得函数曲线经过这些点,并能够求出曲线上其他点的值,这一过程称为一维插值。

#### 1.1 interp1d()

scipy.interpolate.interp1d(x,y,kind)

参数x和参数y表示离散点的x坐标和y坐标,参数kind用于指定插值方法。

kind:插值方法:

1.线性插值

2.临近点插值

3.前点插值

4.后点插值

5.零阶样条插值

6.一阶样条插值

7.三阶样条插值

8.五阶样条插值

import numpy as np from scipy import interpolate import matplotlib.pyplot as plt # 原始样本点 x = np.linspace(0,10,11) y = np.exp(-np.sin(x)/3.0) fig = plt.figure(figsize=(6,4), dpi=100, facecolor='#eaeaea' ) plt.rcdefaults() plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False ax = fig.add_subplot() ax1.plot(x,y,'o',label='原始数据') ax1.legend()

# 插值获取更多的点,注意因为是插值,所以x的范围不能超过原来的x范围 x_new = np.linspace(0,10,100)

# 根据原始样本点,按照不同插值方法,获取函数,计算插值后的数值 fig = plt.figure( figsize=(12,10), dpi=100, facecolor="#eaeaea") # 1.线性插值 f_linear = interpolate.interp1d(x,y,kind='linear') y_new = f_linear(x_new) ax1 = fig.add_subplot(4,2,1) ax1.plot(x_new,f_linear(x_new),'.',label='线性插值') ax1.plot(x,y,'.',c='r',label='原始数') ax1.legend() # 2.临近点插值 f_nearest = interpolate.interp1d(x,y,kind='nearest') ax2 = fig.add_subplot(4,2,2) ax2.plot(x_new,f_nearest(x_new),'.',label='临近点插值') ax2.plot(x,y,'.',c='r',label='原始数据') ax2.legend() # 3.前点插值 f_previous = interpolate.interp1d(x,y,kind='previous') ax3 = fig.add_subplot(4,2,3) ax3.plot(x_new,f_previous(x_new),'.',label='前点插值') ax3.plot(x,y,'.',c='r',label='原始数据') ax3.legend() # 4.后点插值 f_next = interpolate.interp1d(x,y,kind='next') ax4 = fig.add_subplot(4,2,4) ax4.plot(x_new,f_next(x_new),'.',label='后点插值') ax4.plot(x,y,'.',c='r',label='原始数据') ax4.legend() # 5.零阶样条插值 f_zero = interpolate.interp1d(x,y,kind='zero') ax5 = fig.add_subplot(4,2,5) ax5.plot(x_new,f_zero(x_new),'.',label='零阶样条插值') ax5.plot(x,y,'.',c='r',label='原始数据') ax5.legend() # 6.一阶样条插值 f_slinear = interpolate.interp1d(x,y,kind='slinear') ax6 = fig.add_subplot(4,2,6) ax6.plot(x_new,f_slinear(x_new),'.',label='一阶样条插值') ax6.plot(x,y,'.',c='r',label='原始数据') ax6.legend() # 7.三阶样条插值 f_cubic = interpolate.interp1d(x,y,kind='cubic') ax7 = fig.add_subplot(4,2,7) ax7.plot(x_new,f_cubic(x_new),'.',label='三阶样条插值') ax7.plot(x,y,'.',c='r',label='原始数据') ax7.legend() # 8.五阶样条插值 f_quadratic = interpolate.interp1d(x,y,kind='quadratic') ax8 = fig.add_subplot(4,2,8) ax8.plot(x_new,f_quadratic(x_new),'.',label='五阶样条插值') ax8.plot(x,y,'.',c='r',label='原始数据') ax8.legend() #wspace 子图横向间距, hspace 代表子图间的纵向距离,left 代表位于图像不同位置 plt.subplots_adjust(left=None, bottom=0.15, right=None, top=None, wspace=0.2, hspace=0.6)

### 2 二维插值

#### 2.1 interp2d()

scipy.interpolate.interp2d(x,y,z,kind)

参数x,y,z是用来逼近函数z=f(x,y)的数组,x和y是一维数组,z是二维数组,kind用于指定插值方法

kind插值方法:

1.线性插值 linear

2.三阶样条插值 cubic

3.五阶样条插值 quadratic

为了测量一个长6m,宽4m的房间的地暖温度,再房间地面上均匀设置了20行30列的温度传感器矩阵,输出一个20行30列的二维数组,数组的每一个元素对应一个传感器的实测温度

import numpy as np import matplotlib.pyplot as plt # 生成20行,30列矩阵 y,x = np.mgrid[-2:2:20j,-3:3:30j] # 模拟温度数据 z = x*np.exp( -x**2-y**2)*10 20 plt.pcolor(x,y,z, cmap=plt.cm.hsv) plt.colorbar() plt.axis('equal') plt.show()

# 插值目标,80行120列 y_new,x_new = np.mgrid[-2:2:80j,-3:3:120j] # 插值时要求输入的是一维,所以使用的时候,其实很方便 print(x[0,:],'\n',y[:,0]) [-3. -2.79310345 -2.5862069 -2.37931034 -2.17241379 -1.96551724 -1.75862069 -1.55172414 -1.34482759 -1.13793103 -0.93103448 -0.72413793 -0.51724138 -0.31034483 -0.10344828 0.10344828 0.31034483 0.51724138 0.72413793 0.93103448 1.13793103 1.34482759 1.55172414 1.75862069 1.96551724 2.17241379 2.37931034 2.5862069 2.79310345 3. ] [-2. -1.78947368 -1.57894737 -1.36842105 -1.15789474 -0.94736842 -0.73684211 -0.52631579 -0.31578947 -0.10526316 0.10526316 0.31578947 0.52631579 0.73684211 0.94736842 1.15789474 1.36842105 1.57894737 1.78947368 2. ]

from scipy import interpolate # 绘图设置 fig = plt.figure( figsize=(12,10),dpi=100) plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False # 原始数据 ax1 = fig.add_subplot(2,2,1) ax1.set_title("原始数据") mappable = ax1.pcolormesh( x,y,z, cmap=plt.cm.jet ) plt.colorbar(mappable,ax=ax1) # 线性插值 f1 = interpolate.interp2d( x[0,:], y[:,0], z, kind='linear') # 生成插值数据 z1 = f1(x_new[0,:], y_new[:,0]) # 绘图 ax2 = fig.add_subplot(2,2,2) ax2.set_title('线性插值') mappable = ax2.pcolor(x_new,y_new,z1, cmap=plt.cm.jet) fig.colorbar(mappable,ax=ax2) # 三阶样条插值 f2 = interpolate.interp2d( x[0,:], y[:,0], z, kind='cubic') z2 = f2(x_new[0,:], y_new[:,0]) ax3 = fig.add_subplot(2,2,3) ax3.set_title("三阶样条插值") mappable = ax3.pcolor( x_new,y_new,z2, cmap=plt.cm.jet ) plt.colorbar(mappable,ax=ax3) # 五阶样条插值 f3 = interpolate.interp2d( x[0,:], y[:,0], z, kind='quintic') z3 = f3(x_new[0,:], y_new[:,0]) ax4 = fig.add_subplot(2,2,4) ax4.set_title('五阶样条插值') mappable = ax4.pcolor(x_new,y_new,z3, cmap=plt.cm.jet) fig.colorbar(mappable,ax=ax4) #wspace 子图横向间距, hspace 代表子图间的纵向距离,left 代表位于图像不同位置 plt.subplots_adjust(left=None, bottom=0.15, right=None, top=None, wspace=0.2, hspace=0.4)

### 3 离散数据插值到网格

#### 3.1 griddata

griddata(points,values,xi,method =‘linear’,fill_value = nan,rescale = False )

参数:points:离散数据点的位置信息元素

values:离散数据点的值

xi:网格数据

method:nearest 临近点插值

:linear 线性插值

:cubic 三阶样条插值

import numpy as np import matplotlib.pyplot as plt from scipy.interpolate import griddata lons = np.random.random(300)*180 # 经度从0-180°,随机生成300个点 lats = np.random.random(300)*90 # 纬度从0-90°,随机生成300个点 # 生成300个温度点 temperature = ((lons-90)/45)*np.exp(-((lons-90)/45)**2-((lats-45)/45)**2) # 将矩形区域裁剪成三角形区域,网格数据变成了离散数据 triangle = np.where( ( (lons<90)&(lats<lons) )|((lons>=90)&(lats<180-lons)) ) lons = lons[triangle] lats = lats[triangle] temperature = temperature[triangle] # 绘图 fig = plt.figure( figsize=(8,6),dpi=120 ) plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False plt.title('原始数据') plt.scatter( lons, lats, s=3, c=temperature, cmap=plt.cm.hsv ) plt.colorbar() plt.axis('equal') plt.xlabel('lons') plt.ylabel('lats')

# 生成目标网格 lat_grid,lon_grid = np.mgrid[0:90:91j,0:180:181j] lon_grid array([[ 0., 1., 2., ..., 178., 179., 180.], [ 0., 1., 2., ..., 178., 179., 180.], [ 0., 1., 2., ..., 178., 179., 180.], ..., [ 0., 1., 2., ..., 178., 179., 180.], [ 0., 1., 2., ..., 178., 179., 180.], [ 0., 1., 2., ..., 178., 179., 180.]])

# 先画一下原始图片 fig = plt.figure( figsize=(10,8),dpi=100) ax1 = fig.add_subplot(2,2,1) mappable = ax1.scatter( lons, lats, s=3, c=temperature, cmap=plt.cm.hsv ) ax1.set_title('原始数据') ax1.set_xlabel('lons') ax1.set_ylabel('lats') ax1.axis('equal') plt.colorbar(mappable, ax=ax1) # 临近点插值 temp_nearest = griddata((lons,lats), temperature, (lon_grid,lat_grid), method='nearest') ax2 = fig.add_subplot(2,2,2) mappable = ax2.scatter( lon_grid, lat_grid, s=3, c=temp_nearest, cmap=plt.cm.hsv ) ax2.set_title('临近点插值') ax2.set_xlabel('lons') ax2.set_ylabel('lats') ax2.axis('equal') plt.colorbar(mappable, ax=ax2) # 线性插值 temp_linear = griddata((lons,lats), temperature, (lon_grid,lat_grid), method='linear') plt.subplot(2,2,3) plt.scatter( lon_grid, lat_grid, s=3, c=temp_linear, cmap=plt.cm.hsv ) plt.title('线性插值') plt.colorbar() plt.axis('equal') # 三阶样条插值 temp_cubic = griddata((lons,lats), temperature, (lon_grid,lat_grid), method='cubic',fill_value=0) plt.subplot(2,2,4) plt.scatter( lon_grid, lat_grid, s=3, c=temp_cubic, cmap=plt.cm.hsv ) plt.title('三阶样条插值') plt.colorbar() plt.axis('equal') plt.subplots_adjust(left=None, bottom=0.15, right=None, top=None, wspace=0.2, hspace=0.4) plt.show()

查看全文
大家还看了
也许喜欢
更多游戏

Copyright © 2024 妖气游戏网 www.17u1u.com All Rights Reserved