# Indexing and Broadcasting in Numpy¶

Embedding mini-languages in Python has a long tradition. In this section of the tutorial, we will explore some examples of this practice.

The first example we will consider is so-called broadcasting in numpy. It may look shallow at first sight, but it and its associated operations constitute a considerable subset of the array programming language APL.

In [3]:

    import numpy as np

In [4]:

    a = np.arange(4)
    b = np.arange(3) * 10
    print(a)
    print(b)

    [0 1 2 3]
    [ 0 10 20]

In [7]:

    a.reshape(-1, 1).shape

Out[7]:

    (4, 1)

In [10]:

    x = a.reshape(-1, 1) + b
    x

Out[10]:

    array([[ 0, 10, 20],
           [ 1, 11, 21],
           [ 2, 12, 22],
           [ 3, 13, 23]])

In [12]:

    np.sum(x, axis=0)

Out[12]:

    array([ 6, 46, 86])