Numpy is a package for scientific computing in Python. It is blazing fast due to its implementation in C.
You can install numpy via pip
pip install numpy
In the datascience world numpy is often imported like this:
import numpy as np
The “as” keyword defines a so called alias. Now you can use structures from numpy by referencing them with “np” instaed of the whole name.
The most important data structure is ndarray, which is short for n-dimensional array.
You can convert a list to an numpy array with the array-method
my_list = [1, 2, 3, 4] my_array = np.array(my_list)
You can also convert an array back to a list with
my_new_list = my_array.tolist()
You can retrieve the dimensionality of an array with the ndim property:
and get the number of data points with the shape property
Addition / Subtraction
a = np.array([1, 2, 3, 4]) b = np.array([4, 3, 2, 1]) a + b array([5, 5, 5, 5]) a - b array([-3, -1, 1, 3])
a = np.array([1, 2, 3, 4]) a * 3 array([3, 6, 9, 12])
To see why it is charming to use numpy’s array for this operation You have to consider the alternative:
c = [1,2,3,4] d = [x * 3 for x in c]
a = np.array([1,2,3,4]) b = np.array([4,3,2,1]) a.dot(b) 20 # 1*3 + 2*3 + 3*2 + 4*1
Learn more about numpy: