How to Create a 2D NumPy Array in Python [6 Methods]

In this NumPy blog, I will explain how to create a 2D NumPy array in Python using various functions with some illustrative examples. I will also explain how to check if the array is of 2nd dimension or not, and what is its shape and size.

To create a 2D NumPy array in Python, you can utilize various methods provided by the NumPy library. Use np.array with a list of lists for custom values, np.zeros or np.ones for arrays of zeros or ones respectively, np.full to fill with a specific value, np.arange combined with np.reshape for sequential values in a 2D format, and np.random for arrays with random numbers.

Methods to create a 2D NumPy array in Python

There are six different methods to create a 2D NumPy array in Python:

  1. Using np.array
  2. Using np.zeros
  3. Using np.ones
  4. Using np.full
  5. Using np.arange with np.reshape
  6. Using np.random

Let’s see them one by one using some illustrative examples:

Before this, we will see the process we need to follow to confirm that whatever we have created is a 2D NumPy array in Python.

import numpy as npImports the NumPy library and renames it as ‘np’.
print(type(array_name))Outputs the data type of the variable ‘array_name’ (e.g., NumPy array).
print(np.ndim(array_name))Prints the number of dimensions (e.g., 1D, 2D) of ‘array_name’.
print(array_name.shape)Shows the dimensions of the array ‘array_name’ (e.g., (rows, columns)).
print(array_name.size)Displays the total number of elements in the array ‘array_name’.
List of functions needed to check if the created array is a 2D array or not.

Method 1: np 2d array in Python with the np.array() function.

The np.array() function creates a 2D array by passing a list of lists, allowing for manual specification of array contents in Python. For instance:

import numpy as np

Data = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
print("The array is:\n", Data)
print(type(Data))
print(np.ndim(Data))
print(Data.size)
print(Data.shape)

Output: This will create a 2D array in Python with two rows and four columns.

The array is:
 [[1 2 3 4]
 [5 6 7 8]]
<class 'numpy.ndarray'>
2
8
(2, 4)
create a 2D NumPy array in Python

We can create a 2D NumPy array in Python by manually specifying array contents using np.array.

READ:  How to use Python Django pre_save

Method 2: Create a 2d NumPy array using np.zeros() function

The np.zeros() function in NumPy Python generates a 2D array filled entirely with zeros, useful for initializing arrays with a specific shape and size. For example:

import numpy as np

Data = np.zeros((2, 3))
print("The array is:\n", Data)
print(type(Data))
print(np.ndim(Data))
print(Data.size)
print(Data.shape)

Output: This code creates a 2×3 array filled with zeros through Python NumPy.

The array is:
 [[0. 0. 0.]
 [0. 0. 0.]]
<class 'numpy.ndarray'>
2
6
(2, 3)
2d array numpy python

This way, to create a 2D NumPy array in Python, we can use the np.zeros function.

Method 3: NumPy 2D array initialize using np.ones() function

The np.ones() function creates a 2D array in Python where all elements are ones, handy for initializing arrays of a specific shape with a default value of one.

import numpy as np

Data = np.ones((2, 3))
print("The array is:\n", Data)
print(type(Data))
print(np.ndim(Data))
print(Data.size)
print(Data.shape)

Output: This creates a 2×3 array filled with ones in Python.

The array is:
 [[1. 1. 1.]
 [1. 1. 1.]]
<class 'numpy.ndarray'>
2
6
(2, 3)
numpy create 2d array in Python

We can create a 2D NumPy array in Python using the np.ones function.

Method 4: How to create a 2d NumPy array using np.full() function

The np.full() function forms a 2D array filled with a specified value, providing control over the initial content of the array in Python.

import numpy as np

Data = np.full((2, 3), 7)
print("The array is:\n", Data)
print(type(Data))
print(np.ndim(Data))
print(Data.size)
print(Data.shape)

Output: This creates a 2×3 array where each element is 7 through Python.

The array is:
 [[7 7 7]
 [7 7 7]]
<class 'numpy.ndarray'>
2
6
(2, 3)
create 2d numpy array in Python

This way, we can create a 2D NumPy array in Python using np.full function.

Method 5: NumPy arange 2D array using np.arange with np.reshape

The np.arange with np.reshape function creates a 1D array with a range of numbers and reshapes it into a 2D array in Python NumPy, offering a way to generate sequential data and format it as needed.

import numpy as np

Data = np.arange(6).reshape(2, 3)
print("The array is:\n", Data)
print(type(Data))
print(np.ndim(Data))
print(Data.size)
print(Data.shape)

Output: This code creates a 2×3 array with values from 0 to 5 in Python.

The array is:
 [[0 1 2]
 [3 4 5]]
<class 'numpy.ndarray'>
2
6
(2, 3)
how to create a 2d array in numpy in Python

We can create a 2D NumPy array in Python, using the np.arange with np.reshape function.

READ:  Python NumPy Matrix Operations

Method 6: 2D array using numpy.random function

The np.random() function generates a 2D array with random values, ideal for simulations or when random initial data is required.

import numpy as np

Data = np.random.random((2, 3))
print("The array is:\n", Data)
print(type(Data))
print(np.ndim(Data))
print(Data.size)
print(Data.shape)

Output: This creates a 2×3 array with random floating-point numbers through Python.

The array is:
 [[0.29021833 0.2852124  0.78524396]
 [0.88524634 0.80241381 0.22616022]]
<class 'numpy.ndarray'>
2
6
(2, 3)
initialize 2d array python numpy

This way we can create a 2D NumPy array in Python using np.random function.

Conclusion

This article explains how to create a 2D NumPy array in Python using six different methods with examples. Whether we need to manually specify array contents using np.array, initialize arrays with zeros using np.zeros, ones using np.ones, or a specific value using np.full, generate sequential data using np.arange with np.reshape, or create arrays with random elements using np.random, NumPy provides a straightforward and efficient solution.

Understanding these methods is essential for anyone working in data science, scientific computing, or any field that requires efficient and effective numerical computation in Python.

You may also like to read: