NumPy
Kyunghwa Yu
Data Science Package
[Link]
NumPy: fast numerical arrays and matrices
[Link]
Pandas : data structures and simplifies data analysis tasks
[Link]
Matplotlib : comprehensive plotting library
[Link]
Seaborn : high-level interface to Matplotlib, better data plotting
[Link]
NumPy
[Link]
NumPy notebook demo
[Link]
NumPy tutorial
[Link]
Scipy lecture notes
[Link]
The book Python Data Science Handbook by Jake VanderPlas
[Link]
Install
If you use Anaconda, type conda install numpy (or pip install numpy)
%pip install numpy
Otherwise, use Terminal (Mac) or cmd (Windows) and type pip install numpy
Then, go to the Jupyter Notebook and type import numpy
No errors confirm the successful installment.
Import
import numpy
import numpy as np
from numpy import arrange
from numpy import *
dir() & help()
import numpy as np
# What kinds of functions can be used?
dir(np)
# How many functions/methods are available for Numpy
len(dir(np))
# Tab completion
np.a
# Check definition
help([Link])
NumPy
NumPy is more useful than lists or tuples.
Let’s compare NumPy and lists for a quick introduction.
Numpy List
x=[Link](10) y=list(range(10))
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) y
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
type(x) type(y)
[Link] list
x[0:4] y[0:4]
array([0, 1, 2, 3]) [0, 1, 2, 3]
NumPy
Beneficial to numerical computation
Numpy List
Multiplication Repetition
x*2 y*2
array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2,
18])) 3, 4, 5, 6, 7, 8, 9]
x<4 y<4
array([ True, True, True, True, False, TypeError: '<' not supported between
False, False, False, False, False]) instances of 'list' and 'int'
x[x<4]
array([0, 1, 2, 3])
NumPy
Numpy List
a a
array([1,2]))
[1,2]
b b
array([1,2,3]))
[1,2,3]
a+a a+a
array([2,4])) [1,2,1,2]
a+b a+b
[1,2,1,2,3]
Creating an Array arange(start, end, step)
import numpy as np
[Link]([])
[Link]([0,1,2,3,4])
array([0, 1, 2, 3, 4])
[Link](5) # similar to range()
[Link](1,6)
[Link](1,11,2)
array([1, 3, 5, 7, 9])
NumPy Array
# Create a 1x3 numpy array
a = [Link]([1,2,3])
print(type(a))
[Link] # n-dimensional array
[Link]
dtype(‘int32’)
Array([1, 2, 3])
#Print some values in a
0 1 2
print(a[0], a[1], a[2])
1 2 3
NumPy Array
# shape: returns a tuple corresponding to the
size or number of each dimension
print([Link])
(3,)
# size: the total number of elements in the array
[Link]
3
NumPy Array
# Create a 2x2 numpy array
b = [Link]([[1,2],[3,4]])
array([1,2],
[3,4])
0 1
print([Link]) 0 ([1, 2],
(2,2) 1 [3, 4])
[Link]
4
# Print some values in b
print(b[0,0], b[0,1], b[1,1])
1 2 4
NumPy Array
# Create a 3x2 numpy array
c = [Link]([[1,2],[3,4],[5,6]])
array([[1, 2],
[3, 4],
0 1
[5, 6]])
0 ([1, 2],
print([Link]) 1 [3, 4]
(3,2) 2 [5, 6])
[Link]
6
print(c[0,1], c[1,0], c[2,0], c[2,1])
2 3 5 6
NumPy Array
# 3x2 zero array
[[0. 0.]
x1 = [Link]((3,2)) [0. 0.]
print(x1) [0. 0.]]
# 4x2 array of ones [[1. 1.]
x2 = [Link]((4,2)) [1. 1.]
[1. 1.]
print(x2) [1. 1.]]
NumPy Array
# 3x3 constant array [[2 2 2]
f = [Link]((3,3), 2) [2 2 2]
print(f) [2 2 2]]
# 3x3 random array, [0,1)
g = [Link]((3,3))
print(g)
Creating an Array – random
(1) Uniform sampling, [0,1) # randomly generate 10
[Link](10) numbers from 0 to 1
(2) Normal sampling (mean=0, std=1) # randomly generate 100
[Link](100) numbers from normal
distribution
(3) Uniform sampling, integers # randomly generate 5
[Link](0,11,5) integers from 0 to 10
[Link](0, 5, size=(2,2))
Creating an Array – random
Normal sampling (mean=0, std=1)
x=[Link](10000)
[Link](x)
[Link](x)
import seaborn as sns
[Link](data=x, kde=True)
Creating an Array – linspace
linspace(start, end, num)
: create num elements and assigns values that are evenly spaced between start
and end inclusive. num has default value 50 if it is not provided.
[Link](0,10) # num=50 default
[Link](0,10,5)
array([ 0. , 2.5, 5. , 7.5, 10. ])
print([Link](0,100,3))
[ 0. 50. 100. ]
Indexing Arrays
Indexing Arrays
arr = [Link]([1, 2, 3, 4, 5, 6, 7, 8])
print(arr[:4]) # up to index 4 but excluded
[1 2 3 4]
print(arr[4:])
[5 6 7 8]
print(arr[1::2]) # [start:end:step]
[2 4 6 8]
print(arr[0:5:2])
[1 3 5]
Indexing Arrays
a=[Link](10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
a[1] 1
a[3:5] array([3,4])
a[0::2] array([0,2,4,6,8])
a[1]=15 [0,15,2,3,4,5,6,7,8,9]
a[3:5]=7 [0,15,2,7,7,5,6,7,8,9]
a[1::2]*=0 [0,0,2,0,7,0,6,0,8,0]
a[::2]+=2 [2,0,4,0,9,0,8,0,10,0]
a[2:5]=2,3,4 [5,0,2,3,4,0,11,0,13,9]
Indexing Arrays
[Link]: (3,3)
[Link] = 9
Indexing Arrays
A[0:2, 2]
Array Indexing
# Create 3x4 array
h = [Link]([[1,2,3,4,], [5,6,7,8], [9,10,11,12]])
print(h)
h[0,0] 1
0 1 2 3
h[0,2] 3
h[2,2] 11 0 ([1, 2, 3, 4],
1 [5, 6, 7, 8]
2 [9,10,11,12])
Array Indexing
# slice array to make a 2x2 sub-array
[[ 2 3 ]
Print(h[:2, 1:3]) [ 6 7 ]]
print(h[1:2,:]) [[ 5 6 7 8 ]
[[4]
print(h[0:2,3:4]) [8]] 0 1 2 3
0 ([1, 2, 3, 4],
1 [5, 6, 7, 8]
2 [9,10,11,12])
NumPy Basic Operations
NumPy Basic Operations
x=[Link](1,11)
print(x)
x/2
x**2
x+2
x*2
x//2
x%2
[Link](x)
np.log10(x)
NumPy Basic Operations
x=[Link](1,11)
[Link](x) [Link]
[Link](x) np.e
[Link](x)
[Link](x)
[Link](x)
[Link](x)
[Link](x)
[Link](x)
[Link](x)
[Link]
Array Math
Array Math 1 2 5 6
X = Y =
3 4 7 8
x = [Link]([[1,2],[3,4]]
y = [Link]([[5,6],[7,8]]
# Elementwise sum
print(x + y) [[6 8]
print([Link](x, y)) [10 12]]
# Elementwise difference [[-4 -4]
print(x - y) [-4 -4]]
print([Link](x, y))
1 2 5 6
Array Math X =
3 4 Y = 7 8
# Elementwise product
[[5 12]
print(x * y) [21 32]]
print([Link](x, y))
[[0.2 0.33333333]
# Elementwise division
[0.42857143 0.5 ]]
print(x / y)
print([Link](x, y))
# Elementwise square root
print([Link](x))
Array Math 1 2
X =
3 4
# Compute sum of all elements
print([Link](x))
10
# Compute sum of each column
print([Link](x, axis=0))
[4 6]
# Compute sum of each row
print([Link](x, axis=1))
[3 7]
Array Math 1 2
X =
3 4
# Compute mean of all elements
print([Link](x))
2.5
# Compute mean of each column
print([Link](x, axis=0))
[2.0 3.0]
# Compute mean of each row
print([Link](x, axis=1))
[1.5 3.5]
Array Math – Data Type 1 2
X =
3 4
# Compute mean of each column
print([Link](x, axis=0)).dtype
[2.0 3.0].dtype
dtype(‘float64’)
# force data type
print([Link](x, axis=0, dtype=np.int64))
[2 3]
Datatypes in Arrays
a = [Link]([1, 2])
[Link]
dtype(‘int64’)
# force data type
b = [Link]([1, 2], dtype=np.float64)
print(b)
print([Link])
[1. 2.]
float64
Datatypes in Arrays
b = [Link]([1.5, 2.5])
print([Link])
float64
# force data type
b = [Link]([1.5, 2.5], dtype=np.int64)
print(b)
print([Link])
[1 2]
int64
Boolean mask
a=[Link](10)
print(a)
[0 1 2 3 4 5 6 7 8 9]
print(a>4)
[False False False False False True True True True True]
a[a>4]
array([5 6 7 8 9])
a[a>4]=0
a
array([0, 1, 2, 3, 0, 0, 0, 0, 0, 0])
Boolean mask
a[a==3]=-99
a
array([0, 1, 2, -99, 0, 0, 0, 0, 0, 0])
a[a<3]*=10
a
array([0, 10, 20, -99, 0, 0, 0, 0, 0, 0])
Array Math – Dot product
U = [ 1 2 ]
3
V =
1
[Link](u,v) = 1*3 + 2*1 = 5
Array Math – Dot product
Array Math – Dot product
A = [Link]([[0, 1, 1], [1, 0, 1]]) # 2x3
B = [Link]([[1, 1], [1, 1], [-1, 1]]) # 3x2
Z=[Link](A,B) [[0 2]
print(Z) # 2x2 [0 2]]
Z=[Link](B,A)
print(Z) # 3x3
Transpose
[[1 2]
m = [Link]([[1,2],[3,4],[5,6]]) [3 4]
print(m) [5 6]]
[[1 3 5]
print(m.T) [2 4 6]]
A=[Link]([[1,2,0],[3,4,0],[5,6,0]]) [[1 2 0]
print(A) [3 4 0]
[5 6 0]]
A.T [[1 3 5]
[2 4 6]
[0 0 0]]
Practice - Plotting Mathematical Functions
x=[Link](0, 2*[Link], 100)
y=[Link](x)
# %pip install matplotlib
import [Link] as plt
[Link](x,y)