numpy & matplotlib Introduction

Abhishek Gupta, Artificial Intelligence Club, DA-IICT

version 0.1, Released 25/1/2014

This work is licensed under a GNU General Public License, version 2

What is numpy?

NumPy is the fundamental package for scientific computing in Python. It is a Python library that provides a multidimensional array object, various derived objects (such as masked arrays and matrices), and an assortment of routines for fast operations on arrays, including mathematical, logical, shape manipulation, sorting, selecting, I/O, discrete Fourier transforms, basic linear algebra, basic statistical operations, random simulation and much more.

At the core of the NumPy package, is the ndarray object. This encapsulates n-dimensional arrays of homogeneous data types, with many operations being performed in compiled code for performance. There are several important differences between NumPy arrays and the standard Python sequences:

  • NumPy arrays have a fixed size at creation, unlike Python lists (which can grow dynamically). Changing the size of an ndarray will create a new array and delete the original.
  • The elements in a NumPy array are all required to be of the same data type, and thus will be the same size in memory. The exception: one can have arrays of (Python, including NumPy) objects, thereby allowing for arrays of different sized elements.
  • NumPy arrays facilitate advanced mathematical and other types of operations on large numbers of data. Typically, such operations are executed more efficiently and with less code than is possible using Python’s built-in sequences.
  • A growing plethora of scientific and mathematical Python-based packages are using NumPy arrays; though these typically support Python-sequence input, they convert such input to NumPy arrays prior to processing, and they often output NumPy arrays. In other words, in order to efficiently use much (perhaps even most) of today’s scientific/mathematical Python-based software, just knowing how to use Python’s built-in sequence types is insufficient - one also needs to know how to use NumPy arrays.

Benefits of numpy:

  • NumPy's arrays are more compact than Python lists. Access in reading and writing items is also faster with NumPy. Maybe you don't care that much for just a million cells, but you definitely would for a billion cells -- neither approach would fit in a 32-bit architecture, but with 64-bit builds NumPy would get away with 4 GB or so, Python alone would need at least about 12 GB (lots of pointers which double in size) -- a much costlier piece of hardware!

    The difference is mostly due to "indirectness" -- a Python list is an array of pointers to Python objects, at least 4 bytes per pointer plus 16 bytes for even the smallest Python object (4 for type pointer, 4 for reference count, 4 for value -- and the memory allocators rounds up to 16). A NumPy array is an array of uniform values -- single-precision numbers takes 4 bytes each, double-precision ones, 8 bytes. Less flexible, but you pay substantially for the flexibility of standard Python lists!

  • Numpy is not just more efficient, it is also more convenient. You get a lot of vector and matrix operations for free, which sometimes allow one to avoid unnecessary work. And they are also efficiently implemented.

    Functionality: You get a lot built in with Numpy, FFTs, convolutions, fast searching, basic statistics, linear algebra, histograms, etc. And really, who can live without FFTs?

Introduction

Basic numpy


In [2]:
from numpy import *

In [108]:
a = array((1,2,3,4)) #What is (1,2,3,4)

array?

print 'type:', type(a)
print 'dtype', a.dtype
print 'itemsize', a.itemsize
print 'shape', a.shape
print 'size', a.size
print 'len', len(a)
print 'nbytes', a.nbytes
print 'ndim', a.ndim
print a


 type: <type 'numpy.ndarray'>
dtype int64
itemsize 8
shape (4,)
size 4
len 4
nbytes 32
ndim 1
[1 2 3 4]

By default operations are element-wise.


In [7]:
a + 1


Out[7]:
array([2, 3, 4, 5])

In [9]:
b = a + 1

print a + b
print a ** b


[3 5 7 9]
[   1    8   81 1024]

In [20]:
print sin(a)


[ 0.84147098  0.90929743  0.14112001 -0.7568025 ]

range() counterpart to numpy:


In [10]:
arange(11)


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

In [11]:
arange(1,11,2)


Out[11]:
array([1, 3, 5, 7, 9])

In [17]:
linspace(2,20,10)


Out[17]:
array([  2.,   4.,   6.,   8.,  10.,  12.,  14.,  16.,  18.,  20.])

In [19]:
linspace(0.1,0.1254,10)


Out[19]:
array([ 0.1       ,  0.10282222,  0.10564444,  0.10846667,  0.11128889,
        0.11411111,  0.11693333,  0.11975556,  0.12257778,  0.1254    ])

Setting Array:


In [109]:
a[2] = 10

