In NumPy, a matrix is essentially a two-dimensional NumPy array with a special subclass. In this article, we will see how we can convert NumPy Matrix to Array. Also, we will see different ways to convert NumPy Matrix to Array.
Convert Python NumPy Matrix to an Array
Below are the ways by which we can convert Python NumPy Matrix to an NumPy Array:
Table of Content
Convert NumPy Matrix to Array using flatten()
In this example, we are using numpy.flatten() method to convert a NumPy Matrix into a NumPy Array. It utilizes the complete N-dimensional array of the matrix’s elements. NumPy flatten() serves to return a clone of the source input array, which is then flattened into a one-dimensional array.
import numpy as np
# Create NumPy 2-D array matrix = np.matrix([[ 5 , 10 , 15 ],[ 20 , 25 , 30 ],[ 35 , 40 , 45 ]])
print ( "Given Matrix:" ,matrix)
print ( type (matrix))
# Convert numpy matrix to array using flatten() resulting_array = matrix.flatten()
print ( 'After Conversion:' ,resulting_array)
print ( type (matrix))
|
Output
Given Matrix: [[ 5 10 15]
[20 25 30]
[35 40 45]]
<class 'numpy.matrix'>
After Conversion: [ 5 10 15 20 25 30 35 40 45]
<class 'numpy.ndarray'>
Python NumPy Matrix to Array using ravel()
In this example, we are using numpy.ravel() to convert NumPy Matrix to Array. The numpy.ravel() functions is used to flatten the whole array into one and continuous shape. It is a time taking process that produces a Compressed one-dimensional array. The type of the returned array will be same as the type of the input array.
import numpy as np
# Create NumPy 2-D array matrix = np.matrix([[ 5 , 10 , 15 ],[ 20 , 25 , 30 ],[ 35 , 40 , 45 ]])
print ( 'Given Matrix' ,matrix)
print ( type (matrix))
# Convert numpy matrix to array using flatten() resulting_array = matrix.ravel()
print ( 'After Conversion:' ,resulting_array)
print ( type (resulting_array))
|
Output:
Given Matrix: [[ 5 10 15]
[20 25 30]
[35 40 45]]
<class 'numpy.matrix'>
After Conversion: [ 5 10 15 20 25 30 35 40 45]
<class 'numpy.ndarray'>
Convert NumPy Matrix to Array with reshape()
In this example, we are using numpy.reshape(). The numpy.reshape() function can also be used to flatten an array to a matrix. We can use np.reshape(arr,-1) and this would transform any array’s shape to a flattened array with -1 as the form. If you want to convert multi-dimension into 1-D, refer to the code snippet below.
import numpy as np
# Create NumPy 2-D array matrix = np.matrix([[ 5 , 10 , 15 ],[ 20 , 25 , 30 ],[ 35 , 40 , 45 ]])
print ( 'Given Matrix' ,matrix)
print ( type (matrix))
# Convert numpy matrix to array using flatten() resulting_array = matrix.reshape( - 1 )
print ( 'After Conversion:' ,resulting_array)
print ( type (resulting_array))
|
Output
Given Matrix: [[ 5 10 15]
[20 25 30]
[35 40 45]]
<class 'numpy.matrix'>
After Conversion: [ 5 10 15 20 25 30 35 40 45]
<class 'numpy.ndarray'>
Convert NumPy Matrix to Array with A1
In this example, we are using matrix.A1 to convert NumPy Matrix to NumPy Array.
import numpy as np
# Create NumPy 2-D array matrix = np.matrix([[ 5 , 10 , 15 ],[ 20 , 25 , 30 ],[ 35 , 40 , 45 ]])
print ( 'Given Matrix:' ,matrix)
print ( type (matrix))
# Convert numpy matrix to array using A1 resulting_array = matrix.A1
print ( 'After Conversion:' ,resulting_array)
print ( type (resulting_array))
|
Output
Given Matrix: [[ 5 10 15]
[20 25 30]
[35 40 45]]
<class 'numpy.matrix'>
After Conversion: [ 5 10 15 20 25 30 35 40 45]
<class 'numpy.ndarray'>