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:
    1. ML project workflow overview (data collection to deployment).
    2. Hands-on introduction to Scikit-learn—fitting and predicting.
    3. Live coding demonstration.
    4. 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:

  1. 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().
  2. 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).
  3. 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.
  4. 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.
  5. 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).
  6. 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.
  7. Hyperparameter Tuning

    • Grid search or randomized search to optimize hyperparameters.
    • Tools: GridSearchCV, RandomizedSearchCV in Scikit-learn.
  8. 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:
    1. Import data and split—best practice: fixed random_state for reproducibility.
    2. Use Pipeline to bundle preprocessing and model—keeps code organized and avoids data leakage.
    3. Fit and evaluate quickly with classification_report.
  • 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:
    1. Load the Wine dataset (load_wine).
    2. Create a pipeline with MinMaxScaler and a KNeighborsClassifier.
    3. Split data (train/test = 70/30), fit the pipeline, and print accuracy.
  • Bonus:
    • Experiment with different neighbor counts (k=3,5,7) and observe changes in performance.