Convert a 1D array to a 2D Numpy array using numpy.reshape

Here, we are using np.reshape to convert a 1D  array to 2 D array. You can divide the number of elements in your array by ncols.

Python3




import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
 
# np.reshape(A,(-1,ncols))
B = np.reshape(arr, (-1, 2))
 
print('2D Numpy array: \n', B)


Output:

2D Numpy array: 
 [[ 1  2]
 [ 3  4]
 [ 5  6]
 [ 7  8]
 [ 9 10]


Convert a 1D array to a 2D Numpy array

Here we will learn how to convert 1D NumPy to 2D NumPy Using two methods. Numpy is a Python package that consists of multidimensional array objects and a collection of operations or routines to perform various operations on the array and processing of the array. 

Similar Reads

Convert a 1D array to a 2D Numpy array using reshape

This package consists of a function called numpy.reshape which is used to convert a 1-D array into a 2-D array of required dimensions (n x m).  This function gives a new required shape without changing the data of the 1-D array....

Convert a 1D array to a 2D Numpy array using numpy.reshape

...