Numpy Overview

This tutorial gives a quick overview of the numpy arrays and its features

  • Creating arrays and initializing
  • Reading arrays from files
  • Special initializing functions
  • Slicing and indexing
  • reshaping arrays
  • Numpy Maths
  • Combining arrays
  • Basic algebraic operations using numpy arrays
    • Solving linear equations
    • Matix inversions
    • Calculating eigen vectors

Importing numpy library

In [2]:
import numpy as np

Creating one dimensional numpy arrays and initializing

In [3]:
## Create one dimensional array
a = np.array([1, 2, 3])
a
Out[3]:
array([1, 2, 3])
In [4]:
## Find the typbe of the object
type(a)
Out[4]:
numpy.ndarray

Numpy Arrays - dtypes & shapes

In [5]:
## Find the dimension of the array
a.shape
Out[5]:
(3,)
In [7]:
a.dtype
Out[7]:
dtype('int32')
In [10]:
print( a[1] )
2

Creating two dimensional numpy arrays and initializing

In [9]:
# Create a two dimensional array
b = np.array([[1,2,3],[4,5,6]])   # Create a rank 2 array
print( b )
[[1 2 3]
[4 5 6]]
In [70]:
print( b.shape )
(2, 3)
In [71]:
print( b[0, :] )
[1 2 3]
In [72]:
print( b[:, 1] )
[2 5]
In [73]:
b
Out[73]:
array([[1, 2, 3],
     [4, 5, 6]])
In [74]:
b[:]
Out[74]:
array([[1, 2, 3],
     [4, 5, 6]])

Reading an array from a file

In [28]:
f = np.loadtxt("narray.txt", delimiter=',')
In [30]:
f
Out[30]:
array([[ 2.,  4.,  6.,  7.],
     [ 1.,  2.,  3.,  5.],
     [ 3.,  2.,  7.,  8.],
     [ 1.,  5.,  6.,  4.]])
In [31]:
f.dtype
Out[31]:
dtype('float64')
In [32]:
?np.loadtxt
In [34]:
f = np.loadtxt("narray.txt", delimiter=',', dtype = "int")
In [36]:
f
Out[36]:
array([[2, 4, 6, 7],
     [1, 2, 3, 5],
     [3, 2, 7, 8],
     [1, 5, 6, 4]])
In [37]:
f.dtype
Out[37]:
dtype('int32')

Special Initializing functions

In [75]:
a = np.zeros((2,2))  # Create an array of all zeros
a
Out[75]:
array([[ 0.,  0.],
     [ 0.,  0.]])
In [76]:
b = np.ones((1,2)) # Create an array of all ones
b
Out[76]:
array([[ 1.,  1.]])
In [38]:
d = np.eye(3)
In [39]:
d
Out[39]:
array([[ 1.,  0.,  0.],
     [ 0.,  1.,  0.],
     [ 0.,  0.,  1.]])
In [85]:
c = np.full((2,2), 7)
In [86]:
c
Out[86]:
array([[ 7.,  7.],
     [ 7.,  7.]])
In [87]:
e = np.random.random((2,2) )
In [88]:
e
Out[88]:
array([[ 0.73155607,  0.82598003],
     [ 0.72991034,  0.42626126]])
In [40]:
f = np.random.randint(100, size = (4,4) )
In [73]:
f
Out[73]:
array([[75, 35, 16, 57],
     [34, 43, 73, 55],
     [ 5,  1, 44, 14],
     [95, 80, 51, 93]])

Slicing & Indexing an array

In [91]:
# Get first row
f[0,:]
Out[91]:
array([0, 2, 2, 2])
In [92]:
# Get 1 and 2 row
f[0:2,:]
Out[92]:
array([[0, 2, 2, 2],
     [6, 3, 6, 0]])
In [93]:
f[:, 1]
Out[93]:
array([2, 3, 7, 7])
In [94]:
# Get first column
f[:,0]
Out[94]:
array([0, 6, 9, 1])
In [95]:
f[:,0:2]
Out[95]:
array([[0, 2],
     [6, 3],
     [9, 7],
     [1, 7]])
In [96]:
## slicing an array 
## All 
b = f[:2, 1:3]
In [53]:
b
Out[53]:
array([[4, 2],
     [3, 1]])
In [54]:
## Get specific elements
np.array([f[0,1], f[2,2]])
Out[54]:
array([4, 9])
In [55]:
## Boolean indexing
f>2
Out[55]:
array([[ True,  True, False,  True],
     [ True,  True, False, False],
     [False, False,  True,  True],
     [ True,  True, False, False]], dtype=bool)
In [56]:
g = f[f>2]
In [57]:
g
Out[57]:
array([3, 4, 3, 3, 3, 9, 6, 8, 4])

Reshaping an array

In [76]:
k = np.reshape( f, ( 8,2 ) )
In [77]:
k
Out[77]:
array([[75, 35],
     [16, 57],
     [34, 43],
     [73, 55],
     [ 5,  1],
     [44, 14],
     [95, 80],
     [51, 93]])

Updating an array

