How to Convert NumPy Matrix to Array

Convert the NumPy matrix to an array can be done by taking an N-Dimensional array (matrix) and converting it to a single dimension array. There are various ways to transform the matrix to an array in NumPy, for example by using flatten(), ravel() and reshape() functions. In this article, I will explain how to use convert matrix to array in different ways with examples.

1. Quick Examples of Convert Matrix to Array

If you are in a hurry, below are some quick examples of how to convert NumPy matrix to array.


# Below are a quick examples

# Example 1: Convert numpy matrix to array using flatten()
arr2 = arr.flatten()
print(arr2)

# Example 2: Convert NumPy matrix to array using ravel()
arr2 = arr.ravel()
print(arr2)

# Example 3: convert numpy matrix to array with reshape()
arr2 = arr.reshape(-1)
print(arr2)

2. Convert NumPy Matrix to Array using flatten()

We can use numpy.flatten() function to convert the matrix to an array. It takes all N elements of the matrix placed into a single dimension array.

2.1 Syntax of flatten()

Following is the syntax of the numpy.flatten() function.


# Syntax of flatten()
ndarray.flatten(order='C')

Following are the parameters of the flatten() function.

  • order – {‘C’, ‘F’, ‘A’, ‘K’}, optional: ‘C’: means to flatten in row-major using C-style order. ‘F’: means to flatten in column-major (Fortran- style) order. ‘A’: means to flatten in column-major order if a is Fortran contiguous in memory, row-major order otherwise. K’: means to flatten an in the order the elements occur in memory. By default, the ‘C’ index order is used.

2.2 NumPy flatten() Example

The NumPy flatten() function is used to return a copy of the input array, which gets flattened into one-dimensional.


import numpy as np

# Create NumPy 2-D array
arr = np.array([[2,4,6],[8,10,12],[14,16,18]])

# Convert numpy matrix to array using flatten()
arr2 = arr.flatten()
print(arr2)

# Output
# [ 2  4  6  8 10 12 14 16 18]

3. Convert NumPy Matrix to Array using ravel()

The numpy.ravel() functions are used to create a contiguous flattened array. This function returns a flattened one-dimensional array. The returned array will have the same type as that of the input array.

3.1 Syntax of ravel()

Following is the syntax of the numpy.ravel() function.


# Syntax of ravel()
numpy.ravel(arr, order='C')

Following are the parameters of the ravel() function.

  • arr – Input array. The array elements are read in the order specified by the order parameter and packed as a 1-D array.
  • order – {‘C’, ‘F’, ‘A’, ‘K’}, optional to read the elements of a using this index order. ‘C’ means to index the elements in row-major using C-style order. ‘F’: means to index the elements in column-major i.e. using Fortran-like index order. ‘A’: means to read the elements in Fortran-like index order if a is Fortran contiguous in memory, C-like order otherwise. ‘K’ means to read the elements in the order they occur in memory, except for reversing the data when strides are negative. By default, the ‘C’ index order is used.

3.2 NumPy ravel() Example

It returns the 1-dimensional array containing all the elements of the input array with shape (a.size ()).


# Convert NumPy matrix to array using ravel()
arr2 = arr.ravel()
print(arr2)

Yields the same output as above.

4. Convert NumPy Matrix to Array with reshape()

We can also use reshape() function to convert the matrix to array. We can use np.reshape(arr,-1), Use -1 as the shape, so that it will convert the array of any shape to a flat array. Follow the below example to convert a multi-dimension array to a 1-D array.


# convert numpy matrix to array with reshape()
arr2 = arr.reshape(-1)
print(arr2)

Yields the same output as above.

5. Conclusion

In this article, I have explained how to use convert matrix to array in different ways with examples. Also learned how to convert a matrix to an array by using NumPy flatten(), ravel(), and reshape() function.

Happy Learning!!

Related Artices

References

Leave a Reply

You are currently viewing How to Convert NumPy Matrix to Array