Exercise 555

Give the python code numpy which transforms the matrix: A = np.array ([1, 2, 3, 4, 5, 6, 7, 8, 9]) into matrix : B = np.array([[1, 2, 3], [4, 5, 6],[7, 8, 9]])

Solution

import numpy as np
A = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
B = A.reshape(3,3)
print(B)
# The output is : [[1 2 3] [4 5 6] [7 8 9]]

Younes Derfoufi
my-courses.net

Leave a Reply