A NumPy array is a data structure that can be used to store single or multi-dimensional arrays or matrices. Those arrays can later be used for advanced mathematical operations like matrix operations. As we discussed in our previous article, a NumPy array can be created from a Python list. In Python, we can use numpy.ndarray.shape to know the shape of a NumPy array.
import numpy list1 = [1, 2, 3] list2 = [[1, 2, 3], [4, 5, 6]] list3 = [[1, 2, 3]] array1 = numpy.array(list1) array2 = numpy.array(list2) array3 = numpy.array(list3) print("Array 1: ", array1) print("Shape of Array 1: ", array1.shape) print("Array 2: ", array2) print("Shape of Array 2: ", array2.shape) print("Array 3: ", array3) print("Shape of Array 3: ", array3.shape)
Here, we are first importing the numpy module. After that, we are creating three arrays array1, array2, and array3 from three lists list1, list2, and list3, respectively. Please note that list2 and list3 are list of lists.
After creating the NumPy arrays, we are using numpy.ndarray.shape to know the shape of the NumPy array.
The output of the above program will be like the following:
0 Comments