By the end of this lesson, you will understand the core workflow for building a predictive model and be able to implement a basic linear regression using Python.
What it is
Model building is the process of creating a mathematical representation of real-world phenomena. In data analytics, we use algorithms to learn patterns from historical data (training data) so we can make predictions or decisions about new, unseen data. The most common type is supervised learning, where the algorithm learns from labeled examples (e.g., predicting house prices based on square footage).
Key terms include:
- Features (
X): Input variables used for prediction. - Target (
y): The output variable we want to predict. - Training: Fitting the model to known data.
- Inference: Using the trained model to predict on new data.
Why it matters
- Automation: Replaces manual rule-based decision making with data-driven logic.
- Scalability: Models can process millions of records faster than humans.
- Insight: Reveals hidden relationships between variables that are not obvious in raw data.
- Forecasting: Enables proactive planning by predicting future trends (sales, demand, risk).
Syntax or steps
The standard workflow for building a simple model involves four steps:
- Data Preparation: Clean data and split features (
X) from target (y). - Model Selection: Choose an algorithm (e.g., Linear Regression).
- Fitting: Train the model using
.fit(X_train, y_train). - Prediction: Generate outputs using
.predict(X_new).
Example
This example uses scikit-learn to build a linear regression model predicting sales based on advertising spend.
import numpy as np
from sklearn.linear_model import LinearRegression
# 1. Data Preparation
# Simulated data: Advertising Spend (X) vs Sales (y)
X = np.array([[10], [20], [30], [40], [50]]) # Features
y = np.array([100, 200, 300, 400, 500]) # Target
# 2. Model Selection
model = LinearRegression()
# 3. Fitting (Training)
model.fit(X, y)
# 4. Prediction (Inference)
new_spend = np.array([[60]])
predicted_sales = model.predict(new_spend)
print(f"Predicted Sales for $60 spend: {predicted_sales[0]:.2f}")
print(f"Coefficient (Slope): {model.coef_[0]}")
print(f"Intercept: {model.intercept_}")
Explanation:
Xmust be a 2D array (matrix), even for single features, hence the double brackets[[...]].model.fit()calculates the best line through the data points.model.predict()applies the learned formula to new inputs.coef_represents how much sales increase per unit of spend.
Common mistakes
- Shape Mismatch: Passing 1D arrays instead of 2D matrices for features. Fix: Use
X.reshape(-1, 1)if needed. - Data Leakage: Including information in training data that wouldn't be available at prediction time. Fix: Strictly separate train/test sets before preprocessing.
- Ignoring Scale: Some models perform poorly if features have vastly different ranges. Fix: Normalize or standardize features.
- Overfitting: Training too long or using too complex a model for small data. Fix: Use cross-validation and simpler models first.
When to use it
Linear regression is ideal for continuous numerical targets with linear relationships. For classification tasks (Yes/No), use Logistic Regression.
| Task Type | Target Variable | Recommended Model |
|---|---|---|
| Regression | Continuous (Price, Temp) | Linear Regression |
| Classification | Categorical (Spam/Ham) | Logistic Regression |
| Clustering | None (Unlabeled) | K-Means |
Practice
Guided Exercise: Modify the code above to predict sales for an advertising spend of $80. What is the result?
Challenge: Add a second feature "Social Media Spend" to X. How does the shape of X change? (Hint: It becomes (n_samples, 2)).
Quick check
Q: Why do we need to call .fit() before .predict()?
A: .fit() trains the model by learning parameters from the data. Without fitting, the model has no knowledge of the relationship between features and target.
Summary
Model building transforms raw data into actionable insights through a structured pipeline: prepare, select, fit, and predict. Mastering this workflow allows analysts to automate decisions and uncover patterns efficiently.