user3333975
user3333975

Reputation: 125

How to compress a matrix into an array/set in NumPy the fastest?

How can I make a matrix like mat0 = np.array([[1,2,3],[4,5,6],[7,8,9]]) flatten to an array like arr0 = np.array([1,2,3,4,5,6,7,8,9])? Or perhaps a set?

Upvotes: 0

Views: 381

Answers (1)

falsetru
falsetru

Reputation: 369074

Use reshape:

>>> import numpy as np
>>> mat0 = np.array([[1,2,3],[4,5,6],[7,8,9]])
>>> mat0.reshape(-1)
array([1, 2, 3, 4, 5, 6, 7, 8, 9])

or ravel:

>>> mat0.ravel()
array([1, 2, 3, 4, 5, 6, 7, 8, 9])

Upvotes: 1

Related Questions