What is NumPy

NumPy is an Open Source package available for Python 2 and Python 3 that provides:

  • ndarray: a high-performance class to represent multidimensional arrays;
  • vectorized functions that apply to all elements of an array without explicit looping, including linear algebra operations, random number generators and Fourier transforms;
  • efficient loading/saving of arrays to disk and memory-mapped files;
  • interfaces to integrate C, C++ and Fortran codebases.

Introduction to ndarray

The np.array function is the simplest way to create an ndarray:


In [1]:
import numpy as np
a0 = np.array([1.1, 2.2, 3.3])

The np.array function creates instances of the ndarray class:


In [22]:
type(a0)


Out[22]:
numpy.ndarray

All elements of an ndarray are of the same type, which you can inspect as the .dtype property:


In [3]:
a0.dtype


Out[3]:
dtype('float64')

In [24]:
a1 = np.array(range(10))

In [25]:
a1


Out[25]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [26]:
a1.dtype


Out[26]:
dtype('int64')

In [27]:
a1 = np.arange(10)
a1


Out[27]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

NumPy can pick a type automatically, or you can assign one explicitly:


In [34]:
ab = np.array([1, 2, 3], dtype=np.uint8)
ab


Out[34]:
array([1, 2, 3], dtype=uint8)

A sequence of nested sequences with equal lenghts can be used to build a multidimensional array, like this 2D array:


In [11]:
a2 = np.array([[10, 20], [30, 40], [50, 60]])
a2


Out[11]:
array([[10, 20],
       [30, 40],
       [50, 60]])

In [19]:
at = a2.transpose()
at


Out[19]:
array([[10, 99, 50],
       [20, 40, 60]])

Many array operations return views which share the undelying data with the source array. That's the case of the T property and the .transpose() method:


In [20]:
at[0, 1] = 99

In [21]:
a2


Out[21]:
array([[10, 20],
       [99, 40],
       [50, 60]])

You can also use the np.zeros, np.ones and np.identity to build arrays filled with zeros and ones:


In [41]:
np.ones((3, 5))


Out[41]:
array([[ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.]])

In [42]:
np.identity(4)


Out[42]:
array([[ 1.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0.],
       [ 0.,  0.,  1.,  0.],
       [ 0.,  0.,  0.,  1.]])

The np.random module has functions to build arrays filled with random numbers using several distributions:


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


Out[29]:
array([[ 0.89382349,  0.94526305,  0.14999355],
       [ 0.22736863,  0.76230151,  0.75217348]])

Many scalar operations are supported, and are vectorized:


In [30]:
a3 * 10


Out[30]:
array([[ 8.93823487,  9.45263051,  1.49993552],
       [ 2.2736863 ,  7.62301513,  7.52173478]])

In [47]:
a3 > .5


Out[47]:
array([[ True,  True, False],
       [False,  True,  True]], dtype=bool)

Starting with Python 3.5, the @ operator does matrix multiplication (dot product):


In [32]:
a2 @ a3


Out[32]:
array([[  13.48560746,   24.69866077,   16.54340509],
       [  97.5832704 ,  124.0731026 ,   44.93630081],
       [  58.33329213,   93.00124335,   52.63008632]])

If you are using Python 3.4 or older, you must use the .dot() method for the dot product:


In [33]:
a2.dot(a3)


Out[33]:
array([[  13.48560746,   24.69866077,   16.54340509],
       [  97.5832704 ,  124.0731026 ,   44.93630081],
       [  58.33329213,   93.00124335,   52.63008632]])

In [ ]: