日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python提取一行_如何从numpy数组中提取任意一行值?

發布時間:2024/3/12 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python提取一行_如何从numpy数组中提取任意一行值? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

@Sven的答案很簡單,但對于大型數組來說效率相當低。如果處理的是一個相對較小的數組,則不會注意到差異,如果要從較大的數組(例如50 MB)獲取配置文件,則可能需要嘗試其他幾種方法。不過,您需要在“像素”坐標系中處理這些問題,因此有一個額外的復雜層。

還有兩種更節省內存的方法。1) 如果需要雙線性或三次插值,請使用^{}。2) 如果您只需要最近鄰采樣,那么直接使用索引。

作為第一個例子:import numpy as np

import scipy.ndimage

import matplotlib.pyplot as plt

#-- Generate some data...

x, y = np.mgrid[-5:5:0.1, -5:5:0.1]

z = np.sqrt(x**2 + y**2) + np.sin(x**2 + y**2)

#-- Extract the line...

# Make a line with "num" points...

x0, y0 = 5, 4.5 # These are in _pixel_ coordinates!!

x1, y1 = 60, 75

num = 1000

x, y = np.linspace(x0, x1, num), np.linspace(y0, y1, num)

# Extract the values along the line, using cubic interpolation

zi = scipy.ndimage.map_coordinates(z, np.vstack((x,y)))

#-- Plot...

fig, axes = plt.subplots(nrows=2)

axes[0].imshow(z)

axes[0].plot([x0, x1], [y0, y1], 'ro-')

axes[0].axis('image')

axes[1].plot(zi)

plt.show()

使用最近鄰插值的等效方法如下:import numpy as np

import matplotlib.pyplot as plt

#-- Generate some data...

x, y = np.mgrid[-5:5:0.1, -5:5:0.1]

z = np.sqrt(x**2 + y**2) + np.sin(x**2 + y**2)

#-- Extract the line...

# Make a line with "num" points...

x0, y0 = 5, 4.5 # These are in _pixel_ coordinates!!

x1, y1 = 60, 75

num = 1000

x, y = np.linspace(x0, x1, num), np.linspace(y0, y1, num)

# Extract the values along the line

zi = z[x.astype(np.int), y.astype(np.int)]

#-- Plot...

fig, axes = plt.subplots(nrows=2)

axes[0].imshow(z)

axes[0].plot([x0, x1], [y0, y1], 'ro-')

axes[0].axis('image')

axes[1].plot(zi)

plt.show()

但是,如果使用最近鄰,則可能只需要每個像素處的采樣,因此可能會執行更類似的操作,而不是。。。import numpy as np

import matplotlib.pyplot as plt

#-- Generate some data...

x, y = np.mgrid[-5:5:0.1, -5:5:0.1]

z = np.sqrt(x**2 + y**2) + np.sin(x**2 + y**2)

#-- Extract the line...

# Make a line with "num" points...

x0, y0 = 5, 4.5 # These are in _pixel_ coordinates!!

x1, y1 = 60, 75

length = int(np.hypot(x1-x0, y1-y0))

x, y = np.linspace(x0, x1, length), np.linspace(y0, y1, length)

# Extract the values along the line

zi = z[x.astype(np.int), y.astype(np.int)]

#-- Plot...

fig, axes = plt.subplots(nrows=2)

axes[0].imshow(z)

axes[0].plot([x0, x1], [y0, y1], 'ro-')

axes[0].axis('image')

axes[1].plot(zi)

plt.show()

總結

以上是生活随笔為你收集整理的python提取一行_如何从numpy数组中提取任意一行值?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。