What is a scientific toolkit for learning (scikit-learn)
Scikit-learn is an open-source Python library widely used for machine learning tasks. It provides simple and efficient tools for data preprocessing, feature extraction, model selection, and a variety of supervised and unsupervised learning algorithms. Built on NumPy, SciPy, and Matplotlib, it is designed for ease of use, flexibility, and seamless integration into Python workflows. Scikit-learn is ideal for prototyping and deploying machine learning models in diverse applications.
Why scikit-learn
Why Scikit-learn is Widely Used
- Ease of Use:
- Consistent and intuitive API.
- Simplifies the process of implementing machine learning models.
- Comprehensive Toolset:
- Offers algorithms for classification, regression, clustering, and dimensionality reduction.
- Includes tools for data preprocessing, feature selection, and model evaluation.
- Integration with Python Ecosystem:
- Seamlessly works with NumPy, SciPy, Pandas, and Matplotlib.
- Facilitates data analysis and visualization in Python workflows.
- Efficiency and Performance:
- Optimized implementations of machine learning algorithms.
- Suitable for both small-scale and large-scale datasets.
- Open Source and Community Support:
- Actively maintained with regular updates.
- Extensive documentation and tutorials available.
- Large community of users and contributors.
- Prototyping and Deployment:
- Ideal for quickly testing ideas and deploying models in production.
- Compatible with other machine learning tools and frameworks.
Scikit-learn’s simplicity, flexibility, and robust feature set make it a cornerstone of modern machine learning projects.
Preface
This topic was briefly introduced in my other blog post How do I work with data using a Machine Learning Model? This time I will go deeper into the details. It will be in a more organized fashion. I will introduce the concept of Machine Learning Workflow. Using the technological stack introduced below, the workflow can be applied to almost any classical machine-learning model.
The whole topic is split into four parts:
- scikit-learn library explained by a practical example with ML Workflow.
- scikit-learn example with Exploratory Data Analysis.
- scikit-learn example with ML model fine-tuning.
- Machine Learning Workflow explained.
It looks like an upside-down order, but I know all like practical examples instead of theory.
The tech stack
- pandas
- NumPy
- matplotlib
- scikit-learn
- Python
- Conda
- Jupyter Notebook
Machine Learning Workflow – Introduction
- Data – load & analyze
- Model – choose ML model
- Split the data – features, labels; training, testing
- Train & Predict – ML model
- Evaluate – ML model
- Tune & Improve – ML model

Learning by practical example
Predict whether the patient has heart disease or not based on the medical records.
Import required libraries
Importing all necessary libraries to have them in one place.
# importing the scikit-learn library & checking its version
# importing all required libraries
import pandas as pd
import numpy as np
import joblib
import warnings
import sklearn
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
print(f"Matplotlib version: {sklearn.__version__}")
scikit-learn version: 1.5.1
Machine Learning Workflow – Practice
1. Data – load & analyze
I will work with the data using the pandas library. How to use it I wrote it in more detail in my other blog posts pandas – Data Analysis and Manipulation in Python – Introduction and How do I work with data using a Machine Learning Model?
NOTE: In this case, I assume that we have ideal data: no missing values, all values are numeric.
TODO: Theory with more details will be explained in a separate blog post – Machine Learning Workflow – now just practice.
# Get the data with medical records
# Load the data from CVS into pandas DataFrame
heart_df = pd.read_csv("dataset_heart-attack-analysis-prediction-dataset_kaggle-rashikrahmanpritom.csv")
# Display loaded data - first 5 rows
heart_df.head()
| age | sex | cp | trtbps | chol | fbs | restecg | thalachh | exng | oldpeak | slp | caa | thall | output | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 63 | 1 | 3 | 145 | 233 | 1 | 0 | 150 | 0 | 2.3 | 0 | 0 | 1 | 1 |
| 1 | 37 | 1 | 2 | 130 | 250 | 0 | 1 | 187 | 0 | 3.5 | 0 | 0 | 2 | 1 |
| 2 | 41 | 0 | 1 | 130 | 204 | 0 | 0 | 172 | 0 | 1.4 | 2 | 0 | 2 | 1 |
| 3 | 56 | 1 | 1 | 120 | 236 | 0 | 1 | 178 | 0 | 0.8 | 2 | 0 | 2 | 1 |
| 4 | 57 | 0 | 0 | 120 | 354 | 0 | 1 | 163 | 1 | 0.6 | 2 | 0 | 2 | 1 |
2. Model – choose ML model
Choosing a machine learning model is super important. In this blog post, I will show how to pick the right machine learning model using the scikit-learn – algorithm cheat sheet. The full interactive version can be found on scikit-learn > User Guide > 12. Choosing the right estimator.
TODO: Theory and more details will be explained in a separate blog post – Machine Learning Workflow – now just practice.

