我在2D阵列上的恒定大小的移动窗口上应用操作.是否有一个有效的类似矢量化的操作,我可以实现这样做而无需在Python中循环?我目前的结构看起来像这样
for i in range(1,xmax-1):
for j in range(1,ymax-1):
out[i][j] = f(in[i][j],in[i+1][j],in[i-1][j],in[i][j+1],in[i][j-1],...)
Run Code Online (Sandbox Code Playgroud)
这些评论可以吃留在这个问题暗指矢量化这种操作这种可能性,但没有进一步的细节矢量索引/切片在numpy的/ SciPy的?
是否有任何便利工具在Numpy阵列上进行块运算?
我正在考虑像Ising自旋重整化这样的操作,其中将矩阵划分为块并返回矩阵,其中每个块由其和,平均或其他函数替换.
通过一个滑动窗口示例为numpy.试图了解,None
的start_idx = np.arange(B[0])[:,None]
foo = np.arange(10)
print foo
print foo[:]
print foo[:,]
print foo[:,None]
Run Code Online (Sandbox Code Playgroud)
的效果None
似乎是转置阵.
[0 1 2 3 4 5 6 7 8 9]
[0 1 2 3 4 5 6 7 8 9]
[0 1 2 3 4 5 6 7 8 9]
[[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]]
Run Code Online (Sandbox Code Playgroud)
但我不完全确定.我无法找到解释第二个参数(None
)的内容的文档.这也是google的一个难点.该numpy的阵列文档让我觉得它是与先进的索引,但我不能肯定不够.
有没有办法将2D阵列分区并重塑为3D阵列.如下例所示:
基本上,我左边有一个4x4矩阵,我想要一个2x2x4矩阵,所以我可以在第3轴上应用numpy.mean.实际上我拥有的矩阵非常庞大,所以这就是为什么循环使用块不是一种选择.
任何帮助是极大的赞赏.
我有一张图片.
我想为图像中的每个像素获得3x3窗口(相邻像素).
我有这个Python代码:
for x in range(2,r-1,1):
for y in range(2,c-1,1):
mask5=numpy.array([cv.Get2D(copy_img,x-1,y-1),cv.Get2D(copy_img,x-1,y),cv.Get2D(copy_img,x-1,y+1),cv.Get2D(copy_img,x,y-1),cv.Get2D(copy_img,x,y),cv.Get2D(copy_img,x,y+1),cv.Get2D(copy_img,x+1,y-1),cv.Get2D(copy_img,x+1,y),cv.Get2D(copy_img,x+1,y+1)])
cent=[cv.Get2D(copy_img,x,y)]
Run Code Online (Sandbox Code Playgroud)
mask5是3x3窗口.cent是中心像素.
有没有更有效的方法来做到这一点 - 即使用map,iterators - 除了我使用的两个嵌套循环之外的任何东西?
我有一个大NumPy.array
field_array
而小的数组match_array
,都由int
值组成.使用以下示例,如何检查match_array形状的任何段是否field_array
包含与其中的值完全对应的值match_array
?
import numpy
raw_field = ( 24, 25, 26, 27, 28, 29, 30, 31, 23, \
33, 34, 35, 36, 37, 38, 39, 40, 32, \
-39, -38, -37, -36, -35, -34, -33, -32, -40, \
-30, -29, -28, -27, -26, -25, -24, -23, -31, \
-21, -20, -19, -18, -17, -16, -15, -14, -22, \
-12, -11, -10, -9, -8, -7, -6, -5, -13, \
-3, -2, -1, …
Run Code Online (Sandbox Code Playgroud) 我试图定义一个函数,它将返回输入单元格的3x3邻域.现在我有:
def queen_neighbourhood(in_forest, in_row, in_col):
neighbourhood = in_forest[in_row-1:in_row+1, in_col-1:in_col+1]
return neighbourhood
Run Code Online (Sandbox Code Playgroud)
(in_forest是输入数组).
当我运行它时,它似乎只返回2x2矩阵,而不是3x3.为什么是这样?在我看来,我正在输入行和列引用,然后切出一个在输入行后面开始一行的正方形,并在它之前结束一行,然后对于列进行相同的处理.
例如,给定一个输入数组:
[ 01, 02, 03, 04, 05
06, 07, 08, 09, 10
11, 12, 13, 14, 15
16, 17, 18, 19, 20
21, 22, 23, 24, 25 ]
Run Code Online (Sandbox Code Playgroud)
然后使用第2行第3列,我想返回一个矩阵:
[ 02, 03, 04
07, 08, 09
12, 13, 14 ]
Run Code Online (Sandbox Code Playgroud) 我正在尝试仅使用 NumPy 来实现图像卷积代码,类似于cv2.filter2D(...)的做法。
import numpy as np
import time
# kernal
H = np.array([[0,1,0],[1,-4,1],[0,1,0]])
# image
SEED = 23
img = np.random.RandomState(SEED).randint(10, size=(4, 4))
# shapes
Hi, Wi = img.shape
Hk, Wk = H.shape
hk = Hk//2
wk = Wk//2
# padding
new_img = np.pad(img, (hk, wk), 'constant', constant_values=0)
pHi, pWi = new_img.shape
print('img: ')
print(new_img)
print('kernal: ')
print(H)
print('\n')
# image convolution
##################################################
# Method 1
st = time.time()
out = np.zeros((Hi, Wi))
for i in range(hk, …
Run Code Online (Sandbox Code Playgroud)