print a


[ 1  2 10  4]

In [110]:
a.fill(0)
print a


[0 0 0 0]

In [111]:
a.fill(-4.8)
print a


[-4 -4 -4 -4]

Matplotlib Introduction


In [24]:
from matplotlib import pyplot as plt

%matplotlib inline

In [25]:
plt.plot(a,b)


Out[25]:
[<matplotlib.lines.Line2D at 0x426f210>]

In [32]:
x = linspace(0,20,100)

plt.plot(x,sin(x))
plt.grid()



In [33]:
plt.plot(x, sin(x), x, sin(2*x))


Out[33]:
[<matplotlib.lines.Line2D at 0x4693c10>,
 <matplotlib.lines.Line2D at 0x4693e50>]

In [35]:
plt.plot(x, sin(x), 'r-^')


Out[35]:
[<matplotlib.lines.Line2D at 0x42a3e10>]

In [36]:
plt.plot?

In [61]:
N = 70
a = random.rand(N)
b = random.rand(N)
area = pi * (15 * random.rand(N))**2  # 0 to 15 point radiuses

plt.scatter(a, b, s=area, alpha=0.5)


Out[61]:
<matplotlib.collections.PathCollection at 0x5446f90>

In [64]:
plt.plot(a, b, 'bo', markersize=20, alpha=0.5)


Out[64]:
[<matplotlib.lines.Line2D at 0x685e050>]

Multiple Figures:


In [68]:
fig1 = plt.figure()
plt.plot(a)
fig2 = plt.figure()
plt.plot(b)


Out[68]:
[<matplotlib.lines.Line2D at 0x68bbe90>]

In [69]:
plt.subplot(2,1,1)
plt.plot(a)
plt.subplot(2,1,2)
plt.plot(b)


Out[69]:
[<matplotlib.lines.Line2D at 0x6cca890>]

By default hold is true.


In [71]:
plt.plot(a)
plt.plot(b)


Out[71]:
[<matplotlib.lines.Line2D at 0x6678f50>]

In [77]:
plt.plot(a)
plt.hold(False)
plt.plot(b)

plt.hold(True);



In [85]:
plt.plot(sin(x))
plt.xlabel('radians')
plt.ylabel('amplitude', fontsize='large')
plt.title('Sin(x)')
plt.show()



In [83]:
plt.plot(sin(x), label='sin(x)')
plt.plot(cos(x), label='cos(x)')
plt.legend()


Out[83]:
<matplotlib.legend.Legend at 0x77f4710>

In [96]:
from scipy.misc import lena

img = lena()
plt.imshow(img, cmap=plt.get_cmap('gray'))
plt.colorbar()


Out[96]:
<matplotlib.colorbar.Colorbar instance at 0x7dc5998>

In [100]:
plt.hist(random.randn(1000))


Out[100]:
(array([  3,  22,  82, 184, 276, 232, 137,  48,  14,   2]),
 array([-3.43971854, -2.73178275, -2.02384697, -1.31591118, -0.6079754 ,
        0.09996039,  0.80789617,  1.51583196,  2.22376774,  2.93170353,
        3.63963931]),
 <a list of 10 Patch objects>)

In [101]:
plt.hist(random.randn(1000),30)


Out[101]:
(array([ 1,  1,  0,  3,  5,  8,  8, 25, 24, 29, 44, 70, 73, 85, 75, 84, 87,
       80, 79, 64, 44, 35, 22, 25, 12,  9,  4,  1,  0,  3]),
 array([-3.3569654 , -3.14026842, -2.92357144, -2.70687446, -2.49017748,
       -2.2734805 , -2.05678352, -1.84008654, -1.62338957, -1.40669259,
       -1.18999561, -0.97329863, -0.75660165, -0.53990467, -0.32320769,
       -0.10651071,  0.11018627,  0.32688325,  0.54358023,  0.76027721,
        0.97697419,  1.19367117,  1.41036814,  1.62706512,  1.8437621 ,
        2.06045908,  2.27715606,  2.49385304,  2.71055002,  2.927247  ,
        3.14394398]),
 <a list of 30 Patch objects>)

In [106]:
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

theta = linspace(-4 * pi, 4 * pi, 100)
z = linspace(-2, 2, 100)
r = z**2 + 1
x = r * sin(theta)
y = r * cos(theta)
ax.plot(x, y, z, label='parametric curve')
ax.legend()

plt.tight_layout()


Numpy

Slicing

