dangerChihuahua007
dangerChihuahua007

Reputation: 20895

How do I turn this into a numpy matrix?

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

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

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

Upvotes: 4

Views: 134

Answers (1)

nneonneo
nneonneo

Reputation: 179422

Use np.array to construct the array, then reshape to mold it into the right 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]])

Upvotes: 3

Related Questions