How to turn this into a matrix?

How to convert an array a = [[[[1, 2, 3, 4], [5, 6, 7, 8]], [[9, 10, 11, 12], [13, 14, 15, 16]]]] to the numpy matrix of the form

 [[1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16]] 

? I tried np.bmat(a) no avail. When I do this, I get a 2x6 matrix.

+4
source share
1 answer

Use np.array to build the array, then reshape to turn it into the desired shape:

 >>> np.array([[[[1, 2, 3, 4], [5, 6, 7, 8]], [[9, 10, 11, 12], [13, 14, 15, 16]]]]).reshape((4,4)) array([[ 1, 2, 3, 4], [ 5, 6, 7, 8], [ 9, 10, 11, 12], [13, 14, 15, 16]]) 
+3
source

Source: https://habr.com/ru/post/1441169/


All Articles