简体   繁体   English

Python:根据列/行创建具有填充值的数组

[英]Python: Creation of array with fill values according to column/row

10x10阵列

Suppose you are trying to create a synthetic image 10x10 represented by a matrix of values (numpy array). 假设您尝试创建一个由值矩阵(numpy数组)表示的合成图像10x10。 This image has three blocked sections. 该图像有三个被遮挡的部分。 In the upper left block, columns 0-4 and rows 0-4, the value will be 1. the right block, columns 5-9 and rows 0-9, will be 0. the remaining area, columns 0-4 and row 5-9, will be 2.(see attached image) 在左上方的块中,第0-4列和第0-4行,该值为1。在右边的块中,第5-9列和0-9行,将为0。其余区域,第0-4列和第5-9,将是2。(请参阅附件图片)

What is the fastest way to create such an object? 创建此类对象的最快方法是什么? I understand that you could create an array of zeros and iteratively change the values in each column, but I also understand this is not an efficient method. 我知道您可以创建一个零数组并迭代地更改每列中的值,但是我也知道这不是一种有效的方法。 I assume it involves simply generating the array using np.array, but I'm not quite sure of the syntax. 我认为它只涉及使用np.array生成数组,但是我不太确定语法。

My first thought is to create an 'empty' array of 0 , and then fill the blocks of 1 and 2 . 我的第一个想法是创建一个0的“空”数组,然后填充12的块。 Eg 例如

In [145]: C = np.zeros((10,10), int)

In [146]: C[:4,:4]=1

In [147]: C[:4,5:9]=2

In [148]: C
Out[148]: 
array([[1, 1, 1, 1, 0, 2, 2, 2, 2, 0],
       [1, 1, 1, 1, 0, 2, 2, 2, 2, 0],
       [1, 1, 1, 1, 0, 2, 2, 2, 2, 0],
       [1, 1, 1, 1, 0, 2, 2, 2, 2, 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, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

You could also make the blocks (with np.ones etc), and concatenate them. 您也可以制作块(使用np.ones等),并将它们连接起来。 hstack and vstack are just alternative APIs for concatenate. hstackvstack只是用于连接的替代API。 But concatenate ends up using, in compiled code, this initialize and assign method. 但是连接最终会在编译的代码中使用此initialize和assign方法。 It's a good idea to be familiar with both methods. 熟悉这两种方法是一个好主意。

What about, 关于什么,

import numpy as np

a = np.ones((5,5))
b = a*2.
c = np.zeros((10,5))

np.hstack((np.vstack((a,b)),c))

Is this a homework question? 这是一个作业问题吗? Have a play with numpy.concatenate and numpy.ones and see how you go. numpy.concatenatenumpy.ones ,看看你怎么走。

For simple patterns similar to yours, you can use basic broadcasting: 对于类似于您的简单模式,可以使用基本广播:

>>> numpy.array([1]*5 + [2]*5)[:,None] * numpy.array([1]*5 + [0]*5)
array([[1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
       [1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
       [1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
       [1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
       [1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
       [2, 2, 2, 2, 2, 0, 0, 0, 0, 0],
       [2, 2, 2, 2, 2, 0, 0, 0, 0, 0],
       [2, 2, 2, 2, 2, 0, 0, 0, 0, 0],
       [2, 2, 2, 2, 2, 0, 0, 0, 0, 0],
       [2, 2, 2, 2, 2, 0, 0, 0, 0, 0]])

[:,None] just adds a second axis, so that instead of a (10,) size array, we have a (10,1) size array which we can then multiply with the (10,) array on the right using broadcasting. [:,None]只是添加了第二个轴,所以我们有一个(10,1)大小数组,而不是(10,)大小数组,然后可以使用广播将其与右边的(10,)数组相乘。

Or, more concisely: 或者,更简洁地说:

>>> numpy.outer([1]*5 + [2]*5, [1]*5 + [0]*5)

which gives the same result. 得到相同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM