liveBook · Manning

Scikit-learn

As explained in Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI

Scikit-learn is an open-source Python library providing classical machine learning algorithms, preprocessing utilities, model evaluation tools, and workflow components.

Scikit-learn is an open-source Python library for classical machine learning on tabular data. It combines preprocessing, model building, evaluation, and reusable workflows behind a consistent API, making it useful for building a robust baseline before moving to more specialized approaches. section 4.0 section 4.1

Its classes use a common learning interface. An estimator learns from data with .fit; transformers map data with .transform; predictors return outcomes with .predict or probabilities with .predict_proba. Learned parameters and other fitted information are exposed as object attributes. section 4.1.1 section 4.1.2

A typical tabular-data workflow is:

  • Create or load features and a target, then select preprocessing and a model. section 4.1
  • Chain preprocessing and modeling in a Pipeline so the same steps can be reused. section 4.1 section 4.1.1
  • Use cross-validation to evaluate performance before refitting the selected pipeline on all available data. section 4.1
  • Keep training data separate from validation and test data when fitting and assessing models. section 4.1.2

Classification pipeline

A synthetic balanced classification dataset is standardized with StandardScaler, passed to LogisticRegression in a Pipeline, evaluated by cross-validation accuracy, and then refit when performance is suitable. The point is the repeatable procedure rather than the particular model. section 4.1

The library covers classification, regression, clustering, dimensionality reduction, preprocessing, and model selection. It accepts NumPy arrays and sparse matrices, and later support includes pandas DataFrames with retained column information and transformations in version 1.1.3 onward. section 4.1.1

GradientBoostingClassifier and GradientBoostingRegressor provide gradient boosting for classification and regression. Their decision-tree ensembles offer detailed parameter control, but training can take considerable time because decision trees are the training bottleneck. section 5.3

Listing 4.1 Example using Scikit-learn for a classification problem
import numpy as np
from sklearn.datasets import make_classification
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_validate
from sklearn.pipeline import Pipeline
 
X, y = make_classification(n_features=32,
                           n_redundant=0,
                           n_informative=24,
                           random_state=1,
                           n_clusters_per_class=1
                           )                            #1
 
model = LogisticRegression()                            #2
 
pipeline = Pipeline(
    [('processing', StandardScaler()),
     ('modeling', model)])                              #3
 
cv_scores = cross_validate(estimator=pipeline, 
                           X=X, 
                           y=y,
                           scoring="accuracy",
                           cv=5)                        #4
 
mean_cv = np.mean(cv_scores['test_score'])
std_cv = np.std(cv_scores['test_score'])
print(f"accuracy: {mean_cv:0.3f} ({std_cv:0.3f})")      #5
 
model.fit(X, y) 

Inspect a complete minimal workflow that generates data, standardizes features, fits logistic regression, and cross-validates accuracy. section 4.1

How Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI covers it

How deep it goes
Explains the common estimator API and workflow, while deferring lengthy mathematics and model-specific theory.
Examples
Demonstrates synthetic binary classification with StandardScaler and LogisticRegression, plus Airbnb listing-value classification with gradient boosting.
What it assumes
Assumes Python-style classes and tabular features, then teaches estimator operations from basic .fit, .predict, and .transform calls.
What you can do after
Apply it via scikit-learn to assemble pipelines, evaluate models with cross-validation, and refit suitable tabular-data models.
How it connects
Connects pipelines, cross-validation, logistic regression, preprocessing, train-test separation, pandas DataFrames, and gradient boosting.

Also explained in

Compare how 5 books explain scikit-learn →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Machine Learning for Tabular Data: XGBoost, Deep Learning, and AI. The section links point to the parts of the book it draws on.