NumPy

NumPy is a python library that makes it easy to perform mathematical and logical operations on arrays.

What is my numpy version

import numpy as np
print (np.__version__)

output

1.9.3

Creating numpy arrays

There are many ways to create numpy arrays

# create arrays
avalues = [12, 34, 45]
a = np.array(avalues)
b = np.array([11.1,22.2,33.3])

# 2D array, matrix
c = np.array([(1, 2, 3), (4 , 5, 6)])

# print results
print('a = ', a)              
print('b = ', b)               
print(c)

output

a =  [12 34 45]
b =  [ 11.1  22.2  33.3]
[[1 2 3]
 [4 5 6]] 

Using arange() function to create arrays

d = np.arange(5)              # create array of 5 numbers starting from 0
e = np.arange(9)+1            # create array of 9 numbers starting from 1
f = np.arange(10, 20)         # array of numbers 10 to 19
g = np.arange(10, 31, 5)      # array 10 to 30 with 5 increments
h = len(np.arange(10,15))     # get size of array
i = np.arange(10, 20).size    # get size of array
j = np.arange(9).reshape(3,3) # create 2D array

# print results
print('d = ', d)
print('e = ', e)
print('f = ', f)
print('g = ', g)
print('h = ', h)
print('i = ', i)
print(j)

output

d =  [0 1 2 3 4]
e =  [1 2 3 4 5 6 7 8 9]
f =  [10 11 12 13 14 15 16 17 18 19]
g =  [10 15 20 25 30]
h =  5
i =  10
[[0 1 2]
 [3 4 5]
 [6 7 8]]

Using numpy function linspace()

d = np.linspace(10, 20, 5)                # create elements with 2.5 intervals
e = np.linspace(10, 20, 5, retstep=True)  # also shows the interval size
print(d)
print(e)
print(e[1])                               # get the interval size

output

[ 10.   12.5  15.   17.5  20. ]
(array([ 10. ,  12.5,  15. ,  17.5,  20. ]), 2.5)
2.5

Using zeros() and ones() functions

a = np.zeros(4)               # array of 4 zero elements
b = np.ones(4)                # array of 4 one elements
c = np.ones(5, dtype='int64') # create int64 array
d = np.zeros((4,3))           # 4 x 3 matrix of zeros
e = np.zeros((2,3,4))         # Two 3 x 4 matrices of zeros
print(a, "\n", b, "\n", c, "\n-----\n", d, "\n-----\n", e)

output

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

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

Array Type

A numpy array can only have one type. To find the type of an array,

print(a.dtype)         # int64
print(b.dtype)         # float64

output

type of a =  int64
type of b =  float64

Since every element of a numpy array must have the same data type, in the following example, int and float elements will be converted to complex numbers:

tpl = (10, -1.23, 2+3j)
c = np.array(tpl)
print(c)

output

[ 10.00+0.j  -1.23+0.j   2.00+3.j]

Using arrays

Working with 1D array

a = np.array([-3, -2, 0, 1, 2])
print(a)
print(a[1])
a[2] = 100
print(a)
print(a.size)

output

[-3 -2  0  1  2]
-2
[ -3  -2 100   1   2]
5

Working with 2D array

b = np.arange(21)
b.shape = (3, 7)
print(b)
print('-----')
print(b[2])
print(b[2,1])
print(b[2][1])

output

[[ 0  1  2  3  4  5  6]
 [ 7  8  9 10 11 12 13]
 [14 15 16 17 18 19 20]]
-----
[14 15 16 17 18 19 20]
15
15

Working with 3D array

c = np.arange(27)
c.shape = (3,3,3)
print(c)
print('-----')
print(c[0])
print('-----')
print(c[0,1])
print('-----')
print(c[0,1,2])
print('-----')
c[0,1,2] = 999
print(c)

output

[[[ 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]]]
-----
[[0 1 2]
 [3 4 5]
 [6 7 8]]
-----
[3 4 5]
-----
5
-----
[[[  0   1   2]
  [  3   4 999]
  [  6   7   8]]

 [[  9  10  11]
  [ 12  13  14]
  [ 15  16  17]]

 [[ 18  19  20]
  [ 21  22  23]
  [ 24  25  26]]]

Arithmetic operations on arrays

Note that numpy overloads operators operate differently on python arrays and numpy arrays.

# subtract all values
print(a - b)           # [  0.9  11.8  11.7]
# cube all values
print(a**3)            # [ 1728 39304 91125]
print(2 * np.cos(a))   # [  1367.631  10941.048  36926.037]
print(a < 30)          # [ True False False]
# multiply values
print(a * b)           # [  133.2   754.8  1498.5]
# matrix product
print(a.dot(b))        # 2386.5

Output is in the code comments.