UU Python Training Session 2 2022 02 15 v02
UU Python Training Session 2 2022 02 15 v02
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.
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
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
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
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.
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
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.
Linear regression
Simple linear regression Practice 07
Exploring the data
Fitting • Linear Regression
Evaluating the model with scikitlearn
Multiple linear regression
Polynomial regression
Regularization
Practice 08
• Data manipulation
Thank you