Python:Numpy切片深度分解 [英] Python: Numpy slicing indepth explnation
问题描述
我在conway的生活克隆游戏中找到了以下代码. 我不明白以下代码是如何运行的.有人可以对代码的执行方式进行深入的解释吗?
I found the following code in a conway's game of life clone. I don't understand how exactly the following code is run. Can someone give an in depth explanation in how the Code is executed?
def iterate(Z):
# find number of neighbors that each square has
N = np.zeros(Z.shape)
N[1:, 1:] += Z[:-1, :-1]
N[1:, :-1] += Z[:-1, 1:]
N[:-1, 1:] += Z[1:, :-1]
N[:-1, :-1] += Z[1:, 1:]
N[:-1, :] += Z[1:, :]
N[1:, :] += Z[:-1, :]
N[:, :-1] += Z[:, 1:]
N[:, 1:] += Z[:, :-1]
# a live cell is killed if it has fewer than 2 or more than 3 neighbours.
part1 = ((Z == 1) & (N < 4) & (N > 1))
# a new cell forms if a square has exactly three members
part2 = ((Z == 0) & (N == 3))
return (part1 | part2).astype(int)
推荐答案
在所有N+=
行之后添加print(N)
行,并尝试各种Z
数组.
Add a print(N)
line after all the N+=
lines, and try various Z
arrays.
例如
定义一个小的z
,中间用1 s的块表示:
Define a small z
with a block of 1s in the middle:
In [29]: z = np.zeros((10,10),int)
In [31]: z[4:6,4:6]=1
In [34]: z[4:8,5]=1
In [35]: z
Out[35]:
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
将其传递给函数:
In [36]: iterate(z)
[[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 1. 2. 2. 1. 0. 0. 0.]
[ 0. 0. 0. 2. 3. 3. 2. 0. 0. 0.]
[ 0. 0. 0. 2. 4. 4. 3. 0. 0. 0.]
[ 0. 0. 0. 1. 4. 3. 3. 0. 0. 0.]
[ 0. 0. 0. 0. 2. 1. 2. 0. 0. 0.]
[ 0. 0. 0. 0. 1. 1. 1. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
N
计算出的邻居数为1.自己检查一下计数.
N
has counted the number of neighbors that are 1. Check the counts yourself.
Out[36]:
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
尝试各种图案,重复进行迭代,然后观察图案变化.有些人死了,有些人以命令的方式移动,有些眨眼"等.
Try various patterns, repeat the iterate and watch the pattern change. Some die away, some move in an order fashion, some 'blink', etc.
在这样的行中:
N[1:, 1:] += Z[:-1, :-1]
RHS是左上部分(此处为9x9); LHS是右下角,同样是9x9.有8个N+=
表达式,计算8个邻居(在3x3块中减去中心).使用此偏移量切片,它可以立即对Z
中的所有点进行计数.
the RHS is an upper left portion (here 9x9); LHS is a bottom right, again 9x9. There are 8 N+=
expressions, calculating the 8 neighbors (in a 3x3 block, minus the center). With this offset slicing it can do the count for all points in Z
at once.
一开始,1行数组可能更容易可视化
For a start, a 1 row array might be easier to visualize
In [47]: z = np.zeros((1,10),int)
In [49]: z[0,4:7]=1
In [50]: z
Out[50]: array([[0, 0, 0, 0, 1, 1, 1, 0, 0, 0]])
In [51]: iterate(z)
[[ 0. 0. 0. 1. 1. 2. 1. 1. 0. 0.]]
Out[51]: array([[0, 0, 0, 0, 0, 1, 0, 0, 0, 0]])
我认为,如果z
的所有边值均为0,则效果最好.
I think this works best if all the edge values of z
are 0.
此数组创建glider
,该动画在 https://en上进行动画处理.wikipedia.org/wiki/Glider_(Conway%27s_Life)
This array creates the glider
that is animated on https://en.wikipedia.org/wiki/Glider_(Conway%27s_Life)
In [64]: z = np.zeros((10,10),int)
In [65]: z[1,2]=1;z[2,3]=1;z[3,1:4]=1
这篇关于Python:Numpy切片深度分解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!