Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 高效地创建0和255的棋盘格图案_Python_Numpy - Fatal编程技术网

Python 高效地创建0和255的棋盘格图案

Python 高效地创建0和255的棋盘格图案,python,numpy,Python,Numpy,我编写了一个函数,通过for循环生成一个示例numpy数组。但它对于更大的维度来说太慢了 import numpy as np mat = np.zeros((5, 5)) def func(im): im = im.copy() h,w = im.shape for i in range(h): for j in range(w): if (i + j) % 2 == 0: im[i, j] =

我编写了一个函数,通过for循环生成一个示例numpy数组。但它对于更大的维度来说太慢了

import numpy as np
mat = np.zeros((5, 5))

def func(im):
    im = im.copy()
    h,w = im.shape
    for i in range(h):
        for j in range(w):
            if (i + j) % 2 == 0:
                im[i, j] = 255
    return im
print(func(mat))
#    array([[ 255.,    0.,  255., ...,  255.,    0.,  255.],
#        [   0.,  255.,    0., ...,    0.,  255.,    0.],
#        [ 255.,    0.,  255., ...,  255.,    0.,  255.],
#        ..., 
#        [ 255.,    0.,  255., ...,  255.,    0.,  255.],
#        [   0.,  255.,    0., ...,    0.,  255.,    0.],
#        [ 255.,    0.,  255., ...,  255.,    0.,  255.]])

我们将从零初始化数组开始。那就是:
np.zero((高,宽))
。接下来,我们几乎没有办法用这种交替的
255s
模式来填充它

方法#1:使用开放范围数组与模拟这些迭代器,并以矢量化方式直接转换比较-

方法#2:仔细观察,似乎您正在将每行中的每个其他元素设置为
255
,从第一行的第一个元素开始,第二行的第二个元素开始,然后返回到第三行的第一个元素,依此类推。因此,我们也可以使用,并且应该非常有效-

im[::2,::2] = 255
im[1::2,1::2] = 255
运行时测试

接近-

def func(im):
    h,w = im.shape
    for i in range(h):
        for j in range(w):
            if (i + j) % 2 == 0:
                im[i, j] = 255
    return im

def app1(im):
    h,w = im.shape    
    I,J = np.ogrid[:h,:w]
    im[(I+J)%2==0] = 255
    return im

def app2(im):
    im[::2,::2] = 255
    im[1::2,1::2] = 255
    return im
核实-

In [74]: im = np.random.randint(0,255,(1000,1000))

In [75]: im1 = im.copy()
    ...: im2 = im.copy()
    ...: im3 = im.copy()
    ...: 

In [76]: func(im1)
    ...: app1(im2)
    ...: app2(im3)
    ...: 
Out[76]: 
array([[255, 133, 255, ...,  14, 255,  41],
       [235, 255, 191, ..., 255,  40, 255],
       [255, 151, 255, ...,  51, 255,  18],
       ..., 
       [ 50, 255, 177, ..., 255, 193, 255],
       [255, 245, 255, ..., 114, 255,  27],
       [223, 255, 148, ..., 255, 200, 255]])

In [77]: print np.allclose(im1,im2)
    ...: print np.allclose(im1,im3)
    ...: 
True
True
时间安排-

In [78]: %timeit func(im)
10 loops, best of 3: 106 ms per loop

In [79]: %timeit app1(im)
100 loops, best of 3: 14 ms per loop

In [80]: %timeit app2(im)
1000 loops, best of 3: 415 µs per loop

In [82]: 106/0.415 # Speedup with approach #2 over original one
Out[82]: 255.42168674698797

我们将从零初始化数组开始。那就是:
np.zero((高,宽))
。接下来,我们几乎没有办法用这种交替的
255s
模式来填充它

方法#1:使用开放范围数组与模拟这些迭代器,并以矢量化方式直接转换比较-

方法#2:仔细观察,似乎您正在将每行中的每个其他元素设置为
255
,从第一行的第一个元素开始,第二行的第二个元素开始,然后返回到第三行的第一个元素,依此类推。因此,我们也可以使用,并且应该非常有效-

im[::2,::2] = 255
im[1::2,1::2] = 255
运行时测试

接近-

def func(im):
    h,w = im.shape
    for i in range(h):
        for j in range(w):
            if (i + j) % 2 == 0:
                im[i, j] = 255
    return im

def app1(im):
    h,w = im.shape    
    I,J = np.ogrid[:h,:w]
    im[(I+J)%2==0] = 255
    return im

def app2(im):
    im[::2,::2] = 255
    im[1::2,1::2] = 255
    return im
核实-

In [74]: im = np.random.randint(0,255,(1000,1000))

In [75]: im1 = im.copy()
    ...: im2 = im.copy()
    ...: im3 = im.copy()
    ...: 

In [76]: func(im1)
    ...: app1(im2)
    ...: app2(im3)
    ...: 
Out[76]: 
array([[255, 133, 255, ...,  14, 255,  41],
       [235, 255, 191, ..., 255,  40, 255],
       [255, 151, 255, ...,  51, 255,  18],
       ..., 
       [ 50, 255, 177, ..., 255, 193, 255],
       [255, 245, 255, ..., 114, 255,  27],
       [223, 255, 148, ..., 255, 200, 255]])

In [77]: print np.allclose(im1,im2)
    ...: print np.allclose(im1,im3)
    ...: 
True
True
时间安排-

In [78]: %timeit func(im)
10 loops, best of 3: 106 ms per loop

In [79]: %timeit app1(im)
100 loops, best of 3: 14 ms per loop

In [80]: %timeit app2(im)
1000 loops, best of 3: 415 µs per loop

In [82]: 106/0.415 # Speedup with approach #2 over original one
Out[82]: 255.42168674698797
您还可以使用重复模式,例如您的模式

import numpy as np
n = 5
x = np.tile(np.array([[0,255],[255,0]]),(n,n))
您还可以使用重复模式,例如您的模式

import numpy as np
n = 5
x = np.tile(np.array([[0,255],[255,0]]),(n,n))

可以使用numpy数组的奇特索引属性

您需要的一个例子是:

import numpy as np
arr = np.zeros((10, 10))
a = np.arange(0, 10, 2)
b = np.arange(1, 10, 2)
arr[a[:, None], a[None, :]] = 255
arr[b[:, None], b[None, :]] = 255

print(arr)
array([[ 255.,    0.,  255., ...,    0.,  255.,    0.],
       [   0.,  255.,    0., ...,  255.,    0.,  255.],
       [ 255.,    0.,  255., ...,    0.,  255.,    0.],
       ..., 
       [   0.,  255.,    0., ...,  255.,    0.,  255.],
       [ 255.,    0.,  255., ...,    0.,  255.,    0.],
       [   0.,  255.,    0., ...,  255.,    0.,  255.]])

可以使用numpy数组的奇特索引属性

您需要的一个例子是:

import numpy as np
arr = np.zeros((10, 10))
a = np.arange(0, 10, 2)
b = np.arange(1, 10, 2)
arr[a[:, None], a[None, :]] = 255
arr[b[:, None], b[None, :]] = 255

print(arr)
array([[ 255.,    0.,  255., ...,    0.,  255.,    0.],
       [   0.,  255.,    0., ...,  255.,    0.,  255.],
       [ 255.,    0.,  255., ...,    0.,  255.,    0.],
       ..., 
       [   0.,  255.,    0., ...,  255.,    0.,  255.],
       [ 255.,    0.,  255., ...,    0.,  255.,    0.],
       [   0.,  255.,    0., ...,  255.,    0.,  255.]])