Module 27
Scikit-learn
Train, evaluate, and tune regression, classification, and clustering models with scikit-learn, from train and test splits to pipelines and saved models.
01
What Is Scikit-learn
Meet scikit-learn, the Python library for machine learning: every model is an estimator you create, fit on examples, and then ask to predict.
02
Features and Labels
Split a table into X, the two-dimensional feature table, and y, the answer column, and learn why models need numbers, double brackets, and reshape.
03
Training and Test Sets
Hold back rows the model never sees with train_test_split, repeat the split with random_state, keep class balance with stratify, and score honestly.
04
Linear Regression
Predict a number with LinearRegression: fit a line, read coef_ and intercept_, add more features, and check the fit with the R squared score.
05
Measuring Regression Error
Measure how far regression predictions miss with residuals, mean absolute error, RMSE, and R squared, and compare the model against a simple baseline.
06
Logistic Regression
Predict a yes or no answer with LogisticRegression, read probabilities with predict_proba, pick your own threshold, and check accuracy on test rows.
07
Measuring Classification
Go beyond accuracy with the confusion matrix, precision, recall, and F1, and see why a model that never says yes can still score high accuracy.
08
K-Nearest Neighbours
Classify a learner by the votes of the most similar learners with KNeighborsClassifier, choose n_neighbors, and see why distance needs scale.
09
Decision Trees
Build a DecisionTreeClassifier, read its questions with export_text, control memorising with max_depth, and see which features the tree relies on.
10
Random Forests
Combine many decision trees with RandomForestClassifier, see how their votes beat a single tree, and tune n_estimators and max_depth for new data.
11
Feature Scaling
Put features on comparable scales with StandardScaler and MinMaxScaler, fit the scaler on training rows only, and watch nearest neighbours recover.
12
Encoding Categories
Turn text columns into numbers with OneHotEncoder and OrdinalEncoder, handle unseen categories safely, and feed the encoded columns to a model.
13
Missing Values
Find gaps in a table, see why models refuse NaN, and fill them with SimpleImputer using the mean, median, or most frequent value per column.
14
Pipelines
Chain preprocessing and a model into one object with Pipeline, handle numeric and text columns together with ColumnTransformer, and predict raw rows.
15
Cross-Validation
Score a model on several different test folds with cross_val_score, KFold, and StratifiedKFold, and report a mean and spread instead of one lucky split.
16
Hyperparameter Tuning
Tell learned parameters from settings you choose, search settings with GridSearchCV and cross-validation, and test the winning model exactly once.
17
K-Means Clustering
Group rows without labels using KMeans, read the cluster centres and labels, choose the number of clusters with inertia, and describe each group.
18
Saving and Loading Models
Save a trained model or pipeline with joblib or pickle, load it back to predict later, and store the details needed to use it safely afterwards.