ESS391H

The Application of Machine Learning and Artificial Neural Networks in Geosciences Lecture 2 – Python II

Hosein Shahnas

University of Toronto, Department of Earth Sciences,

1 Modules in Python

The import statement allows you to import one or more modules into your Python program, letting you make use of the definitions constructed in those modules.

Examples: import sys import numpy import pandas import math

Sys This module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. Ex. sys.exit('System exited at this point')

2 Modules in Python

Numpy This is a library for the Python , adding support for large, multi- dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays. Numpy is the fundamental package for scientific computing with Python. It contains among other things: a) A powerful N-dimensional array object b) Sophisticated functions ) Tools for integrating C/C++ and code d) Useful linear algebra, Fourier transform, and random number capabilities

Ex. x = numpy.zeros(10)

3 Modules in Python

Pandas This is a software library written for the Python programming language for data manipulation and analysis. In particular, it offers data structures and operations for manipulating numerical tables and time series. Pandas is an open source Python package that provides numerous tools for data analysis.

Ex. data_file = pd.read_csv(„my_file.csv')

Math In python a number of mathematical operations can be performed with ease by importing a module named “math” which defines various functions which makes our tasks easier.

Ex. sq = math.sqrt(4) etc…

4 Arrays in Python

Arrays Arrays are used to store multiple values in one single variable.

Ex. Ex. x = [1,2,3] import numpy as np x = [[1. 0. 5.] n1 = 2 [9. 6. 2.]] n2 = 3 s = (n1,n2) x = np.zeros(s)

Arrays Methods

Method Description append() Adds an element at the end of the list clear() Removes all the elements from the list copy() Returns a copy of the list count() Returns the number of elements with the specified value extend() Add the elements of a list (or any iterable), to the end of the current list index() Returns the index of the first element with the specified value insert() Adds an element at the specified position pop() Removes the element at the specified position remove() Removes the first item with the specified value reverse() Reverses the order of the list sort() Sorts the list alphabetically 5 Arrays Methods append() Adds an element at the end of the list.

Ex. cars = ['Ford', 'Volvo', 'BMW'] cars.append(„Honda‟) clear() Removes all the elements from the list.

Ex. cars = ['Ford', 'Volvo', 'BMW'] cars.clear() copy() Returns a copy of the list.

Ex. import copy as cp old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] new_list = cp.copy(old_list)

https://www.w3schools.com/python/python_arrays.asp 6 Arrays Methods count() Returns the number of elements with the specified value.

Ex. fruits = ['cherry', 'apple', 'banana', 'cherry'] x = fruits.count("cherry") extend() Add the elements of a list (or any iterable), to the end of the current list.

Ex. fruits = ['apple', 'banana', 'cherry'] cars = ['Ford', 'BMW', 'Volvo'] fruits.extend(cars)

Index() Returns the index of the first element with the specified value.

Ex. fruits = ['apple', 'banana', 'cherry'] x = fruits.index(„cherry‟)

7

Arrays Methods insert() Adds an element at the specified position.

Ex. fruits = ['apple', 'banana', 'cherry'] fruits.insert(1, 'orange') pop() Removes the element at the specified position.

Ex. fruits = ['apple', 'banana', 'cherry'] fruits.pop(1)

Remove() Removes the first item with the specified value.

Ex. fruits = ['apple', 'banana', 'cherry', 'banana'] fruits.remove('banana')

8 Arrays Methods reverse() Reverses the order of the list.

Ex. fruits = ['apple', 'banana', 'cherry'] fruits.reverse() sort() Sorts the list alphabetically.

Ex. cars = ['Ford', 'BMW', 'Volvo'] cars.sort()

9 Arrays Index Rules

Starting and Ending Indices Part of the array between two indices n1 and n2, where both n1 and n2 are smaller than the length of the array. x[n1:n2], n1: starting index, n2: ending index

Ex. x = [0 1 2 3 4 5 6 7 8 9] x[2:5] = [2 3 4] x[3:] = [3 4 5 6 7 8 9] x[:3] = [0 1 2]

10 Arrays

