最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

scipy.interpolate插值方法實(shí)例講解

 更新時(shí)間:2022年12月29日 15:00:29   作者:tony365  
這篇文章主要介紹了scipy.interpolate插值方法介紹,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

scipy.interpolate插值方法

1 一維插值

from scipy.interpolate import interp1d
1維插值算法

from scipy.interpolate import interp1d
x = np.linspace(0, 10, num=11, endpoint=True)
y = np.cos(-x**2/9.0)
f = interp1d(x, y)
f2 = interp1d(x, y, kind='cubic')
xnew = np.linspace(0, 10, num=41, endpoint=True)
import matplotlib.pyplot as plt
plt.plot(x, y, 'o', xnew, f(xnew), '-', xnew, f2(xnew), '--')
plt.legend(['data', 'linear', 'cubic'], loc='best')
plt.show()

數(shù)據(jù)點(diǎn),線性插值結(jié)果,cubic插值結(jié)果:

在這里插入圖片描述

2 multivariate data

from scipy.interpolate import interp2d

from scipy.interpolate import griddata
多為插值方法,可以應(yīng)用在2Dlut,3Dlut的生成上面,比如當(dāng)我們已經(jīng)有了兩組RGB映射數(shù)據(jù), 可以插值得到一個(gè)查找表。

二維插值的例子如下:

import numpy as np
from scipy import interpolate
import matplotlib.pyplot as plt

from scipy.interpolate import griddata, RegularGridInterpolator, Rbf

if __name__ == "__main__":
    x_edges, y_edges = np.mgrid[-1:1:21j, -1:1:21j]
    x = x_edges[:-1, :-1] + np.diff(x_edges[:2, 0])[0] / 2.
    y = y_edges[:-1, :-1] + np.diff(y_edges[0, :2])[0] / 2.

    # x_edges, y_edges 是 20個(gè)格的邊緣的坐標(biāo), 尺寸 21 * 21
    # x, y 是 20個(gè)格的中心的坐標(biāo), 尺寸 20 * 20

    z = (x + y) * np.exp(-6.0 * (x * x + y * y))

    print(x_edges.shape, x.shape, z.shape)
    plt.figure()
    lims = dict(cmap='RdBu_r', vmin=-0.25, vmax=0.25)
    plt.pcolormesh(x_edges, y_edges, z, shading='flat', **lims) # plt.pcolormesh(), plt.colorbar() 畫圖
    plt.colorbar()
    plt.title("Sparsely sampled function.")
    plt.show()

    # 使用grid data
    xnew_edges, ynew_edges = np.mgrid[-1:1:71j, -1:1:71j]
    xnew = xnew_edges[:-1, :-1] + np.diff(xnew_edges[:2, 0])[0] / 2. # xnew其實(shí)是 height new
    ynew = ynew_edges[:-1, :-1] + np.diff(ynew_edges[0, :2])[0] / 2.
    grid_x, grid_y = xnew, ynew

    print(x.shape, y.shape, z.shape)
    points = np.hstack((x.reshape(-1, 1), y.reshape(-1, 1)))
    z1 = z.reshape(-1, 1)

    grid_z0 = griddata(points, z1, (grid_x, grid_y), method='nearest').squeeze()
    grid_z1 = griddata(points, z1, (grid_x, grid_y), method='linear').squeeze()
    grid_z2 = griddata(points, z1, (grid_x, grid_y), method='cubic').squeeze()

    rbf = Rbf(points[:, 0], points[:, 1], z, epsilon=2)
    grid_z3 = rbf(grid_x, grid_y)

    plt.subplot(231)
    plt.imshow(z.T, extent=(-1, 1, -1, 1), origin='lower')
    plt.plot(points[:, 0], points[:, 1], 'k.', ms=1)
    plt.title('Original')
    plt.subplot(232)
    plt.imshow(grid_z0.T, extent=(-1, 1, -1, 1), origin='lower')
    plt.title('Nearest')
    plt.subplot(233)
    plt.imshow(grid_z1.T, extent=(-1, 1, -1, 1), origin='lower', cmap='RdBu_r')
    plt.title('Linear')
    plt.subplot(234)
    plt.imshow(grid_z2.T, extent=(-1, 1, -1, 1), origin='lower')
    plt.title('Cubic')
    plt.subplot(235)
    plt.imshow(grid_z3.T, extent=(-1, 1, -1, 1), origin='lower')
    plt.title('rbf')
    plt.gcf().set_size_inches(8, 6)
    plt.show()


在這里插入圖片描述

示例2:

def func(x, y):
    return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2


grid_x, grid_y = np.mgrid[0:1:100j, 0:1:200j]


rng = np.random.default_rng()
points = rng.random((1000, 2))
values = func(points[:,0], points[:,1])

