Enhance your career prospects with our Data Science TrainingKNOW MORE
Numpy is a module in Python that refers to Numerical Python. It is an essential package when working with data that is fed as input to machine learning algorithms. It is considered to be a general-purpose package that is used for quick array computations. They contain array data structure, which has advantages in comparison to plain Python lists.
The numpy package can be installed using the ‘pip’ command.
pip insall numpy
The numpy package can be imported and given an alias name so that it is easy to reference the package.
import numpy as np
Numpy stores multidimensional array object that helps in efficient storage and computation of arrays and matrix data types. There are a wide number of mathematical functions which can be used to manipulate these arrays in a short span of time.
import numpy as np my_list = [1,2,6,7,8,0] my_np_array = np.array(my_list) print(my_np_array)
[1 2 6 7 8 0]
The row and column size have to be passed as parameters to the ones function present in the numpy package.
import numpy as np my_np_array = np.ones((3,3)) print(my_np_array)
[[1. 1. 1.] [1. 1. 1.] [1. 1. 1.]]
The empty function in numpy package can be used to create an empty numpy array.
import numpy as np my_np_array = np.empty((3,2)) print(my_np_array)
[[0.e+000 0.e+000] [0.e+000 5.e-324] [5.e-324 5.e-324]]
The zeroes function can be used to create multi-dimensional array, as well as providing the data type of the values.
import numpy as np my_np_array = np.zeros((2,4,3),dtype=np.int16) print(my_np_array)
[[[0 0 0] [0 0 0] [0 0 0] [0 0 0]] [[0 0 0] [0 0 0] [0 0 0] [0 0 0]]]
The attributes of the numpy array needs to be known to the user so that it is easy for them to manipulate such an array. Different functions like shape, data, dtype, can be used to know more about the numpy array.
Note: The np.eye function is used to create an identity matrix or a matrix that has horizontal row values of 1.
import numpy as np my_np_array = np.eye(3,3) print(my_np_array) print(my_np_array.ndim) print(my_np_array.size) print(my_np_array.flags) print(my_np_array.itemsize) print(my_np_array.nbytes)
[[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]] 2 9 C_CONTIGUOUS : True F_CONTIGUOUS : False OWNDATA : True WRITEABLE : True ALIGNED : True WRITEBACKIFCOPY : False UPDATEIFCOPY : False 8
In this post, we understood the significance of numpy package and we saw a few functions associated with the Numpy package.