Ex-01 Ex-02 n = 5 n1 = 2 s = (5) n2 = 3 x = np.zeros(n) s = (n1,n2) for i in range(n): x = np.zeros(s) x[i] = float(i) for i in range(n1): print('x[ ',i, ']= ', x[i]) for j in range(n2): print('shape of x = ', x.shape ) x[i,j] = float(i+j**2) print('x[ ', i, ',' , j,']= ', x[i,j]) Output print('shape of x = ', x.shape ) x[ 0 ]= 0.0 x[ 1 ]= 1.0 Output x[ 2 ]= 2.0 x[ 0 , 0 ]= 0.0 x[ 3 ]= 3.0 x[ 0 , 1 ]= 1.0 x[ 4 ]= 4.0 x[ 0 , 2 ]= 4.0 shape of x = (5,) x[ 1 , 0 ]= 1.0 x[ 1 , 1 ]= 2.0 x[ 1 , 2 ]= 5.0 shape of x = (2, 3)

11 Basic Operations and Functions

Ex-01 Ex-03 k = 1 j = 8 m = 7 j -= 2 a = k+m**2 print('j = ', j) print('k, m, a = ', k, m, a) Output Output j = 6 k, m, a = 1 7 50 Ex-04 Ex-02 j = 8 j = 2 j *= 2 j += 8 print('j = ', j) print('j = ', j) j += 8 Output j = 16 Output j = 10 j = 18

12 Basic Operations and Functions

Ex-05 Ex-06 x = 11 x = 11 y = 3 y = 3 z = x / y z = divmod(x,y) print('z = ', z) print('z = ', z) z = x // y x = 11.9 print('z = ', z) y = 3.1 z = x % y z = divmod(x,y) print('z = ', z) print('z = ', z)

Output Output z = 3.6666666666666665 z = (3, 2) z = 3 z = (3.0, 2.6) z = 2

13 Basic Operations and Functions

Ex-07 Ex-09 j = 8 q = 8.2 i, j = j, j+1 c = math.ceil(q) print('i, j = ', i, j) print('ceil(q) = ', c)

Output Output i, j = 8 9 ceil(q) = 9

Ex-08 q = 8 Ex-10 sq = math.sqrt(q) q = 8.2 print('sqrt(q) = ', sq) c = math.ceil(q) print('ceil(q) = ', c) Output sqrt(q) = 2.8284271247461903 Output ceil(q) = 9

14 Basic Operations and Functions

Ex-11 Ex-13 q = 8.2 q = 2.72 c = math.floor(q) c = math.log(q) print('floor(q) = ', c) print('log(q) = ', c)

Output Output floor(q) = 8 log(q) = 1.000631880307906

Ex-12 Ex-14 q = 1 q = 100 c = math.exp(q) c = math.log10(q) print('exp(q) = ', c) print('log10(q) = ', c)

Output Output exp(q) = 2.718281828459045 log10(q) = 2.0

15 Basic Operations and Functions

Ex-15 Ex-17 q = 1 q = 1 c = math.acos(q) c = math.atan(q) print('acos(q) = ', c) print('atan(q) = ', c)

Output Output acos(q) = 0.0 atan(q) = 0.7853981633974483

Ex-16 Ex-18 q = 1 q = 4 c = math.asin(q) p = 4 print('asin(q) = ', c) c = math.atan2(q, p)

Output Output asin(q) = 1.5707963267948966 latan2(q, p) = 0.7853981633974483

16 Basic Operations and Functions

Ex-19 y = -1 y = 1 x = -1 x = 1 c = math.atan2(y, x) c = math.atan2(y, x) print('atan2(y, x) = ', c) print('atan2(y, x) = ', c) y = 1 Output x = -1 atan2(y, x) = 0.7853981633974483 c = math.atan2(y, x) atan2(y, x) = 2.356194490192345 print('atan2(y, x) = ', c) atan2(y, x) = -0.7853981633974483 y = -1 atan2(y, x) = 0.0 x = 1 atan2(y, x) = 1.5707963267948966 c = math.atan2(y, x) atan2(y, x) = -2.356194490192345 print('atan2(y, x) = ', c) y = 0 x = 1 c = math.atan2(y, x) print('atan2(y, x) = ', c) y = 1 x = 0 c = math.atan2(y, x) print('atan2(y, x) = ', c)

17 Basic Operations and Functions

Ex-20 x = 2 c = math.sinh(x) print('sinh(x) = ', c)

Output sinh(x) = 3.6268604078470186

Ex-21 c = math.e print('e = ', c)

Output e = 2.718281828459045

푒푥−푒−푥 sinh(x) = 2 푒푥+푒−푥 cosh(x) = 2 sinh (푥) tanh(x) = cosh (푥)

18

Basic Operations and Functions

