How to Convert NumPy Matrix to Array
Last Updated :
28 Apr, 2025
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:
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.
Python3
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.
Python3
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.
Python3
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.
Python3
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'>
Similar Reads
How to convert NumPy array to list ? This article will guide you through the process of convert a NumPy array to a list in Python, employing various methods and providing detailed examples for better understanding. Convert NumPy Array to List There are various ways to convert NumPy Array to List here we are discussing some generally us
4 min read
How to Convert images to NumPy array? Images are an easier way to represent the working model. In Machine Learning, Python uses the image data in the format of Height, Width, Channel format. i.e. Images are converted into Numpy Array in Height, Width, Channel format. Â In this article we will see How to Convert images to NumPy array? Mod
6 min read
How To Convert Numpy Array To Tensor? We are given a NumPy array, and our task is to convert it into a TensorFlow tensor. This is useful when integrating NumPy-based data with TensorFlow pipelines, which support acceleration using GPU and TPU. For example, converting [[1, 2], [3, 4]] results in a TensorFlow object that looks like: Pytho
2 min read
Convert Python List to numpy Arrays NumPy arrays are more efficient than Python lists, especially for numerical operations on large datasets. NumPy provides two methods for converting a list into an array using numpy.array() and numpy.asarray(). In this article, we'll explore these two methods with examples for converting a list into
4 min read
Convert Numpy Array to Dataframe Converting a NumPy array into a Pandas DataFrame makes our data easier to understand and work with by adding names to rows and columns and giving us tools to clean and organize it.In this article, we will take a look at methods to convert a numpy array to a pandas dataframe. We will be discussing tw
4 min read
How to convert a list and tuple into NumPy arrays? In this article, let's discuss how to convert a list and tuple into arrays using NumPy. NumPy provides various methods to do the same using Python. Example: Input: [3, 4, 5, 6]Output: [3 4 5 6]Explanation: Python list is converted into NumPy ArrayInput: ([8, 4, 6], [1, 2, 3])Output: [[8 4 6] [1 2 3]
2 min read
How to convert NumPy array to dictionary in Python? The following article explains how to convert numpy array to dictionary in Python. Array in Numpy is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In Numpy, number of dimensions of the array is called rank of the array. A tuple of integers givi
3 min read
Convert a NumPy array to an image Converting a NumPy array to an image is a simple way to turn numbers into pictures. A NumPy array holds pixel values, which are just numbers that represent colors. Images, like PNG or JPEG, store these pixel values in a format we can see. In this process, the NumPy array turns into an image, with ea
3 min read
Convert Numpy Array To Xarray Xarray is a powerful Python library for working with labeled multi-dimensional arrays. In Python, NumPy provides basic data structures and APIs for working with raw ND arrays, but, in the real world, the data is more complex, in some cases, which are encoded. The data array maps to positions in spac
3 min read
How to Convert a Dataframe Column to Numpy Array NumPy and Pandas are two powerful libraries in the Python ecosystem for data manipulation and analysis. Converting a DataFrame column to a NumPy array is a common operation when you need to perform array-based operations on the data. In this section, we will explore various methods to achieve this t
2 min read