Open In App

Convert a 1D array to a 2D Numpy array

Last Updated : 08 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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. 

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.

Examples 1:

Convert a 1-D array into a 2-D array of required dimensions (n x m).

Python3




import numpy as np
# 1-D array having elements [1 2 3 4 5 6 7 8]
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
 
# Now we can convert this 1-D array into 2-D in two ways
 
# 1. having dimension 4 x 2
arr1 = arr.reshape(4, 2)
print ('After reshaping having dimension 4x2:')
print (arr1)
print ('\n')
 
# 2. having dimension 2 x 4
arr2 = arr.reshape(2, 4)
print ('After reshaping having dimension 2x4:')
print (arr2)
print ('\n')


Output:

After reshaping having dimension 4x2:
[[1 2]
 [3 4]
 [5 6]
 [7 8]]
 
After reshaping having dimension 2x4:
[[1 2 3 4]
 [5 6 7 8]]

Example 2: 

Let us see an important observation whether we can reshape a 1-D array into any 2-D array.

Python3




import numpy as np
# 1-D array having elements [1 2 3 4 5 6 7 8]
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
 
# Print the 1-D array
print('Before reshaping:')
print(arr)
print('\n')
 
# let us try to convert into 2-D array having dimension 3x3
arr1 = arr.reshape(3, 3)
print('After reshaping having dimension 3x3:')
print(arr1)
print('\n')


Output:

 

This concludes that the number of elements should be equal to the product of dimension i.e. 3×3=9 but total elements = 8;

Example 3: 

Another example is that we can use the reshape method without specifying the exact number for one of the dimensions. Just pass -1 as the value and NumPy will calculate the number.

Python3




import numpy as np
# 1-D array having elements [1 2 3 4 5 6 7 8]
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
 
# Print the 1-D array
print('Before reshaping:')
print(arr)
print('\n')
 
 
arr1 = arr.reshape(2, 2, -1)
print('After reshaping:')
print(arr1)
print('\n')


Output:

Before reshaping:
[1 2 3 4 5 6 7 8]
After reshaping:
[[[1 2]
 [3 4]]
[[5 6]
 [7 8]]]

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]


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads