What is Supervised Machine Learning?
In [Supervised Learning], we train a model by providing it with a paired dataset containing both input features and their correct output targets. The model learns a mathematical mapping to link inputs to outputs, allowing it to predict unseen test values.
- Supervised learning is divided into two major types:
- [Regression]: Predicting continuous, numerical values (e.g., forecasting house prices based on square footage, predicting future temperatures).
- [Classification]: Predicting discrete, categorical class labels (e.g., flagging emails as spam or not spam, diagnosing benign or malignant tumors).
+-----------------------+
| Supervised Learning |
+-----------------------+
/ \
/ \
+------------------+ +--------------------+
| REGRESSION | | CLASSIFICATION |
| (Continuous) | | (Discrete) |
| e.g., $450,000 | | e.g., [Spam / OK]|
+------------------+ +--------------------+
In this lesson, we will build a linear regression model to predict housing prices using Python's premier tool: Scikit-Learn.
---
The Core Principles of Training
To ensure our model generalizes well to new data rather than just memorizing our training dataset, we partition our data using a [Train-Test Split]:
* Training Set (typically 80%): Used by the algorithm to adjust its internal mathematical parameters.
* Testing Set (typically 20%): Saved as a hidden dataset to evaluate how the model handles new, unseen information.
---
Coding a Linear Regression Model
Create a new file called house_predictor.py and run the code below. We will generate a synthetic dataset representing house sizes (features) and prices (targets), split the data, train our model, and make dynamic predictions.
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
1. Generate synthetic housing data
Let's assume size is in square feet (sqft) and price is in dollars ($)
np.random.seed(42) # Ensures identical random numbers for reproducibility sizes = np.random.randint(1000, 4000, size=(100, 1))Price formula: Price = Size * 150 + random noise (for variance)
prices = sizes * 150 + np.random.normal(0, 15000, size=(100, 1))2. Perform Train-Test Split (80% training, 20% testing)
X_train, X_test, y_train, y_test = train_test_split(sizes, prices, test_size=0.2, random_state=42)3. Initialize and train the model
model = LinearRegression() model.fit(X_train, y_train) print("--- Model Training Complete! ---") print(f"Learned Rate: ${model.coef_[0][0]:.2f} per sqft") print(f"Base Price Intercept: ${model.intercept_[0]:.2f}\n")4. Evaluate the model using the test set
y_pred = model.predict(X_test) mse = mean_squared_error(y_test, y_pred) print(f"Mean Squared Error on Test Set: {mse:.2f}")5. Make a real prediction for a customer
new_house_size = np.array([[2500]]) predicted_price = model.predict(new_house_size) print(f"Predicted price for a 2,500 sqft house: ${predicted_price[0][0]:,.2f}")#### Output Analysis
Run the predictive script in your terminal:
python house_predictor.py
Terminal Output:
--- Model Training Complete! ---
Learned Rate: $148.91 per sqft
Base Price Intercept: $1424.01
Mean Squared Error on Test Set: 195724213.11
Predicted price for a 2,500 sqft house: $373,695.42
Our trained model has extracted the underlying trend from noisy data: every additional square foot costs roughly $148.91. If a user enters a custom house size of 2500, the system automatically processes the mathematical formula to output a realistic estimate of $373,695.42.
---
Frequently Asked Questions (FAQs)
#### Q1: What is the difference between Features and Labels?
Features (often abbreviated as X) are the input measurements used to make a prediction (e.g., size, number of bedrooms, ZIP code). The label or target (often abbreviated as y) is the actual outcome we want our model to predict (e.g., the house price).
#### Q2: What is Overfitting?
Overfitting occurs when a machine learning model memorizes the noise and specific details of its training data instead of learning the underlying trend. While it performs perfectly on training data, it fails when processing new, unseen testing data.
#### Q3: How do we choose between Regression and Classification algorithms?
Look at the target variable you want to predict. If the target is a continuous number (e.g., stock price, temperature), use regression. If the target is categorical (e.g., dog vs. cat, low risk vs. high risk), use classification.
