In this article, we’ll learn about creating a Python NumPy array (1D and 2D). Find the size, dimensions, type, and shape of the Python NumPy array. Also, we’ll find the data type of elements of the NumPy array. Eventually, we’ll include the images of the code as well.
So let’s get right into its implementation.
Python NumPy Array Creation
In our previous article, we discussed the Python NumPy array theoretically. Click here to read it out. In this article, we’ll discuss its implementation practically with proper code.
Let’s create these two arrays:
- Python One-Dimensional NumPy Array
- Python Two-Dimensional NumPy Array
1. Python One-Dimensional NumPy Array
Open the Jupyter Notebook or other IDE and import NumPy.
import numpy as np
We’ll use the numPy array and pass a list to it. See below:
array_1d = np.array([7,8,10]) print(array_1d)
Output
[ 7 8 10]
This is a one-dimensional Python NumPy array that has been created using a Python list.
2. Python Two-Dimensional NumPy Array
The process is the same but in this case, we’ve to pass lists inside the list so the outcome will come as a 2D NumPy array. See below:
array_2d = np.array([[3,2,14],[6,4,3]]) // can give array name of your choice print(array_2d)
Output
Find the Type of Python NumPy Array
Wait a second, the output looks the same as a Python list. It means a list in Python is denoted by square brackets. So, we need to make sure that it’s a NumPy array. To do that, we’ve to use the Python type function. See below:
type(array_1d)
Output
numpy.ndarray
The output specifies that the created array is a NumPy array. We can do the same for 2D arrays as well.
Find the Dimensions of the NumPy Array
Let’s find out if the arrays that we’ve created are one-dimensional, two-dimensional, or multi-dimensional. For that, we’ve to first write the name of the array and then use .dim to get the dimension. See below:
array_1d.ndim
Output
1
The output shows 1 which means the array is one-dimensional. Let’s test it with 2D as well. See below:
array_2d.ndim
Output
2
It shows 2 which means a 2D array.
Find the Size of the Python NumPy Array
Let’s find the length/size of the NumPy array. For that, we need to specify the name of the array and then use .size to get the size. See the below code:
array_1d.size
Output
3
Our 1D array has three items so the returned size is 3 as shown in the output. Let’s test it for the 2D array as well. See below:
array_2d.size
Output
6
In the above section, we can see that our 2D NumPy array has 6 items so the size is 6 as displayed in the above output.
Find the Shape of Python NumPy Array
Finding the shape of an array means how many it has rows or columns. We can use the name of the array and then use .shape to fetch the number of rows and columns. See below:
array_2d.shape
Output
(2, 3)
It means the specified 2D array has 2 rows and 3 columns. It’s specified inside a Python tuple. The first item represents the rows while the second item represents the columns.
Let’s try it with a 1D array as well. See below:
array_1d.shape
Output
(3,)
It indicates that the array is 1 dimensional with a length of 3.
Find the Data Type of Items of NumPy Array
To find the data type of items of the array, we’ve to write the name of array and the use .dtype to fetch the type of items. See below:
array_1d.dtype array_2d.dtype
Output
dtype('int32') dtype('int32')
Both arrays show a datatype of int in 32-bit format as all the items are in integer format. In this way, we can easily find the item’s data type within the array.
Source Code Images
Conclusion
In conclusion, we’ve learned how to create Python NumPy 1D and 2D arrays. Find the size, type, shape, and dimensions of the Python NumPy array. Also, we’ve learned how to find the data type of items of the NumPy array.
Visit our other articles to explore Python Numpy in more detail. Thank you for reading.
Pingback: Python NumPy Functions - Ones, Zeros, Empty Functions - Machine Learning PY