Skip to main content

Your First Scikit-learn Model

From CSV to a trained classifier in under 30 lines of Python.

AI & AutomationBeginner 12 min read 5 lessons
PythonML

What You'll Learn

  • Load and explore a dataset with Pandas
  • Preprocess features with Scikit-learn pipelines
  • Train and evaluate a classification model
  • Interpret a confusion matrix and classification report
  • Save and load a trained model with joblib

Prerequisites

  • Python 3.9+ installed
  • pip install pandas scikit-learn matplotlib
  • Basic Python (lists, dicts, functions)

Step-by-Step Guide

  1. Step 1

    1. Load & Explore the Data

    import pandas as pd; df = pd.read_csv('data.csv'). Run df.head(), df.info(), df.describe(), and df.isnull().sum() to understand shape, types, and missing values. For a classification task, check class balance: df['target'].value_counts(). Imbalanced classes (90:10) require stratified splitting and adjusted metrics — don't just optimize for accuracy.

  2. Step 2

    2. Preprocess with a Pipeline

    Use ColumnTransformer + Pipeline to avoid data leakage: numeric features → SimpleImputer(strategy='median') + StandardScaler. Categorical features → SimpleImputer(strategy='most_frequent') + OneHotEncoder(handle_unknown='ignore'). Wrap the preprocessor and model in a Pipeline so fit/transform always applies to train data only. This is the most common source of bugs in ML projects.

  3. Step 3

    3. Split & Train

    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, stratify=y). Always set random_state for reproducibility. Train a Logistic Regression baseline first: model.fit(X_train, y_train). A simple model that works beats a complex model that's wrong.

  4. Step 4

    4. Evaluate Properly

    from sklearn.metrics import classification_report, confusion_matrix, ConfusionMatrixDisplay. Print classification_report(y_test, model.predict(X_test)) — this shows precision, recall, and F1 per class. Plot the confusion matrix to see which classes are confused. If precision is your concern (spam filter), optimize for it. If recall (cancer detection), optimize for that. Accuracy alone is misleading for imbalanced data.

  5. Step 5

    5. Save & Load the Model

    import joblib; joblib.dump(model, 'model.pkl'). Load later with: model = joblib.load('model.pkl'). Because the Pipeline includes the preprocessor, you just call model.predict(new_data) without re-applying transformations manually. Version your model files alongside your code. For production, consider MLflow or a model registry instead of raw pickle files.

Further Reading & Resources

Ready to put this into practice?

Apply for a real AI & Automation internship and ship production projects with mentorship.

Related Tutorials