numpy.reshape() in Python Last Updated : 13 Jan, 2025 Comments Improve Suggest changes Like Article Like Report In Python, numpy.reshape() function is used to give a new shape to an existing NumPy array without changing its data. It is important for manipulating array structures in Python. Let's understand with an example: Python import numpy as np # Creating a 1D NumPy array arr = np.array([1, 2, 3, 4, 5, 6]) # Reshaping the 1D array into a 2D array with 2 rows and 3 columns reshaped_arr = np.reshape(arr, (2, 3)) print(reshaped_arr) Output[[1 2 3] [4 5 6]] Explanation:array arr is reshaped into a 2x3 matrix, where 2 is number of rows and 3 is number of columns.Each element from the original array is rearranged into the new shape while maintaining the order.Table of ContentSyntax of numpy.reshape() :Using -1 to infer a dimensionReshaping with column-major orderSyntax of numpy.reshape() :numpy.reshape(array, shape, order = 'C')Parameters : array : [array_like]Input arrayshape : [int or tuples of int] e.g. The desired shape of the array. If one dimension is -1, the value is inferred from the length of the array and the remaining dimensions.order : [C-contiguous, F-contiguous, A-contiguous; optional] 'C' (default): Row-major order.'F': Column-major order. 'A': Fortran-like index order if the array is Fortran-contiguous; otherwise, C-like order.'K': Keeps the array's order as close to its original as possible.Return Type: Array which is reshaped without changing the data.Using -1 to infer a dimensionIt allows to automatically calculate the dimension that is unspecified as long as the total size of the array remains consistent. Python import numpy as np # Creating a 1D NumPy array arr = np.array([1, 2, 3, 4, 5, 6]) # Reshaping the array into a 2D array # '-1' allows to calculate the number of rows based on the total number of elements reshaped_arr = np.reshape(arr, (-1, 2)) print(reshaped_arr) Output[[1 2] [3 4] [5 6]] Explanation:-1 allows NumPy to automatically calculate the number of rows needed based on the total size and the other given dimension.resulting array has 3 rows and 2 columns, as NumPy calculates the required number of rows.Reshaping with column-major orderWe can specify the order in which the elements are read from the original array and placed into the new shape. Python import numpy as np # Creating a 1D NumPy array arr = np.array([1, 2, 3, 4, 5, 6]) # Reshaping the array into a 2D array with 2 rows and 3 columns reshaped_arr = np.reshape(arr, (2, 3), order='F') print(reshaped_arr) Output[[1 3 5] [2 4 6]] Explanation:order='F' argument reshapes the array in a column-major (Fortran-style) order, meaning the elements are filled by columns instead of rows.The result is a 2x3 matrix where the data is arranged column-wise. Comment More infoAdvertise with us Next Article numpy.reshape() in Python M Mohit Gupta_OMG Improve Article Tags : Python Python-numpy Python numpy-arrayManipulation Practice Tags : python Similar Reads numpy.repeat() in Python The numpy.repeat() function repeats elements of the array - arr. Syntax :Â numpy.repeat(arr, repetitions, axis = None) Parameters :Â array : [array_like]Input array. repetitions : No. of repetitions of each array elements along the given axis. axis : Axis along which we want to repeat values. By def 2 min read Python | Numpy matrix.reshape() With the help of Numpy matrix.reshape() method, we are able to reshape the shape of the given matrix. Remember all elements should be covered after reshaping the given matrix. Syntax : matrix.reshape(shape) Return: new reshaped matrix Example #1 : In the given example we are able to reshape the give 1 min read numpy.ravel() in Python The numpy.ravel() functions returns contiguous flattened array(1D array with all the input-array elements and with the same type as it). A copy is made only if needed. Syntax : numpy.ravel(array, order = 'C')Parameters : array : [array_like]Input array. order : [C-contiguous, F-contiguous, A-contigu 3 min read numpy.take() in Python The numpy.take() function returns elements from array along the mentioned axis and indices. Syntax: numpy.take(array, indices, axis = None, out = None, mode ='raise') Parameters : array : array_like, input array indices : index of the values to be fetched axis : [int, optional] axis over which we ne 2 min read numpy.stack() in Python NumPy is a famous Python library used for working with arrays. One of the important functions of this library is stack(). Important points:stack() is used for joining multiple NumPy arrays. Unlike, concatenate(), it joins arrays along a new axis. It returns a NumPy array.to join 2 arrays, they must 6 min read numpy.roll() in Python The numpy.roll() function rolls array elements along the specified axis. Basically what happens is that elements of the input array are being shifted. If an element is being rolled first to the last position, it is rolled back to the first position. Syntax : numpy.roll(array, shift, axis = None) Par 2 min read numpy.tile() in Python The numpy.tile() function constructs a new array by repeating array - 'arr', the number of times we want to repeat as per repetitions. The resulted array will have dimensions max(arr.ndim, repetitions) where, repetitions is the length of repetitions. If arr.ndim > repetitions, reps is promoted to 3 min read numpy.squeeze() in Python The numpy.squeeze() is a useful Python function, which is utilized for the removal of single-dimensional elements from the shape of a NumPy array. It comes in very handy when you have to discard redundant dimensions (like a dimension with size 1) after operations that introduce extra dimensions.Basi 3 min read Python | Numpy matrix.resize() With the help of Numpy matrix.resize() method, we are able to resize the shape of the given matrix. Remember all elements should be covered after resizing the given matrix. Syntax : matrix.resize(shape) Return: new resized matrix Example #1 : In the given example we are able to resize the given matr 1 min read numpy.rot90() in Python The numpy.rot90() method performs rotation of an array by 90 degrees in the plane specified by axis(0 or 1). Syntax: numpy.rot90(array, k = 1, axes = (0, 1)) Parameters : array : [array_like]i.e. array having two or more dimensions. k : [optional , int]No. of times we wish to rotate array by 90 degr 2 min read Like