Representing Data with NumPy
In this lesson you will learn how to use NumPy to represent large, multi-dimensional arrays and matrices.
Introduction to NumPy
Sometimes, we need a data structure called arrays to represent a group of the same data type together. For this purpose, in Python, we can either use a list or use NumPy arrays. NumPy is a library for the Python programming language that adds support for large, multi-dimensional arrays and matrices, along with an extensive collection of high-level mathematical functions to operate on these arrays. To use NumPy, you will need to import it, as shown in the code snippet below.
import numpy as npprint(np.__version__)
NumPy arrays
You can create an uninitialized array using the empty(shape, type)
function. Alternatively, you can create an array of zeros or ones by using the zeros(shape, type)
or ones(shape, type)
function, respectively.
For creating a one-dimensional array, the argument shape
has to be equal to the length of the array.
import numpy as np#creating an uninitialised arrayarr = np.empty((2), dtype = int)print("Empty array:", arr)#creating an array of zerosarr1 = np.zeros((3), dtype = int)print("Array of zeros:", arr1)#creating an array of onesarr2 = np.ones((7), dtype = int)print("Array of ones:", arr2)
For creating a two-dimensional or ...