The Wayback Machine - https://web.archive.org/web/20240526073159/https://www.geeksforgeeks.org/flatten-specific-dimensions-of-numpy-array/
Open In App

Flatten Specific Dimensions of NumPy Array

Last Updated : 13 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 Array

  • Using reshape() function
  • Using reshape() with shape[] function
  • Using numpy.vstack()

Using reshape() function

The function used to change the shape of an array, i.e., changing the number of elements in each dimension is known as the reshape function(). In this way, we will see how we can flatten some dimensions of the Numpy array using the reshape function.

Python3




import numpy as np
arr = np.zeros((5, 2, 5))
flattened_arr = arr.reshape(10, 5)
print(flattened_arr)


Output:

Screenshot-2023-09-18-134110

Using reshape() with shape[] function

The function which is used to give a new shape to an array without changing its data is known as reshape function(), while shape function is used to calculate the number of elements in each dimension of an array. In this way, we will see how we can flatten some dimensions of numpy array using reshape along with shape[] function.

Python3




import numpy as np
arr = np.zeros((5, 2, 5))
flattened_arr = arr.reshape(-2, arr.shape[-2])
print(flattened_arr)


Output:

Screenshot-2023-09-18-135741

Using numpy.vstack()

numpy.vstack() function is used to stack the sequence of input arrays vertically to make a single array.Here this way, we will see how we can flatten some dimensions of numpy array using numpy.vstack() function.

Python




import numpy as np
arr = np.zeros((3, 1, 4))
print(arr)
new_arr = np.vstack(arr)
print(new_arr)


Output:

[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]


Similar Reads

Python | Flatten a 2d numpy array into 1d array
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() C/C++ Code # 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(
2 min read
Accessing Data Along Multiple Dimensions Arrays in Python Numpy
NumPy (Numerical Python) is a Python library that comprises of multidimensional arrays and numerous functions to perform various mathematical and logical operations on them. NumPy also consists of various functions to perform linear algebra operations and generate random numbers. NumPy is often used along with packages like SciPy and Matplotlib for
3 min read
How to get the number of dimensions of a matrix using NumPy in Python?
In this article, we will discuss how to get the number of dimensions of a matrix using NumPy. It can be found using the ndim parameter of the ndarray() method. Syntax: no_of_dimensions = numpy.ndarray.ndim Approach: Create an n-dimensional matrix using the NumPy package.Use ndim attribute available with the NumPy array as numpy_array_name.ndim to g
3 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
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
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
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