Flatten Specific Dimensions of NumPy Array Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like 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 ArrayUsing reshape() functionUsing reshape() with shape[] functionUsing numpy.vstack()Using reshape() functionThe 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: Using reshape() with shape[] functionThe 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: 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.]] Comment More infoAdvertise with us Next Article Flatten Specific Dimensions of NumPy Array I ishita28rai Follow Improve Article Tags : Python Geeks Premier League Numpy Python-numpy Geeks Premier League 2023 +1 More Practice Tags : python Similar Reads Change the dimension of a NumPy array Let's discuss how to change the dimensions of an array. In NumPy, this can be achieved in many ways. Let's discuss each of them. Method #1: Using Shape() Syntax : array_name.shape()Python3 # importing numpy import numpy as np def main(): # initialising array print('Initialised array') gfg = np.arra 3 min read Python slicing multi-dimensional arrays Python's NumPy package makes slicing multi-dimensional arrays a valuable tool for data manipulation and analysis. It enables efficient subset data extraction and manipulation from arrays, making it a useful skill for any programmer, engineer, or data scientist.Python Slicing Multi-Dimensional Arrays 4 min read Creating a one-dimensional NumPy array One-dimensional array contains elements only in one dimension. In other words, the shape of the NumPy array should contain only one value in the tuple. We can create a 1-D array in NumPy using the array() function, which converts a Python list or iterable object. Pythonimport numpy as np # Create a 2 min read Indexing Multi-dimensional arrays in Python using NumPy In this article, we will cover the Indexing of Multi-dimensional arrays in Python using NumPy. NumPy is a general-purpose array-processing package. It provides a high-performance multidimensional array object and tools for working with these arrays. It is the fundamental package for scientific compu 3 min read Numpy - Iterating Over Arrays NumPy provides flexible and efficient ways to iterate over arrays of any dimensionality. For a one-dimensional array, iterating is straightforward and similar to iterating over a Python list.Let's understand with the help of an example:Pythonimport numpy as np # Create a 1D array arr = np.array([1, 3 min read Like