Python Libraries - NumPy

NumPy

NumPy is the core library for scientific computing in Python. It It adds support for large, multi-dimensional arrays and matrices, along with a large library of high-level mathematical functions to operate on these arrays.

Installation

The easiest way to get NumPy installed is to install one of the Python distirbutions, like Anaconda, which include all of the key packages for the scientific computing stack

Usage

To start using NumPy you need to import it

import numpy as np

You can initialize an array using Python array methods;

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> arr.shape
(10,)
>>> type(arr)
<type 'numpy.ndarray'>
>>> arr[0], arr[9]
(0, 9)
>>> b = np.array([[1,2,3],[4,5,6]])
>>> b.shape
(2, 3)
>>> b[0, 0], b[0, 1], b[1, 0]
(1, 2, 4)

Or you can uyse NumPy’s many functions to create arrays conviniently;

>>> a = np.zeros((2,2))
>>> a
array([[ 0.,  0.],
       [ 0.,  0.]])
>>> b = np.ones((1,2))
>>> b
array([[ 1.,  1.]])
>>> c = np.full((2,2), 7)
>>> c
array([[ 7.,  7.],
       [ 7.,  7.]])
>>> d = np.eye(2)
>>> d
array([[ 1.,  0.],
       [ 0.,  1.]])
>>> e = np.random.random((2,2))
>>> e
array([[ 0.11744039,  0.53292772],
       [ 0.38588565,  0.74553119]])

Indexing in the arrays can be achieved using similar slicing syntax for Python lists;

>>> a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
>>> a
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])
>>> a[:2, 1:3]
array([[2, 3],
       [6, 7]])
>>>

You can pick elements in the arrays using boolean indexing;

>>> a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
>>> a
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])
>>> (a > 3)
array([[False, False, False,  True],
       [ True,  True,  True,  True],
       [ True,  True,  True,  True]], dtype=bool)
>>> a[a > 3]
array([ 4,  5,  6,  7,  8,  9, 10, 11, 12])

Basic array math can be done;

# Elementwise operations
>>> a = np.array([[1,2],[3,4]])
>>> b = np.array([[5,6],[7,8]])
>>> a
array([[1, 2],
       [3, 4]])
>>> b
array([[5, 6],
       [7, 8]])
>>> a + b
array([[ 6,  8],
       [10, 12]])
>>> np.add(a,b)
array([[ 6,  8],
       [10, 12]])
>>> a - b
array([[-4, -4],
       [-4, -4]])
>>> np.subtract(a,b)
array([[-4, -4],
       [-4, -4]])
>>> b/a
array([[5, 3],
       [2, 2]])
>>> np.divide(b,a)
array([[5, 3],
       [2, 2]])


# Vector multiplication
>>> c = np.array([9,10])
>>> d = np.array([11, 12])
>>> c
array([ 9, 10])
>>> d
array([11, 12])
>>> c.dot(d)
219
>>> np.dot(c,d)
219
>>> a.dot(c)
array([29, 67])
>>> np.dot(a,c)
array([29, 67])
>>> a.dot(b)
array([[19, 22],
       [43, 50]])
>>> np.dot(a,b)
array([[19, 22],
       [43, 50]])

There are also many useful functions in NumPy for performing computations on arrays;

>>> a
array([[1, 2],
       [3, 4]])
>>> np.sum(a)
10
>>> np.sum(a, axis=0)
array([4, 6])
>>> np.sum(a, axis=1)
array([3, 7])

Full documentation on how to use NumPy can be dound in their documentation

 
comments powered by Disqus