Designing an end-to-
end machine
learning use case
END-TO-END MACHINE LEARNING
Joshua Stapleton
Machine Learning Engineer
The case study
Predicting heart disease
Goal: inform decision-making of
cardiologists
1 Image source: [Link]
END-TO-END MACHINE LEARNING
The model's role
Models can inform, but should not make decisions
Especially important in healthcare
END-TO-END MACHINE LEARNING
The machine learning lifecycle
END-TO-END MACHINE LEARNING
Understanding end user requirements
Accuracy Reliability
Security Interpretability
END-TO-END MACHINE LEARNING
Data collection
Collect relevant data
Private dataset from company
Public dataset
Understand data and context
Representation and measurement
Potential bias
END-TO-END MACHINE LEARNING
Let's practice!
END-TO-END MACHINE LEARNING
Exploratory Data
Analysis
END-TO-END MACHINE LEARNING
Joshua Stapleton
Machine Learning Engineer
The EDA process
Examine and analyse the dataset
Understand the dataset
Visualize the dataset
Characterize / classify the dataset
END-TO-END MACHINE LEARNING
Understanding our data
[Link]() [Link]()
Shows first rows of the dataset Summarizes features
Provides snapshot of data's structure Shows non-null entries and feature types
# Print the first 5 rows # Print out details
print(heart_disease_df.head()) print(heart_disease_df.info())
END-TO-END MACHINE LEARNING
Class (im)balance
df.value_counts()
Counts number of unique occurrences of each class
Class: binary presence of heart disease (1/0)
Important for modeling
# print the class balance
print(heart_disease_df['target'].value_counts(normalize=True))
END-TO-END MACHINE LEARNING
Missing values
Can lead to errors
Unrepresentative, biased results
Use [Link]()
Checks for null/empty/missing values
Applied to column or collection of columns
Usage
# check whether all values in a column are null
print(heart_disease_df['oldpeak'].isnull().all())
True
END-TO-END MACHINE LEARNING
Outliers
Anomalous values
Measurement errors
Data entry errors
Rare events
Can skew model performance
Model learns based on extreme values
Doesn't capture general data trend
Sometimes can be useful:
Rare values
Detection: use boxplot, or IQR
END-TO-END MACHINE LEARNING
Visualizing our data
Visualizations show: df['age'].plot(kind='hist')
[Link]('Age')
General trends
[Link]('Frequency')
Missing values and outliers [Link]()
Other types of visualizations:
Kernel density estimation
Empirical cumulative distributions
Bivariate distributions
1[Link] [Link]
data-visualization-with-seaborn
END-TO-END MACHINE LEARNING
Goals of EDA
Understand the data Detect outliers
Are there any patterns? Does any data fall outside what is
acceptable?
Eg: do men have higher rate of heart
disease? Are there incorrect or missing values?
Formulate hypotheses Check assumptions
What should we expect from the data? Does what we expect line up with reality?
END-TO-END MACHINE LEARNING
Let's practice!
END-TO-END MACHINE LEARNING
Data preparation
END-TO-END MACHINE LEARNING
Joshua Stapleton
Machine Learning Engineer
Data preparation steps
Dataset has: Data preparation:
Missing values Based on insights from EDA
Outliers Critical for model performance downstream
Imbalances
Empty columns
Duplicates
END-TO-END MACHINE LEARNING
Null / empty values
Drop missing or sparse rows/columns
Null values can break model
Use [Link]() for columns
Use [Link](how='all') for rows
# count missing values
print(df['oldpeak'].isnull().sum())
# Drop empty column(s) and row(s)
columns_dropped = heart_disease_df.drop(['oldpeak'], axis='columns')
rows_and_columns_dropped = columns_dropped.dropna(how='all')
END-TO-END MACHINE LEARNING
Dealing with null / empty values
Data cleaning / dropping values depends on EDA findings
If given column has too many missing values:
Drop column
If target column has missing values:
Drop rows with missing targets
Or treat as separate category
END-TO-END MACHINE LEARNING
Imputation
What to do when there are only a few missing values?
Imputation:
Fill missing values with substitutes
Strategies
Fill with mean or median
Use constant or previous value
# Calculate the mean cholestrol value
mean_value = heart_disease_df['chol'].mean()
# Fill missing cholestrol values with the mean
heart_disease_df['chol'].fillna(mean_value, inplace=True)
END-TO-END MACHINE LEARNING
Advanced imputation
Advanced techniques:
K-nearest neighbors
SMOTE (synthetic minority oversampling technique)
from [Link] import KNNImputer
# Initialize KNNImputer
imputer = KNNImputer(n_neighbors=2, weights="uniform")
# Perform the imputation on your DataFrame
df_imputed['oldpeak'] = imputer.fit_transform(df['oldpeak'])
END-TO-END MACHINE LEARNING
Dropping duplicates
Data must be clean, concise, and rich
Redundancies are unhelpful
Duplicates can bias or confuse model
Look at unique identifiers as a criteria for dropping records / rows.
# Drop duplicate rows
heart_disease_duplicates_dropped = heart_disease_column_dropped.drop_duplicates()
END-TO-END MACHINE LEARNING
Let's practice!
END-TO-END MACHINE LEARNING