What are the differences between training data and testing data?

Stack Overflow
Joi KozeyJoi Kozey

If you are new to machine learning (ML), you need a thorough understanding of two types of datasets: training data and testing data. You will also want to understand how we use these datasets to develop an ML model. In this article, I will provide a guide on what, why, how, and when to use these training datasets in developing machine learning or deep learning (DL) models.

Training Data

Why do we need a training dataset?

Machine learning algorithms train the model and make accurate predictions using informative data. For this purpose, you need data with truth, false results, and attributes from which the algorithm can learn and develop a pattern for decision-making.

The training data could contain various types of data, including images, time series, numerical data, or text, that must be fed into the algorithm according to the specific task the model requires training for.

Let’s look at some examples of training datasets for different contexts.

Image Data

Image Data

Let’s consider a training dataset with images for object detection. The key component of such a dataset would contain,

  • High/low-resolution images with various objects you want the model to detect covering all interest classes.
  • Annotations: These annotations could be bounding boxes, as shown in the above figure, with specific coordinates in the image plane or bounding boxes with a class label (e.g., ‘car,’ ‘bus,’ ‘bike’).
  • A metadata file could contain image information such as size, format, source, and contextual data like time of day and scene descriptions.

Numerical Data

Numerical Data Numerical Data

Consider the above numerical dataset, which represents the task of heart attack prediction. As you can see, 13 attributes represent a patient’s details and level of health. The last attribute, the output, has a binary class(0 or 1) representing whether a heart attack is possible.

ML models learn from these 13 attributes and develop a pattern that outputs the respective binary value.

Key Characteristics of a Good Training Dataset

A quality training dataset is vital for developing an accurate and precise ML model. Following are some characteristics a good training dataset should possess.

  • The data should suit the problem we are trying to solve, including the features directly impacting the outcome you are trying to predict.
  • The dataset should cover every possible scenario and case so the model can be well-generalized during training.
  • Data should be collected from accurate and reliable sources to guarantee the dataset’s quality and avoid duplicates, errors, and outliers.
  • The dataset should be sufficiently large for the model to capture the underlying patterns and relationships among the attributes and the output.
  • A good dataset should be balanced and have equal representations of all classes.

Important: Our training data varies depending on our model development approach. Supervised learning uses labels to represent output/target variables, semi-supervised learning uses labels and attributes without labels, and unsupervised learning uses no labels.

Testing Data

Why do we need a testing dataset?

Once you have completed training your model on the training dataset, it must be tested on unseen data to validate its performance capabilities. The data used in this testing process is called the testing data.

In this process, you can identify whether the model has overfitted and if its prediction accuracy in training is high but performs poorly in the testing dataset.

Key Characteristics of a Testing Dataset

  • The testing data should be independent and not replicated from the training dataset.
  • The dataset size should be sufficiently large enough to provide significant results.
  • The dataset should represent real-world data, reflecting use cases and practical scenarios.

Key Differences Between Training Data and Testing Data

Key Differences Between Training Data and Testing Data

What is Overfitting and Underfitting?

Overfitting usually occurs when a model learns well from the underlying data patterns and noise with random fluctuations, yielding high accuracy in the training dataset but not replicating these results with the testing data.

Underfitting occurs when the model is too simple and does not learn from the underlying data patterns. We can identify underfitting when training and testing performances are significantly low.

Examples to identify overfitting & underfitting

Example 1:

Suppose we observe the model performance on the training and testing data

  • Training Accuracy: 97%
  • Testing Accuracy: 68%

As you can see, the training accuracy is very high, while the testing accuracy shows poor performance. In this scenario, the model most likely overfits the data.

Example 2:

Training Accuracy: 60%

Testing Accuracy: 55%

Here, you can see that the training accuracy is very low, and the testing accuracy also shows poor performance. In this scenario, the model most likely underfits the data.

Examples to identify overfitting & underfitting

Importance of the Distinction

Model Validation

This is a vital step in the ML pipeline to guarantee the model performs well on unseen data. Keeping the training and testing data separate helps prevent data leakages, assess how well the model generalizes beyond the training dataset, identify overfitting or underfitting, find the best model that performs well on unseen data, and help in hyperparameter tuning.


This is the ability of an ML model to perform well on unseen data. Since an ML model aims to predict future data, it is vitally important for the model to generalize to the testing dataset.

Bias and Variance Trade-off

Bias is the error that occurs when you make overly simplistic assumptions in the learning algorithm. High bias means the model misses the correct relations between attributes/features and the target outputs, causing underfitting.

  • We can add relevant features to the training dataset to reduce bias and increase the model’s complexity.
  • If the testing data performs poorly, the model might not have captured the true patterns and might be biased.

Variance is the error that occurs because the model is sensitive to noisy data and random fluctuations in the training dataset. A high variance indicates that it models the noise in the training dataset rather than the actual outputs, making the model overfit the data.

  • Techniques like pruning, regularization, and reducing the complexity of the model could help reduce variance and prevent overfitting the noise in the training data.
  • Cross-validation on the test dataset can help estimate the model’s performance and generalization ability, which helps manage variance.

Data Splitting

Data splitting is an important step in the ML pipeline. Let’s look at some techniques used to define the data split.

  • Holdout method: Split the dataset into two distinct sets, the training and testing sets.
    • Training dataset: (70-80)%
    • Testing dataset: (20-30)%
  • K-fold cross-validation: This divides the dataset into k equal-sized folds. The model is trained k times using k-1 folds and testing with the remaining fold.
  • Stratified K-fold cross-validation: is a variation of K-fold cross-validation that ensures each fold has an identical distribution of the target variable. This is particularly helpful with imbalanced datasets.
  • Leave-One-Out cross-validation: Here, k equals the number of data points, and each data point is used once as a test instance. This helps to provide an unbiased estimate of the model’s performance.
  • Time Series Split: The dataset is split according to time, with training data from the past and future data for testing. This is a more realistic approach for time series forecasting and related tasks.

Data Splitting


In conclusion, training and testing data are crucial in developing and improving an ML/DL model. Understanding the differences between these two datasets is vital; the training dataset is used in the model’s training process, and the testing set is used to evaluate the trained model. A well-balanced dataset with a proper split will achieve a highly accurate model and reduce the risk of overfitting or underfitting. Therefore, considering all these facts discussed in the article significantly contributes to a model’s success and robustness.