GOD Through Sin, the divine likeness was marred, and well- nigh obliterated. Man’s physical powers were weakened, his mental capacity was lessened, his spiritual vision dimmed. He had become subject to death… (EGW, Education) 5. Functions Demonstrates keyword arguments and default parameter values def birthday1(name, age): print("Happy birthday,", name, "!", " I hear you're", age, "today.\n") # parameters with default values def birthday2(name = "Jackson", age = 1): print("Happy birthday,", name, "!", " I hear you're", age, "today.\n") birthday1("Jackson", 1) birthday1(1, "Jackson") birthday1(name = "Jackson", age = 1) birthday1(age = 1, name = "Jackson") birthday2() 5. Functions birthday2(name = "Katherine") birthday2(age = 12) birthday2(name = "Katherine", age = 12) birthday2("Katherine", 12) input("\n\n Press the enter key to exit.") 5. Functions Demonstrates global variables def read_global(): print("From inside the local scope of read_global(), value is:", value) def shadow_global(): value = -10 print("From inside the local scope of shadow_global(), value is:", value) def change_global(): global value value = -10 print("From inside the local scope of change_global(), value is:", value) 5. Functions # value is a global variable because we're in the global scope here value = 10 print("In the global scope, value has been set to:", value, "\n") read_global() print("Back in the global scope, value is still:", value, "\n") shadow_global() print("Back in the global scope, value is still:", value, "\n") change_global() print("Back in the global scope, value has now changed to:", value) input("\n\nPress the enter key to exit.") CHAPT III: THE NUMPY LIBRARY 1. Definition, History and installation of Numpy Library • Definition Python Basic package for scientific computing (Data Analysis) Currently the most widely used package for the calculation of multidimensional arrays and large arrays • Installation of Numpy Library On Linux (Ubuntu and Debian): sudo apt-get install python-numpy On Linux (Fedora): sudo yum install numpy scipy On Windows with Anaconda: conda install numpy On window through CMD: Pip install numpy • Import Numpy Library Import : import numpy as np 2. NdArray object and Data Type • NumPy N-dimensional Array The main data structure in NumPy is the ndarray from numpy import array l = [1.0, 2.0, 3.0] It is a fixed-sized array in memory that contains data of the same type a = array(l) print(a) The data type supported by an array can be accessed via the “dtype” attribute on the array print(a.shape) print(a.dtype) 2. NdArray object and Data Type • Create an empty Array with Empty () from numpy import empty a = empty([3,3]) print(a) • Create a zero Array with Zero () from numpy import zeros a = zeros([3,5]) print(a) 2. NdArray object and Data Type • Create a one Array with ones () from numpy import ones a = ones([5]) print(a) • Combining Arrays Vertical Stack Horizontal Stack 3. NdArray object and Data Type • Combining Arrays Using vstack() from numpy import array from numpy import vstack a1 = array([1,2,3]) print(a1) a2 = array([4,5,6]) print(a2) a3 = vstack((a1, a2)) print(a3) print(a3.shape) 3. NdArray object and Data Type • Combining Arrays Using hstack() from numpy import array from numpy import hstack a1 = array([1,2,3]) print(a1) a2 = array([4,5,6]) print(a2) a3 = hstack((a1, a2)) print(a3) print(a3.shape) 4. Numpy Linear Algebra • Reasons for learning Numpy Linear Algebra : You Need to Learn Linear Algebra Notation to be able to read and write Algebra Notation You Need to Learn Linear Algebra Arithmetic to know how to add, subtract, and multiply scalars, vectors, and matrices You Need to Learn Linear Algebra for Statistics in order to be able to read and interpret statistics You Need to Learn Matrix Factorization to know how to factorize a matrix and what it means You Need to Learn Linear Least Squares to know the minimization of squared error 4. Numpy Linear Algebra • Vectors A vector is a tuple of one or more values called scalars We can represent a vector in Python as a NumPy array Two vectors of equal length can be multiplied together Example how to create a vector: from numpy import array v = array([1, 2, 3]) print(v) Example how to multiply a vector : a = array([1, 2, 3]) print(a) b = array([1, 2, 3]) print(b) c=a*b print(c) 4. Numpy Linear Algebra • Matrices A two-dimensional array of scalars with one or more columns and one or more rows We can represent a matrix in Python using a two-dimensional NumPy array Two matrices with the same dimensions can be added together to create a new third matrix The matrix multiplication operation can be implemented in NumPy using the dot() function Example how to create a Matrix from numpy import array A = array([[1, 2, 3], [4, 5, 6]]) print(A) 4. Numpy Linear Algebra Example how to add Matrix A = array([[1, 2, 3], [4, 5, 6]]) print(A) B = array([[1, 2, 3], [4, 5, 6]]) print(B) C=A+B print(C) Example how to multiply matrix with dot product A = array([[1, 2], [3, 4], [5, 6]]) print(A) B = array([[1, 2], [3, 4]]) print(B) C = A.dot(B) print(C) 4. Numpy Linear Algebra • Matrix Types and Operations We can transpose a matrix in NumPy by calling the T attribute A matrix can be inverted in NumPy using the inv() function. Example how to Transpose a matrix: from numpy import array from numpy.linalg import inv A = array([[1, 2], [3, 4], [5, 6]]) print(A) C = A.T print(C) 4. Numpy Linear Algebra Example how to inverse a matrix: A = array([[1.0, 2.0], [3.0, 4.0]]) print(A) B = inv(A) print(B) 4. Numpy Linear Algebra • Matrix Factorization A matrix decomposition is a way of reducing a matrix into its constituent parts The LU decomposition is for square matrices and decomposes a matrix into L and U components The LU decomposition can be implemented in Python with the lu() function Example of LU Decomposition: from numpy import array from scipy.linalg import lu A = array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print("A=",A) 4. Numpy Linear Algebra # LU decomposition P, L, U = lu(A) print("P=",P) print("L=",L) print("U=",U) Example of LU Reconstruction: from numpy import array from scipy.linalg import lu B = P.dot(L).dot(U) print("B=",B) 4. Numpy Linear Algebra • Singular-Value Decomposition Method for reducing a matrix to its constituent parts in order to make certain subsequent matrix calculations simpler The SVD can be calculated by calling the svd() function Example of Singular Value Decomposition: from numpy import array from scipy.linalg import svd A = array([[1, 2], [3, 4], [5, 6]]) print(A) U, s, V = svd(A) print(U) print(s) print(V)