The Wayback Machine - https://web.archive.org/web/20240609234121/https://www.geeksforgeeks.org/python-flatten-a-2d-numpy-array-into-1d-array/
Open In App

Python | Flatten a 2d numpy array into 1d array

Last Updated : 03 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a 2d numpy array, the task is to flatten a 2d numpy array into a 1d array. Below are a few methods to solve the task.

Method #1 : Using np.flatten() 

Python3




# Python code to demonstrate
# flattening a 2d numpy array
# into 1d array
 
import numpy as np
 
ini_array1 = np.array([[1, 2, 3], [2, 4, 5], [1, 2, 3]])
 
# printing initial arrays
print("initial array", str(ini_array1))
 
# Multiplying arrays
result = ini_array1.flatten()
 
# printing result
print("New resulting array: ", result)


Output:

initial array [[1 2 3]
 [2 4 5]
 [1 2 3]]
New resulting array:  [1 2 3 2 4 5 1 2 3]

Time complexity: O(n), where n is the total number of elements in the 2D numpy array.
Auxiliary space: O(n), as the result array is also of size n. The flatten function returns a flattened 1D array, which is stored in the “result” variable.

Method #2: Using np.ravel() 

Python3




# Python code to demonstrate
# flattening a 2d numpy array
# into 1d array
 
import numpy as np
 
ini_array1 = np.array([[1, 2, 3], [2, 4, 5], [1, 2, 3]])
 
# printing initial arrays
print("initial array", str(ini_array1))
 
# Multiplying arrays
result = ini_array1.ravel()
 
# printing result
print("New resulting array: ", result)


Output:

initial array [[1 2 3]
 [2 4 5]
 [1 2 3]]
New resulting array:  [1 2 3 2 4 5 1 2 3]

Method #3: Using np.reshape() 

Python3




# Python code to demonstrate
# flattening a 2d numpy array
# into 1d array
 
import numpy as np
 
ini_array1 = np.array([[1, 2, 3], [2, 4, 5], [1, 2, 3]])
 
# printing initial arrays
print("initial array", str(ini_array1))
 
# Multiplying arrays
result = ini_array1.reshape([1, 9])
 
# printing result
print("New resulting array: ", result)


Output:

initial array [[1 2 3]
 [2 4 5]
 [1 2 3]]
New resulting array:  [[1 2 3 2 4 5 1 2 3]]

Time Complexity: O(n), where n is the total number of elements in the 2D numpy array.
Auxiliary Space: O(n), as the program creates a new 1D list with the same number of elements as the original 2D array.



Similar Reads

Python | Numpy matrix.flatten()
With the help of Numpy matrix.flatten() method, we are able to flatten of a given matrix and gives output as one dimensional matrix. Syntax : matrix.flatten() Return : Return flatten 1-D matrix Example #1 : In this example we can see that with the help of matrix.flatten() method we are able to flatten a given matrix. # import the important module i
1 min read
Numpy recarray.flatten() function | Python
In numpy, arrays may have a data-types containing fields, analogous to columns in a spreadsheet. An example is [(a, int), (b, float)], where each entry in the array is a pair of (int, float). Normally, these attributes are accessed using dictionary lookups such as arr['a'] and arr['b']. Record arrays allow the fields to be accessed as members of th
3 min read
Numpy MaskedArray.flatten() function | Python
numpy.MaskedArray.flatten() function is used to return a copy of the input masked array collapsed into one dimension. Syntax : numpy.ma.flatten(order='C') Parameters: order : [‘C’, ‘F’, ‘A’, ‘K’, optional] Whether to flatten in C (row-major), Fortran (column-major) order, or preserve the C/Fortran ordering from a. The default is ‘C’. Return : [ nda
2 min read
Flatten a Matrix in Python using NumPy
Let's discuss how to flatten a Matrix using NumPy in Python. By using ndarray.flatten() function we can flatten a matrix to one dimension in python. Syntax:numpy_array.flatten(order='C') order:'C' means to flatten in row-major.'F' means to flatten in column-major.'A' means to flatten in column-major order if a is Fortran contiguous in memory, row-m
1 min read
Numpy ndarray.flatten() function | Python
In this article, we will explore the syntax, definition, and usage of the NumPy `ndarray.flatten()` function. We will provide a comprehensive explanation along with an illustrative example to enhance understanding. numpy.ndarray.flatten() Function Syntax numpy.ndarray.flatten() function returns a copy of the array collapsed into one dimension. Synt
4 min read
Flatten Specific Dimensions of NumPy Array
A general-purpose array-processing package that is used for working with arrays is called NumPy. Do you want to collapse your Numpy array into one dimension? If yes, then you can do so by flattening your Numpy array. In this article, we will see how we can flatten only some dimensions of a Numpy array. Flatten only some dimensions of a NumPy ArrayU
2 min read
Differences between Flatten() and Ravel() Numpy Functions
We have two similar kinds of ways to convert a ndarray to a 1D array of Flatten() and Ravel() Numpy function in Python programming language. Example of Flatten() Numpy function Here, we will create a Numpy array, and then by using flatten function we have changed the element in the flattened 1D NumPy array. C/C++ Code import numpy as np # Create a
3 min read
Flatten A list of NumPy arrays
Prerequisite Differences between Flatten() and Ravel() Numpy Functions, numpy.ravel() in Python, In this article, we will see how we can flatten a list of numpy arrays. NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical fun
3 min read
Python | Ways to flatten a 2D list
Given a 2D list, Write a Python program to convert the given list into a flattened list. Method #1: Using chain.iterable() C/C++ Code # Python code to demonstrate # converting 2d list into 1d list # using chain.from_iterables # import chain from itertools import chain ini_list = [[1, 2, 3], [3, 6, 7], [7, 5, 4]] # printing initial list print (
5 min read
Python | Sort Flatten list of list
The flattening of list of lists has been discussed earlier, but sometimes, in addition to flattening, it is also required to get the string in a sorted manner. Let's discuss certain ways in which this can be done. Method #1 : Using sorted() + list comprehension This idea is similar to flattening a list of list but in addition to it, we add a sorted
7 min read
Practice Tags :