Home > Software design >  How to create a numpy ndarray with bool elements?
How to create a numpy ndarray with bool elements?

Time:07-06

I have a numpy array boo_array with bool elements. 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. So, I try to create my_boo_array with the following code:

> 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]

CodePudding user response:

This is because boo_array[0][0] is not an array. 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.

boo_array[0] is a one-dimensional array. np.ndarray will convert that to an array, which is a no-op.

CodePudding user response:

ndarray([2,1]) creates a (2,1) shape array with float dtype. 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).

For an example a (2,1) shape with bool dtype, and all False values:

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. 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. That's not an array. It's type is numpy.bool_, which does have some array like qualities, including a shape () (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". Use np.array when you want to make an array from values.

Making a (2,) shape array with 2 False values:

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

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:

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