Numpy is a package for scientific computing in Python.
import numpy as np
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:
my_array.ndim
and get the number of data points with the shape property
my_array.shape
Vector arithmetic
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])
Scalar Multiplication
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]
Dot Product
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
Stay tuned for more algebraic stuff with numpy!