简体   繁体   English

如何使用 bool 元素创建一个 numpy ndarray?

[英]How to create a numpy ndarray with bool elements?

I have a numpy array boo_array with bool elements.我有一个带有 bool 元素的 numpy 数组boo_array Following is how I generated it以下是我生成它的方式

> a = np.ndarray([2, 1])
> boo_array = a>1

> print(boo_array)
[[False]
 [False]]

I check the type of elements我检查元素的类型

> print(type(boo_array[0]))
> print(type(boo_array[0][0]))
<class 'numpy.ndarray'>
<class 'numpy.bool_'>

I see that the first element [False] is an numpy.ndarray .我看到第一个元素[False]是一个numpy.ndarray So, I try to create my_boo_array with the following code:因此,我尝试使用以下代码创建my_boo_array

> my_boo_arr = np.ndarray(boo_array[0][0]) # Which should generate an ndarray '[False]'
TypeError: an integer is required

Why is this error thrown when I manually create it, but allows to generate a similar array shown above boo_array[0]为什么当我手动创建它时会引发此错误,但允许生成上面显示的类似数组boo_array[0]

This is because boo_array[0][0] is not an array.这是因为boo_array[0][0]不是数组。 It is a single value.它是一个单一的值。 When np.ndarray is presented with a single (non-list, non-array) value, it assumes that it is the size of the array it is supposed to create.np.ndarray呈现单个(非列表,非数组)值时,它假定它是它应该创建的数组的大小。

boo_array[0] is a one-dimensional array. boo_array[0]是一个一维数组。 np.ndarray will convert that to an array, which is a no-op. np.ndarray会将其转换为一个数组,这是一个空操作。

ndarray([2,1]) creates a (2,1) shape array with float dtype. ndarray([2,1])创建一个具有 float dtype 的 (2,1) 形状数组。 If you read the docs, you'll see described as a low-level array creator.如果您阅读文档,您会看到描述为低级数组创建者。 Usually we use np.array , or np.zeros (and a few others).通常我们使用np.arraynp.zeros (以及其他一些)。

For an example a (2,1) shape with bool dtype, and all False values:例如,具有 bool dtype 和所有 False 值的 (2,1) 形状:

In [202]: a = np.zeros((2,1), dtype=bool)    
In [203]: a
Out[203]: 
array([[False],
       [False]])

We can select a "row" from that with:我们可以从中选择一个“行”:

In [204]: a[0]
Out[204]: array([False])

That has (1,) shape, the 2nd dimension of a .它具有 (1,) 形状,是a的第二维。 Indexing both row and column:索引行和列:

In [205]: a[0,0]
Out[205]: False

That's a scalar value.这是一个标量值。 For integer indexing a[0][0] does the same thing.对于整数索引a[0][0]做同样的事情。 That's not an array.那不是数组。 It's type is numpy.bool_ , which does have some array like qualities, including a shape () (0d).它的类型是numpy.bool_ ,它确实具有一些类似数组的特性,包括形状() (0d)。

We can make an array from that with:我们可以使用以下方法制作一个数组:

In [206]: np.array(a[0,0])
Out[206]: array(False)

This too has shape () (no brackets).这也有形状 () (没有括号)。 np.ndarray does not work here because the first argument is supposed to be "shape". np.ndarray在这里不起作用,因为第一个参数应该是“形状”。 Use np.array when you want to make an array from values.当您想从值创建一个数组时,请使用np.array

Making a (2,) shape array with 2 False values:制作具有 2 个 False 值的 (2,) 形状数组:

In [207]: np.array([False, False])
Out[207]: array([False, False])

In [208]: np.array([[False], [True]])  # (2,1) shape
Out[208]: 
array([[False],
       [ True]])

In [215]: a.shape
Out[215]: (2, 1)

a with 2dim, can be indexed with a[0] or a[0,0] . a 2dim 的 a 可以使用a[0]a[0,0]进行索引。

A 1d array can only be indexed with one integer:一维数组只能用一个整数索引:

In [216]: b = np.array([False])    
In [217]: b.shape
Out[217]: (1,)    
In [218]: b[0]
Out[218]: False

A 0d array cannot be indexed with a number (or iterated): 0d 数组不能用数字索引(或迭代):

In [219]: b = np.array(False)
In [220]: b.shape
Out[220]: ()
In [221]: b[0]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Input In [221], in <cell line: 1>()
----> 1 b[0]

IndexError: too many indices for array: array is 0-dimensional, but 1 were indexed

It can be indexed with an empty tuple - with 0 indices:它可以用空元组索引 - 索引为 0:

In [222]: b[()]
Out[222]: False

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

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