Python numpy matrix to list

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 the 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.

Читайте также:  Can you use xml in html

2.1 Syntax of flatten()

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

 # Syntax of flatten() ndarray.flatten(order='C') 
  • order – , 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') 
  • arr – Input array. The array elements are read in the order specified by the order parameter and packed as a 1-D array.
  • order – , 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.
Читайте также:  Javascript array foreach this

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.

References

You may also like reading:

Источник

Convert Matrix to Array in NumPy

Convert Matrix to Array in NumPy

  1. Use the numpy.flatten() Function to Convert a Matrix to an Array in NumPy
  2. Use the numpy.ravel() Function to Convert a Matrix to an Array in NumPy
  3. Use the numpy.reshape() Function to Convert a Matrix to an Array in NumPy

NumPy has many functions and classes available for performing different operations on matrices.

In this tutorial, we will learn how to convert a matrix to an array in NumPy.

Use the numpy.flatten() Function to Convert a Matrix to an Array in NumPy

The flatten() takes an N-Dimensional array and converts it to a single dimension array.

It works only with ndarray objects.

It can convert a matrix to an array as shown below.

import numpy as np arr = np.array([[1,2,3],[4,5,6],[7,8,9]]) print(arr.flatten()) 

Note that if we work with a matrix type object, we have to use the asarray() function to convert it to an array and then use the flatten() function. It can be done for all the methods.

import numpy as np arr = np.matrix([[1,2,3],[4,5,6],[7,8,9]]) arr_d = (np.asarray(arr)).flatten() print(arr_d) 

Use the numpy.ravel() Function to Convert a Matrix to an Array in NumPy

The ravel() function works exactly like the flatten() function with a few notable differences. Both are used to transform N-Dimensional arrays to single dimension arrays.

However, the ravel() function is a library function and can also work on objects like a list of arrays. The flatten() returns a copy of the original, whereas ravel() always returns a view of the original whenever possible.

In the following code, we will use this function to convert a matrix.

import numpy as np arr = np.array([[1,2,3],[4,5,6],[7,8,9]]) print(arr.ravel()) 

Use the numpy.reshape() Function to Convert a Matrix to an Array in NumPy

The reshape() modified the overall shape of the array without altering its contents. If we assign the new shape of a matrix as -1 , we get a one-dimensional array.

import numpy as np arr = np.array([[1,2,3],[4,5,6],[7,8,9]]) print(arr.reshape(-1)) 

Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.

Источник

Оцените статью