ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing
ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing

All Courses

Introduction to Machine Learning
Chapter 1: What is Machine Learning?
Defining Machine Learning
Why Learn Machine Learning?
Examples of ML in Everyday Life
Distinguishing ML from Traditional Programming
Types of Machine Learning Systems
The Machine Learning Workflow Overview
Tools You Might Use
Quiz for Chapter 1
Chapter 2: Fundamental Concepts
Data: The Fuel for Machine Learning
Features and Labels Explained
Training, Validation, and Test Sets
Models: Learning from Data
Parameters and Hyperparameters
Introduction to Overfitting and Underfitting
Measuring Performance: Basic Metrics
Quiz for Chapter 2
Chapter 3: Supervised Learning: Regression
Understanding Regression Problems
Introduction to Linear Regression
How Linear Regression Learns
Cost Functions: Measuring Error
Gradient Descent: Finding the Best Fit
Simple Linear Regression Example
Practice: Implementing Simple Linear Regression
Quiz for Chapter 3
Chapter 4: Supervised Learning: Classification
Understanding Classification Problems
Introduction to Logistic Regression
The Sigmoid Function
Decision Boundaries
Introduction to K-Nearest Neighbors (KNN)
How KNN Works
Evaluating Classification Models
Practice: Implementing KNN for Classification
Quiz for Chapter 4
Chapter 5: Unsupervised Learning: Clustering
What is Unsupervised Learning?
Introduction to Clustering
The K-Means Algorithm
Choosing the Number of Clusters (K)
How K-Means Finds Clusters
Limitations of K-Means
Practice: Applying K-Means to Simple Data
Quiz for Chapter 5
Chapter 6: Preparing Your Data
The Importance of Data Preprocessing
Handling Missing Values
Introduction to Feature Scaling
Encoding Categorical Features
Splitting Data into Training and Testing Sets Revisited
Hands-on Practical: Basic Data Cleaning Steps
Quiz for Chapter 6
Chapter 7: Building Your First Machine Learning Model
Recap: The ML Workflow Steps
Choosing the Right Algorithm
Loading and Preparing Data with a Library
Training a Simple Model
Making Predictions
Evaluating Model Performance
Hands-on Practical: End-to-End Simple Model Building
Quiz for Chapter 7

Evaluating Model Performance

After data loading and preparation, a machine learning model is trained using algorithms such as Linear Regression or K-Nearest Neighbors and libraries like Scikit-learn. The trained model learns patterns from its training data. A main question then arises: How well did the model learn? Does it make accurate predictions on new, unseen data? Model evaluation addresses these questions.

Remember back in Chapter 2 ("Fundamental Concepts") and Chapter 6 ("Preparing Your Data"), we discussed splitting our dataset into training and testing sets. The training set is used to teach the model, but the test set is kept aside. We evaluate the model's performance on this untouched test set because it gives us a realistic estimate of how the model will perform on brand-new data it hasn't encountered before. Evaluating on the training data itself can be misleading; a model might simply memorize the training examples (overfitting) and perform poorly when faced with slightly different inputs.

The specific metrics you use for evaluation depend heavily on the type of problem you solved: regression or classification.

Evaluating Regression Models

If your goal was to predict a continuous numerical value, like predicting house prices (a regression task, as covered in Chapter 3), you'll use metrics that measure the average error between the model's predictions and the actual values in the test set.

Common regression metrics include:

  1. Mean Absolute Error (MAE): This metric calculates the average of the absolute differences between the predicted values and the actual values. It gives you a straightforward measure of the average magnitude of errors, in the original units of your target variable (e.g., dollars for house prices).

    MAE=1n∑i=1n∣yi−y^i∣MAE = \frac{1}{n} \sum_{i=1}^{n} |y_i - \hat{y}_i|MAE=n1​i=1∑n​∣yi​−y^​i​∣

    Here, nnn is the number of samples in the test set, yiy_iyi​ is the actual value for the iii-th sample, and y^i\hat{y}_iy^​i​ is the predicted value. A lower MAE generally indicates a better fit.

  2. Mean Squared Error (MSE): This metric calculates the average of the squared differences between predictions and actual values. Squaring the errors penalizes larger errors more heavily than smaller ones. The units of MSE are the square of the target variable's units (e.g., dollars squared), which can sometimes be less intuitive.

    MSE=1n∑i=1n(yi−y^i)2MSE = \frac{1}{n} \sum_{i=1}^{n} (y_i - \hat{y}_i)^2MSE=n1​i=1∑n​(yi​−y^​i​)2

    Like MAE, a lower MSE is better. Due to the squaring, MSE is sensitive to outliers (predictions that are very far off).

  3. Root Mean Squared Error (RMSE): This is simply the square root of the MSE. Taking the square root brings the error metric back into the original units of the target variable (e.g., dollars), making it easier to interpret than MSE while still penalizing larger errors.

    RMSE=1n∑i=1n(yi−y^i)2=MSERMSE = \sqrt{\frac{1}{n} \sum_{i=1}^{n} (y_i - \hat{y}_i)^2} = \sqrt{MSE}RMSE=n1​i=1∑n​(yi​−y^​i​)2​=MSE​

    A lower RMSE indicates a better fit to the data.

When using libraries like Scikit-learn, calculating these is straightforward. You typically provide the actual target values from your test set (y_test) and the predictions your model made (y_pred) to functions like mean_absolute_error or mean_squared_error.

# Example using Scikit-learn (assuming y_test and y_pred exist)
from sklearn.metrics import mean_absolute_error, mean_squared_error
import numpy as np

mae = mean_absolute_error(y_test, y_pred)
mse = mean_squared_error(y_test, y_pred)
rmse = np.sqrt(mse) # Or use mean_squared_error(y_test, y_pred, squared=False)

print(f"Mean Absolute Error: {mae}")
print(f"Mean Squared Error: {mse}")
print(f"Root Mean Squared Error: {rmse}")

Evaluating Classification Models

If your goal was to predict a category or class label, like identifying spam emails or classifying images of animals (a classification task, discussed in Chapter 4), you'll use different metrics.

Common classification metrics include:

  1. Accuracy: This is often the first metric people think of. It's simply the proportion of predictions the model got correct.

    Accuracy=Number of Correct PredictionsTotal Number of PredictionsAccuracy = \frac{\text{Number of Correct Predictions}}{\text{Total Number of Predictions}}Accuracy=Total Number of PredictionsNumber of Correct Predictions​

    While easy to understand, accuracy can be misleading, especially if you have an imbalanced dataset (where one class is much more frequent than others). For example, if 95% of emails are not spam, a model that always predicts "not spam" would achieve 95% accuracy but would be useless for identifying actual spam.

  2. Confusion Matrix: This table provides a more detailed breakdown of correct and incorrect predictions for each class. For a binary classification problem (two classes, often labeled Positive and Negative or 1 and 0), it looks like this:

    A confusion matrix summarizes classification performance, showing True Positives (correct positive predictions), True Negatives (correct negative predictions), False Positives (incorrectly predicted positive), and False Negatives (incorrectly predicted negative).

    The confusion matrix allows us to calculate more detailed metrics:

  3. Precision: Out of all the instances the model predicted as Positive, what fraction actually were Positive? High precision means fewer False Positives. It's important when the cost of a False Positive is high (e.g., marking a legitimate email as spam).

    Precision=TPTP+FPPrecision = \frac{TP}{TP + FP}Precision=TP+FPTP​
  4. Recall (Sensitivity or True Positive Rate): Out of all the instances that were actually Positive, what fraction did the model correctly identify? High recall means fewer False Negatives. It's important when the cost of a False Negative is high (e.g., failing to detect a serious disease).

    Recall=TPTP+FNRecall = \frac{TP}{TP + FN}Recall=TP+FNTP​

Often, there's a trade-off between precision and recall. Adjusting a model's threshold might increase one but decrease the other. The specific needs of your application will determine which metric (or balance of metrics) is most significant.

Libraries like Scikit-learn provide functions like accuracy_score, confusion_matrix, and classification_report (which conveniently computes precision, recall, and another metric called F1-score) to evaluate your classifier.

# Example using Scikit-learn (assuming y_test and y_pred exist)
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report

accuracy = accuracy_score(y_test, y_pred)
conf_matrix = confusion_matrix(y_test, y_pred)
class_report = classification_report(y_test, y_pred)

print(f"Accuracy: {accuracy}")
print("\nConfusion Matrix:\n", conf_matrix)
print("\nClassification Report:\n", class_report)

Interpreting the Results

Evaluation isn't just about calculating numbers. It's about interpreting what those numbers mean for your specific problem. An RMSE of 50 might be excellent if you're predicting house prices in millions of dollars, but terrible if you're predicting temperatures in Celsius. An accuracy of 90% might seem good, but a closer look at the confusion matrix might reveal the model performs poorly on a specific, important class.

These evaluation metrics provide objective feedback on your model's performance using the unseen test data. They help you understand its strengths and weaknesses and guide potential next steps, such as trying a different algorithm, collecting more data, or performing more sophisticated feature engineering (which are topics for more advanced study). Having gone through these steps, you've now completed a full cycle of building and evaluating a basic machine learning model!

  • Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow, Aurélien Géron, 2022 (O'Reilly Media) - This book provides practical guidance on machine learning concepts and implementation, including dedicated chapters on model evaluation for both regression and classification, with detailed examples using Scikit-learn.
  • Metrics and Scoring: Quantifying the Quality of Predictions, scikit-learn developers, 2024 - This official documentation offers a comprehensive overview of the evaluation metrics available in Scikit-learn for both regression and classification, including detailed explanations and usage examples.
  • Pattern Recognition and Machine Learning, Christopher M. Bishop, 2006 (Springer) - This textbook offers a foundational understanding of machine learning principles, covering essential topics like model assessment and selection, which are fundamental to evaluating model performance.

© 2026 ApX Machine Learning