Lecture 02 DS

Lecture 02 DS

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 programming language, 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 c) Tools for integrating C/C++ and Fortran 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 sign 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 infinity 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 sine of x atan(x) Returns the arc tangent of x atan2(y, x) Returns atan(y / x) cos(x) Returns the cosine of x hypot(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

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    33 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us