Python 在NumPy数组上[:,:]是什么意思
对不起,我问了个愚蠢的问题。 我在PHP上编程,但在Python上找到了一些不错的代码,我想在PHP上“重新创建”它。 但我对这条线很失望Python 在NumPy数组上[:,:]是什么意思,python,arrays,numpy,matrix-indexing,Python,Arrays,Numpy,Matrix Indexing,对不起,我问了个愚蠢的问题。 我在PHP上编程,但在Python上找到了一些不错的代码,我想在PHP上“重新创建”它。 但我对这条线很失望 self.h = -0.1 self.activity = numpy.zeros((512, 512)) + self.h self.activity[:, :] = self.h 但我不明白这是什么意思 [:, :] 意思是 此外,我不能“谷歌它” 完整代码 import math import numpy import pygame fro
self.h = -0.1
self.activity = numpy.zeros((512, 512)) + self.h
self.activity[:, :] = self.h
但我不明白这是什么意思
[:, :]
意思是
此外,我不能“谷歌它”
完整代码
import math
import numpy
import pygame
from scipy.misc import imsave
from scipy.ndimage.filters import gaussian_filter
class AmariModel(object):
def __init__(self, size):
self.h = -0.1
self.k = 0.05
self.K = 0.125
self.m = 0.025
self.M = 0.065
self.stimulus = -self.h * numpy.random.random(size)
self.activity = numpy.zeros(size) + self.h
self.excitement = numpy.zeros(size)
self.inhibition = numpy.zeros(size)
def stimulate(self):
self.activity[:, :] = self.activity > 0
sigma = 1 / math.sqrt(2 * self.k)
gaussian_filter(self.activity, sigma, 0, self.excitement, "wrap")
self.excitement *= self.K * math.pi / self.k
sigma = 1 / math.sqrt(2 * self.m)
gaussian_filter(self.activity, sigma, 0, self.inhibition, "wrap")
self.inhibition *= self.M * math.pi / self.m
self.activity[:, :] = self.h
self.activity[:, :] += self.excitement
self.activity[:, :] -= self.inhibition
self.activity[:, :] += self.stimulus
class AmariMazeGenerator(object):
def __init__(self, size):
self.model = AmariModel(size)
pygame.init()
self.display = pygame.display.set_mode(size, 0)
pygame.display.set_caption("Amari Maze Generator")
def run(self):
pixels = pygame.surfarray.pixels3d(self.display)
index = 0
running = True
while running:
self.model.stimulate()
pixels[:, :, :] = (255 * (self.model.activity > 0))[:, :, None]
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
elif event.key == pygame.K_s:
imsave("{0:04d}.png".format(index), pixels[:, :, 0])
index = index + 1
elif event.type == pygame.MOUSEBUTTONDOWN:
position = pygame.mouse.get_pos()
self.model.activity[position] = 1
pygame.quit()
def main():
generator = AmariMazeGenerator((512, 512))
generator.run()
if __name__ == "__main__":
main()
这是切片分配。从技术上讲,它叫1 它将
self.activity
中的所有元素设置为self.h
存储的任何值。你的代码看起来真的是多余的。据我所知,您可以删除前一行中的添加项,或者简单地使用切片分配:
self.activity = numpy.zeros((512,512)) + self.h
或
也许最快的方法是分配一个空数组并。用期望值填充它:
self.activity = numpy.empty((512,512))
self.activity.fill(self.h)
1实际上,在调用\uuuu setitem\uuuu
之前会尝试使用\uuuu setslice\uuuu
,但是\uuuu setslice\uuuuu
已被弃用,除非你有很好的理由,否则不应在现代代码中使用。[:,:]
代表从开始到结束的一切,就像代表列表一样。区别在于第一个:
代表第一个维度,第二个:
代表第二个维度
a = numpy.zeros((3, 3))
In [132]: a
Out[132]:
array([[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]])
分配给第二行:
In [133]: a[1, :] = 3
In [134]: a
Out[134]:
array([[ 0., 0., 0.],
[ 3., 3., 3.],
[ 0., 0., 0.]])
分配给第二列:
In [135]: a[:, 1] = 4
In [136]: a
Out[136]:
array([[ 0., 4., 0.],
[ 3., 4., 3.],
[ 0., 4., 0.]])
分配给所有人:
In [137]: a[:] = 10
In [138]: a
Out[138]:
array([[ 10., 10., 10.],
[ 10., 10., 10.],
[ 10., 10., 10.]])
numpy使用元组作为索引。
在本例中,这是一个详细的示例
它相当于更简单的
self.activity[:] = self.h
(这也适用于常规列表)检查这个@ZangMingJie——是的,这是切片表示法的标准答案。然而,大多数答案(隐式地)集中在\uu getitem\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
上,而不是\uuuuuuuuuu setitem\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
上,因此我认为从不同的角度重新回答是合理的。因此我或者至少接近它。@user2432721——这实际上取决于arr
的类型——但是对于numpy.ndarray
,是的。这是正确的。@JochenRitzel--“它将self.activity
中的所有元素设置为self.h
存储的任何值”…这里self.activity[:,:]=self.h
是多余的,因为self.activity
已经是self.h
。与此类似:a=0+2
,然后a=2
。这其实不一样,但可能有助于理解。你能再解释一下最后一个[:,:]
吗?
In [137]: a[:] = 10
In [138]: a
Out[138]:
array([[ 10., 10., 10.],
[ 10., 10., 10.],
[ 10., 10., 10.]])
[0] #means line 0 of your matrix
[(0,0)] #means cell at 0,0 of your matrix
[0:1] #means lines 0 to 1 excluded of your matrix
[:1] #excluding the first value means all lines until line 1 excluded
[1:] #excluding the last param mean all lines starting form line 1
included
[:] #excluding both means all lines
[::2] #the addition of a second ':' is the sampling. (1 item every 2)
[::] #exluding it means a sampling of 1
[:,:] #simply uses a tuple (a single , represents an empty tuple) instead
of an index.
self.activity[:] = self.h