Ex-22 Ex-24 j = 8 pi = math.pi i, j = j, j+1 tem = math.sin(pi) print('i, j = ', i, j) print('sin x = ', tem) Output print('sin x = %12.6f' % (tem)) i, j = 8 9 tem = math.cos(pi) print('cos x = ', tem) Ex-23 tem = math.tan(pi) q = 8 print('tan x = ', tem) sq = math.sqrt(q) tem = math.tan(pi/2) print('sqrt(q) = ', sq) print('tan x = ', tem)

Output Output sqrt(q) = 2.8284271247461903 sin x = 1.2246467991473532e-16 sin x = 0.000000 cos x = -1.0 tan x = -1.2246467991473532e-16 tan x = 1.633123935319537e+16

19 Functions in Python math-Module

Function Description eil(x) Returns the smallest integer greater than or equal to x. copysign(x, y) Returns x with the of y fabs(x) Returns the absolute value of x factorial(x) Returns the factorial of x floor(x) Returns the largest integer less than or equal to x fmod(x, y) Returns the remainder when x is divided by y frexp(x) Returns the mantissa and exponent of x as the pair (m, e) fsum(iterable) Returns an accurate floating point sum of values in the iterable isfinite(x) Returns True if x is neither an nor a NaN (Not a Number) isinf(x) Returns True if x is a positive or negative infinity isnan(x) Returns True if x is a NaN ldexp(x, i) Returns x * (2**i) modf(x) Returns the fractional and integer parts of x trunc(x) Returns the truncated integer value of x exp(x) Returns e**x expm1(x) Returns e**x - 1 log(x[, base]) Returns the logarithm of x to the base (defaults to e) log1p(x) Returns the natural logarithm of 1+x log2(x) Returns the base-2 logarithm of x log10(x) Returns the base-10 logarithm of x pow(x, y) Returns x raised to the power y sqrt(x) Returns the square root of x acos(x) Returns the arc cosine of x asin(x) Returns the arc of x atan(x) Returns the arc tangent of x atan2(y, x) Returns atan(y / x) cos(x) Returns the cosine of x (x, y) Returns the Euclidean norm, sqrt(x*x + y*y) sin(x) Returns the sine of x tan(x) Returns the tangent of x degrees(x) Converts angle x from radians to degrees radians(x) Converts angle x from degrees to radians acosh(x) Returns the inverse hyperbolic cosine of x asinh(x) Returns the inverse hyperbolic sine of x atanh(x) Returns the inverse hyperbolic tangent of x cosh(x) Returns the hyperbolic cosine of x sinh(x) Returns the hyperbolic cosine of x tanh(x) Returns the hyperbolic tangent of x erf(x) Returns the error at x erfc(x) Returns the complementary error function at x gamma(x) Returns the Gamma function at x lgamma(x) Returns the natural logarithm of the absolute value of the Gamma function at x pi Mathematical constant, the ratio of circumference of a circle to it's diameter (3.14159...) e mathematical constant e (2.71828...)

https://www.programiz.com/python-programming/modules/math 20 Writing to Files

Ex-01 file_name = 'file_01' completeName = os.path.join(save_path, file_name+".dat") my_file = open(completeName, "w")

for i in range(n1): for j in range(n2): my_file.write('%5d %5d %10.6f \n' % (i, j, x[i,j])) 1 my_file.close();

Output (written into file_01.dat) 0 0 0.000000 0 1 1.000000 0 2 4.000000 1 0 1.000000 1 1 2.000000 1 2 5.000000

21 Writing to Files

Ex-02 file_name = 'file_02' completeName = os.path.join(save_path, file_name+".dat") my_file = open(completeName, "w")

for i in range(n1): for j in range(n2): my_file.write('%10.6f' % (x[i,j])) 1 my_file.write(" \n " ) my_file.close();

Output (written into file_02.dat) 0.000000 1.000000 4.000000 1.000000 2.000000 5.000000

22 Reading from Files

Ex-01 import numpy as np File_name = 'Features' completeName = os.path.join(save_path, File_name+".dat") Feature_data = np.loadtxt(completeName) print('1-Feature_data is: ',Feature_data) print('2-Feature_data is: ',Feature_data[0,0]) print('3-Feature_data is: ',Feature_data[0,1]) print('4-Feature_data is: ',Feature_data[1,1]) a = Feature_data[0,1] b = Feature_data[1,1] print('a + b = ',a + b)