from scipy.interpolate import griddata
grid_z0 = griddata(points, values, (grid_x, grid_y), method='nearest')
grid_z1 = griddata(points, values, (grid_x, grid_y), method='linear')
grid_z2 = griddata(points, values, (grid_x, grid_y), method='cubic')

import matplotlib.pyplot as plt
plt.subplot(221)
plt.imshow(func(grid_x, grid_y).T, extent=(0,1,0,1), origin='lower')
plt.plot(points[:,0], points[:,1], 'k.', ms=1)
plt.title('Original')
plt.subplot(222)
plt.imshow(grid_z0.T, extent=(0,1,0,1), origin='lower')
plt.title('Nearest')
plt.subplot(223)
plt.imshow(grid_z1.T, extent=(0,1,0,1), origin='lower')
plt.title('Linear')
plt.subplot(224)
plt.imshow(grid_z2.T, extent=(0,1,0,1), origin='lower')
plt.title('Cubic')
plt.gcf().set_size_inches(6, 6)
plt.show()

在這里插入圖片描述

3 Multivariate data interpolation on a regular grid

from scipy.interpolate import RegularGridInterpolator

已知一些grid上的值。
可以應(yīng)用在2Dlut,3Dlut,當(dāng)我們已經(jīng)有了一個(gè)多維查找表,然后整個(gè)圖像作為輸入,得到查找和插值后的輸出。

二維網(wǎng)格插值方法(好像和resize的功能比較一致)

# 使用RegularGridInterpolator
import matplotlib.pyplot as plt
from scipy.interpolate import RegularGridInterpolator

def F(u, v):
    return u * np.cos(u * v) + v * np.sin(u * v)

fit_points = [np.linspace(0, 3, 8), np.linspace(0, 3, 8)]
values = F(*np.meshgrid(*fit_points, indexing='ij'))

ut, vt = np.meshgrid(np.linspace(0, 3, 80), np.linspace(0, 3, 80), indexing='ij')
true_values = F(ut, vt)
test_points = np.array([ut.ravel(), vt.ravel()]).T

interp = RegularGridInterpolator(fit_points, values)
fig, axes = plt.subplots(2, 3, figsize=(10, 6))
axes = axes.ravel()
fig_index = 0
for method in ['linear', 'nearest', 'linear', 'cubic', 'quintic']:
    im = interp(test_points, method=method).reshape(80, 80)
    axes[fig_index].imshow(im)
    axes[fig_index].set_title(method)
    axes[fig_index].axis("off")
    fig_index += 1
axes[fig_index].imshow(true_values)
axes[fig_index].set_title("True values")
fig.tight_layout()
fig.show()
plt.show()

在這里插入圖片描述

4 Rbf 插值方法

interpolate scattered 2-D data

import numpy as np
from scipy.interpolate import Rbf
import matplotlib.pyplot as plt
from matplotlib import cm

# 2-d tests - setup scattered data
rng = np.random.default_rng()
x = rng.random(100) * 4.0 - 2.0
y = rng.random(100) * 4.0 - 2.0
z = x * np.exp(-x ** 2 - y ** 2)


edges = np.linspace(-2.0, 2.0, 101)
centers = edges[:-1] + np.diff(edges[:2])[0] / 2.

XI, YI = np.meshgrid(centers, centers)
# use RBF
rbf = Rbf(x, y, z, epsilon=2)
Z1 = rbf(XI, YI)

points = np.hstack((x.reshape(-1, 1), y.reshape(-1, 1)))
Z2 = griddata(points, z, (XI, YI), method='cubic').squeeze()

# plot the result
plt.figure(figsize=(20,8))
plt.subplot(1, 2, 1)
X_edges, Y_edges = np.meshgrid(edges, edges)
lims = dict(cmap='RdBu_r', vmin=-0.4, vmax=0.4)
plt.pcolormesh(X_edges, Y_edges, Z1, shading='flat', **lims)
plt.scatter(x, y, 100, z, edgecolor='w', lw=0.1, **lims)
plt.title('RBF interpolation - multiquadrics')
plt.xlim(-2, 2)
plt.ylim(-2, 2)
plt.colorbar()

plt.subplot(1, 2, 2)
X_edges, Y_edges = np.meshgrid(edges, edges)
lims = dict(cmap='RdBu_r', vmin=-0.4, vmax=0.4)
plt.pcolormesh(X_edges, Y_edges, Z2, shading='flat', **lims)
plt.scatter(x, y, 100, z, edgecolor='w', lw=0.1, **lims)
plt.title('griddata - cubic')
plt.xlim(-2, 2)
plt.ylim(-2, 2)
plt.colorbar()
plt.show()

得到結(jié)果如下, RBF一定程度上和 griddata可以互用, griddata方法比較通用

