numPy notes
In [ ]:
Copied!
# PART I :
### Arrays vs Lists
## Array : More convinient when dealing with numberical operation (creates vectors : +/-/*... )
## Lists : deals with different type of data type
# PART I :
### Arrays vs Lists
## Array : More convinient when dealing with numberical operation (creates vectors : +/-/*... )
## Lists : deals with different type of data type
In [3]:
Copied!
# Lists
list_two = list(range(1,4))
# print(list_two)
list_three = list(range(1,4))
list_sum = []
for idx in range(3):
list_two[idx] = list_two[idx] ** 2 #(a^2 + a^3)
list_three[idx] = list_three[idx] ** 3
list_sum.append(list_two[idx] + list_three[idx])
print(list_sum)
# Lists
list_two = list(range(1,4))
# print(list_two)
list_three = list(range(1,4))
list_sum = []
for idx in range(3):
list_two[idx] = list_two[idx] ** 2 #(a^2 + a^3)
list_three[idx] = list_three[idx] ** 3
list_sum.append(list_two[idx] + list_three[idx])
print(list_sum)
[2, 12, 36]
In [2]:
Copied!
# Array : in NumPy
import numpy as np # (import functions and Maths modules)
#array(without a comma) : data structure intended for numerical data
array_two = np.arange(1,4) # arange similar to range() of a list
array_three = np.arange(1,4)
print(array_two ** 2 + array_three ** 3) #(a^2 + a^3)
# Array : in NumPy
import numpy as np # (import functions and Maths modules)
#array(without a comma) : data structure intended for numerical data
array_two = np.arange(1,4) # arange similar to range() of a list
array_three = np.arange(1,4)
print(array_two ** 2 + array_three ** 3) #(a^2 + a^3)
[ 2 12 36]
In [22]:
Copied!
# Some numpy maths functions :
sample_array = np.array([1,2,3]) # converts a list to array
print(sample_array)
# power function
print(np.power(sample_array,3) )
# negative
print(np.negative(sample_array))
#exponencial
print(np.exp(sample_array))
# log
print(np.log(sample_array))
# sin
print(np.sin(sample_array))
# cos
print(np.cos(sample_array))
#tan
print(np.tan(sample_array))
print(np.sin(sample_array)/np.cos(sample_array))
# Some numpy maths functions :
sample_array = np.array([1,2,3]) # converts a list to array
print(sample_array)
# power function
print(np.power(sample_array,3) )
# negative
print(np.negative(sample_array))
#exponencial
print(np.exp(sample_array))
# log
print(np.log(sample_array))
# sin
print(np.sin(sample_array))
# cos
print(np.cos(sample_array))
#tan
print(np.tan(sample_array))
print(np.sin(sample_array)/np.cos(sample_array))
[1 2 3] [ 1 8 27] [-1 -2 -3] [ 2.71828183 7.3890561 20.08553692] [0. 0.69314718 1.09861229] [0.84147098 0.90929743 0.14112001] [ 0.54030231 -0.41614684 -0.9899925 ] [ 1.55740772 -2.18503986 -0.14254654] [ 1.55740772 -2.18503986 -0.14254654]
In [1]:
Copied!
# N dimension Array : to create Vectors
# Shape:
# 1D Array : (n,)
# 2D Array : (n,m)
import numpy as np
x = np.arange(3) # Starts from zero same : x = np.arange(0,3)
y = np.arange(3)
z = np.arange(3)
multi_array = np.array([x, y, z])
print(multi_array)
print(np.shape(multi_array))
# N dimension Array : to create Vectors
# Shape:
# 1D Array : (n,)
# 2D Array : (n,m)
import numpy as np
x = np.arange(3) # Starts from zero same : x = np.arange(0,3)
y = np.arange(3)
z = np.arange(3)
multi_array = np.array([x, y, z])
print(multi_array)
print(np.shape(multi_array))
[[0 1 2] [0 1 2] [0 1 2]] (3, 3)
In [29]:
Copied!
# Generate sample or random numbers
w = np.linspace(1,10, 50)
print(w)
# Generate sample or random numbers
w = np.linspace(1,10, 50)
print(w)
[ 1. 1.18367347 1.36734694 1.55102041 1.73469388 1.91836735 2.10204082 2.28571429 2.46938776 2.65306122 2.83673469 3.02040816 3.20408163 3.3877551 3.57142857 3.75510204 3.93877551 4.12244898 4.30612245 4.48979592 4.67346939 4.85714286 5.04081633 5.2244898 5.40816327 5.59183673 5.7755102 5.95918367 6.14285714 6.32653061 6.51020408 6.69387755 6.87755102 7.06122449 7.24489796 7.42857143 7.6122449 7.79591837 7.97959184 8.16326531 8.34693878 8.53061224 8.71428571 8.89795918 9.08163265 9.26530612 9.44897959 9.63265306 9.81632653 10. ]
In [12]:
Copied!
#Create arrays
a = np.arange(1,30)
b = np.linspace(1,30, 3,)
# b = np.linspace(1,30, 3, False)
print(a)
print(b)
#Create arrays
a = np.arange(1,30)
b = np.linspace(1,30, 3,)
# b = np.linspace(1,30, 3, False)
print(a)
print(b)
[ 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] [ 1. 15.5 30. ]
In [14]:
Copied!
import numpy as np
x = np.arange(3) # Starts from zero same : x = np.arange(0,3)
y = np.arange(3, 6)
z = np.arange(6, 9)
multi_array = np.array([x, y, z])
print(multi_array)
print(np.shape(multi_array))
import numpy as np
x = np.arange(3) # Starts from zero same : x = np.arange(0,3)
y = np.arange(3, 6)
z = np.arange(6, 9)
multi_array = np.array([x, y, z])
print(multi_array)
print(np.shape(multi_array))
[[0 1 2] [3 4 5] [6 7 8]] (3, 3)
In [25]:
Copied!
# Act on specific element of the matrix/Vectors
# print(multi_array[0][0])
print(multi_array[0, 0])
# print(multi_array[0][1])
print(multi_array[0, 1])
# print(multi_array[0][2])
print(multi_array[0, 2])
# Act on specific element of the matrix/Vectors
# print(multi_array[0][0])
print(multi_array[0, 0])
# print(multi_array[0][1])
print(multi_array[0, 1])
# print(multi_array[0][2])
print(multi_array[0, 2])
0 1 2
In [29]:
Copied!
# change type array
import numpy as np
x = np.arange(3) # Starts from zero same : x = np.arange(0,3)
y = np.arange(3, 6)
z = np.arange(6, 9)
multi_array = np.array([x, y, z], dtype = np.uint8)
print(multi_array)
print(np.shape(multi_array))
print(multi_array.dtype)
# change type array
import numpy as np
x = np.arange(3) # Starts from zero same : x = np.arange(0,3)
y = np.arange(3, 6)
z = np.arange(6, 9)
multi_array = np.array([x, y, z], dtype = np.uint8)
print(multi_array)
print(np.shape(multi_array))
print(multi_array.dtype)
[[0 1 2] [3 4 5] [6 7 8]] (3, 3) uint8
In [30]:
Copied!
# 1D slicing
# [start stop step]
import numpy as np
x = np.arange(1, 10)
print(x)
# 1D slicing
# [start stop step]
import numpy as np
x = np.arange(1, 10)
print(x)
[1 2 3 4 5 6 7 8 9]
In [35]:
Copied!
print(x[1:10:2])
print(x[1:10:2])
[2 4 6 8]
In [37]:
Copied!
# Reshaping Array
import numpy as np
x = np.arange(9).reshape(3, 3)
print(x)
# Reshaping Array
import numpy as np
x = np.arange(9).reshape(3, 3)
print(x)
[[0 1 2] [3 4 5] [6 7 8]]
In [38]:
Copied!
# respect the dimmension of the array when reshaping dim (x) = 3x3
x = np.arange(9).reshape(3, 5)
print(x)
# respect the dimmension of the array when reshaping dim (x) = 3x3
x = np.arange(9).reshape(3, 5)
print(x)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-38-5f5d8f53278b> in <module> ----> 1 x = np.arange(9).reshape(3, 5) 2 print(x) ValueError: cannot reshape array of size 9 into shape (3,5)
In [39]:
Copied!
# respect the dimmension of the array when reshaping dim (x) = 3x3
x = np.arange(9).reshape(3, -1)
print(x)
# respect the dimmension of the array when reshaping dim (x) = 3x3
x = np.arange(9).reshape(3, -1)
print(x)
[[0 1 2] [3 4 5] [6 7 8]]
In [41]:
Copied!
# Reshaping twà Array of [(3x3), (3x3)]
# Pratical for Excel
x = np.arange(18).reshape(2, 3, 3) # [Dim, n, m]
print(x)
# Reshaping twà Array of [(3x3), (3x3)]
# Pratical for Excel
x = np.arange(18).reshape(2, 3, 3) # [Dim, n, m]
print(x)
[[[ 0 1 2] [ 3 4 5] [ 6 7 8]] [[ 9 10 11] [12 13 14] [15 16 17]]]
In [44]:
Copied!
# Miltidimensional Slicing :
import numpy as np
x = np.arange(18).reshape(3, 2, 3)
print(x)
# Miltidimensional Slicing :
import numpy as np
x = np.arange(18).reshape(3, 2, 3)
print(x)
[[[ 0 1 2] [ 3 4 5]] [[ 6 7 8] [ 9 10 11]] [[12 13 14] [15 16 17]]]
In [47]:
Copied!
x[1 , 1, 2]
x[1 , 1, 2]
Out[47]:
11
In [59]:
Copied!
# slicing
# x[1 , :1, 2]
x[1, :2, :3 :2]
# slicing
# x[1 , :1, 2]
x[1, :2, :3 :2]
Out[59]:
array([[ 6, 8],
[ 9, 11]])
In [51]:
Copied!
# access first bloc (1st row and colons)
x[1 , ...]
# access first bloc (1st row and colons)
x[1 , ...]
Out[51]:
array([[ 6, 7, 8],
[ 9, 10, 11]])
In [60]:
Copied!
comp = x > 5
print(comp)
comp = x > 5
print(comp)
[[[False False False] [False False False]] [[ True True True] [ True True True]] [[ True True True] [ True True True]]]
In [61]:
Copied!
x[x>5] # this can be reshapped
x[x>5] # this can be reshapped
Out[61]:
array([ 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17])
In [66]:
Copied!
x.max()
x.max()
Out[66]:
17
In [64]:
Copied!
x.min()
x.min()
Out[64]:
0
In [79]:
Copied!
# Manipulation Array shapes
import numpy as np
x = np.arange(9).reshape(3, 3)
print(x)
# Manipulation Array shapes
import numpy as np
x = np.arange(9).reshape(3, 3)
print(x)
[[0 1 2] [3 4 5] [6 7 8]]
In [80]:
Copied!
#Effilocher
#return a view of the orignal array
ravelled_array = x.ravel()
print(ravelled_array)
#Effilocher
#return a view of the orignal array
ravelled_array = x.ravel()
print(ravelled_array)
[0 1 2 3 4 5 6 7 8]
In [81]:
Copied!
# Mettre à plat == aplatir
# malloc and Return a copy
flatten_array = x.flatten()
print(flatten_array)
# Mettre à plat == aplatir
# malloc and Return a copy
flatten_array = x.flatten()
print(flatten_array)
[0 1 2 3 4 5 6 7 8]
In [82]:
Copied!
# Effilocher
# return a view of the orignal array :
# La modification sur l'un élément se repercute sur le tableau de départ
ravelled_array = x.ravel()
#ravelled_array[2] = 1000000
print(ravelled_array)
print(x)
# Effilocher
# return a view of the orignal array :
# La modification sur l'un élément se repercute sur le tableau de départ
ravelled_array = x.ravel()
#ravelled_array[2] = 1000000
print(ravelled_array)
print(x)
[0 1 2 3 4 5 6 7 8] [[0 1 2] [3 4 5] [6 7 8]]
In [83]:
Copied!
# Mettre à plat == aplatir
# malloc and Return a copy
# La modification sur l'un élément NE repercute PAS sur le tableau de départ
flatten_array = x.flatten()
flatten_array[2] = 1000000
print(flatten_array)
print(x)
# Mettre à plat == aplatir
# malloc and Return a copy
# La modification sur l'un élément NE repercute PAS sur le tableau de départ
flatten_array = x.flatten()
flatten_array[2] = 1000000
print(flatten_array)
print(x)
[ 0 1 1000000 3 4 5 6 7 8] [[0 1 2] [3 4 5] [6 7 8]]
In [92]:
Copied!
# CREATE A BEAUTIFUL MATRIX !!!
y = np.arange(9)
y.shape = [3, 3]
print(y)
# CREATE A BEAUTIFUL MATRIX !!!
y = np.arange(9)
y.shape = [3, 3]
print(y)
[[0 1 2] [3 4 5] [6 7 8]]
In [93]:
Copied!
# TRANSPOSED MATRIX
y = np.arange(9)
y.shape = [3, 3]
# print(y.transpose())
print(y.T)
# TRANSPOSED MATRIX
y = np.arange(9)
y.shape = [3, 3]
# print(y.transpose())
print(y.T)
[[0 3 6] [1 4 7] [2 5 8]]
In [94]:
Copied!
print(np.resize(y, (6 , 6)))
print(np.resize(y, (6 , 6)))
[[0 1 2 3 4 5] [6 7 8 0 1 2] [3 4 5 6 7 8] [0 1 2 3 4 5] [6 7 8 0 1 2] [3 4 5 6 7 8]]
In [100]:
Copied!
# matrice Zeros
print(np.zeros((6, ), dtype = int))
# matrice Zeros
print(np.zeros((6, ), dtype = int))
[0 0 0 0 0 0]
In [103]:
Copied!
# matrice identité
print(np.eye(3))
# matrice identité
print(np.eye(3))
[[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]]
In [104]:
Copied!
print(np.random.rand(4, 4))
print(np.random.rand(4, 4))
[[0.17396231 0.14428123 0.89033868 0.54503796] [0.93716007 0.97994999 0.17468047 0.84210292] [0.50495833 0.31100454 0.67891019 0.22924788] [0.76267273 0.68601865 0.62384405 0.37916795]]
In [ ]:
Copied!
#### MATRIX MMULTIPLICATION
# A*B = sum of elements (row(i) * colons(i))
#### MATRIX MMULTIPLICATION
# A*B = sum of elements (row(i) * colons(i))
In [115]:
Copied!
import numpy as np
mat_a = np.matrix([0, 3, 5, 5, 5, 2]).reshape(2, 3)
mat_b = np.matrix([3, 4, 3, -2, 4, -2]).reshape(3,2)
print(mat_a)
print(mat_b)
import numpy as np
mat_a = np.matrix([0, 3, 5, 5, 5, 2]).reshape(2, 3)
mat_b = np.matrix([3, 4, 3, -2, 4, -2]).reshape(3,2)
print(mat_a)
print(mat_b)
[[0 3 5] [5 5 2]] [[ 3 4] [ 3 -2] [ 4 -2]]
In [118]:
Copied!
product = np.matmul(mat_a, mat_b)
print(product)
product = np.matmul(mat_a, mat_b)
print(product)
[[ 29 -16] [ 38 6]]
In [120]:
Copied!
product(mat_a @ mat_b)
product(mat_a @ mat_b)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-120-d35781e5c8d7> in <module> ----> 1 product(mat_a @ mat_b) TypeError: 'matrix' object is not callable
In [121]:
Copied!
# STACKING : matrix concatenation
x = np.arange(4)
x.shape = [2, 2]
print(x)
# STACKING : matrix concatenation
x = np.arange(4)
x.shape = [2, 2]
print(x)
[[0 1] [2 3]]
In [122]:
Copied!
y = np.arange(4, 8)
y.shape = [2, 2]
print(y)
y = np.arange(4, 8)
y.shape = [2, 2]
print(y)
[[4 5] [6 7]]
In [123]:
Copied!
z = np.hstack((x, y))
print(z)
z = np.hstack((x, y))
print(z)
[[0 1 4 5] [2 3 6 7]]
In [ ]:
Copied!
##### COMPUTER VISION ######
# The way computer sees things.
#
##### COMPUTER VISION ######
# The way computer sees things.
#