Slicing on python lists return new list (by copy), but slicing on numpy return reference/view (by reference)


In [113]:
a = linspace(0,10,10)

print a[-2:]


[  8.88888889  10.        ]

Multi-dimensional Array


In [115]:
am = array([[1,2,3,4],
            [5,6,7,8]])
print 'type:', type(am)
print 'dtype', am.dtype
print 'itemsize', am.itemsize
print 'shape', am.shape
print 'size', am.size
print 'len', len(am)
print 'nbytes', am.nbytes
print 'ndim', am.ndim
print am


type: <type 'numpy.ndarray'>
dtype int64
itemsize 8
shape (2, 4)
size 8
len 2
nbytes 64
ndim 2
[[1 2 3 4]
 [5 6 7 8]]

In [119]:
am.shape = (4,-1)
print am


[[1 2]
 [3 4]
 [5 6]
 [7 8]]

In [121]:
am = arange(50).reshape(-1, 5)
print am


[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]
 [20 21 22 23 24]
 [25 26 27 28 29]
 [30 31 32 33 34]
 [35 36 37 38 39]
 [40 41 42 43 44]
 [45 46 47 48 49]]

Array from/to file

Example Data Format:

----Beginning of file
%Day, Month, Year, skip, Avg, Power
01, 01, 2000, x836, 13 %crazy day
%we don't have jan 2nd
02, 01, 2000, xfed, 55


In [125]:
#Python's way
#file = open('data.txt')
with open('data.txt') as file:
    data = []
    for line in file:
        fields = line.split()
        raw_data = [float(x) for x in fields]
        data.append(raw_data)
    data = array(data)


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-125-c71a67ca9ff5> in <module>()
      5     for line in file:
      6         fields = line.split()
----> 7         raw_data = [float(x) for x in fields]
      8         data.append(raw_data)
      9     data = array(data)

ValueError: could not convert string to float: ----Beginning

In [156]:
dat = loadtxt('data.txt', skiprows=1, dtype=int, delimiter=',', usecols=(0,1,2,4), comments='%')
print dat


[[   1    1 2000   13]
 [   2    1 2000   55]]

In [157]:
savetxt('data2.txt', dat)

2-D Array Indexing

Slicing


In [132]:
a = arange(0, 40).reshape(4,-1)
print a


[[ 0  1  2  3  4  5  6  7  8  9]
 [10 11 12 13 14 15 16 17 18 19]
 [20 21 22 23 24 25 26 27 28 29]
 [30 31 32 33 34 35 36 37 38 39]]

In [131]:
a[0, 3:8]


Out[131]:
array([3, 4, 5, 6, 7])

In [133]:
a[1:,5:]


Out[133]:
array([[15, 16, 17, 18, 19],
       [25, 26, 27, 28, 29],
       [35, 36, 37, 38, 39]])

In [134]:
a[:,2]


Out[134]:
array([ 2, 12, 22, 32])

In [135]:
a[1,:]


Out[135]:
array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])

Fancy Indexing


In [154]:
a = arange(0,80,10)
print a

indices = [1,2,3]
print a[indices]


[ 0 10 20 30 40 50 60 70]
[10 20 30]

In [162]:
mask = array([0,1,1,0,0,1,0,0], dtype=bool)
print mask

print a[mask]


[False  True  True False False  True False False]
[10 20 50]

In [164]:
mask2 = a < 30
print mask2

print a[mask2]


[ True  True  True False False False False False]
[ 0 10 20]

Multi-dimensional


In [169]:
a = arange(40).reshape(5,-1)
print a


[[ 0  1  2  3  4  5  6  7]
 [ 8  9 10 11 12 13 14 15]
 [16 17 18 19 20 21 22 23]
 [24 25 26 27 28 29 30 31]
 [32 33 34 35 36 37 38 39]]
[ 1 10 19 28 37]

In [170]:
print a[(0,1,2,3,4), (1,2,3,4,5)]


[ 1 10 19 28 37]

In [171]:
print a[3:, [0, 2, 5]]


[[24 26 29]
 [32 34 37]]

In [173]:
a[2,mask]


Out[173]:
array([17, 18, 21])

Unlike slicing Fancy Indexing creates copy. Do you knwo why? - Strides(reshape, slice, transpose)

What is the difference between [(),()] and [[]/:, []/:]?

Incomplete Indexing

No need to mention columns if you want all the columns.


In [149]:
a = arange(36).reshape(-1,6)
a[:3]


Out[149]:
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17]])