23 Reading from Files

Output (written into file_02.dat) 1-Feature_data is: [[0. 0.3125 0.23873518 0.0990991 ] [0. 0.55875 0.61778656 0.00900901] [1. 1. 0.84229248 0. ] [1. 1. 0.80632411 1. ] [1. 0.3125 0.35810277 0.0990991 ] [1. 0. 0.33992094 0. ] [0. 0.3125 0.14505929 0.00900901] [0. 0.55875 0.50474309 0.00900901] [0. 1. 0.75889328 0.0990991 ] [1. 0.55875 0.73083004 0. ] [0. 1. 0.74743082 0.00900901] [0. 0.8125 0.5541502 0.0990991 ] [1. 0.8125 0.75810276 0.0990991 ] [1. 0.55875 0.6339921 1. ] [0. 0. 0.17391304 0.0990991 ]] 2-Feature_data is: 0.0 3-Feature_data is: 0.3125 4-Feature_data is: 0.55875 a + b = 0.87125

24 For Loop

Ex-01 for i in range(5): print(i) Entering for loop for each item in the sequence Output 0 1 2 yes 3 Last element reached? 4

Ex-02 No words = ['cat', 'window', 'aircraft'] for w in words: Body of the for loop print(w, len(w))

Output Exiting loop cat 3 window 6 aircraft 8

25 For Loop

Ex-03 for i in range(5,8): print(i) Entering for loop for each item in the sequence Output 5 6 7 yes Last element reached? Ex-04 for i in range(2, 10, 3): print(i) No

Output Body of the for loop 2 5 8 Exiting loop

See more examples in for.py

26 If Statement

Ex – 01 a = 150 b = 77 if a > b: print("a is greater than b") elif a==b: True print("a and b are equal") Condition else: print("a is less than b") false Do something Output Do something else a is greater than b or do nothing

Rest of the code

27 If Statement

Ex – 02 weight = 27 suitcase weigh? ")) if weight > 50: print("There is a $25 charge for luggage that heavy.") elif 25 < weight < 50: print("There is a $10 charge for luggage that heavy.") True Condition else: print("There no charge for luggage that heavy.") false Output Do something There is a $10 charge for luggage that heavy. Do something else or do nothing

Rest of the code

28 If – And - Or Statement

Ex – 01 a = 200 b = 33 c = 500 if a > b and c > a: print("Both conditions are True.") True else: Condition print("at lest of the conditions is false.") false Output Do something Both conditions are True. Do something else

or do nothing Ex – 02 a = 200 b = 33 c = 500 if a < b and c > a: print("Both conditions are True.") else: print("At lest of the conditions is false.") Rest of the code

Output At lest of the conditions is false. 29 If – And - Or Statement

Ex – 03 a = 200 b = 33 c = 500 if a < b or c > a: print(“At least one of the conditions is true.") True else: Condition print(“None of the conditions are true.") false Output Do something At least one of the conditions is true. Do something else

or do nothing Ex – 04 a = 200 b = 33 c = 500 if a < b or c < a: print("At lest of the conditions is true.") else: print("None of the conditions are true.") Rest of the code

Output See more examples in if.py None of the conditions are true. 30 While Loop

Ex – 01 n = 3 while n > 0: n = n -1 Entering for loop print("n = ", n)

Output n = 2 No n = 1 Is condition true? n = 0

Ex – 02 Yes a = 200 b = 33 Body of the while loop c = 500 if a < b or c < a: print("At lest of the conditions is true.") exiting loop else: print("None of the conditions are true.")

Output None of the conditions are true. 31 While Loop

Ex – 01 n = 3 while n > 0: n = n -1 Entering for loop print("n = ", n)

Output n = 2 No n = 1 Is condition true? n = 0

Ex – 02 Yes a = ['first', 'second', 'third'] while a: Body of the while loop print(a.pop(-1), '------', a)

Output exiting loop third ------['first', 'second'] second ------['first'] first ------[]

32 While Loop

Ex – 03 loop_value = 0 while (loop_value < 3): print ('The loop value is:', loop_value) Entering for loop loop_value = loop_value + 1

Output The loop value is: 0 No The loop value is: 1 Is condition true? The loop value is: 2

Ex – 04 Yes var = 1 while var == 1 : Body of the while loop num = input("Enter some number: ") print ("You entered: ", num) print ("Goodbye!") # the program execution exiting loop never reaches this point

Output

33