ML Project Workflow & Scikit-learn Introduction
1. Welcome and Icebreaker:
- Icebreaker: “If you could ask an ML model any question, what would it be and why?”
- Encourage students to share stories: maybe they’ve used recommendation engines or spam filters without realizing it’s ML at work.
- Collect a few fun answers and note how ML models help in everyday life (from Netflix suggestions to voice assistants).
2. Outline the Session:
- Agenda:
- ML project workflow overview (data collection to deployment).
- Hands-on introduction to Scikit-learn—fitting and predicting.
- Live coding demonstration.
- Short exercise.
- Learning outcomes:
- Understand end-to-end ML project steps.
- Familiarity with Scikit-learn API patterns.
- Ability to fit a simple model and evaluate it.
- Expectations: interactive Q&A, code exploration on your machine, ask if any step seems unclear.
3. Concept Explanation:
An end-to-end machine learning project is like managing a cooking recipe from sourcing ingredients to serving a meal. Each step matters:
-
Data Collection & Acquisition
- Gathering raw data from sources: CSV files, APIs, databases. Quality of ingredients affects final taste—poor data leads to garbage-in, garbage-out.
- Example: loading the Iris dataset using
sklearn.datasets.load_iris().
-
Data Preprocessing
- Cleaning: handle missing values, remove duplicates.
- Transformation: scaling (StandardScaler), encoding categorical features (OneHotEncoder).
- Pitfall: forgetting to scale features with different magnitudes can skew model training (just as adding too much salt can ruin a dish).
-
Feature Engineering & Selection
- Creating new features or selecting relevant ones. Analogous to chopping ingredients into the right size for efficient cooking.
- Example: combining date fields into a single “day_of_week” feature.
-
Model Selection
- Choosing the algorithm: linear models, tree-based, etc. Think of choosing the right cooking technique: baking vs frying.
- Consider pros and cons: speed, interpretability, performance on your data.
-
Model Training
- Fitting the model to training data. In code:
model.fit(X_train, y_train). - Learning rate and hyperparameters are like cooking temperature—too high can burn (overfitting), too low undercook (underfitting).
- Fitting the model to training data. In code:
-
Evaluation & Validation
- Using validation and test splits. Accuracy, precision, recall metrics.
- Pitfall: testing on data you trained on is like tasting your own cooking before adjusting spice—you get a biased sense.
-
Hyperparameter Tuning
- Grid search or randomized search to optimize hyperparameters.
- Tools:
GridSearchCV,RandomizedSearchCVin Scikit-learn.
-
Deployment
- Exporting trained model, containerizing, serving via API (Flask, FastAPI). Monitoring performance in production.
- Pitfall: model drift—like food spoilage over time if not refrigerated, models need occasional retraining on fresh data.
Why Scikit-learn?
- A user-friendly, consistent API for classical ML algorithms.
- Integrates well with NumPy and pandas.
- Excellent documentation and community support.
4. Live Coding Demonstration:
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
# 1. Load data
dataset = load_iris()
X, y = dataset.data, dataset.target
# 2. Split data
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
# 3. Preprocessing pipeline
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
pipeline = Pipeline([
('scaler', StandardScaler()),
('model', LogisticRegression())
])
# 4. Fit model
pipeline.fit(X_train, y_train)
# 5. Predictions & evaluation
from sklearn.metrics import classification_report
preds = pipeline.predict(X_test)
print(classification_report(y_test, preds))
- Step-by-step:
- Import data and split—best practice: fixed
random_statefor reproducibility. - Use
Pipelineto bundle preprocessing and model—keeps code organized and avoids data leakage. - Fit and evaluate quickly with
classification_report.
- Import data and split—best practice: fixed
- Common mistakes:
- Fitting scaler on entire dataset before splitting—we must fit only on training set.
- Forgetting to shuffle—data order could leak time dependencies.
5. Tasks:
- Short Exercise:
- Load the Wine dataset (
load_wine). - Create a pipeline with
MinMaxScalerand aKNeighborsClassifier. - Split data (train/test = 70/30), fit the pipeline, and print accuracy.
- Load the Wine dataset (
- Bonus:
- Experiment with different neighbor counts (k=3,5,7) and observe changes in performance.