In [151]:
a[a[:,4]>10]


Out[151]:
array([[12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34, 35]])

When to use this?


In [178]:
print a
print where(a>10)


[ 4 12 20 28 36]
[[ 0  1  2  3  4  5  6  7]
 [ 8  9 10 11 12 13 14 15]
 [16 17 18 19 20 21 22 23]
 [24 25 26 27 28 29 30 31]
 [32 33 34 35 36 37 38 39]]
(array([1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4,
       4, 4, 4, 4, 4, 4]), array([3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 0, 1,
       2, 3, 4, 5, 6, 7]))

Newaxis


In [179]:
a = arange(12)
b = a[:,newaxis]
print b


[[ 0]
 [ 1]
 [ 2]
 [ 3]
 [ 4]
 [ 5]
 [ 6]
 [ 7]
 [ 8]
 [ 9]
 [10]
 [11]]

In [181]:
c = a[newaxis,:]
print a


[ 0  1  2  3  4  5  6  7  8  9 10 11]

In [183]:
d = a[:, newaxis, newaxis]
print d


[[[ 0]]

 [[ 1]]

 [[ 2]]

 [[ 3]]

 [[ 4]]

 [[ 5]]

 [[ 6]]

 [[ 7]]

 [[ 8]]

 [[ 9]]

 [[10]]

 [[11]]]

Flattening Arrays


In [185]:
a = arange(4).reshape(2,2)
print a


[[0 1]
 [2 3]]

In [189]:
b = a.flatten() #it returns new copy (of array)
print b


[0 1 2 3]
151857184 154811312 151575264

In [190]:
c = a.flat #an iterator object that access data in multi-dim as 1-D (akin pass by reference)
print c


<numpy.flatiter object at 0x94a0e90>

(Un)raveling Arrays

Same as flatten() but returns reference if possible.


In [193]:
print a
b = a.ravel() #pass by ?
print b


[[0 1]
 [2 3]]
[0 1 2 3]

In [194]:
c = a.T.ravel() #pass  by ?
print c


[0 2 1 3]

Squeezing Arrays


In [196]:
a = arange(40).reshape(4,1,1,-1)
print a


[[[[ 0  1  2  3  4  5  6  7  8  9]]]


 [[[10 11 12 13 14 15 16 17 18 19]]]


 [[[20 21 22 23 24 25 26 27 28 29]]]


 [[[30 31 32 33 34 35 36 37 38 39]]]]

In [197]:
print a.shape
print a.squeeze().shape


(4, 1, 1, 10)
(4, 10)

Diagonals


In [200]:
a = array([[11,21,31],
           [12,22,32],
           [13,23,33]])
print a.diagonal()


[11 22 33]

In [201]:
print a.diagonal(offset=1)
print a.diagonal(offset=-1)


[21 32]
[12 23]

In [203]:
i = 0,1,2
print a[i,i]


[11 22 33]

In [206]:
i= array([0,1])
print a[i,i+1]
print a[i+1,i]


[21 32]
[12 23]

What is the need of ndarray in second i?

Complex Numbers


In [207]:
a = array([1+1j, 3,2,1j,4])
print a


[ 1.+1.j  3.+0.j  2.+0.j  0.+1.j  4.+0.j]

In [208]:
print a.real
print a.imag


[ 1.  3.  2.  0.  4.]
[ 1.  0.  0.  1.  0.]

In [209]:
a.imag = [1,2,3,4,2]
print a


[ 1.+1.j  3.+2.j  2.+3.j  0.+4.j  4.+2.j]

In [211]:
print a.conj()


[ 1.-1.j  3.-2.j  2.-3.j  0.-4.j  4.-2.j]

For float and other arrays real and imag are available but you can't set the imag. Its only readable.

Array Calculation Methods

Basic Operations


In [214]:
a = arange(30).reshape(3,-1)
print a


[[ 0  1  2  3  4  5  6  7  8  9]
 [10 11 12 13 14 15 16 17 18 19]
 [20 21 22 23 24 25 26 27 28 29]]

In [215]:
print a.sum()
print a.sum(axis=0)


435
[30 33 36 39 42 45 48 51 54 57]

In [217]:
print a.prod()
print a.prod(axis=1)


0
[             0   335221286400 72684900288000]

In [219]:
print a.min(axis=0)
print amin(a, axis=0)


[0 1 2 3 4 5 6 7 8 9]
[0 1 2 3 4 5 6 7 8 9]

