lassence.net

Field notes from the AI frontier

Train Machine Learning algorithms with Python scikit-learn

--- archive: codebook, 2020–2023 · topic: Python ---

One of the most used Python libraries for Machine Learning is scikit-learn. It provides implementation a broad range of algorithm, and is relatively simple to use out-of-the-box. Let’s look at an example of Logistic Regression with the classic ‘iris’ dataset.

# Import libraries
import pandas as pd
import seaborn as sns
import sklearn
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression

Load dataset

# Load sample dataset into a DataFrame
X = sns.load_dataset('iris')
X
sepal_lengthsepal_widthpetal_lengthpetal_widthspecies
05.13.51.40.2setosa
14.93.01.40.2setosa
24.73.21.30.2setosa
34.63.11.50.2setosa
45.03.61.40.2setosa
………………
1456.73.05.22.3virginica
1466.32.55.01.9virginica
1476.53.05.22.0virginica
1486.23.45.42.3virginica
1495.93.05.11.8virginica
# Pop the target column 'species' and store in a distinct Series
y = X.pop('species')

Split into train and test sets

# Use `train_test_split()` function from sklearn to extract a 20% random test set 
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

Fit model

# Instantiate a logistic regression model with default parameters
model = LogisticRegression()

# Fit model on training set
model.fit(X_train, y_train)

# Predict on test set
y_pred = model.predict(X_test)

Evaluate model performance

# Classification report
print(sklearn.metrics.classification_report(y_test, y_pred))
        precision    recall  f1-score   support

      setosa       1.00      1.00      1.00         6
  versicolor       0.83      1.00      0.91        10
   virginica       1.00      0.86      0.92        14

    accuracy                           0.93        30
   macro avg       0.94      0.95      0.94        30
weighted avg       0.94      0.93      0.93        30
# Plot confusion matrix
sklearn.metrics.plot_confusion_matrix(model, X_test, y_test)
Confusion matrix of the logistic regression on the iris test set