HOWTO · NumPy
How to Convert Matrix to Array in NumPy
Convert a Python matrix to a NumPy array, keep its two-dimensional shape, or flatten it to one dimension with np.asarray(), np.array(), flatten(), ravel(), or reshape(-1).
On this page
Use np.asarray() or np.array() to convert a matrix to a NumPy ndarray while retaining its dimensions. To make a one-dimensional result, call flatten(), ravel(), or reshape(-1) on the array. The choice matters when you later modify the result: flatten() always makes a copy, while ravel() and reshape(-1) can share data with the source when the layout permits.
The example below starts with a Python list of lists, converts it, and compares the three ways to produce a flat array. It also shows how a write to a flat result relates to the source. This example was executed; its output shows the retained two-dimensional shape and the mutation of the flattened copy.
Convert a matrix to a 2D NumPy array
For a nested list, np.asarray(matrix) creates an array with the same two-dimensional shape. np.array(matrix) is another direct option. Both infer dimensions from the nested structure; neither call means “flatten.” For an existing ndarray, np.asarray() can return that array itself when no conversion is needed, whereas np.array() creates a copy by default. Use the latter when you specifically need an independent array, or pass copy=True to make that intent explicit where supported by your NumPy version.
Run this example:
import numpy as np
matrix = [[1, 2, 3], [4, 5, 6]]
array = np.asarray(matrix)
flat_copy = array.flatten()
flat_view_when_possible = array.ravel()
flat_reshape_when_possible = array.reshape(-1)
print(array.shape)
print(array)
print(flat_copy)
print(flat_view_when_possible)
print(flat_reshape_when_possible)
flat_copy[0] = 99
print(array[0, 0], flat_copy[0])
(2, 3)
[[1 2 3]
[4 5 6]]
[1 2 3 4 5 6]
[1 2 3 4 5 6]
[1 2 3 4 5 6]
1 99
The first result has shape (2, 3): it is still two-dimensional. The later results have shape (6,), which is one-dimensional. The final assignment changes the flattened copy; it does not change array. The executed example completed successfully with exit code 0.
Flatten a matrix into a 1D array
array.flatten() returns a one-dimensional copy, so changing an element in its result leaves the original array unchanged. This is a useful default when later code may modify the flattened data and should not affect the source.
array.ravel() also produces a one-dimensional array. It returns a view when possible and makes a copy when the array’s memory layout prevents a view. np.ravel(array) is the corresponding NumPy function; array.ravel() is its method form. Because sharing depends on layout, do not rely on writes through a ravel() result to update the source unless you have established that it is a view.
The method array.reshape(-1) asks NumPy to infer the single dimension from the number of elements. It can return a view when possible and copy when necessary. It changes the shape of the result, not the element order requested by this example. All three approaches flatten in the default C order; specify an order argument when a different traversal order is needed.
Choose a flattening method
Choose flatten() when you need an independent copy and predictable mutation isolation. Choose ravel() when you want a concise flat result and can accept that it may share memory or copy depending on the source layout. Choose reshape(-1) when expressing the operation as a shape change fits the surrounding code; it likewise may share data or copy.
The conversion step and flattening step solve different problems. Start with np.asarray() or np.array() to obtain an ndarray while preserving a two-dimensional shape. Apply one of the flattening operations only when the next function expects a one-dimensional sequence. If source data must remain untouched, use flatten() or explicitly copy the result before modifying it. If memory sharing matters for performance or correctness, check the relationship between the arrays rather than assuming every ravel() or reshape is a view.