In [220]:
print a.max(axis=0)
print amax(a, axis=0)


[20 21 22 23 24 25 26 27 28 29]
[20 21 22 23 24 25 26 27 28 29]

Which to use??


In [222]:
print a.argmin(axis=0)
print a.argmax(axis=0)


[0 0 0 0 0 0 0 0 0 0]
[2 2 2 2 2 2 2 2 2 2]

Statistical Methods


In [223]:
print a.mean(axis=0)


[ 10.  11.  12.  13.  14.  15.  16.  17.  18.  19.]

In [224]:
print average(a,axis=0)


[ 10.  11.  12.  13.  14.  15.  16.  17.  18.  19.]

In [232]:
print average(a,weights=[4,1,2],axis=0)


[  7.14285714   8.14285714   9.14285714  10.14285714  11.14285714
  12.14285714  13.14285714  14.14285714  15.14285714  16.14285714]

In [233]:
a.std(axis=0)


Out[233]:
array([ 8.16496581,  8.16496581,  8.16496581,  8.16496581,  8.16496581,
        8.16496581,  8.16496581,  8.16496581,  8.16496581,  8.16496581])

In [234]:
a.var(axis=0)


Out[234]:
array([ 66.66666667,  66.66666667,  66.66666667,  66.66666667,
        66.66666667,  66.66666667,  66.66666667,  66.66666667,
        66.66666667,  66.66666667])

More Methods


In [237]:
b = a.clip(3,20) #set all values less than 3 to 3 and greater than 20 to 5
print a
print b


[[ 0  1  2  3  4  5  6  7  8  9]
 [10 11 12 13 14 15 16 17 18 19]
 [20 21 22 23 24 25 26 27 28 29]]
[[ 3  3  3  3  4  5  6  7  8  9]
 [10 11 12 13 14 15 16 17 18 19]
 [20 20 20 20 20 20 20 20 20 20]]

In [240]:
b = random.randn(10)
print b
print b.round()


[-1.61401004  1.34924326  0.60005574  0.50997615  0.08032236  0.21116167
  0.44219768 -1.30551562  0.23036981 -0.89972937]
[-2.  1.  1.  1.  0.  0.  0. -1.  0. -1.]

In [241]:
b.round(decimals=1)


Out[241]:
array([-1.6,  1.3,  0.6,  0.5,  0.1,  0.2,  0.4, -1.3,  0.2, -0.9])

Search/Sort


In [248]:
print a.nonzero()


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

In [260]:
b= random.randn(3,5).round()
print b

b.sort(axis=1)
print b


[[-2.  0. -2. -2.  0.]
 [ 0. -1. -2.  1. -2.]
 [ 1.  2.  0. -1. -0.]]
[[-2. -2. -2.  0.  0.]
 [-2. -2. -1.  0.  1.]
 [-1.  0. -0.  1.  2.]]

In [265]:
a[0].searchsorted(5)


Out[265]:
5

Array Creation


In [266]:
print arange(5, 100, 5, dtype=float)


[  5.  10.  15.  20.  25.  30.  35.  40.  45.  50.  55.  60.  65.  70.  75.
  80.  85.  90.  95.]

In [267]:
print ones((2,3), dtype=int)


[[1 1 1]
 [1 1 1]]

In [268]:
print zeros((2,3), dtype=float)


[[ 0.  0.  0.]
 [ 0.  0.  0.]]

In [269]:
print identity(3, dtype=short)


[[1 0 0]
 [0 1 0]
 [0 0 1]]

In [273]:
empty((2,4), dtype=float)


Out[273]:
array([[  0.00000000e+000,   1.56265217e-316,   1.56264980e-316,
          2.01054362e-316],
       [  6.91627452e-310,   1.63041663e-322,   6.91627383e-310,
          6.91627383e-310]])

In [271]:
a.fill(5.)
a[:] = 5.

Which to use??

Mathematical Operations

+, -, *, /, %, **, //


In [295]:
a = arange(4).reshape(2,-1)
b = a.T
print add(a, b, a)


[[0 3]
 [3 6]]

Comaprison and Logical Operators


In [298]:
print a == b
print equal(a,b)


[[ True  True]
 [ True  True]]
[[ True  True]
 [ True  True]]

In [300]:
print a != b
print not_equal(a,b)


[[False False]
 [False False]]
[[False False]
 [False False]]

In [299]:
print a > b
print greater(a,b)


[[False False]
 [False False]]
[[False False]
 [False False]]

