Choose and Tune a Model: Classic Algorithms to a Saved Pipeline
Who it is for
What you will achieve
Prerequisites
Curriculum
- Step 1: K-Nearest Neighbours (KNN): Distance, k and Why Scaling Is a Must
Price a house from its three most similar neighbours, by hand and with KNeighborsRegressor, see scaling swap Pine for Birch, then pick k on the wine data with cross-validation: best k is 17 and the test accuracy is 1.0.
- Step 2: Naive Bayes: Prior Times Likelihood, a Spam Filter by Hand and GaussianNB
Build a four-word spam filter by hand: prior 0.56 vs 0.44, a spam score of exactly zero, Laplace smoothing to fix it and logs so tiny numbers don't vanish. Then GaussianNB scores 0.967 on iris.
- Step 3: Support Vector Machines (SVM): The Widest Road, C, Kernels and gamma
An SVM builds the widest road between two groups, and only the houses on the edge decide it. See C squeeze the road from 4.36 to 1.46, a kernel lift a dose problem from 0.67 to 1.0, gamma overfit two moons, and a scaled SVC reach 0.972 on wine.
- Step 4: Decision Trees: Gini, max_depth and Feature Importances on Rain in Australia
A decision tree plays 20 questions with your data. Compute gini by hand, read a depth-2 rain tree that scores 0.82, watch an unlimited tree hit 1.000 on train and 0.770 on test, and see humidity take 65% of the importance.
- Step 5: Bagging and Random Forest: Many Shaky Trees, One Steady Answer
Five single trees on bootstrap samples score between 0.75 and 0.769 on rain. A forest of 100 trees scores 0.85. See the bootstrap by hand, max_features picking 4 of 16 columns, and where adding trees stops helping.
- Step 6: Boosting: Residuals by Hand, AdaBoost, Gradient Boosting and XGBoost
Each new small tree fixes what the team still gets wrong. Watch the MSE fall from 36.89 to 4.49 in three rounds on six kids, then AdaBoost 0.831, GradientBoosting 0.845 and XGBoost 0.846 on the rain data.
- Step 7: Voting and Stacking: Does a Team of Models Beat the Best One?
Three different judges on the rain data: logistic regression 0.846, KNN 0.836, a tree 0.823. Hard and soft voting land in between; stacking reaches 0.848, just four more days right out of 2,000. An honest look at when a team helps.
- Step 8: Hyperparameter Tuning with GridSearchCV and RandomizedSearchCV
Let scikit-learn try the settings for you. A random forest grid of 6 candidates (30 fits) lifts F1 from 0.558 to 0.630, and a random search of 10 out of 54 XGBoost combinations finds the same winner as the full grid, F1 0.649, in a third of the time.
- Step 9: Which Features Matter? Feature Importances, Permutation Importance and SHAP
Ask a tuned rain model why. Tree importances show what the trees used, permutation importance shows what the score needs (shuffling Humidity3pm drops F1 from 0.637 to 0.453), and SHAP explains one day the model is 97% sure will rain.
- Step 10: Project: The Classification Master Template, from Raw CSV to a Saved Model
One notebook that takes Rain in Australia from raw CSV to a tested, saved model: six models auditioned on the same folds, the best two tuned, a 0.4 threshold picked from out-of-fold probabilities, and one test run with AUC 0.872.