October 2019
Beginner to intermediate
674 pages
16h 43m
English
Several operations on arrays call for the creation of arrays or matrices with ones and zeros. Some special functions in NumPy provide for easy creation of such arrays. Usually, these functions take in the shape of the resultant array as an input argument in the form of a tuple:
# Creating an array of ones with shape (2, 3, 4)In [27]: np.ones((2, 3, 4))Out [27]:array([[[1., 1., 1., 1.],[1., 1., 1., 1.],[1., 1., 1., 1.]],[[1., 1., 1., 1.],[1., 1., 1., 1.],[1., 1., 1., 1.]]])# Creating an array of zeros with shape (2, 1, 3)In [28]: np.zeros((2, 1, 3))Out [28]:array([[[0., 0., 0.]],[[0., 0., 0.]]])
The identity function returns a 2D n x n square matrix, where n is the order of the matrix passed as an input argument:
Read now
Unlock full access