在這里插入圖片描述

[1]https://docs.scipy.org/doc/scipy/tutorial/interpolate.html

到此這篇關(guān)于scipy.interpolate插值方法介紹的文章就介紹到這了,更多相關(guān)scipy.interpolate插值內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python利用蒙版摳圖(使用PIL.Image和cv2)輸出透明背景圖

    python利用蒙版摳圖(使用PIL.Image和cv2)輸出透明背景圖

    這篇文章主要介紹了python利用蒙版摳圖(使用PIL.Image和cv2)輸出透明背景圖,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Python實(shí)現(xiàn)微信好友數(shù)據(jù)爬取及分析

    Python實(shí)現(xiàn)微信好友數(shù)據(jù)爬取及分析

    這篇文章會(huì)基于Python對微信好友進(jìn)行數(shù)據(jù)分析,這里選擇的維度主要有:性別、頭像、簽名、位置,主要采用圖表和詞云兩種形式來呈現(xiàn)結(jié)果,其中,對文本類信息會(huì)采用詞頻分析和情感分析兩種方法,感興趣的小伙伴可以了解一下
    2021-12-12
  • 使用Python實(shí)現(xiàn)TCP/IP客戶端和服務(wù)端通信功能

    使用Python實(shí)現(xiàn)TCP/IP客戶端和服務(wù)端通信功能

    TCP/IP(傳輸控制協(xié)議/互聯(lián)網(wǎng)協(xié)議)是互聯(lián)網(wǎng)的基礎(chǔ)協(xié)議,用于在網(wǎng)絡(luò)中的計(jì)算機(jī)之間進(jìn)行可靠的數(shù)據(jù)傳輸,在Python中,可以使用socket模塊來實(shí)現(xiàn)TCP/IP通信,本文給大家介紹了使用Python實(shí)現(xiàn)TCP/IP客戶端和服務(wù)端通信功能,需要的朋友可以參考下
    2024-12-12
  • python操作列表的函數(shù)使用代碼詳解

    python操作列表的函數(shù)使用代碼詳解

    這篇文章主要介紹了python操作列表的函數(shù)使用代碼詳解,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2017-12-12
  • python實(shí)現(xiàn)在函數(shù)中修改變量值的方法

    python實(shí)現(xiàn)在函數(shù)中修改變量值的方法

    今天小編就為大家分享一篇python實(shí)現(xiàn)在函數(shù)中修改變量值的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • Python之列表推導(dǎo)式最全匯總(下篇)

    Python之列表推導(dǎo)式最全匯總(下篇)

    這篇文章主要介紹了Python之列表推導(dǎo)式最全匯總(下篇),本文章內(nèi)容詳細(xì),通過案例可以更好的理解列表推導(dǎo)式的相關(guān)知識,本模塊分為了三部分,本次為下篇,需要的朋友可以參考下
    2023-01-01
  • python中@Property屬性使用方法

    python中@Property屬性使用方法

    這篇文章主要介紹了python中@Property屬性使用方法,在Python中,可以通過@property裝飾器將一個(gè)方法轉(zhuǎn)換為屬性,從而實(shí)現(xiàn)用于計(jì)算的屬性,下面文章圍繞主題展開更多相關(guān)詳情,感興趣的小伙伴可以參考一下
    2022-06-06
  • 打包PyQt5應(yīng)用時(shí)的注意事項(xiàng)

    打包PyQt5應(yīng)用時(shí)的注意事項(xiàng)

    這篇文章主要介紹了打包PyQt5應(yīng)用時(shí)的注意事項(xiàng)的相關(guān)資料,需要的朋友可以參考下
    2020-02-02
  • python使用RNN實(shí)現(xiàn)文本分類

    python使用RNN實(shí)現(xiàn)文本分類

    這篇文章主要為大家詳細(xì)介紹了python使用RNN進(jìn)行文本分類,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • python實(shí)現(xiàn)將字符串中的數(shù)字提取出來然后求和

    python實(shí)現(xiàn)將字符串中的數(shù)字提取出來然后求和

    這篇文章主要介紹了python實(shí)現(xiàn)將字符串中的數(shù)字提取出來然后求和,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04

最新評論

新宾| 赣州市| 贵港市| 仪征市| 和政县| 张北县| 肃南| 望江县| 临汾市| 神木县| 册亨县| 石狮市| 九龙城区| 紫阳县| 安丘市| 绥芬河市| 页游| 义乌市| 芒康县| 扶风县| 宁河县| 辽源市| 安达市| 亳州市| 普宁市| 清远市| 清镇市| 谢通门县| 昆明市| 德令哈市| 淮安市| 兰坪| 中超| 安仁县| 翁源县| 临清市| 余姚市| 当雄县| 涡阳县| 巫溪县| 饶阳县|