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.
Table of Contents
Methods to create a 2D NumPy array in Python
There are six different methods to create a 2D NumPy array in Python:
- Using np.array
- Using np.zeros
- Using np.ones
- Using np.full
- Using np.arange with np.reshape
- 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 np | Imports 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’. |
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 npData = 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'>28(2, 4)
We can create a 2D NumPy array in Python by manually specifying array contents using np.array.
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 npData = 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'>26(2, 3)
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 npData = 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'>26(2, 3)
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 npData = 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'>26(2, 3)
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 npData = 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'>26(2, 3)
We can create a 2D NumPy array in Python, using the np.arange with np.reshape function.
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 npData = 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'>26(2, 3)
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:
- Python NumPy Array
- Python NumPy zeros
- Python NumPy arange
- How to create an empty matrix in Python
Bijay Kumar
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.