Similarily, we have less()/<, greater_equal()/>=, less_equal()/<=.


In [301]:
logical_and(a,b)


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

Similarily, we have logical_or(), logical_not(), logical_xor()


In [303]:
print all(logical_and(a,b))
print any(logical_and(a,b))


False
True

Bitwise Operations

`bitwise_and(), bitwise_or(), bitwise_xor(), invert(), right_shift(), left_shift()`

In [308]:
print (a>1) & (b>1)


[[False  True]
 [ True  True]]

Matrix Objects


In [275]:
m = mat('1,2,3;4,5,6;7,8,9') #matlab style
print m


[[1 2 3]
 [4 5 6]
 [7 8 9]]

In [276]:
m * m


Out[276]:
matrix([[ 30,  36,  42],
        [ 66,  81,  96],
        [102, 126, 150]])

In [277]:
m**4


Out[277]:
matrix([[ 7560,  9288, 11016],
        [17118, 21033, 24948],
        [26676, 32778, 38880]])

All are matrix operations, not element-wise


In [282]:
ma = mat(arange(4).reshape(2,-1))
mb = ma.T
print ma
print mb


[[0 1]
 [2 3]]
[[0 2]
 [1 3]]

In [285]:
mBlock = bmat('ma,mb;mb,ma')
print mBlock


[[0 1 0 2]
 [2 3 1 3]
 [0 2 0 1]
 [1 3 2 3]]

Universal Function Methods

op : {mathematical, comparative, logical, bitwise}

op.reduce(a, axis=0): Reduce 1-D array to single value by applying op.
op.accumulate(a, axis=0): Create new array contain intermediate result of the op.reduce at each element.
op.outer(a, b): Forms all possible combinations of elements in a and b using the op.
op.reduceat(a, indicies): Reduce at specified indicies(1-D). For M-D, it always applicable on last axis.


In [309]:
a = arange(30).reshape(3,-1)
print a


[[ 0  1  2  3  4  5  6  7  8  9]
 [10 11 12 13 14 15 16 17 18 19]
 [20 21 22 23 24 25 26 27 28 29]]

In [313]:
add.reduce(a,axis=0)


Out[313]:
array([30, 33, 36, 39, 42, 45, 48, 51, 54, 57])

In [315]:
add.accumulate(a,axis=1)


Out[315]:
array([[  0,   1,   3,   6,  10,  15,  21,  28,  36,  45],
       [ 10,  21,  33,  46,  60,  75,  91, 108, 126, 145],
       [ 20,  41,  63,  86, 110, 135, 161, 188, 216, 245]])

In [319]:
add.outer(a[0:2,0:2], a[0:2,1:3])


Out[319]:
array([[[[ 1,  2],
         [11, 12]],

        [[ 2,  3],
         [12, 13]]],


       [[[11, 12],
         [21, 22]],

        [[12, 13],
         [22, 23]]]])

In [322]:
add.reduceat(a[0],[2,5,6])


Out[322]:
array([ 9,  5, 30])

Array Functions

choose()


In [328]:
#y = choose(choose_array, (option1, option2, option3))
a = array([[1,11,20],
           [21,2,12],
           [10,3,22]])
choice_array = array([[0,1,2],
                      [0,2,1],
                      [2,0,1]])
print choose(choice_array, (a, [-1,-2,-3], 100))


[[  1  -2 100]
 [ 21 100  -3]
 [100   3  -3]]

In [329]:
#Use case
print choose((a<10) + 2*(a>15), (a,10,15))


[[10 11 15]
 [15 10 12]
 [10 10 15]]

concatenate()

Except the axis mentioned all other axis must have same size


In [331]:
x = array([[0,1],[10,11]])
y = x + 5
print x
print y


[[ 0  1]
 [10 11]]
[[ 5  6]
 [15 16]]

In [338]:
cat = concatenate((x,y))
print cat
print cat.shape


[[ 0  1]
 [10 11]
 [ 5  6]
 [15 16]]
(4, 2)

In [337]:
cat = concatenate((x,y),axis=1)
print cat
print cat.shape


[[ 0  1  5  6]
 [10 11 15 16]]
(2, 4)

In [336]:
cat = array((x,y))
print cat
print cat.shape


[[[ 0  1]
  [10 11]]

 [[ 5  6]
  [15 16]]]
(2, 2, 2)

References

Version


In [339]:
import sys
print sys.version


2.7.4 (default, Sep 26 2013, 03:20:26) 
[GCC 4.7.3]