[:,:]在NumPy数组上是什么意思 [英] What does [:, :] mean on NumPy arrays

查看:76
本文介绍了[:,:]在NumPy数组上是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉这个愚蠢的问题. 我正在用PHP编程,但是在Python上找到了一些不错的代码,并想在PHP上重新创建"它. 但是我对这条线感到很沮丧

Sorry for the stupid question. I'm programming on PHP but found some nice code on Python and want to "recreate" it on PHP. But I'm quite frustrated about the line

self.h = -0.1    
self.activity = numpy.zeros((512, 512)) + self.h
self.activity[:, :] = self.h

但是我不明白是什么

[:, :]

平均值.

此外,我无法使用"Google It".

Besides I wasn't able to "Google It".

完整代码

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()

推荐答案

[:, :]代表从头到尾的所有内容,就像列表一样.区别在于,第一个:代表第一维,第二个:代表第二维.

The [:, :] stands for everything from the beginning to the end just like for lists. The difference is that the first : stands for first and the second : for the second dimension.

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数组上是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