Exercise 5

Convert a numpy binary matrix (containing only '0' and '1') to a numpy boolean matrix (i.e. '1' will be replaced by 'True' and '0' by 'False')

Solution

import numpy as np

A = np.array([[1, 0, 1],
[0, 0, 1],
[1, 1, 0]])

B = A.astype('bool')

print(B)
"""
output:
[[ True False True]
[False False True]
[True True False]]
"""

Younes Derfoufi
my-courses.net

Leave a Reply