You are on page 1of 22

Partner Logo

Python for Hydrology


Training Course Programme
Alex Rodrigues – Digital Solutions
Architect
Session II - 15 February 2022

Revision to exercises from session I


Functions
Creating simple functions in Python
Introduction to Pandas library – Part A
Basic Math functions
More modules in python
Statistics in python - Introduction
Exercises
Revision to exercises
from session I
Revision to exercises from session I
Topics covered

​Last session we covered:


 Interpreted programming language
 Compiled programming language
 Data Types
 Introspection in Python
 Packages
 Plots
 Flow Control
 Arrays

Mott MacDonald 3 February 2022


Revision to exercises from session I Practice 01

Some relevant code lines • Consolidate


session I

​General: ​Data manipulation: ​packages: ​string:


 print()  import pandas as pd  import mypackage
 input()  Series()  from mypackage import myfunction  index()
 isinstance()  pd.read_excel()  !{sys.executable} -m pip install  len()
 type  pd.read_csv() mypackage  count()
 dropna()  as  del
 isnull()  append()

​Data types:  head()


 tail() ​Flow control:
 sorted() ​visualisation:
 update()  import plotly.express as px
 dict ​math:  for
 scatter()
 str  if
 line()
 int  round()  else
 bar()
 float  range()  elif
 pie()

Mott MacDonald 3 February 2022


Functions
Functions
Definition and Syntax

​Functions in general allow us to aggregate statements into a logical block.


​We can pass specific data, known as parameters, into a function and having return data as an output.

​In the last session we used many of the built-in functions in Python, today we will build our own functions.

​We can also aggregate functions together into a Python module and create our own libraries of functionality.

​Syntax ​Keep in mind:

def FunctionName :  The keyword def marks the start of the function header.
<statement −1>  The function name uniquely identifies the function.
.  Function arguments through which we pass values to a function. Optional.
.  Colon (:) to mark the end of the function header.
<statement−N>  Docstring to describe what the function does. Optional.
return ...  Valid statements that make up the function body. Statements must have the
same indentation level.
 Return statement to return a value from the function. Optional.
Mott MacDonald 3 February 2022
Functions
Scripts vs Functions

​While a function is a block of code which only runs when it is called, a script is a Python file that is built to be run
directly to do something. Scripts often contain code written outside the scope of any functions

​Functions:
​Scripts
 Normally operate on inputs passed into the
 A group of commands that you can
functions and return outputs
execute in the Editor
 Have internal variables that are only valid
 Automates repetitive tasks
inside the function
 Your user-defined functions work the same way
as the built-in functions that we addressed last
session

Mott MacDonald 3 February 2022


Creating simple
functions in Python
Creating simple functions in Python
Call a Function. Functions with multiple return values

​Once the function is defined, we can call it from another program, script, or function.
​To call a function just type the function name with the appropriate parameters

​How does it work? ​Functions with multiple return values


 we can return more than one value
from a function Practice 02
 Use return output1,output2
• Building functions
• Scrips vs Functions

Mott MacDonald 3 February 2022


Introduction to Pandas
library – Part A
Introduction to Pandas library – Part A
Pandas Data Structures: Series and DataFrames

​The Pandas Series data structure is a one-dimensional array. It is the main building block for a Pandas DataFrame.
​The Pandas DataFrame is a two-dimensional data structure made of columns and rows. DataFrames are similar to
an excel or relational database table.

Practice 03
​Creating Series ​Creating Dataframes
 Using a Dictionary  From a Dictionary of Series • Creating Series
 From ndarray  From a Dictionary of Lists
• Creating Dataframes
 From Scalar Values

Mott MacDonald 3 February 2022


Introduction to Pandas library – Part A
Reading and Writing Data with Pandas

​Methods to read data are all named pd.read_* where * is the file type. Series and DataFrames can be saved to disk
using their to_* method.
​Before loading a file into a pandas data frame, take a look at the file. It will help us estimate which columns you
should import and determine what data types your columns should have.

​Read files with: ​Save files with:


 read_csv()  to_csv()
 read_excel()  to_excel() Practice 04
 read_html()  to_json() • Read files of several
 read_sql()  … types
 read_pickle()
 read_json() • Exploring arguments
 … of read_

Mott MacDonald 3 February 2022


Basic Math functions
Basic Math functions

​Python is an amazing tool for mathematical calculations. The Python Standard Library has different modules for
basic mathematics and we don’t need to install these separately, but we need to import them when you want to use
some of the functions within these modules

​Some math functions ​mathematical constants and important values


 Factorial  Pi
 Greatest Common Divisor (GCD)  Tau
 Ceiling and Floor Value  Euler’s number Practice 05
 Absolute Value  Infinity
• Maths functions
 Square Root  Not a number (NaN)
 Natural Exponent
 Natural Logarithm·
 Log2() and log10()
 Trigonometric Functions

Mott MacDonald 3 February 2022


More modules in
python
More modules in python

​There are many Python packages that offer amazing tools for data science work and machine learning model
development. The list is huge but there are certain packages we should focus more as they are some of the most
commonly used.

​scikit-learn ​sciPy ​Statsmodels ​requests


 Identify which  Multi-dimensional  Complex statistics  Customize, inspect,
category an object image processing authorize, and
is likely to belong to  Ability to solve configure HTTP
 Predict a Fourier transforms, requests.
​Seaborn
continuous variable and differential  Add parameters,
based on available equations  visualizations headers, and multi-
features  Due to its optimized part files.
 Group similar algorithms, it can  Decompress data
objects into clusters do linear algebra automatically.
computations very  Upload multiple
robustly and files at the same
efficiently time.

Mott MacDonald 3 February 2022


Statistics in python - Introduction
Statistics in python - Introduction
How to Describe Our Data
​A fundamental aspect of working with data is the capability to summarize, describe and represent data visually.
Python statistics libraries are widely used tools that will assist us in working with data.

​Describe data ​Types of Measures

 Descriptive statistics in pure Python  Central tendency


 Descriptive statistics with available Python  Variability
libraries  Correlation or joint variability
 Visualize your datasets
​Visualizing data
​Approaches Practice 06
 Box plots
 The quantitative approach  Histograms • descriptive
 The visual approach  Pie charts statistics
 Bar charts
 • visualize your
X-Y plots
 Heatmaps datasets

Mott MacDonald 3 February 2022


Statistics in python - Introduction
linear regression, a quick glance with scikitlearn
​After cleaning and manipulating your data with Pandas or NumPy, scikit-learn is used to build statistics and
machine learning models as it has a lot of tools used for data analysis.
​Linear regression is one of the best statistical models that studies the relationship between a dependent
​variable with a given set of independent variables. The relationship can be established with the help of fitting a best
line.

​Linear regression
 Simple linear regression Practice 07
 Exploring the data
 Fitting • Linear Regression
 Evaluating the model with scikitlearn
 Multiple linear regression
 Polynomial regression
 Regularization

Mott MacDonald 3 February 2022


Exercices

Practice 08
• Data manipulation
Thank you

You might also like