Can We Predict Traffic Congestion Before It Happens?
Traffic congestion is a common feature of urban transportation systems, particularly in rapidly growing cities where the number of vehicles and demand for road space continue to increase. Beyond the inconvenience experienced by commuters, c…

Traffic congestion is a common feature of urban transportation systems, particularly in rapidly growing cities where the number of vehicles and demand for road space continue to increase. Beyond the inconvenience experienced by commuters, congestion can result in longer travel times, increased fuel consumption, delays in the movement of goods and services, and wider economic costs. As cities continue to grow, understanding how and why traffic conditions change has become increasingly important for effective transportation planning and management. Traffic congestion is defined as a condition of stopped or stop-and-go traffic caused by an excess of vehicles on the roadway, leading to increased travel times for drivers1.
Traffic, however, does not occur randomly. The number of vehicles on a road, their speed, the time of day, the day of the week, and the characteristics of a particular location can all influence traffic conditions. Studies of traffic patterns have shown that these conditions can vary considerably across different times and locations. For example, a study of the Ikorodu-Jibowu section of Lagos examined traffic volumes across different days and hours and identified recurring differences in traffic flow throughout the week and across the day 2,7. Such patterns suggest that the conditions leading to congestion may be observable before a traffic jam fully develops.
This raises an interesting possibility: if these patterns can be captured as data, could they be used to anticipate what will happen next? With the development of machine learning, researchers can train computational models to identify relationships within large amounts of historical and real-time data and use those relationships to make predictions. This leads to the question at the centre of this research story: how can a machine predict a traffic jam before it happens?
Machine Learning
Machine learning is a branch of Artificial Intelligence that enables computer algorithms to identify patterns and relationships within data and use these pattern for predictions3,4. For traffic prediction, these algorithms can be trained using data such as vehicle speed, traffic volume, travel time, road conditions, time of day, and day of the week. By learning how these factors have been associated with previous traffic conditions, a machine learning model can estimate the likelihood that congestion will occur at a particular location and time. The question, therefore, is not simply whether a machine can recognize traffic congestion, but whether it can recognize the patterns that appear before congestion develops.
As previously established, predicting traffic congestion relies heavily on information about what is happening on the road, where it is happening, and when it is happening. This information can be collected from several sources, including GPS devices, traffic cameras, road sensors, and other monitoring systems. GPS (Global Positioning System) is a satellite-based navigation system that provides information about the location and time of a receiver⁵. In traffic studies, GPS data can therefore provide information about where vehicles are located, the routes they follow, and, depending on the data collected, how quickly they are moving.
However, knowing the coordinates of a vehicle alone does not provide much information about its surrounding environment. This is where Geographic Information Systems (GIS) become useful. GIS is a system used to collect, manage, analyze, and visualize geographic or spatial data. GPS-derived locations can be combined with information about roads, intersections, land use, and other geographical features to provide a more complete representation of the environment in which traffic is occurring. In this way, GIS can help researchers examine not only where a vehicle is located, but also the characteristics of the road and its surroundings.
The information collected from these different sources cannot usually be given directly to a machine-learning model. It first needs to be processed into a suitable dataset. During preprocessing, the data may be cleaned to remove errors or missing values, transformed into a consistent format, and encoded when categorical information needs to be represented numerically. Different sources can also be combined so that variables such as vehicle speed, traffic volume, travel time, road characteristics, location, weather, and time of day are represented together. The resulting dataset contains the features that the machine-learning model can use to learn relationships between traffic conditions and congestion.
For example, a dataset might contain the average speed of vehicles on a particular road, the number of vehicles recorded during a given period, the time of day, the day of the week, the location of the road, and whether congestion occurred. Historical examples such as these provide the model with information from which it can learn. Once the data have been prepared, the next stage is to use them to train a machine-learning model and determine whether the patterns within the data can be used to predict future traffic conditions. There are several machine-learning algorithms that can be applied to traffic prediction, each learning from the data in a different way. To understand how a machine can move from traffic data to a prediction, it is useful to examine how these model work.
Random Forest
Random Forest is an ensemble machine-learning algorithm that combines multiple decision trees to make predictions. Instead of relying on a single decision tree, it creates a collection, or “forest,” of trees and combines their individual predictions to produce a final result 6. In traffic congestion prediction, the preprocessed traffic dataset provides the information from which these trees learn. Variables such as vehicle speed, traffic volume, time of day, day of the week, location, and road characteristics can serve as features, while the occurrence or absence of congestion can serve as the target that the model is trained to predict7.
During training, Random Forest builds multiple decision trees using different samples of the training data and different subsets of the available features. Each tree learns relationships between the traffic conditions and the corresponding congestion outcome. A tree may, for example, learn that combinations of low vehicle speed, high traffic volume, and a particular time of day are frequently associated with congestion. Another tree may identify a slightly different combination of conditions 7.
Once the trees have been trained, they can be used to classify new traffic observations. Suppose that new data indicate that vehicles on a particular road are moving at 19 km/h, 185 vehicles have been recorded, and it is 7:50 a.m. on a weekday. These values are passed through each of the trained decision trees. Some trees may predict that congestion will occur, while others may predict that it will not. The Random Forest combines these individual predictions and uses the majority decision as the final classification.
For example, if most of the trees classify the new traffic conditions as congestion, the Random Forest predicts that congestion is likely to occur. The model is therefore not predicting the future by itself; rather, it is identifying whether the current traffic conditions resemble patterns that have previously been associated with congestion. If those patterns tend to occur before congestion develops, the model can provide an early warning before traffic reaches a congested state.
K-Nearest Neighbour (KNN)
Another machine-learning algorithm that can be used for traffic prediction is K-Nearest Neighbour (KNN). Unlike Random Forest, which builds multiple decision trees during training, KNN makes predictions by comparing a new observation with similar observations in the existing dataset. The “nearest” observations are those that are most similar to the new observation based on the selected features.
For example, suppose new traffic data show a vehicle speed of 19 km/h, a traffic volume of 185 vehicles, and a weekday morning at a particular road location. KNN compares these conditions with observations in the historical dataset and identifies the KK observations that are most similar. If most of these neighbouring observations were followed by congestion within the specified prediction period, KNN can classify the new observation as likely to result in congestion. If most were not followed by congestion, the model can classify it as non-congested.
Because KNN relies on the similarity between observations, the choice and preparation of features are particularly important. Features such as vehicle speed, traffic volume, time of day, and location can influence which observations are considered neighbours. This makes KNN useful for examining traffic conditions that resemble previously observed conditions, including conditions recorded at geographically or operationally similar locations.
Testing the Models
After the models have been trained, their ability to predict future traffic conditions must be evaluated using data that were not used during training. The prepared dataset can therefore be divided into a training set and a test set. The training set is used to train the Random Forest and KNN models, while the test set contains traffic observations that the models have not previously seen.⁶
The models are then given observations from the test set and asked to predict whether congestion will occur within the specified prediction period. Their predictions are compared with the actual traffic conditions recorded during that period. If a model predicts congestion and congestion subsequently occurs, the result is a true positive. If it predicts congestion but congestion does not occur, the result is a false positive. Similarly, if the model predicts that congestion will not occur when it actually does, the result is a false negative.
These results can be summarized using evaluation measures such as accuracy, precision, recall, and the F1-score. Together, these measures provide a way to assess how well the models distinguish between traffic conditions that are likely to lead to congestion and those that are not ⁶.
Conclusion
Traffic congestion is not simply a consequence of too many vehicles on the road. It is the result of changing relationships between factors such as traffic volume, vehicle speed, time, location, road characteristics, and other conditions. The ability to collect and combine these forms of information provides an opportunity to understand how congestion develops and, importantly, to identify patterns that may occur before it develops.
References
1.Traffic congestion: an overview . Amsterdam: Elsevier Available from: https://www.sciencedirect.com/topics/social-sciences/traffic-congestion
2.Imoh UU, Rad MM. Analysis and prediction of traffic conditions using machine learning models on Ikorodu Road in Lagos State, Nigeria. https://www.mdpi.com/2412-3811/10/5/122
3. Bergmann D. What is machine learning? . Armonk (NY): IBM; 2025 Aug 18 [updated 2026 Apr 2; cited 2026 Sep 27] https://www.ibm.com/think/topics/machine-learning
4.U.S. Department of Energy, Office of Science. DOE explains...machine learning [Internet].Washington (DC): U.S. Department of Energy; [cited 2026 Sep 27]. Available from: https://www.energy.gov/science/doe-explainsmachine-learning
5. OPTRAFFIC. The use of GPS in modern traffic equipment . 2024 Aug 7 [cited 2026 Sep 27]. Available from: https://optraffic.com/blog/use-of-gps-in-modern-traffic-equipment
6. Atsir EM, Nurmalitasari N, Sari AA. Traffic accident severity classification system using Random Forest algorithm. J-INTECH. 2025 Dec;13(2):272-81. doi:10.32664/j-intech.v13i02.2089.
7. Hammoumi L, Farah S, Benayad M, Maanan M, Rhinane H. Leveraging machine learning to predict traffic jams: case study of Casablanca, Morocco. J Urban Manag. 2025 Sep;14(3):813-26. doi:10.1016/j.jum.2025.02.004.

