Saturday, May 7, 2016

NumPy Tutorial

NumPy is a fundamental Python package used for scientific computing. Within the package, there are many useful features including n-dimensional arrays, element-wise operations, broadcasting functions, linear algebra, random number capabilities, etc. This tutorial provides some fundamental examples of those features in NumPy.

Create An Array with NumPy

In [26]:

import numpy as np #import from python libary
a = np.array([1, 2, 3, 4, 5])
print a
print a*2

Out [26]:

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

Create A One-Dimensional Array

In [27]:

a = np.arange(20) # one dimension
print a
print a.shape
print a.ndim
print a.dtype

Out[27]:
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]
(20L,)
1
int32

Create A Two-Dimensional Array

In [28]:

a = np.array([[1, 2, 3], [4, 5, 6]]) # two dimensions
print a 
print a.shape
print a.ndim
print a.dtype

Out[28]:

[[1 2 3]
 [4 5 6]]
(2L, 3L)
2
int32

Create A Three-Dimensional Array

In [29]:

a = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]) # three dimensions
print a
print a.shape
print a.ndim
print a.dtype

Out[29]:
[[[ 1  2  3]
  [ 4  5  6]]

 [[ 7  8  9]
  [10 11 12]]]
(2L, 2L, 3L)
3
int32

How to Generate an Array from a Sequence

In [21]:

print np.arange(20, 30, 2) # return evenly space numbers over a specified interval.(integers)
print np.linspace(0, 1, 20) # return evenly space numbers over a specified interval.(float numbers)
print np.random.rand(5)# returns random numbers between 0 and 1

Out[21]:

[20 22 24 26 28]
[ 0.          0.05263158  0.10526316  0.15789474  0.21052632  0.26315789
  0.31578947  0.36842105  0.42105263  0.47368421  0.52631579  0.57894737
  0.63157895  0.68421053  0.73684211  0.78947368  0.84210526  0.89473684
  0.94736842  1.        ]
[ 0.05481854  0.83857248  0.31190602  0.30903261  0.17243025]

Indexing and Slicing

Indexing and Slicing are very important when dealing with vast amounts of data. This allows you to cut down the array and narrow it to the part of the data you want to analyze.

In [39]:

print a
print a[0:2, 0:2, 2]

Out[39]:
[[[ 1  2  3]
  [ 4  5  6]]

 [[ 7  8  9]
  [10 11 12]]]
[[ 3  6]
 [ 9 12]]

Manipulating Array Shape

Array shape manipulation can help when needing to perform operations with other multi-dimensional arrays.

In [54]:

b = np.copy(a)
b.shape = (12L,)
print b

Out[54]:
[ 1  2  3  4  5  6  7  8  9 10 11 12]

Boolean Masking

Boolean Masking can help with cutting down an array given a specific condition. In this case the array "b" is a list of boolean values which are true when the value of a is a multiple of 5. "b" is then masked against "a" to get those values.

In [72]:

a = np.arange(0, 105) + 1
b = a%5==0
print a[b]

Out[72]:
[  5  10  15  20  25  30  35  40  45  50  55  60  65  70  75  80  85  90
  95 100 105]

Element-wise Operations

Element wise operations are used to efficiently perform functional operations against each element of the array.

In [77]:

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print a+b

Out[77]:
[5 7 9]

Matrix Multiplication

In [78]:

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

Out[78]:

array([[ 58,  64],
       [139, 154]])

Logical Operations

In [86]:

c = a>2
d = a>8
print c
print d
print np.logical_and(c, d)

Out[86]:

[[False False  True]
 [ True  True  True]]
[[False False False]
 [False False False]]
[[False False False]
 [False False False]]

Basic Reductions

In [106]:

a.shape = (6L) # One dimension
print a
print np.sum(a)
print np.mean(a)
print np.std(a)
print np.size(a)

Out[106]:

[1 2 3 4 5 6]
21
3.5
1.70782512766
6

In [125]:

a = np.arange(0, 105) + 1
a.shape = (3L, 5L, 7L)
print a
print np.sum(np.sum(a, axis=2), axis=0)

Out[125]:

[[[  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]
  [ 50  51  52  53  54  55  56]
  [ 57  58  59  60  61  62  63]
  [ 64  65  66  67  68  69  70]]

 [[ 71  72  73  74  75  76  77]
  [ 78  79  80  81  82  83  84]
  [ 85  86  87  88  89  90  91]
  [ 92  93  94  95  96  97  98]
  [ 99 100 101 102 103 104 105]]]

array([ 819,  966, 1113, 1260, 1407])

In [126]:

sum(range(1, 8)+range(36, 43)+range(71, 78))

Out[126]:

819

All of these examples shown above are the basics of operation with the NumPy library. It can greatly help a coder with efficiency while working with big data. For more information on NumPy, visit here

No comments:

Post a Comment