In [80]:
k[7,1] = 0
In [82]:
k
Out[82]:
array([[75, 35],
     [16, 57],
     [34, 43],
     [73, 55],
     [ 5,  1],
     [44, 14],
     [95, 80],
     [51,  0]])
In [84]:
k[5:] = 0
In [85]:
k
Out[85]:
array([[75, 35],
     [16, 57],
     [34, 43],
     [73, 55],
     [ 5,  1],
     [ 0,  0],
     [ 0,  0],
     [ 0,  0]])

Numpy Maths

Adding, substracting, multiplying, tranposing arrays

In [43]:
x = np.random.randint( 100, size = (5,5) )
y = np.random.randint( 100, size = (5,5) )
In [44]:
x
Out[44]:
array([[75,  4, 46, 31, 59],
     [95, 86, 80,  4, 62],
     [ 1, 90, 24, 88, 97],
     [29, 41,  3, 85, 84],
     [34, 49, 59,  3, 83]])
In [45]:
y
Out[45]:
array([[73,  6, 71, 45, 55],
     [13,  1, 55, 35, 18],
     [66, 62, 68, 28, 16],
     [65, 66, 95, 90, 83],
     [18, 66, 87, 54, 22]])
In [46]:
## Add two matrices x + y or np.add( x, y )
x + y
Out[46]:
array([[148,  10, 117,  76, 114],
     [108,  87, 135,  39,  80],
     [ 67, 152,  92, 116, 113],
     [ 94, 107,  98, 175, 167],
     [ 52, 115, 146,  57, 105]])
In [47]:
np.add( x, y )
Out[47]:
array([[148,  10, 117,  76, 114],
     [108,  87, 135,  39,  80],
     [ 67, 152,  92, 116, 113],
     [ 94, 107,  98, 175, 167],
     [ 52, 115, 146,  57, 105]])
In [48]:
# np.substract( x, y )
x - y
Out[48]:
array([[  2,  -2, -25, -14,   4],
     [ 82,  85,  25, -31,  44],
     [-65,  28, -44,  60,  81],
     [-36, -25, -92,  -5,   1],
     [ 16, -17, -28, -51,  61]])
In [49]:
# np.multiply( x, y )
x * y
Out[49]:
array([[5475,   24, 3266, 1395, 3245],
     [1235,   86, 4400,  140, 1116],
     [  66, 5580, 1632, 2464, 1552],
     [1885, 2706,  285, 7650, 6972],
     [ 612, 3234, 5133,  162, 1826]])
In [50]:
# Matrix Transpose
x.T
Out[50]:
array([[75, 95,  1, 29, 34],
     [ 4, 86, 90, 41, 49],
     [46, 80, 24,  3, 59],
     [31,  4, 88, 85,  3],
     [59, 62, 97, 84, 83]])

Calculating column sums and row sums

In [63]:
x = np.random.randint( 10, size = (4,4) )
In [64]:
x
Out[64]:
array([[7, 2, 7, 7],
     [6, 1, 8, 4],
     [6, 6, 7, 8],
     [7, 8, 1, 9]])
In [65]:
np.sum( x )
Out[65]:
94
In [66]:
np.sum( x, axis = 0 )
Out[66]:
array([26, 17, 23, 28])
In [67]:
np.sum( x, axis = 1 )
Out[67]:
array([23, 19, 27, 25])
In [68]:
np.mean( x, axis = 0 )
Out[68]:
array([ 6.5 ,  4.25,  5.75,  7.  ])

Combine Arrays

In [87]:
x = np.random.randint( 10, size = (2,2) )
y = np.random.randint( 100, size = (2,2) )
In [88]:
x
Out[88]:
array([[2, 8],
     [9, 2]])
In [89]:
y
Out[89]:
array([[36, 89],
     [96, 61]])
In [91]:
np.vstack( [x, y])
Out[91]:
array([[ 2,  8],
     [ 9,  2],
     [36, 89],
     [96, 61]])
In [92]:
np.hstack([x, y])
Out[92]:
array([[ 2,  8, 36, 89],
     [ 9,  2, 96, 61]])

Linear Algebra.. Advanced Matrix Operation

In [45]:
from numpy import linalg

Solving a set of linear equations

  • 2x + 2y = 5
  • 3x + y = 7
In [46]:
a = np.array([[2,2], [3,1]])
b = np.array([5,7])
x = np.linalg.solve(a, b)
x
Out[46]:
array([ 2.25,  0.25])

Matrix Inversion

In [47]:
a = np.array([[1, 2], [3, 4]])
linalg.inv( a )
Out[47]:
array([[-2. ,  1. ],
     [ 1.5, -0.5]])

Calculating an eigen value and vector for a matrix

In [48]:
m1 = np.diag((1, 2, 3))
m1
Out[48]:
array([[1, 0, 0],
     [0, 2, 0],
     [0, 0, 3]])
In [49]:
eigval, eigvec = linalg.eig( m1 )
In [50]:
eigval
Out[50]:
array([ 1.,  2.,  3.])
In [51]:
eigvec
Out[51]:
array([[ 1.,  0.,  0.],
     [ 0.,  1.,  0.],
     [ 0.,  0.,  1.]])