Creating the machine learning model
Thanks to the scikit-learn library it’s as simple as that!
# Instantiate LogisticRegression to create a Machine Learning Model model = LogisticRegression()
3. Split the data – features, labels; training, testing
TODO: Theory with more details will be explained in a separate blog post – Machine Learning Workflow – now just practice.
Features and labels
# X - training input samples, features
# X - contains all features/columns except 'output'
X = heart_df.drop("output", axis=1)
X.head()
| age | sex | cp | trtbps | chol | fbs | restecg | thalachh | exng | oldpeak | slp | caa | thall | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 63 | 1 | 3 | 145 | 233 | 1 | 0 | 150 | 0 | 2.3 | 0 | 0 | 1 |
| 1 | 37 | 1 | 2 | 130 | 250 | 0 | 1 | 187 | 0 | 3.5 | 0 | 0 | 2 |
| 2 | 41 | 0 | 1 | 130 | 204 | 0 | 0 | 172 | 0 | 1.4 | 2 | 0 | 2 |
| 3 | 56 | 1 | 1 | 120 | 236 | 0 | 1 | 178 | 0 | 0.8 | 2 | 0 | 2 |
| 4 | 57 | 0 | 0 | 120 | 354 | 0 | 1 | 163 | 1 | 0.6 | 2 | 0 | 2 |
# y - training input labels, the desired result, the target value # y - contains only the label/column 'output' y = heart_df["output"] y.head()
0 1 1 1 2 1 3 1 4 1 Name: output, dtype: int64
Training and testing datasets
# Split the data into training and test sets # train_test_split() function splits arrays or matrices into random train and test subsets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Printing information about the length of splitted datasets
print(f"{len(X)} - number of elements in the entire dataset.")
print(f"{len(X_train)} - number of training elements.")
print(f"{len(X_test)} - number of testing elements.")
print(f"{len(X_train) + len(X_test)} - sum of training and testing elements.")
303 - number of elements in the entire dataset. 242 - number of training elements. 61 - number of testing elements. 303 - sum of training and testing elements.
4. Train & Predict – ML model
TODO: Theory with more details will be explained in a separate blog post – Machine Learning Workflow – now just practice.
Training model aka fitting
# fit() function builds a forest of trees from the training set (X, y) model.fit(X_train, y_train)

Predicting labels for the given features
# Predicting labels # predict() function predicts class for X y_pred = model.predict(X_test) # Show predicted labels y_pred
array([0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0,
0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0])
5. Evaluate – ML model
How to evaluate machine model I wrote in more detail in my other blog post How do I evaluate the Machine Learning Model? Does it perform well? Now just practice.
# score() function returns the mean accuracy on the given test data and labels model.score(X_test, y_test)
0.8852459016393442
6. Tune & Improve – ML model
TODO: Theory and practice with more details will be explained in a separate blog post – Machine Learning Workflow.
The whole code in one place
# WORKFLOW - 1. Data - load & analyze
# Get the data with medical records
# Load the data from CVS into pandas DataFrame
heart_df = pd.read_csv("dataset_heart-attack-analysis-prediction-dataset_kaggle-rashikrahmanpritom.csv")
heart_df.head()
# WORKFLOW - 2. Model - choose ML model
# Instantiate LogisticRegression to create a Machine Learning Model
model = LogisticRegression()
# WORKFLOW - 3. Split the data - features, labels; training, testing
# X - training input samples, features
# X - contains all features/columns except 'output'
X = heart_df.drop("output", axis=1)
X.head()
# y - training input labels, the desired result, the target value
# y - contains only the label/column 'output'
y = heart_df["output"]
y.head()
# Split the data into training and test sets
# train_test_split() function splits arrays or matrices into random train and test subsets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# WORKFLOW - 4. Train & Predict - ML model
# fit() function builds a forest of trees from the training set (X, y)
model.fit(X_train, y_train)
# WORKFLOW - 5. Evaluate - ML model
# Predicting labels
# predict() function predicts class for X
y_pred = model.predict(X_test)
# score() function returns the mean accuracy on the given test data and labels
model.score(X_test, y_test)
NOTE: In this article, I’m just barely scratching the surface. This topic needs more reading and research on your own. I’m still at the beginning of my learning process of AI & ML!




Leave a Reply