Table of Contents
Converting a NumPy matrix to an array is a simple process and can be done using the numpy.asarray() function. This function takes in the matrix as an argument and returns the corresponding array. It is important to note that the matrix and array will share the same data buffer, meaning any changes made to one will affect the other.
Some examples of converting a NumPy matrix to an array include:
1. Using the numpy.asarray() function:
matrix = np.matrix([[1, 2], [3, 4]])
array = np.asarray(matrix)
2. Using the numpy.array() function:
matrix = np.matrix([[1, 2], [3, 4]])
array = np.array(matrix)
3. Using the numpy.matrix.tolist() function:
matrix = np.matrix([[1, 2], [3, 4]])
array = np.matrix.tolist(matrix)
4. Using the numpy.matrix.flatten() function:
matrix = np.matrix([[1, 2], [3, 4]])
array = np.matrix.flatten(matrix)
Converting a NumPy matrix to an array allows for easier manipulation and access to individual elements. It also makes it possible to use array-specific functions and operations on the matrix data.
Convert NumPy Matrix to Array (With Examples)
You can use the following methods to convert a NumPy matrix to an array:
Method 1: Use A1
my_array = my_matrix.A1
Method 2: Use ravel()
my_array = np.asarray(my_matrix).ravel()
Both methods return the same result, but the second method simply requires more typing.
The following examples show how to use each method in practice.
Example 1: Convert NumPy Matrix to Array Using A1
The following code shows how to use the to convert a NumPy matrix to an array:
import numpy as np
#create NumPy matrix with 3 columns and 5 rows
my_matrix = np.matrix(np.arange(15).reshape((5, 3)))
#view NumPy matrix
print(my_matrix)
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]
[12 13 14]]
#convert matrix to array
my_array = my_matrix.A1#view NumPy array
print(my_array)
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
We can see that the NumPy matrix has been converted to an array with 15 values.
We can confirm that it is NumPy array by using the type() function:
#check type of my_array
type(my_array)
numpy.ndarray
It is indeed a NumPy array.
Example 2: Convert NumPy Matrix to Array Using ravel()
The following code shows how to use the function to convert a NumPy matrix to an array:
import numpy as np
#create NumPy matrix with 3 columns and 5 rows
my_matrix = np.matrix(np.arange(15).reshape((5, 3)))
#view NumPy matrix
print(my_matrix)
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]
[12 13 14]]
#convert matrix to array
my_array = np.asarray(my_matrix).ravel()
#view NumPy array
print(my_array)
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
We can confirm that it is NumPy array by using the type() function:
#check type of my_array
type(my_array)
numpy.ndarray
It is indeed a NumPy array.
The following tutorials explain how to perform other common tasks in NumPy:
Cite this article
stats writer (2024). How can I convert a NumPy matrix to an array and what are some examples of doing so?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-convert-a-numpy-matrix-to-an-array-and-what-are-some-examples-of-doing-so/
stats writer. "How can I convert a NumPy matrix to an array and what are some examples of doing so?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-convert-a-numpy-matrix-to-an-array-and-what-are-some-examples-of-doing-so/.
stats writer. "How can I convert a NumPy matrix to an array and what are some examples of doing so?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-convert-a-numpy-matrix-to-an-array-and-what-are-some-examples-of-doing-so/.
stats writer (2024) 'How can I convert a NumPy matrix to an array and what are some examples of doing so?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-convert-a-numpy-matrix-to-an-array-and-what-are-some-examples-of-doing-so/.
[1] stats writer, "How can I convert a NumPy matrix to an array and what are some examples of doing so?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I convert a NumPy matrix to an array and what are some examples of doing so?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
