Numpy
(Numerical Python)
Prepared by Dr. Mohammad Abdel-majeed
1
Outline
• Create ndarrays
• Indexing and slicing
– Integer
– Boolean
• Mathematical Operations
• Useful Functions
• Save and load numpy arrays
• Linalg and Scipy
2
Introduction
• Numpy
– Numeric Python
– Fast computation with n-dimensional arrays
– Used for datascience
3
Numpy
• Based around one data structure
ndarray
• n-dimensional array
• Import with import numpy as np
• Usage is [Link](xxx)
4
ndarrays
• One dimensional araay: 5,67,43,76,2,21
a=[Link]([5,67,43,76,2,21])
• Two dimensional array:
a=[Link]([[4,5,8,4],[6,3,2,1],[8,6,4,3]])
5
Create ndarray-Example
import numpy as np
a = [Link]([1, 2, 3]) # Create a rank 1 array
print(type(a)) # Prints "<class '[Link]'>"
print([Link]) # Prints "(3,)"
print(a[0], a[1], a[2]) # Prints "1 2 3"
a[0] = 5 # Change an element of the array
print(a) # Prints "[5, 2, 3]"
b = [Link]([[1,2,3],[4,5,6]]) # Create a rank 2 array
print([Link]) # Prints "(2, 3)"
print(b[0, 0], b[0, 1], b[1, 0]) # Prints "1 2 4"
6
Create ndarray-Example
import numpy as np
a = [Link]((2,2)) # Create an array of all zeros
print(a) # Prints "[[ 0. 0.]
# [ 0. 0.]]"
b = [Link]((1,2)) # Create an array of all ones
print(b) # Prints "[[ 1. 1.]]"
c = [Link]((2,2), 7) # Create a constant array
print(c) # Prints "[[ 7. 7.]
# [ 7. 7.]]"
d = [Link](2) # Create a 2x2 identity matrix
print(d) # Prints "[[ 1. 0.]
# [ 0. 1.]]"
e = [Link]((2,2)) # Create an array filled with random values
print(e) # Might print "[[ 0.91940167 0.08143941]
# [ 0.68744134 0.87236687]]"
7
Create ndarray-Example
import numpy as np
data1 = [6, 7.5, 8, 0, 1]
arr1 = [Link](data1)
print (arr1) #[ 6. 7.5 8. 0. 1. ]
data2 = [[1, 2, 3, 4], [5, 6, 7, 8]]
arr2 = [Link](data2)
print(arr2) #[[1 2 3 4]
#[5 6 7 8]]
print([Link]) #2
print ([Link]) #(2,4)
print ([Link][0]) #2
print ([Link][1]) #4
8
NumPy data types 1
9
Create ndarray-Example
import numpy as np
data1 = [6, 7.5, 8, 0, 1]
arr1 = [Link](data1,dtype = [Link])
print (arr1) #[ True True True False True]
data2 = [[1, 2, 3, 4], [5, 6, 7, 8]]
arr2 = [Link](data2,np.float32)
print(arr2) #[[1. 2. 3. 4.]
#[5. 6. 7. 8.]]
print([Link]) #2
print ([Link]) #(2,4)
print ([Link][0]) #2
print ([Link][1]) #4
print([Link]) #float32
10
Change array data type
import numpy as np
arr = [Link]([3.7, -1.2, -2.6])
print(arr) #[ 3.7 -1.2 -2.6]
print ([Link](np.int32)) #[ 3 -1 -2]
11
Indexing and Slicing
• Indexing can be done using the indexes of
the element that want to be accessed or
using slicing
• Similar to Python lists, numpy arrays can be
sliced.
import numpy as np
arr = [Link](10)
print (arr) #[0 1 2 3 4 5 6 7 8 9]
print (arr[5])#5
print (arr[5:8]) #[5 6 7]
arr[5:8] = 12
print (arr) #[ 0 1 2 3 4 12 12 12 8 9]
12
Indexing and Slicing
• Since arrays may be multidimensional, you
must specify a slice for each dimension of
the array:
• A slice of an array is a view into the same
data, so modifying it will modify the original
array import numpy as np
arr = [Link](10)
arr_slice = arr[5:8]
arr_slice[1] = 12345
print (arr_slice) #[5 12345 7]
print (arr) #[0 1 2 3 4 5 12345 7 8 9]
arr_slice[:] = 64 #[0 1 2 3 4 64 64 64 8 9]
print (arr)
13
2d array
• The data of each row should be between two
square brackets.
import numpy as np
arr2d = [Link] ([[1, 2, 3],
[4, 5, 6], [7, 8, 9]])
print(arr2d[2]) #[7 8 9]
print (arr2d[0][2]) #3
print (arr2d[0, 2]) #3
14
Indexing with slices – 2D Array
(Examples)
import numpy as np
arr2d = [Link]([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print (arr2d) #[[1 2 3]
# [4 5 6]
# [7 8 9]]
print (arr2d[:2]) #[[1 2 3]
# [4 5 6]]
print (arr2d[:2, 1:])#[[2 3]
#[5 6]]
15
Indexing with slices – 2D Array
(Examples)
import numpy as np
arr2d = [Link](
[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
print (arr2d[1, :2])
print (arr2d[2, :1])
print (arr2d[:, :1])
16
Indexing with slices – 2D Array
(Examples)
import numpy as np [4 5]
arr2d = [Link](
[[1, 2, 3],
[4, 5, 6], [7]
[7, 8, 9]])
print (arr2d[1, :2])
print (arr2d[2, :1])
[[1]
print (arr2d[:, :1]) [4]
[7]]
17
Indexing elements in a numpy array
18
Two-
dimensional
array
slicing
19
3d 2x2x2
a=[Link]([
[ 2,4
[3, 1],[4, 3] 3,1
3,3
], 4,3
[
[2, 4],[3, 3]
]
])
20
Indexing
a=[Link]([
[ 2,4
[3, 1],[4, 3] 3,1
3,3
], 4,3
[
[2, 4],[3, 3] a[0]
]
])
21
Indexing
a=[Link]([
[ 2,4
[3, 1],[4, 3] 3,1
3,3
], 4,3
[
[2, 4],[3, 3] a[1]
]
])
22
Indexing
a=[Link]([
[ 2,4
[3, 1],[4, 3] 3,1
3,3
], 4,3
[
[2, 4],[3, 3] a[0][0]
]
])
23
Indexing
a=[Link]([
[ 2,4
[3, 1],[4, 3] 3,1
3,3
], 4,3
[
[2, 4],[3, 3] a[0][0][0]
]
])
24
Indexing
a=[Link]([
[ 2,4
[3, 1],[4, 3] 3,1
3,3
], 4,3
[
[2, 4],[3, 3] a[0][1]
]
])
25
Indexing
a=[Link]([
[ 2,4
[3, 1],[4, 3] 3,1
3,3
], 4,3
[
[2, 4],[3, 3] a[1][0]
]
])
26
Slicing
a=[Link]([
[ 2,4
[3, 1],[4, 3] 3,1
3,3
], 4,3
[
[2, 4],[3, 3] a[0,0,0]
]
])
27
Slicing
a=[Link]([
[ 2,4
[3, 1],[4, 3] 3,1
3,3
], 4,3
[
[2, 4],[3, 3] a[0,1,0]
]
])
28
Slicing
a=[Link]([
[ 2,4
[3, 1],[4, 3] 3,1
3,3
], 4,3
[
[2, 4],[3, 3] a[:,0]
]
])
29
Slicing
a=[Link]([
[ 2,4
[3, 1],[4, 3] 3,1
3,3
], 4,3
[
[2, 4],[3, 3]
a[:,:,0]
] Both slices, both
rows, column 0
])
30
Integer Array Indexing
• Integer array indexing allows you to construct
arbitrary arrays using the data from another
array. Here is an example:
import numpy as np
a = [Link]([[1,2,3], [4,5, 5], [7,8, 9],[10,11,12]])
print(a)
print (a[[1, 3, 0]])
print (a[[-3, -1, -2]])
31
Integer Array Indexing
• Integer array indexing allows you to construct
arbitrary arrays using the data from another
array. Here is an example:
#[[ 1 2 3]
# [ 4 5 5]
# [ 7 8 9]
# [10 11 12]]
import numpy as np #[[ 4 5 5]
a = [Link]([[1,2,3], [4,5, 5], [7,8, 9],[10,11,12]])
# [10 11 12]
print(a) # [ 1 2 3]]
print (a[[1, 3, 0]])
#[[ 4 5 5]
print (a[[-3, -1, -2]])
# [10 11 12]
# [ 7 8 9]]
32
Integer Array Indexing
import numpy as np
a = [Link]([[1,2,3], [4,5, 5], [7,8, 9],[10,11,12]])
print(a[[0, 1, 2], [0, 1, 0]])
print([Link]([a[0, 0], a[1, 1], a[2, 0]]))
print(a[[0, 0], [1, 1]])
print([Link]([a[0, 1], a[0, 1]]))
33
Integer Array Indexing
import numpy as np
a = [Link]([[1,2,3], [4,5, 5], [7,8, 9],[10,11,12]])
print(a[[0, 1, 2], [0, 1, 0]]) #[1 5 7]
print([Link]([a[0, 0], a[1, 1], a[2, 0]]))
#[1 5 7]
print(a[[0, 0], [1, 1]])
#[2 2]
print([Link]([a[0, 1], a[0, 1]]))
#[2 2]
34
Boolean Array Indexing
import numpy as np
a_index= [Link]([True, True,False,False,True])
a = [Link]([5,12,50,33,12])
print(a[a_index]) # [ 15 12 12]]
35
Stacking
• Create an array by stacking numpy arrays.
x = [Link](0,10,2) # x=([0,2,4,6,8])
y = [Link](5) # y=([0,1,2,3,4])
m = [Link]([x,y]) # m=([[0,2,4,6,8],
# [0,1,2,3,4]])
xy = [Link]([x,y]) # xy =([0,2,4,6,8,0,1,2,3,4])
36
Stacking
• Create an array by stacking numpy arrays.
– vstack: Stack along first axis.
– hstack: Stack along second axis.
– dstackL Stack along the third axis.
x = [Link](0,10,2) # x=([0,2,4,6,8])
y = [Link](5) # y=([0,1,2,3,4])
m = [Link]([x,y]) # m=([[0,2,4,6,8],
# [0,1,2,3,4]])
xy = [Link]([x,y]) # xy =([0,2,4,6,8,0,1,2,3,4])
37
Broadcasting/Mathematical
operations
• Basic mathematical functions operate
elementwise on arrays,
– Available both as operator overloads and as
functions in the numpy module
– Numpy operations are usually done on pairs of
arrays on an element-by-element basis
– Between arrays and scalar value and array
For more details and examples
[Link]
38
Elementwise
opwerations/Mathematical
(Between arrays)
[[0 1 2]
import numpy as np [3 4 5]
arr = [Link](9).reshape((3, 3)) [6 7 8]]
print (arr)
print (arr*arr) [[ 0 1 4]
[ 9 16 25]
[36 49 64]]
39
Elementwise operations/mathematical
import numpy as np
arr = [Link](9).reshape((3, 3))
print (arr)
print ([Link](arr))
print ([Link](arr))
[[0 1 2]
[3 4 5]
[6 7 8]]
[[0. 1. 1.41421356]
[1.73205081 2. 2.23606798]
[2.44948974 2.64575131 2.82842712]]
[[1.00000000e+00 2.71828183e+00 7.38905610e+00]
[2.00855369e+01 5.45981500e+01 1.48413159e+02]
[4.03428793e+02 1.09663316e+03 2.98095799e+03]]
40
Elementwise Operations
(scalar)
[[0 1 2]
[3 4 5]
[6 7 8]]
import numpy as np
arr = [Link](9).reshape((3, 3)) [[ 0 3 6]
print (arr) [ 9 12 15]
print (arr * 3) [18 21 24]]
print (arr + 4)
[[ 4 5 6]
[ 7 8 9]
[10 11 12]]
41
Elementwise Operations
(scalar)
import numpy as np
data = [Link](5,dtype=np.int32)
print (data)#[0 1 2 3 4]
print (data * 10)#[ 0 10 20 30 40]
print (data + data)#[0 2 4 6 8]
print (1/((data+1)))#[1. 0.5 0.33333333 0.25 0.2 ]
42
Elementwise Operations??
Statistics(max,min ,sum)
import numpy as np
x = [Link](4)
y = [Link](4)
y = [Link](y,1)
print(x)
print(y)
print([Link](x, y))
print([Link](x,y)) #Equivalent to x + y
print([Link](x))
print([Link](y))
[ 0.10762685 -0.10194233 -0.31373994 0.86389688]
[-0.2 -0.9 -0.4 0.3]
[ 0.10762685 -0.10194233 -0.31373994 0.86389688]
[-0.09237315 -1.00194233 -0.71373994 1.16389688]
0.5558414572185999
[1.11363211 0.90308163 0.73070903 2.37238762]
[-0.2 -0.9 -0.4 0.3]
[-0. -1. -0. 0.] 43
Inner Product
[[0 1 2]
[3 4 5]
[6 7 8]]
import numpy as np
[[0 3 6]
import numpy as np
[1 4 7]
arr = [Link](9).reshape((3, 3))
[2 5 8]]
print (arr)
print (arr.T)
[[ 5 14 23]
print ([Link](arr, arr.T))
[ 14 50 86]
print([Link](arr,arr.T))
[ 23 86 149]]
[[ 5 14 23]
[ 14 50 86]
[ 23 86 149]]
44
Axis operations
• Instead of applying the mathematical
operations on the entire array they can be
done per-row or per-column
– You should specify the axis
– axis=0 applied on each column
– axis=1 applied on each row
45
Axis operations
import numpy as np
arr = [Link]([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
print (arr) #[[0 1 2]
# [3 4 5]
#[6 7 8]]
print ([Link](axis=0)) #[3. 4. 5.]
print ([Link](axis=1)) #[1. 4. 7.]
46
Axis operations/sum
import numpy as np
arr = [Link]([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
print (arr) #[[0 1 2]
#[3 4 5]
#[6 7 8]]
print ([Link](0)) #[ 9 12 15]
print ([Link](1))#[ 3 12 21]
print ([Link](0))#[[ 0 1 2]
#[ 3 5 7]
#[ 9 12 15]]
print ([Link](axis=1))#[[ 0 0 0]
#[ 3 12 60]
#[ 6 42 336]]
47
Relational operators and numpy
import numpy as np
x = [Link](1,9)
y = x>5
print(y)
[False False False False False True True True]
48
Where()
• Where(Condition, if True, If False)
– Returns [Link] array
– Use the name of the array to keep the values the
same
import numpy as np
arr = ([Link](16)).reshape(4,4)
print(arr)
print ([Link](arr > 0.5, 2, -2))
[[0.11305372 0.41972489 0.71758276 0.7024291 ] [[-2 -2 2 2]
[0.28989595 0.71371535 0.58332619 0.69298548] [-2 2 2 2]
[0.48567377 0.00463536 0.57581238 0.27679739] [-2 -2 2 -2]
[0.36887073 0.35191625 0.77679602 0.40723983]] [-2 -2 2 -2]]
49
Where()
• Returns elements chosen from x or y depending
on the condition.
import numpy as np
x = [1,2,3]
y = [10, 20, 30]
condition = [True, False, True]
[Link](condition,x,y) #Output is [ 1 20 3]
50
Relational operators and numpy
(with where)
import numpy as np
x = [Link](1,9)
y = x>5
print(y)
print([Link](y))
print([Link](y)[0])
#use [Link](y)
[False False False False False True True True]
(array([5, 6, 7], dtype=int64),)
[5 6 7]
51
Boolean Arrays
import numpy as np
arr = [Link]([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
print ((arr > 4).sum()) #4
arr = [Link](
[False, False, True, False])
print ([Link]()) #True
print ([Link]()) #False
52
Sorting
import numpy as np
arr = [Link]([[0, 5, 2], [6, 4, 1], [6, 7, 3]])
[Link]()
print (arr)#[[0 2 5]
#[1 4 6]
#[3 6 7]]
arr = [Link]([[0, 5, 2], [6, 4, 1], [6, 7, 3]])
[Link](0)
print (arr) )#[[0 4 1]
# [6 5 2]
# [6 7 3]]
arr = [Link]([[0, 5, 2], [6, 4, 1], [6, 7, 3]])
[Link](1)
print (arr) #[[0 2 5]
# [1 4 6]
# [3 6 7]]
53
Unique
import numpy as np
names = [Link](['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe'])
print ([Link](names)) #['Bob' 'Joe' 'Will']
print (sorted(set(names)))#['Bob' 'Joe' 'Will']
54
numpy.in1d
• Test whether each element of a 1-D array is
also present in a second array.
– Returns a boolean array the same length
as ar1 that is True where an element of ar1 is
in ar2 and False otherwise.
print(np.in1d(ar1,ar2))
import numpy as np
arr = [Link]([[0, 5, 2], [6, 4, 1], [6, 7, 3]])
print(np.in1d([1,5],arr)) #[ True True]
55
Get index
(argsort(),argwhere(),argmax())
• Used when interested in the index of the
elements rather than value
• [Link]() Returns the indices of the
maximum values along an axis.
• [Link]() Returns the indices that would
sort an array.
• [Link]()Find the indices of array
elements that are non-zero, grouped by
element.
56
Get index
(argsort(),argwhere(),argmax())
import numpy as np
x = [Link]([3,6,12,5,3,77,67,43,23,50,77,11,24])
print(x)
print([Link](x))
print([Link](x))
print([Link](x>50))
[ 3 6 12 5 3 77 67 43 23 50 77 11 24]
5
[ 0 4 3 1 11 2 8 12 7 9 6 5 10]
[[ 5]
[ 6]
[10]]
57
Save numpy array
• [Link](): Saves an array to a binary file in
numpy .npy format
– Parameters: file name and numpy array
import numpy as np
arr = [Link](10)
print (arr)#[0 1 2 3 4 5 6 7 8 9]
[Link]('some_array', arr)
arr1 = [Link]('some_array.npy')
print (arr1)#[0 1 2 3 4 5 6 7 8 9]
58
Saving multiple numpy arrays
import numpy as np
arr3 = [Link](3)
arr5 = [Link](5)
[Link]('array_archive.npz', a=arr3, b=arr5)
arch = [Link]('array_archive.npz')
print (type(arch))#<class '[Link]'>
print (arch['a'])#[0 1 2]
print (arch['b'])#[0 1 2 3 4]
print (dict(arch))
#{'a': array([0, 1, 2]), 'b': array([0, 1, 2, 3, 4])}
59
Loading text data into numpy array
import numpy as np
arr1 = [Link]('array_ex.txt', delimiter=',')
print (arr1)#[[ 1. 2. 3. 4.]
#[12. 13. 14. 15.]]
print (type(arr1)) #<class '[Link]'>
60
Loading text data into numpy array
• Using genfromtxt: gives you some options like
the parameters missing_values, filling_values
that can help you dealing with an incomplete
data
fill_values = (111, 222, 333, 444, 555) # one for each column
[Link](filename,delimiter=',',filling_values=fill_values)
1,2,,,5 array([[ 1., 2., 333., 444., 5.],
6,,8,, [ 6., 222., 8., 444., 555.],
11,,,, [ 11., 222., 333., 444., 555.]])
61
Scipy
• SciPy is a library that uses NumPy for more
mathematical functions.
• SciPy uses NumPy arrays as the basic data
structure,
• used tasks in scientific programming, including
linear algebra, integration (calculus), ordinary
differential equation solving, and signal
processing.
• For a quick start on the functions check this
[Link]
62
Scipy
from scipy import special
print(special.exp10(5))
63
Linear Algebra
[Link]
• Available in scipy and numpy
• Scipy version is more comprehensive and faster
– Matrix and vector products
– Decompositions
– Matrix eigenvalues
– Norms and other numbers
– Solving equations and inverting matrices
– Exceptions
– Linear algebra on several matrices at once
For more details:
[Link]
64
References
• Numpy Documentation, [Link]
• Python for Data Analysis by Katia Oleinik
65