Introduction to NumPy

numpy is a powerful set of tools to perform mathematical operations of on lists of numbers. It works faster than normal python lists operations and can manupilate high dimentional arrays too.

Additional material:

Import NumPy


In [ ]:
import numpy as np

Why NumPy?

Python has powerful data types. Why do we need another?

Generate a list


In [ ]:
# Regular Python
%timeit python_list_1 = list(range(1, 1000))

python_list_1 = list(range(1, 1000))
python_list_2 = list(range(1, 1000))

In [ ]:
#Numpy
%timeit numpy_list_1 = np.arange(1, 1000)

numpy_list_1 = np.arange(1, 1000)
numpy_list_2 = np.arange(1, 1000)

Basic Operations


In [ ]:
%%timeit
# Regular Python
[(x + y) for x, y in zip(python_list_1, python_list_2)]
[(x - y) for x, y in zip(python_list_1, python_list_2)]
[(x * y) for x, y in zip(python_list_1, python_list_2)]
[(x / y) for x, y in zip(python_list_1, python_list_2)]

In [ ]:
%%timeit
#Numpy
numpy_list_1 + numpy_list_2
numpy_list_1 - numpy_list_2
numpy_list_1 * numpy_list_2
numpy_list_1 / numpy_list_2

np.array

Numpy Arrays are central to Numpy. They ca be created in several ways.
np.arange([start,] stop[, step,], dtype=None)


In [ ]:
np.arange(10)

In [ ]:
np.arange(3, 10)

In [ ]:
np.arange(1, 10, 0.5)

In [ ]:
np.arange(1, 10, 2, dtype=np.float64)

np.array(object, dtype=None, ...)


In [ ]:
np.array([1,2,3])

In [ ]:
np.array([1,2,3], dtype=np.float64)

In [ ]:
a = np.array([[1, 1], [1, 1]])
a

In [ ]:
a = np.array([[[1, 1], [1, 1]], [[1, 1], [1, 1]]])
a

In [ ]:
a.shape

np.linspace(start, stop, num=50, dtype=None, ...)


In [ ]:
np.linspace(1, 10)

In [ ]:
np.linspace(1, 10, num=12)

Examing np arrays


In [ ]:
ds = np.arange(1, 10, 2)
ds.ndim

In [ ]:
ds.shape

In [ ]:
ds.size

In [ ]:
len(ds)

In [ ]:
ds.dtype

In [ ]:
ds

Common methods

zeros(shape, dtype=float, ...)


In [ ]:
np.zeros((3, 4))

In [ ]:
np.zeros((3, 4), dtype=np.int64)

np.linspace(start, stop, num=50, endpoint=True)


In [ ]:
np.linspace(1, 5)

In [ ]:
np.linspace(0, 2, num=4)

In [ ]:
np.linspace(0, 2, num=4, endpoint=False)

random_sample(size=None)


In [ ]:
np.random.random((2,3))

In [ ]:
np.random.choice(np.arange(10), 3)

Statistics


In [ ]:
data_set = np.random.random((3,4))
data_set

In [ ]:
np.max(data_set)

In [ ]:
np.max(data_set, axis=0)

In [ ]:
np.max(data_set, axis=1)

In [ ]:
np.min(data_set)

In [ ]:
np.mean(data_set)

In [ ]:
np.median(data_set)

In [ ]:
np.std(data_set)

In [ ]:
np.sum(data_set)

In [ ]:
np.argmax(data_set)

In [ ]:
np.argmin(data_set)

In [ ]:
np.argsort(data_set)

Reshaping


In [ ]:
np.reshape(data_set, (4, 3))

In [ ]:
np.reshape(data_set, (12, 1))

In [ ]:
np.reshape(data_set, (12))

In [ ]:
np.ravel(data_set)

Slicing


In [ ]:
data_set = np.random.randint(0, 10, size=(5, 10))
data_set

In [ ]:
data_set[1]

In [ ]:
data_set[1][0]

In [ ]:
data_set[1, 0]

Range


In [ ]:
data_set[2:4]

In [ ]:
data_set[2:4, 0]

In [ ]:
data_set[2:4, 0:2]

In [ ]:
data_set[:,0]

Stepping


In [ ]:
data_set[2:4:1]

In [ ]:
data_set[::]

In [ ]:
data_set[::2]

In [ ]:
data_set[2:4,::2]

In [ ]:
data_set[2:4,::-2]