Data Science Analysis with Python
Data Science Analysis with Python
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 2
Chapter 05: Data Science Analysis & Machine Learning
▪ Descriptive Statistics For Distributional Analysis
▪ Test Theory & Statistics
▪ What is a hypothesis test?
▪ Single-sided vs two-sided tests
▪ Parametric vs non-parametric tests
▪ Data preprocessing, cleansing & outlier detection
▪ Regression Models
▪ Machine Learning
▪ What is Machine Learning?
▪ Supervised Learning
▪ Unsupervised Learning
▪ Reinforcement Learning
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 3
Chapter 05: Data Science Analysis & Machine Learning
▪ Descriptive Statistics For Distributional Analysis
▪ Test Theory & Statistics
▪ What is a hypothesis test?
▪ Single-sided vs two-sided tests
▪ Parametric vs non-parametric tests
▪ Data preprocessing, cleansing & outlier detection
▪ Regression Models
▪ Machine Learning
▪ What is Machine Learning?
▪ Supervised Learning
▪ Unsupervised Learning
▪ Reinforcement Learning
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 4
Chapter 05: Data Science Analysis & Machine Learning
Descriptive Statistics For Distributional Analysis
▪ Before you can analyse data with any tool (machine learning, regression models, tests)
often these tools require certain properties from the data used
▪ For example:
▪ the t-test assumes normally distributed random variables
▪ machine learning models can achieve higher performances if data is normalized, etc.
▪ Descriptive statistics allow you to get an initial overview of numerical data and about its
distribution
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 5
Chapter 05: Data Science Analysis & Machine Learning
Descriptive Statistics For Distributional Analysis
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 6
Chapter 05: Data Science Analysis & Machine Learning
Descriptive Statistics For Distributional Analysis
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 7
Chapter 05: Data Science Analysis & Machine Learning
Different types of distributions
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 8
Chapter 05: Data Science Analysis & Machine Learning
▪ Descriptive Statistics For Distributional Analysis
▪ Test Theory & Statistics
▪ What is a hypothesis test?
▪ Single-sided vs two-sided tests
▪ Parametric vs non-parametric tests
▪ Data preprocessing, cleansing & outlier detection
▪ Regression Models
▪ Machine Learning
▪ What is Machine Learning?
▪ Supervised Learning
▪ Unsupervised Learning
▪ Reinforcement Learning
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 9
Chapter 05: Data Science Analysis & Machine Learning
Test Theory & Statistics / What is a hypothesis test?
▪ A hypothesis test is a statistical tool, which can falsify a hypothesis about statistical
properties of one or multiple random variables is false or not based on samples
▪ Example
▪ A factory filling 250ml bottles with a drink. The machines will sometimes fill more,
and sometimes fill less content into the bottles. Is the amount in the bottles on
average really 250ml? (or does the machine fill in too much or too little?)
▪ You are creating a new medicine (drug). You treat 100 patients with the drug, and
100 patients you treat with a placebo. Is the effect of the new medicine significant to
treat a disease / create immunity?
▪ You are wondering if you should change the ingredients of your product to reduce
costs. 50% of the users you sell the old product, 50% of the users you sell the new
product. Are changes significantly impacting (damaging) customer satisfaction?
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 10
Chapter 05: Data Science Analysis & Machine Learning
Test Theory & Statistics / Single-sided vs two-sided tests
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 11
Chapter 05: Data Science Analysis & Machine Learning
Test Theory & Statistics / Parametric vs. Non-Parametric tests
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 12
Chapter 05: Data Science Analysis & Machine Learning
Test Theory & Statistics / How to do a test? (Example t-test)
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 13
Chapter 05: Data Science Analysis & Machine Learning
Test Theory & Statistics / How to do a test? (Example t-test)
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 14
Chapter 05: Data Science Analysis & Machine Learning
Test Theory & Statistics / How to do a test? (Example t-test)
# Imports
import numpy as np
from scipy import stats
# Generate 100 samples of bottle content (in ml) (because we don’t have measurement data here)
# Assuming the actual mean is slightly below 250ml with some variation
n = 100
bottle_content = [Link](loc=245, scale=5, size=n)
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 15
Chapter 05: Data Science Analysis & Machine Learning
Test Theory & Statistics / How to do a test? (Example t-test)
# Perform one-sided t-test
t_statistic, p_value = stats.ttest_1samp(bottle_content, popmean=h0_value, alternative='greater')
# Print results
print(f"Sample mean: {[Link](bottle_content):.2f} ml")
print(f"t-statistic: {t_statistic:.4f}")
print(f"p-value: {p_value:.4f}")
# Interpret results
if p_value < alpha:
print("Reject the null hypothesis.")
print("There is significant evidence that bottles do not have less than 250ml content.")
else:
print("Fail to reject the null hypothesis.")
print("There is not enough evidence to conclude that bottles do not have less than 250ml content.")
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 16
Chapter 05: Data Science Analysis & Machine Learning
Test Theory & Statistics / Types of Tests
Normality Tests
▪ Kolmogorov-Smirnov
▪ Shapiro-Wilk
▪ Anderson-Darling
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 17
Chapter 05: Data Science Analysis & Machine Learning
Test Theory & Statistics / Contingency Matrix, Precision of Test, Errors
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 18
Chapter 05: Data Science Analysis & Machine Learning
▪ Descriptive Statistics For Distributional Analysis
▪ Test Theory & Statistics
▪ What is a hypothesis test?
▪ Single-sided vs two-sided tests
▪ Parametric vs non-parametric tests
▪ Data preprocessing, cleansing & outlier detection
▪ Regression Models
▪ Machine Learning
▪ What is Machine Learning?
▪ Supervised Learning
▪ Unsupervised Learning
▪ Reinforcement Learning
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 19
Chapter 05: Data Science Analysis & Machine Learning
Data preprocessing, cleansing & outlier detection
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 20
Chapter 05: Data Science Analysis & Machine Learning
Data preprocessing, cleansing & outlier detection
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 21
Chapter 05: Data Science Analysis & Machine Learning
Data preprocessing, cleansing & outlier detection
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 22
Chapter 05: Data Science Analysis & Machine Learning
Outlier Detection With Z-Scores (example)
# Imports
import numpy as np
import [Link] as plt
from scipy import stats
# Add outliers
outlier_indices = [Link](n_samples, n_outliers, replace=False)
y[outlier_indices] += [Link](0, 15, n_outliers)
return X, y
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 23
Chapter 05: Data Science Analysis & Machine Learning
Outlier Detection With Z-Scores (example)
X, y = generate_data()
X_clean, y_clean = remove_outliers(X, y)
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 24
Chapter 05: Data Science Analysis & Machine Learning
Data preprocessing: Linearization & Normalization
▪ Linearization
▪ Sometimes, if non-linear relationships are analyzed, it can make sense
to apply functions such as logarithm or exponential function,
to "linearize" the non-linear variable, and make it thus accessible for linear tools
▪ Linearization can also contribute to transforming not-normally distributed variables
to normally distributed variables
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 25
Chapter 05: Data Science Analysis & Machine Learning
▪ Descriptive Statistics For Distributional Analysis
▪ Test Theory & Statistics
▪ What is a hypothesis test?
▪ Single-sided vs two-sided tests
▪ Parametric vs non-parametric tests
▪ Data preprocessing, cleansing & outlier detection
▪ Regression Models
▪ Machine Learning
▪ What is Machine Learning?
▪ Supervised Learning
▪ Unsupervised Learning
▪ Reinforcement Learning
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 26
Chapter 05: Data Science Analysis & Machine Learning
Regression Models
▪ Examples
▪ What is the potential price for a real estate given the square meters, location in the city?
▪ What is the chance of a person to fail paying back debt? (for banks)
▪ What is the chance of a risk to occur? (for insurances)
▪ What stability will my concrete have based on the ingredients used?
▪ What is the amount of rain for rain given temperature, air pressure, wind, and time of the year?
▪ …
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 27
Chapter 05: Data Science Analysis & Machine Learning
Regression Models
▪ Important terms
▪ Y dependent variable(s)
▪ X independent variable(s) (explaining variable)
▪ A model parameters
▪ F model function
▪ R residuals (unexplainable errors)
▪ Optimization
▪ Process of determining A (for F) based on given observations X,Y
Y = F(X, A) + R
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 28
Chapter 05: Data Science Analysis & Machine Learning
Regression Models – Example Linear Regression Model
Y = F(X, A) + R Y = X*A + R
n samples
1 532.23 353.32
▪ intercept (just ones) 1 334.12 642.23
▪ explanatory variable 1 1 325.23 753.64
▪ explanatory variable 2 1 126.23 752.36
▪ … … … …
A_1 A_2
How to determine A (matrix)? → Optimization ??? ???
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 29
Chapter 05: Data Science Analysis & Machine Learning
Regression Models For Non-Linear Data
▪ Regression models by their design assume that the data follows a specific function
e.g. a linear trend, a logistic curve, polynomial, logarithm, etc.
▪ You could use non-linear models and find reasonable parameters to fit the data
▪ but the optimization can become complex and numeric
▪ You could also linearize data (i.e., transforming Y or X)
▪ e.g. if the relationship is logarithmic,
you could do a linear regression on exp(y) instead of y
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 30
Chapter 05: Data Science Analysis & Machine Learning
Advantages of Linear Regression
▪ The optimization (fitting the curve to data) is computationally much less expensive
▪ The optimization can (often) be conducted analytically instead of numerically
▪ There are many tests and tools for linear regression models
▪ You can easily interpret linearized non-linear relationships
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 31
Chapter 05: Data Science Analysis & Machine Learning
Regression Models For Specific Data
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 32
Chapter 05: Data Science Analysis & Machine Learning
Optimization – How to optimize?
▪ Goal function should try to make the errors R (residuals) as small as possible,
then the model F can perfectly explain Y if data X is available
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 33
Chapter 05: Data Science Analysis & Machine Learning
Optimization – Analytic Solution: Ordinary Least Squares for Linear Regression
▪ BUT: imposes various assumptions on y and X, issues with inversion of XTX, etc.
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 34
Chapter 05: Data Science Analysis & Machine Learning
Optimization – Numerical Solution (Common Today)
▪ Examples
▪ Gradient Descent
▪ BFGS (Broyden-Fletcher-Goldfarb-Shanno)
▪ Nelder-Mead Simplex
▪ Heuristics (such as Evolutionary Algorithms)
▪ …
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 35
Chapter 05: Data Science Analysis & Machine Learning
Optimization – Easy Example With Python
# Imports
import numpy as np
import [Link] as plt
from [Link] import mean_squared_error
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 36
Chapter 05: Data Science Analysis & Machine Learning
Optimization – Easy Example With Python
# ...
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 37
Chapter 05: Data Science Analysis & Machine Learning
Optimization – Easy Example With Python
# ...
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 38
Chapter 05: Data Science Analysis & Machine Learning
Interpretation of Regression Analysis
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 39
Chapter 05: Data Science Analysis & Machine Learning
Interpretation of Regression Analysis
intercept
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 40
Chapter 05: Data Science Analysis & Machine Learning
Interpretation of Regression Analysis
explanatory variables
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 41
Chapter 05: Data Science Analysis & Machine Learning
Interpretation of Regression Analysis regression model and
dependent variable
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 42
Chapter 05: Data Science Analysis & Machine Learning
Interpretation of Regression Analysis coefficient (model parameter)
and t-test statistic below
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 43
Chapter 05: Data Science Analysis & Machine Learning
Interpretation of Regression Analysis Fit of the model (R-squared)
measures similar to L2 norm
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 44
Chapter 05: Data Science Analysis & Machine Learning
Interpretation of Regression Analysis F-test assesses overall quality
and significance of the model with
all parameters
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 45
Chapter 05: Data Science Analysis & Machine Learning
Interpretation of Regression Analysis The explanatory variable was non-
linearly distributed, so
linearization improved the model
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 46
Chapter 05: Data Science Analysis & Machine Learning
Interpretation of Regression Analysis
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 47
Chapter 05: Data Science Analysis & Machine Learning
▪ Descriptive Statistics For Distributional Analysis
▪ Test Theory & Statistics
▪ What is a hypothesis test?
▪ Single-sided vs two-sided tests
▪ Parametric vs non-parametric tests
▪ Data preprocessing, cleansing & outlier detection
▪ Regression Models
▪ Machine Learning
▪ What is Machine Learning?
▪ Supervised Learning
▪ Unsupervised Learning
▪ Reinforcement Learning
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 48
Chapter 05: Data Science Analysis & Machine Learning
What is Machine Learning?
▪ Currently:
▪ You define a problem.
▪ You think about how to solve a problem and define an algorithm
▪ You implement the algorithm (e.g., in Python)
▪ You run the software, observe it, evaluate it, and refine it
▪ Outcome: working programme that solves a problem
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 49
Chapter 05: Data Science Analysis & Machine Learning
What is Machine Learning?
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 50
Chapter 05: Data Science Analysis & Machine Learning
What is Machine Learning?
▪ More non-linearity, more data, and more parameters does not necessarily
mean better outcomes!
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 51
Chapter 05: Data Science Analysis & Machine Learning
What is Machine Learning?
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 52
Chapter 05: Data Science Analysis & Machine Learning
Supervised Machine Learning
▪ Machine Learning:
▪ with labelled training data
▪ X – training data
▪ Y – label data
▪ Problem Categories:
▪ Y (continuous variable) – Regression Problems (also time series prediction)
▪ Y (discrete variable) – Classification Problems
▪ Algorithms / Models:
▪ Support-vector machines - K-nearest neighbor algorithm
▪ Linear & Logistic regression - Neural networks (Multilayer perceptrons)
▪ Naïve Bayes - Similarity learning
▪ Linear discriminant analysis
▪ Decision trees
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 53
Chapter 05: Data Science Analysis & Machine Learning
Unsupervised Machine Learning
▪ Machine Learning:
▪ with unlabeled training data
▪ X – training data
▪ Y – label data
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 54
Chapter 05: Data Science Analysis & Machine Learning
Limitations of Machine Learning: Bias-Variance-Tradeoff
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 55
Chapter 05: Data Science Analysis & Machine Learning
Reinforcement Learning
▪ Machine Learning:
▪ with labelled training data
▪ X – training data
▪ Y – label data
▪ But the labelled data are created automatically during the learning process
by interactions with the environment (e.g. simulator, video game)
▪ Major goal: train an agent to learn to interact with environment
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 56
Chapter 05: Data Science Analysis & Machine Learning
What is Generative AI?
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 57
Chapter 05: Data Science Analysis & Machine Learning
▪ Descriptive Statistics For Distributional Analysis
▪ Test Theory & Statistics
▪ What is a hypothesis test?
▪ Single-sided vs two-sided tests
▪ Parametric vs non-parametric tests
▪ Data preprocessing, cleansing & outlier detection
▪ Regression Models
▪ Machine Learning
▪ What is Machine Learning?
▪ Supervised Learning
▪ Unsupervised Learning
▪ Reinforcement Learning
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 58
Chapter 05: Data Science Analysis & Machine Learning
Recommended Readings
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 59
Contact Information
ETH Zürich
Institut für Verkehrsplanung und Transportsysteme (IVT)
HIL F 41.2
Stefano-Franscini-Platz 5
8093 Zürich, Switzerland
[Link]
[Link]/
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 61
Chapter 05: Interactive Part
Meteo Swiss Dataset → Time Series Analysis & Prediction
Computer Programming & Data Science – Introduction with Python, Kevin Riehl, Milos Balac, IVT Institute 10.04.2025 62