Traffic flow prediction method and device
Through random forest selection and dimensionality reduction technology, combined with transformer network, the complex feature engineering and model overfitting problems of traditional traffic flow prediction methods are solved, achieving more accurate traffic flow prediction and reducing traffic congestion.
Patent Information
- Application Number
- CN202510942983.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-07-09
- Publication Date
- 2025-08-08
AI Technical Summary
When traditional traffic flow prediction methods deal with high-dimensional and complex traffic data, there are problems such as complex feature engineering and easy overfitting of models, and it is difficult to effectively integrate multi-source heterogeneous data, resulting in inaccurate traffic flow prediction.
Random forests are used to select important characteristics of traffic flow characteristics in different periods, and key discriminant information is retained by dimensionality reduction. Features with higher importance are input into the transformer network as output vectors. Combined with feature fusion technology, a traffic flow prediction model is constructed.
It improves data stability and representativeness, enhances the adaptability of the model in different traffic scenarios, can predict traffic flow changes more accurately, and reduce traffic congestion.
Smart Images

Figure CN120452209A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of data processing, and specifically to a traffic flow prediction method and device. Background Art
[0002] With the acceleration of urbanization, traffic flow prediction has become a key task in traffic management, intelligent transportation systems and urban planning. Accurate traffic flow prediction is crucial for the effective management and planning of urban traffic. It can help traffic managers understand the changing trends of traffic flow in different areas and sections of roads, reduce the occurrence of traffic congestion; provide travelers with real-time traffic information, optimize travel routes and save travel time.
[0003] Traffic flow data typically exhibits complex spatiotemporal dependencies, including temporal periodicity, trends, and spatial correlations. Traditional time series models and machine learning methods have limitations in handling the complexity of traffic flow data. In recent years, deep learning technology has made significant progress in the field of traffic flow prediction, better capturing the long-term dependencies of sequence data. However, these methods still face challenges in processing high-dimensional and complex traffic data, such as complex feature engineering and the tendency of models to overfit. In addition, traffic flow data is affected by a combination of factors, such as road network structure, meteorological conditions, and special events. How to effectively integrate these multi-source heterogeneous data is also a hot topic and difficulty in current research. Summary of the Invention
[0004] In response to the problems in the prior art, the present application provides a traffic flow prediction method and device to better predict complex traffic conditions, optimize travel routes, and reduce the occurrence of traffic congestion.
[0005] In order to solve at least one of the above problems, the present application provides the following technical solutions: In a first aspect, the present application provides a traffic flow prediction method, comprising: Obtaining raw data related to traffic flow from multiple data sources and preprocessing the raw data to obtain preprocessed data; the raw data includes historical traffic series, weather data, whether it is a holiday, road section information, and timestamp; Dividing the preprocessed data into a training set, a validation set, and a test set in chronological order; setting parameters of a random forest; sampling data and features from the training set to generate multiple training samples, and using the training samples to construct and train a decision tree to obtain a trained random forest model; Obtaining the leaf node index of the training sample in each decision tree, and converting the leaf node index of each training sample into a high-dimensional sparse feature matrix; applying a dimensionality reduction algorithm to the high-dimensional sparse feature matrix to convert it into a low-dimensional dense feature matrix; The low-dimensional dense feature matrix is concatenated with the historical traffic sequence to obtain a two-dimensional matrix. The two-dimensional matrix is input into the transformer network, and the traffic data flow prediction model is output. The model is adjusted using the validation set and tested using the test set to obtain the final prediction model.
[0006] Furthermore, the step of preprocessing the original data includes: A threshold range is set for the raw data, and values exceeding the threshold range are identified as outliers and removed. Missing raw data can be filled with the historical average flow value of the road section at the same time, or a reasonable value can be estimated using linear interpolation in the time series interpolation method. The raw data from different sources are unified to the same time and space granularity. Perform feature extraction and conversion on the collected raw data: convert timestamps into hourly values, expand them to determine whether they are holidays, and use sine / cosine encoding to handle periodicity; perform one-hot encoding on weather data and convert them into multiple binary features; and use embedded encoding to map road segment information into a low-dimensional dense vector space. A sliding window method is used to extract a first feature matrix and a first target vector from the historical traffic sequence; the value of the first feature matrix is the sliding window at the current position, and the value of the first target vector is a data point after the sliding window; if the length of the data processed by the sliding window is less than the size of the sliding window, the data is padded with 0.
[0007] Furthermore, the step of using random forest to generate multiple training samples from the training set and using the training samples to construct and train a decision tree includes: Loading a second feature matrix and a second target vector from the training set, and generating a plurality of training samples from the second feature matrix and the second target vector using a random forest; A decision tree is constructed from the generated training samples using a recursive partitioning algorithm; the recursive partitioning algorithm constructs a tree structure by continuously splitting nodes until a stopping condition is reached; Also included: Calculate the importance score of each feature while building each decision tree.
[0008] Furthermore, the step of obtaining the leaf node index of the training sample in each decision tree and converting the leaf node index of each training sample into a high-dimensional sparse feature matrix includes: Input each sliding window sample into the trained random forest model and record the leaf node index of the sliding window sample in each decision tree; Create a one-hot encoded vector for each tree in the random forest, where the length of the one-hot encoded vector is equal to the number of leaf nodes in the decision tree; The one-hot encoding vectors of all decision trees are concatenated in sequence to form a high-dimensional sparse feature matrix; each row of the high-dimensional sparse feature matrix represents a sliding window sample, and each column represents a leaf node index.
[0009] Furthermore, the step of applying a dimensionality reduction algorithm to the high-dimensional sparse feature matrix to convert it into a low-dimensional dense feature matrix includes: Calculating the covariance matrix of the high-dimensional sparse feature matrix, and finding the eigenvalues and eigenvectors of the covariance matrix; Select the first few eigenvectors in descending order of eigenvalues to form the projection matrix after dimensionality reduction; The high-dimensional sparse feature matrix is multiplied by the projection matrix to obtain the low-dimensional dense feature matrix.
[0010] Furthermore, the step of concatenating the low-dimensional dense feature matrix with the historical traffic sequence to obtain a two-dimensional matrix includes: The historical traffic sequence and the first feature matrix extracted therefrom are used as the main sequence; for each time step in the sequence, a comprehensive feature vector is constructed; the comprehensive feature vector includes the traffic index value and the spatiotemporal features of the time step, and the low-dimensional dense feature matrix corresponding to the time step is spliced into each time step feature to obtain the two-dimensional matrix; The two-dimensional matrix also integrates temporal features, spatial features, and environmental features; the temporal features include the encoded hour, day of the week, and holiday flags; the spatial features include the road segment ID code, the average flow rate of adjacent road segments, POI density, and road type; and the environmental features include weather codes, temperature, and event flags.
[0011] Furthermore, the step of inputting the two-dimensional matrix into the transformer network and outputting the traffic data flow prediction model includes: The transformer network uses the Adam optimizer to guide the training of the model, and the mean square error loss function is used as the evaluation indicator of the model performance.
[0012] In a second aspect, the present application provides a traffic flow prediction device, comprising: A data preprocessing module is used to obtain raw data related to traffic flow from multiple data sources and preprocess the raw data to obtain preprocessed data; the raw data includes historical traffic series, weather data, whether it is a holiday, road section information, and timestamp; A model building module is used to divide the preprocessed data into a training set, a validation set, and a test set in chronological order; set parameters of the random forest; sample data and features from the training set to generate multiple training samples, and use the training samples to build and train a decision tree to obtain a trained random forest model; A feature dimensionality reduction module is used to obtain the leaf node index of the training sample in each decision tree and convert the leaf node index of each training sample into a high-dimensional sparse feature matrix; apply a dimensionality reduction algorithm to the high-dimensional sparse feature matrix to convert it into a low-dimensional dense feature matrix; The model generation module is used to concatenate the low-dimensional dense feature matrix with the historical traffic sequence to obtain a two-dimensional matrix; the two-dimensional matrix is input into the transformer network, the traffic data flow prediction model is output, and the model is adjusted using the validation set and tested using the test set to obtain the final prediction model.
[0013] In a third aspect, the present application provides an electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the steps of the traffic flow prediction method when executing the program.
[0014] In a fourth aspect, the present application provides a computer-readable storage medium having a computer program stored thereon, which implements the steps of the traffic flow prediction method when executed by a processor.
[0015] In a fifth aspect, the present application provides a computer program product, comprising a computer program / instruction, which implements the steps of the traffic flow prediction method when executed by a processor.
[0016] It can be seen from the above technical solution that the present application provides a traffic flow prediction method and device, which uses random forests to select important features of traffic flow characteristics in different time periods, and then uses dimensionality reduction to retain key discriminant information. The features with higher importance are input into the transformer network as output vectors, and the transformer network is used as a prediction model for traffic flow to perform feature learning and prediction. This method uses random forest feature selection to enhance the stability and representativeness of the data to a certain extent. At the same time, through feature fusion, it cleverly injects auxiliary information of different types and granularities into each processing step of the model, enabling the transformer to comprehensively utilize all information for more accurate traffic flow prediction. Combined with the generalization ability of the transformer network, the model has better adaptability in different time periods and different traffic scenarios, can effectively deal with the uncertainty of traffic flow changes, and has better prediction effects on new data and complex traffic conditions. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without any creative work.
[0018] Figure 1 Schematic diagram of the flow of traffic flow prediction method in an embodiment of the present application; Figure 2 Traffic flow example data for the traffic flow prediction method in the embodiment of the present application; Figure 3 A traffic flow prediction structure diagram of the traffic flow prediction method in an embodiment of the present application; Figure 4 is a structural diagram of a traffic flow prediction device in an embodiment of the present application; Figure 5 Schematic diagram of the structure of the electronic device in the embodiment of the present application.
[0019] Reference numerals: Electronic device 9600, central processing unit 9100, memory 9140, communication module 9110, input unit 9120, audio processor 9130, display 9160, power supply 9170, buffer memory 9141, application / function storage unit 9142, data storage unit 9143, driver program storage unit 9144, antenna 9111, speaker 9131, microphone 9132. DETAILED DESCRIPTION
[0020] To make the purpose, technical solutions, and advantages of the embodiments of this application more clear, the technical solutions in the embodiments of this application will be clearly and completely described below in conjunction with the drawings in the embodiments of this application. Obviously, the described embodiments are part of the embodiments of this application, not all of the embodiments. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of this application.
[0021] The acquisition, storage, use, and processing of data in this application's technical solution comply with relevant national laws and regulations.
[0022] Taking into account the problems existing in the prior art, the present application provides a traffic flow prediction method and device, which uses random forests to select important features of traffic flow characteristics in different time periods, and then uses dimensionality reduction to retain key discriminant information. The features with higher importance are input into the transformer network as output vectors, and the transformer network is used as a prediction model for traffic flow to perform feature learning and prediction. This method uses random forest feature selection to enhance the stability and representativeness of the data to a certain extent. At the same time, through feature fusion, it cleverly injects auxiliary information of different types and granularities into each processing step of the model, enabling the transformer to comprehensively utilize all information for more accurate traffic flow prediction. Combined with the generalization ability of the transformer network, the model has better adaptability in different time periods and different traffic scenarios, can effectively deal with the uncertainty of traffic flow changes, and has better prediction effects on new data and complex traffic conditions.
[0023] In order to better predict complex traffic conditions, optimize travel routes, and reduce traffic congestion, this application provides an embodiment of a traffic flow prediction method, see Figure 1-Figure 2 , the traffic flow prediction method specifically includes the following contents: Step S101: obtaining raw data related to traffic flow from multiple data sources, and preprocessing the raw data to obtain preprocessed data; the raw data includes historical traffic sequence, weather data, whether it is a holiday, road section information, and timestamp.
[0024] Optionally, in this embodiment, core traffic data can be obtained from flow, speed, and occupancy data recorded by fixed detectors (e.g., coil detectors embedded in the road, roadside radar detectors, or traffic statistics systems associated with road surveillance cameras). This data is typically output at fixed intervals (e.g., every minute or every five minutes). Simultaneously, travel time and speed can be calculated using GPS trajectory data from floating vehicles (taxis and ride-hailing companies). This requires obtaining GPS trajectory data from a large number of vehicles on ride-hailing platforms or traffic big data platforms. The trajectory points are then mapped to corresponding road segments using a road network matching algorithm to calculate the average travel time and speed for each segment. Furthermore, user-reported road condition information can be collected from mobile app crowdsourced data platforms (e.g., anonymous user-contributed data from Amap and Baidu Maps). This information can serve as a supplementary reference for road conditions.
[0025] Among spatiotemporal correlation data, road network topology data can be obtained from public geographic information platforms, including the connectivity between road nodes and road segments, to construct a road network topology diagram. POI information can be obtained through geographic information service platforms (such as the AutoNavi POI search and processing system) to identify POI types (e.g., commercial areas, residential areas, schools, hospitals, etc.) and their density in different areas. This helps understand the impact of different area functional attributes on traffic flow. Road grade (e.g., expressway, urban main road, secondary road, branch road, etc.) and lane number information can generally be extracted from urban road planning materials or map data to reflect road capacity.
[0026] For time features, date information can be extracted from time series data to determine whether each day is a weekday, weekend, or holiday (referring to the national statutory holiday schedule and school winter and summer vacation time, etc.), and the time of day is divided into peak, off-peak, and off-peak periods (based on the daily changes in urban traffic flow, such as the morning peak period of 7-9 am and the evening peak period of 5 pm-7 pm, etc.), while recording the specific day of the week information.
[0027] Among environmental factors, meteorological data can be obtained from the public data interface of the meteorological department, including weather conditions (sunny, rainy, snowy, foggy, etc.), temperature, precipitation, visibility and other information. Major event information is obtained by collecting event announcements issued by traffic management departments, news media reports and hot topics on social media, such as information on sports events, concert times and locations, and traffic accident records.
[0028] Optionally, in this embodiment, for traffic records with missing values, such as missing traffic data for a particular hour at a particular intersection, the historical average traffic flow value for that intersection at the same hour (e.g., 8:00 AM daily) can be calculated and used to fill the missing value. Similar methods are used to fill missing values for other data types, such as weather data, to ensure data integrity and avoid discarding entire samples due to a small number of missing values. Alternatively, linear interpolation, a time series interpolation method, can be used to estimate reasonable values.
[0029] For outlier processing, taking traffic data as an example, we can first calculate its quartiles (Q1 and Q3), and then obtain the interquartile range IQR=Q3-Q1. The reasonable range of traffic is set to [Q1-1.5IQR, Q3+1.5IQR]. Traffic values outside this range are considered outliers and are eliminated. For speed data, the reasonable range is set to [0, speed limit + 20%] (i.e. 0-72km / h) based on the road speed limit (such as the speed limit on urban roads is 60km / h). Speed values outside this range are considered outliers and are processed, such as replacing them with speed values at nearby time points or interpolated speed values.
[0030] Then, data from different sources is unified to the same temporal and spatial granularity. For example, if traffic data from fixed detectors is updated every minute, while travel time data from floating vehicles is updated every 5 minutes, the traffic data is aggregated into 5-minute time slices (e.g., calculating the average flow rate every 5 minutes). Spatial data from different road sections (e.g., sections of different lengths) is divided into unified spatial units (e.g., dividing road sections into sections by kilometers), ensuring that all data is aligned on key fields such as timestamps and section IDs.
[0031] Optionally, in this embodiment, feature extraction and conversion are performed on the collected raw data through feature engineering. Specifically: Timestamp conversion: The original timestamp (e.g., 2024-01-01 08:30:00) can be converted to an hour value (8), and the day of the week (assuming the 1st is a Wednesday, the day of the week is 3) and whether it is a holiday (assuming the 1st is not a holiday, the day of the week is 0). Sine / cosine encoding is used to handle periodicity. For example, for hour values, it is mapped to the range [0, 2π), and sin(2π×hour / 24) and cos(2π×hour / 24) are calculated. This converts the periodic hour information into a two-dimensional continuous feature, allowing the model to better capture the periodic changes in time.
[0032] Categorical feature encoding: Weather data (such as sunny, rainy, snowy, etc.) can be one-hot encoded and converted into multiple binary features (such as [1,0,0] for sunny, [0,1,0] for rainy, etc.). For high-cardinality categorical features such as road segment IDs, embedded encoding can be used to map them into a low-dimensional dense vector space to reduce feature dimensionality and retain semantic information between categories.
[0033] Sequence feature construction: For example, the sliding window size can be 24 (assuming the time step is 5 minutes, it corresponds to the past 2 hours). For the historical traffic sequence of each road section, starting from the first time step, the data of 24 consecutive time steps are taken as a row of the feature matrix X, and the data of the next time step is taken as an element of the target vector Y. For example, if the sequence has 100 time steps of data, when the sliding window slides from position 1 to position 77 (because 77+24=101 exceeds the sequence length), the feature matrix X and the corresponding target vector Y of 77 samples can be obtained. If the historical traffic sequence of a certain road section has less than 24 time steps at the beginning, the data is padded with 0 to 24 time steps to ensure that the dimension of the feature matrix of all samples is consistent.
[0034] In addition to traffic flow series, the same sliding window approach can be used to construct lag features for indicators such as speed and occupancy, adding these features as additional columns to the feature matrix X. For each time step in the sliding window, the corresponding temporal features (such as the hour code, day of the week code, and holiday symbol) are added as part of the periodic features. Simultaneously, traffic flow and speed data for adjacent sections of the target section are obtained, and their average or weighted average within the sliding window (the weight can be determined based on the distance between the adjacent section and the target section or the degree of traffic correlation) is calculated. This is then added to X as a spatial adjacency feature.
[0035] Numerical features such as flow rate, speed, and occupancy can be normalized by calculating their mean and standard deviation. For example, Z-score normalization can be used. For example, for flow feature F, each value F_i is converted to (F_i - μ_F) / σ_F, where μ_F is the mean flow rate and σ_F is the standard deviation. This process ensures that different features have the same dimension and value range, which facilitates model optimization and convergence.
[0036] Step S102: Divide the preprocessed data into a training set, a validation set, and a test set in chronological order; set the parameters of the random forest; sample data and features from the training set to generate multiple training samples, and use the training samples to construct and train a decision tree to obtain a trained random forest model.
[0037] Optionally, in this embodiment, the data is strictly divided into time series, that is, the preprocessed data is divided into a training set, a validation set, and a test set in chronological order, with a ratio of 8:1:1. For example, assuming that the data covers 2021-2023, the first 80% of the time data (such as November 2021-2022) is used as the training set, the middle 10% (December 2022-March 2023) is used as the validation set, and the last 10% (April 2023-June 2023) is used as the test set. This division method ensures that the model only uses past data during training, avoids interference from future data on the training process, and makes the model evaluation results more realistic and reliable.
[0038] Consider periodicity. When dividing datasets, check whether each dataset contains complete patterns for weekdays, weekends, and holidays. For example, ensure that the training set contains a sufficient number of weekday, weekend, and holiday samples so that the model can learn the traffic characteristics and changes in different periods. This improves the model's generalization ability and enables it to accurately predict traffic conditions in different periods on the validation and test sets.
[0039] Optionally, in this embodiment, the goal of random forest feature sampling and training is to utilize the ensemble learning capabilities of random forests to capture the complex nonlinear relationships and interactions between input features, generating a unique "path signature" for each sliding window sample, thereby providing a basis for subsequent feature dimensionality reduction and fusion. Specifically: First, input features. All relevant features constructed for each sliding window sample in step S101 (including historical traffic data, spatiotemporal features, and environmental features) are used as input features for the random forest. These features have been processed through feature engineering to encompass a wealth of traffic flow-related information. The target variable can be left unspecified (unsupervised mode), allowing the random forest model to focus on learning the inherent structure of the data. Alternatively, it can be specified as the target vector Y (the traffic flow at the current or next time step, in semi-supervised mode). This guides the random forest to learn the feature structure relevant to the prediction target, enhancing the model's targeted traffic flow prediction capabilities.
[0040] Bootstrap sampling is used to generate multiple training samples from the feature matrix X and the target vector Y. The size of each training sample is the same as the original dataset, allowing for repeated sampling. For each decision tree, a recursive partitioning algorithm is used to construct the tree from the training samples generated by bootstrap sampling. At each node split, a subset of features is randomly selected from all features, and the features and threshold that optimize the splitting criterion (such as Gini impurity or information gain) are selected until a stopping criterion (such as the maximum leaf node depth or the minimum number of samples) is met.
[0041] For random forest parameter selection and tuning, you can use n_estimators=100 and random_state=42 as initial parameters. During training, observe the trend of the out-of-bag error (OOB error) to determine whether the number of trees is sufficient. If the OOB error gradually stabilizes as the number of trees increases, the current number of trees is appropriate; otherwise, increase the number of trees appropriately (for example, to 150 or 200). Also, adjust the max_depth parameter, experimenting with different tree depths (such as 10, 20, 30), observing the model's performance on the validation set, and selecting the depth that minimizes the validation set error. For the max_features parameter, try sqrt(n_features), log2(n_features), or other ratios (such as 0.8). Comparing the model performance under different values determines the optimal value. The min_samples_split and min_samples_leaf parameters are adjusted according to the size and feature distribution of the dataset. For example, min_samples_split can be set to an integer between 2 and 10, and min_samples_leaf can be set to an integer between 1 and 5 to prevent the tree model from overfitting and ensure that the model has good generalization ability.
[0042] After training is complete, the importance score of each feature is calculated. For example, by analyzing the feature importance results, it may be found that certain time steps in historical traffic features, traffic characteristics of adjacent road segments, and peak hour codes in temporal features are highly important for traffic forecasting. This helps to gain a deeper understanding of the key drivers of traffic flow changes and also provides a basis for subsequent feature selection and optimization, such as appropriately increasing the weight of important features or further exploring their potential information.
[0043] Step S103: Obtain the leaf node index of the training sample in each decision tree, and convert the leaf node index of each training sample into a high-dimensional sparse feature matrix; apply a dimensionality reduction algorithm to the high-dimensional sparse feature matrix to convert it into a low-dimensional dense feature matrix.
[0044] Optionally, in this embodiment, each sliding window sample (i.e., the input feature vector corresponding to each sample) is input into a trained random forest model. Each sample undergoes a series of feature splitting tests within each tree, ultimately landing on a leaf node. The leaf node index of each sample in each tree is recorded, resulting in an index list of length T (where T is the number of trees in the random forest). These index values represent the path signature of the sample in the random forest, reflecting the sample's unique position in the feature space and its feature combination pattern.
[0045] Create a feature encoding (One-Hot) vector for each tree in the random forest. The length of the vector is equal to the actual number of leaf nodes in the tree. For example, if a tree has 32 leaf nodes, the corresponding One-Hot vector length is 32. The value of the leaf node where the sample falls is set to 1, and the other positions are set to 0. The One-Hot vectors of all trees are sequentially concatenated to form a high-dimensional sparse feature vector SFM (Sample Forest Matrix). , where N represents the number of samples and leave represents the number of leaf nodes in each decision tree. For example, if a random forest has 100 trees, with an average of 20 leaf nodes per tree, the dimension of the SFM is 100 × 20 = 2000. In the SFM vector for each sample, only 100 positions have a value of 1 (one leaf node per tree), and the remaining positions are 0. Although such high-dimensional sparse features contain rich information about sample feature interactions, their direct use leads to inefficient model training and a large amount of redundant information.
[0046] Perform PCA (dimensionality reduction) transformation on the SFM matrix of the training set. First, calculate the covariance matrix of the SFM matrix, and then find the eigenvalues and eigenvectors of the covariance matrix. Select the first K eigenvectors in order from large to small eigenvalues (the sum of the corresponding eigenvalues accounts for a certain proportion of the total eigenvalues, such as 95%) to form the projection matrix after dimensionality reduction. Multiply the SFM matrix of the training set by the projection matrix to obtain a low-dimensional dense feature matrix DFM (Dimensionality-Reduced ForestMatrix). The SFM vectors of the validation set and the test set are also transformed by the projection matrix obtained in the training phase to obtain the corresponding DFM eigenvectors. For example, after PCA dimensionality reduction, the 2000 dimensions of SFM are reduced to 100 dimensions. This not only greatly reduces the feature dimension and the input scale of the model, but also retains most of the useful information in the original SFM, providing efficient and information-rich feature input for subsequent Transformer model training.
[0047] Step S104: Concatenate the low-dimensional dense feature matrix with the historical traffic sequence to obtain a two-dimensional matrix; input the two-dimensional matrix into the transformer network, output the traffic data flow prediction model, adjust it using the validation set, and test it using the test set to obtain the final prediction model.
[0048] Optionally, this embodiment uses sequence embedding for feature fusion: HistSeq (i.e., the historical traffic sequence within the sliding window and its associated feature matrix X) is used as the primary sequence. For each time step t in the sequence, a comprehensive feature vector is constructed. This vector contains the traffic flow value at that time step (flow_t), the corresponding speed, occupancy, and other traffic indicators (speed_t, occup_t), the spatiotemporal features at that time step (SpatioTempFeat_t) (such as time code and adjacent road traffic flow), and the DFM feature vector for the sliding window sample. (DFM is a sample-level global feature that is independent of time and is therefore replicated at each time step in the sequence.) For example, assuming that flow_t is a scalar, speed_t and occup_t are each scalars, SpatioTempFeat_t is a vector of length 5, and DFM is a vector of length 100, the dimension of the fused feature vector is 1+1+1+5+100=108. In this way, the feature vector of each time step not only contains the local traffic information and spatiotemporal context of its own time step, but also incorporates the global sample feature information, enabling the Transformer model to more comprehensively capture the complex dependencies of traffic flow.
[0049] Furthermore, the fused features may also include SpatioTempFeat (spatiotemporal context features), which include temporal features (such as encoded hour, day of the week, and holiday identifiers) and spatial features (such as segment ID codes, average traffic flow on adjacent segments, POI density, road type, etc.). Environmental features (such as weather codes, temperature, and event identifiers) may also be included. Temporal features are mostly slowly changing or static within the entire sliding window sequence HistSeq (for example, a 24-step window typically does not change the day of the week or holiday identifier within an hour; the hour may change, but slowly). Spatial / environmental features are typically completely static (segment ID, POI density) or slowly changing (weather may not change much within the window) for the same target segment / area and the same time window.
[0050] SpatioTempFeat also serves as additional contextual information for each time step. Specifically, the temporal feature component takes the features corresponding to the actual timestamp of time step t. For example, if time step t corresponds to 07:45 AM on Wednesday, then the encoded feature values such as "07", "Wednesday", and "non-holiday" are used. As for the spatial / environmental features, since they are usually static or slowly changing for the entire window, the spatial / environmental feature values calculated for the sliding window sample are directly used. For example, the road segment ID code, the current weather code (if the weather changes little within the window, or the average / main value is taken), the POI density, etc. If the environment changes drastically within the window, it is necessary to obtain the environmental data at the corresponding moment for each time step.
[0051] Therefore, in this embodiment, the final input vector at each time step t is: input_vector_t = [flow_t; (speed_t; occup_t; ...); SpatioTempFeat_t;DFM].
[0052] Dimension = D_hist (number of original sequence features) + D_st (dimensionality of spatiotemporal context features) + K (dimensionality of DFM).
[0053] Then, all 24 time steps in the sliding window are arranged in sequence, with each time step corresponding to an input_vector_t constructed above. This forms a two-dimensional matrix as the input to the Transformer: Sequence length (Seq Len): M = 24 (sliding window size), feature dimension (Feature Dim): D = D_hist + D_st + K: For example, the sequence length is 24 (sliding window size) and the feature dimension is 108. This matrix also contains: Temporal dynamics (HistSeq), which is reflected by the core observation value at each position in the sequence. Spatiotemporal context (SpatioTempFeat), which is reflected by the features attached to each time step, especially the temporal features are tied to the specific moment. The global feature of the sample (DFM), which is reflected by repeatedly adding it at each time step to ensure that the model can utilize this global information at any time step.
[0054] Compared to simple concatenation, this fusion approach's primary advantage is its ability to preserve sequence structure. The Transformer's core strength lies in processing sequential data (such as HistSeq). Sequence embedding preserves the HistSeq as the main sequence, allowing the Transformer's attention mechanism to operate directly on this sequence, learning dependencies between timesteps (such as the gradual increase in traffic during the morning rush hour). Furthermore, it achieves contextual awareness: By binding SpatioTempFeat_t to a specific timestep t, the model can understand traffic conditions at a specific location (such as a city arterial) at a specific time (e.g., 5 p.m.). This is more precise than assigning a general spatiotemporal label to the entire window. Furthermore, this approach injects global information, replicating the DFM to each timestep, effectively imprinting each element in the sequence with a representation of the entire sample context. When the Transformer's self-attention mechanism computes the relationship between a given timestep t and other timesteps, or when computing a representation of t itself, the global information carried by the DFM is incorporated. This allows the model to consistently understand the overall characteristics of the current sample (the complex structure learned by the RF) when understanding historical patterns. For example, when the model observes a pattern of declining traffic, combined with the "bad weather" information contained in the DFM, it can more accurately determine that this is an abnormal drop caused by weather, rather than the end of the normal evening rush hour. This fusion method is also compatible with the Transformer architecture. The Transformer input is naturally a sequence (length M) of vectors (dimension D), and this fusion method directly constructs a tensor that meets the Transformer input requirements.
[0055] Optionally, in this embodiment, the Adam optimizer can be selected for model training. It combines the advantages of momentum optimization and adaptive learning rate, and can automatically adjust the learning rate of each parameter to improve the convergence speed and stability of the model. During the training process, the Adam optimizer will update the model parameters based on the first-order moment estimate and second-order moment estimate of the gradient. The learning rate lr=0.0001 can be set. The learning rate determines the step size of the model parameters at each iterative update. A smaller learning rate can make the model converge more smoothly, but the training speed is slower; a larger learning rate may speed up the training speed, but it is easy to cause unstable convergence. A learning rate of 0.0001 can achieve a better balance between model convergence speed and stability.
[0056] In this embodiment, the mean square error (MSE) loss function can be used as the target for model training optimization. MSE measures the average sum of the squares of the errors between the predicted value and the true value. By minimizing the MSE loss, the model's predicted value can be made as close to the true value as possible, thereby improving prediction accuracy. The calculation formula is: MSE = 1 / n Σ (y_pred - y_true)^2, where n is the number of samples, y_pred is the model's predicted flow value, and y_true is the true flow value.
[0057] In this embodiment, during training, the model loss on the validation set is periodically calculated. When the validation set loss reaches a minimum, the model is considered to have achieved optimal generalization performance, and the model parameters are saved. Furthermore, an early stopping strategy is employed. If the validation set loss does not decrease for a number of consecutive epochs (e.g., 10 epochs), training is stopped. This prevents overfitting of the model on the training set and ensures good test performance.
[0058] Model validation and evaluation: Validation set: During training, the validation set is used to tune hyperparameters, such as the number of layers, number of heads, hidden layer size, dropout rate, and learning rate of the Transformer model. By observing the performance of the model on the validation set, the hyperparameter combination that minimizes the validation set loss is selected, thereby determining the optimal model structure and parameter settings. The validation set is also used to implement early stopping strategies, determining when to stop training to prevent overfitting.
[0059] Test set: After model training and validation are complete, the test set is used to conduct a final performance evaluation of the model. The test set is used only once to ensure the objectivity and accuracy of the evaluation results. The main evaluation metrics reported include RMSE (root mean square error), MAE (mean absolute error), MAPE (mean absolute percentage error; use with caution to avoid zero denominators), and R² (coefficient of determination). These metrics provide a comprehensive measure of the model's prediction accuracy and fit. For example, RMSE reflects the standard deviation of the error between the predicted and true values, MAE represents the average absolute value of the error, MAPE measures the relative size of the error as a percentage, and R² reflects the extent to which the model explains data variation. In addition, by plotting a comparison of the actual and predicted traffic curves for key sections or areas during the test period, one can visually observe the model's prediction performance at different time periods and further analyze the model's strengths and weaknesses.
[0060] The traffic flow prediction method provided by the present application uses random forests to select important features of traffic flow characteristics in different time periods, and then uses dimensionality reduction to retain key discriminant information. The features with higher importance are input into the transformer network as output vectors. The transformer network is used as a prediction model for traffic flow to perform feature learning and prediction. This method uses random forest feature selection to enhance the stability and representativeness of the data to a certain extent. At the same time, through feature fusion, it cleverly injects auxiliary information of different types and granularities into each processing step of the model, enabling the transformer to comprehensively utilize all information for more accurate traffic flow prediction. Combined with the generalization ability of the transformer network, the model has better adaptability in different time periods and different traffic scenarios, can effectively deal with the uncertainty of traffic flow changes, and has better prediction effects on new data and complex traffic conditions.
[0061] In order to better predict complex traffic conditions, optimize travel routes, and reduce traffic congestion, the present application provides an embodiment of a traffic flow prediction device for implementing all or part of the traffic flow prediction method. Figure 4 The traffic flow prediction device specifically includes the following contents: The data preprocessing module 10 is used to obtain raw data related to traffic flow from multiple data sources and preprocess the raw data to obtain preprocessed data; the raw data includes historical traffic sequence, weather data, whether it is a holiday, road section information, and timestamp; The model building module 20 is used to divide the preprocessed data into a training set, a validation set, and a test set in chronological order; set the parameters of the random forest; sample data and features from the training set to generate multiple training samples, and use the training samples to construct and train a decision tree to obtain a trained random forest model; The feature dimensionality reduction module 30 is used to obtain the leaf node index of the training sample in each decision tree, and convert the leaf node index of each training sample into a high-dimensional sparse feature matrix; apply a dimensionality reduction algorithm to the high-dimensional sparse feature matrix to convert it into a low-dimensional dense feature matrix; The model generation module 40 is used to concatenate the low-dimensional dense feature matrix with the historical traffic sequence to obtain a two-dimensional matrix; input the two-dimensional matrix into the transformer network, output the traffic data flow prediction model, adjust it using the validation set, and test it using the test set to obtain the final prediction model.
[0062] From the above description, it can be seen that the traffic flow prediction device provided by the embodiment of the present application uses random forests to select important features of traffic flow characteristics in different time periods, and then uses dimensionality reduction to retain key discriminant information. The features with higher importance are input into the transformer network as output vectors, and the transformer network is used as a prediction model for traffic flow to perform feature learning and prediction. This method uses random forest feature selection to enhance the stability and representativeness of the data to a certain extent. At the same time, through feature fusion, it cleverly injects auxiliary information of different types and granularities into each processing step of the model, so that the transformer can comprehensively utilize all information to make more accurate traffic flow predictions. Combined with the generalization ability of the transformer network, the model has better adaptability in different time periods and different traffic scenarios, can effectively deal with the uncertainty of traffic flow changes, and has better prediction effects on new data and complex traffic conditions.
[0063] From a hardware perspective, in order to better predict complex traffic conditions, optimize travel routes, and reduce traffic congestion, this application provides an embodiment of an electronic device for implementing all or part of the traffic flow prediction method. The electronic device specifically includes the following: A processor, memory, a communications interface, and a bus; wherein the processor, memory, and communications interface communicate with each other via the bus; the communications interface is used to transmit information between the traffic flow prediction device and related devices such as core business systems, user terminals, and related databases; the logic controller can be a desktop computer, a tablet computer, a mobile terminal, etc., but this embodiment is not limited thereto. In this embodiment, the logic controller can be implemented with reference to the embodiments of the traffic flow prediction method and the traffic flow prediction device in the embodiments, and their contents are incorporated herein, and any repetitions are not repeated.
[0064] It is understandable that the user terminal may include a smart phone, a tablet electronic device, a network set-top box, a portable computer, a desktop computer, a personal digital assistant (PDA), a vehicle-mounted device, a smart wearable device, etc. Among them, the smart wearable device may include smart glasses, a smart watch, a smart bracelet, etc.
[0065] In practical applications, portions of the traffic flow prediction method can be executed on the electronic device as described above, or all operations can be performed on the client device. The specific selection can be based on the processing capabilities of the client device and the limitations of the user's usage scenario. This application does not impose any restrictions on this. If all operations are performed on the client device, the client device may also include a processor.
[0066] The aforementioned client device may include a communication module (i.e., a communication unit) capable of establishing a communication connection with a remote server to facilitate data transmission with the server. The server may include a server at the task scheduling center or, in other implementation scenarios, a server on an intermediate platform, such as a server on a third-party server platform that is communicatively linked to the task scheduling center server. The server may comprise a single computer device, a server cluster consisting of multiple servers, or a distributed server configuration.
[0067] Figure 5 Schematic block diagram of the system structure of the electronic device 9600 according to an embodiment of the present application. Figure 5 As shown, the electronic device 9600 may include a central processing unit 9100 and a memory 9140; the memory 9140 is coupled to the central processing unit 9100. It is worth noting that the Figure 5 is exemplary; other types of structures may also be used to supplement or replace this structure to implement telecommunication functions or other functions.
[0068] In one embodiment, the traffic flow prediction method function may be integrated into the central processing unit 9100. The central processing unit 9100 may be configured to perform the following control: Step S101: acquiring raw data related to traffic flow from multiple data sources, and preprocessing the raw data to obtain preprocessed data; the raw data includes historical traffic flow series, weather data, whether it is a holiday, road section information, and timestamp; Step S102: Divide the preprocessed data into a training set, a validation set, and a test set in chronological order; set the parameters of the random forest; sample data and features from the training set to generate multiple training samples, and use the training samples to construct and train a decision tree to obtain a trained random forest model; Step S103: Obtain the leaf node index of the training sample in each decision tree, and convert the leaf node index of each training sample into a high-dimensional sparse feature matrix; apply a dimensionality reduction algorithm to the high-dimensional sparse feature matrix to convert it into a low-dimensional dense feature matrix; Step S104: Concatenate the low-dimensional dense feature matrix with the historical traffic sequence to obtain a two-dimensional matrix; input the two-dimensional matrix into the transformer network, output the traffic data flow prediction model, adjust it using the validation set, and test it using the test set to obtain the final prediction model.
[0069] From the above description, it can be seen that the electronic device provided by the embodiment of the present application uses random forests to select important features of traffic flow characteristics in different time periods, and then uses dimensionality reduction to retain key discriminant information. The features with higher importance are input into the transformer network as output vectors, and the transformer network is used as a prediction model for traffic flow to perform feature learning and prediction. This method uses random forest feature selection to enhance the stability and representativeness of the data to a certain extent. At the same time, through feature fusion, it cleverly injects auxiliary information of different types and granularities into each processing step of the model, enabling the transformer to comprehensively utilize all information for more accurate traffic flow prediction. Combined with the generalization ability of the transformer network, the model has better adaptability in different time periods and different traffic scenarios, can effectively deal with the uncertainty of traffic flow changes, and has better prediction effects on new data and complex traffic conditions.
[0070] In another embodiment, the traffic flow prediction device can be configured separately from the central processing unit 9100. For example, the traffic flow prediction device can be configured as a chip connected to the central processing unit 9100, and the traffic flow prediction method function can be implemented under the control of the central processing unit.
[0071] like Figure 5 As shown, the electronic device 9600 may further include: a communication module 9110, an input unit 9120, an audio processor 9130, a display 9160, and a power supply 9170. It is worth noting that the electronic device 9600 does not necessarily have to include Figure 5 In addition, the electronic device 9600 may also include all components shown in Figure 5 For components not shown, reference may be made to the prior art.
[0072] like Figure 5 As shown, the central processing unit 9100 is sometimes also referred to as a controller or operation control, and may include a microprocessor or other processor device and / or logic device. The central processing unit 9100 receives input and controls the operation of various components of the electronic device 9600.
[0073] Memory 9140 can be, for example, one or more of a cache, flash memory, hard drive, removable media, volatile memory, non-volatile memory, or other suitable devices. It can store the aforementioned failure-related information and also store programs that execute the relevant information. The CPU 9100 can execute the programs stored in memory 9140 to implement information storage or processing.
[0074] The input unit 9120 provides input to the central processing unit 9100. The input unit 9120 may be, for example, a keypad or touch input device. The power supply 9170 is used to provide power to the electronic device 9600. The display 9160 is used to display objects such as images and text. The display may be, for example, an LCD display, but is not limited thereto.
[0075] The memory 9140 may be a solid-state memory, such as a read-only memory (ROM), random access memory (RAM), or SIM card. Alternatively, it may be a memory that retains information even when power is off, can be selectively erased, and is capable of storing additional data. Examples of such memory are sometimes referred to as EPROMs. The memory 9140 may also be some other type of device. The memory 9140 includes a buffer memory 9141 (sometimes referred to as a buffer). The memory 9140 may include an application / function storage unit 9142 for storing application programs and function programs, or processes used by the central processing unit 9100 to execute operations of the electronic device 9600.
[0076] The memory 9140 may also include a data storage unit 9143 for storing data, such as contacts, digital data, images, sounds, and / or any other data used by the electronic device. The driver storage unit 9144 of the memory 9140 may include various driver programs for communication functions of the electronic device and / or for executing other functions of the electronic device (such as messaging applications, address book applications, etc.).
[0077] The communication module 9110 is a transmitter / receiver that transmits and receives signals via the antenna 9111. The communication module 9110 (transmitter / receiver) is coupled to the central processor 9100 to provide input signals and receive output signals, which may be the same as the case of a conventional mobile communication terminal.
[0078] Based on different communication technologies, multiple communication modules 9110 may be provided in the same electronic device, such as cellular network modules, Bluetooth modules, and / or wireless local area network modules. The communication module 9110 (transmitter / receiver) is also coupled to a speaker 9131 and a microphone 9132 via an audio processor 9130, providing audio output via the speaker 9131 and receiving audio input from the microphone 9132, thereby implementing common telecommunication functions. The audio processor 9130 may include any suitable buffer, decoder, amplifier, etc. Furthermore, the audio processor 9130 is coupled to the central processing unit 9100, enabling local recording via the microphone 9132 and playback of stored audio via the speaker 9131.
[0079] The embodiments of the present application also provide a computer-readable storage medium capable of implementing all steps of the traffic flow prediction method in the above-mentioned embodiments, where the execution subject is a server or a client. The computer-readable storage medium stores a computer program. When the computer program is executed by a processor, the computer program implements all steps of the traffic flow prediction method in the above-mentioned embodiments, where the execution subject is a server or a client. For example, when the processor executes the computer program, the following steps are implemented: Step S101: acquiring raw data related to traffic flow from multiple data sources, and preprocessing the raw data to obtain preprocessed data; the raw data includes historical traffic flow series, weather data, whether it is a holiday, road section information, and timestamp; Step S102: Divide the preprocessed data into a training set, a validation set, and a test set in chronological order; set the parameters of the random forest; sample data and features from the training set to generate multiple training samples, and use the training samples to construct and train a decision tree to obtain a trained random forest model; Step S103: Obtain the leaf node index of the training sample in each decision tree, and convert the leaf node index of each training sample into a high-dimensional sparse feature matrix; apply a dimensionality reduction algorithm to the high-dimensional sparse feature matrix to convert it into a low-dimensional dense feature matrix; Step S104: Concatenate the low-dimensional dense feature matrix with the historical traffic sequence to obtain a two-dimensional matrix; input the two-dimensional matrix into the transformer network, output the traffic data flow prediction model, adjust it using the validation set, and test it using the test set to obtain the final prediction model.
[0080] From the above description, it can be seen that the computer-readable storage medium provided by the embodiment of the present application uses random forests to select important features of traffic flow characteristics in different time periods, and then uses dimensionality reduction to retain key discriminant information. The features with higher importance are input into the transformer network as output vectors, and the transformer network is used as a prediction model for traffic flow to perform feature learning and prediction. This method uses random forest feature selection to enhance the stability and representativeness of the data to a certain extent. At the same time, through feature fusion, it cleverly injects auxiliary information of different types and granularities into each processing step of the model, enabling the transformer to comprehensively utilize all information for more accurate traffic flow prediction. Combined with the generalization ability of the transformer network, the model has better adaptability in different time periods and different traffic scenarios, can effectively deal with the uncertainty of traffic flow changes, and has better prediction effects on new data and complex traffic conditions.
[0081] The embodiments of the present application also provide a computer program product capable of implementing all steps of the traffic flow prediction method in the above-mentioned embodiment, where the execution subject is a server or a client. When the computer program / instructions are executed by a processor, the computer program / instructions implement the steps of the traffic flow prediction method. For example, the computer program / instructions implement the following steps: Step S101: acquiring raw data related to traffic flow from multiple data sources, and preprocessing the raw data to obtain preprocessed data; the raw data includes historical traffic flow series, weather data, whether it is a holiday, road section information, and timestamp; Step S102: Divide the preprocessed data into a training set, a validation set, and a test set in chronological order; set the parameters of the random forest; sample data and features from the training set to generate multiple training samples, and use the training samples to construct and train a decision tree to obtain a trained random forest model; Step S103: Obtain the leaf node index of the training sample in each decision tree, and convert the leaf node index of each training sample into a high-dimensional sparse feature matrix; apply a dimensionality reduction algorithm to the high-dimensional sparse feature matrix to convert it into a low-dimensional dense feature matrix; Step S104: Concatenate the low-dimensional dense feature matrix with the historical traffic sequence to obtain a two-dimensional matrix; input the two-dimensional matrix into the transformer network, output the traffic data flow prediction model, adjust it using the validation set, and test it using the test set to obtain the final prediction model.
[0082] From the above description, it can be seen that the computer program product provided by the embodiment of the present application uses random forests to select important features of traffic flow characteristics in different time periods, and then uses dimensionality reduction to retain key discriminant information. The features with higher importance are input into the transformer network as output vectors, and the transformer network is used as a prediction model for traffic flow to perform feature learning and prediction. This method uses random forest feature selection to enhance the stability and representativeness of the data to a certain extent. At the same time, through feature fusion, it cleverly injects auxiliary information of different types and granularities into each processing step of the model, enabling the transformer to comprehensively utilize all information for more accurate traffic flow prediction. Combined with the generalization ability of the transformer network, the model has better adaptability in different time periods and different traffic scenarios, can effectively deal with the uncertainty of traffic flow changes, and has better prediction effects on new data and complex traffic conditions.
[0083] Those skilled in the art will appreciate that embodiments of the present invention may be provided as methods, apparatuses, or computer program products. Thus, the present invention may take the form of an entirely hardware embodiment, an entirely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to magnetic disk storage, CD-ROMs, optical storage, etc.) containing computer-usable program code.
[0084] The present invention is described with reference to flowcharts and / or block diagrams of methods, devices (apparatus), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowcharts and / or block diagrams, as well as combinations of processes and / or blocks in the flowcharts and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowcharts and / or block diagrams. Figure 1 a process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0085] These computer program instructions may also be stored in a computer readable memory that can direct a computer or other programmable data processing device to work in a specific manner, so that the instructions stored in the computer readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 a process or multiple processes and / or boxes Figure 1 The function specified in one or more boxes.
[0086] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operational steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing the instructions executed on the computer or other programmable device for implementing the process. Figure 1 a process or multiple processes and / or boxes Figure 1 A step that specifies a function in one or more boxes.
[0087] Specific embodiments are used in the present invention to illustrate the principles and implementation methods of the present invention. The description of the above embodiments is only used to help understand the method of the present invention and its core ideas. At the same time, for those skilled in the art, according to the ideas of the present invention, there may be changes in the specific implementation methods and application scopes. In summary, the contents of this specification should not be understood as limiting the present invention.
Claims
1. A traffic flow prediction method, characterized in that: The method comprises: Obtaining raw data related to traffic flow from multiple data sources and preprocessing the raw data to obtain preprocessed data; the raw data includes historical traffic series, weather data, whether it is a holiday, road section information, and timestamp; Dividing the preprocessed data into a training set, a validation set, and a test set in chronological order; setting parameters of a random forest; sampling data and features from the training set to generate multiple training samples, and using the training samples to construct and train a decision tree to obtain a trained random forest model; Obtaining the leaf node index of the training sample in each decision tree, and converting the leaf node index of each training sample into a high-dimensional sparse feature matrix; applying a dimensionality reduction algorithm to the high-dimensional sparse feature matrix to convert it into a low-dimensional dense feature matrix; The low-dimensional dense feature matrix is concatenated with the historical traffic sequence to obtain a two-dimensional matrix. The two-dimensional matrix is input into the transformer network, and the traffic data flow prediction model is output. The model is adjusted using the validation set and tested using the test set to obtain the final prediction model.
2. The traffic flow prediction method according to claim 1, characterized in that: The step of preprocessing the original data includes: A threshold range is set for the raw data, and values exceeding the threshold range are identified as outliers and removed. Missing raw data can be filled with the historical average flow value of the road section at the same time, or a reasonable value can be estimated using linear interpolation in the time series interpolation method. The raw data from different sources are unified to the same time and space granularity. Perform feature extraction and conversion on the collected raw data: convert timestamps into hourly values, expand them to determine whether they are holidays, and use sine / cosine encoding to handle periodicity; perform one-hot encoding on weather data and convert them into multiple binary features; and use embedded encoding to map road segment information into a low-dimensional dense vector space. A sliding window method is used to extract a first feature matrix and a first target vector from the historical traffic sequence; the value of the first feature matrix is the sliding window at the current position, and the value of the first target vector is a data point after the sliding window; if the length of the data processed by the sliding window is less than the size of the sliding window, the data is padded with 0.
3. The traffic flow prediction method according to claim 1, characterized in that: The step of using random forest to generate multiple training samples from the training set, and using the training samples to construct and train a decision tree includes: Loading a second feature matrix and a second target vector from the training set, and generating a plurality of training samples from the second feature matrix and the second target vector using a random forest; A decision tree is constructed from the generated training samples using a recursive partitioning algorithm; the recursive partitioning algorithm constructs a tree structure by continuously splitting nodes until a stopping condition is reached; Also included: Calculate the importance score of each feature while building each decision tree.
4. The traffic flow prediction method according to claim 1, characterized in that: The step of obtaining the leaf node index of the training sample in each decision tree and converting the leaf node index of each training sample into a high-dimensional sparse feature matrix includes: Input each sliding window sample into the trained random forest model and record the leaf node index of the sliding window sample in each decision tree; Create a one-hot encoded vector for each tree in the random forest, where the length of the one-hot encoded vector is equal to the number of leaf nodes in the decision tree; The one-hot encoding vectors of all decision trees are concatenated in sequence to form a high-dimensional sparse feature matrix; each row of the high-dimensional sparse feature matrix represents a sliding window sample, and each column represents a leaf node index.
5. The traffic flow prediction method according to claim 1, characterized in that: The step of applying a dimensionality reduction algorithm to a high-dimensional sparse feature matrix to convert it into a low-dimensional dense feature matrix includes: Calculating the covariance matrix of the high-dimensional sparse feature matrix, and finding the eigenvalues and eigenvectors of the covariance matrix; Select the first few eigenvectors in descending order of eigenvalues to form the projection matrix after dimensionality reduction; The high-dimensional sparse feature matrix is multiplied by the projection matrix to obtain the low-dimensional dense feature matrix.
6. The traffic flow prediction method according to claim 1, characterized in that: The step of concatenating the low-dimensional dense feature matrix with the historical traffic sequence to obtain a two-dimensional matrix includes: The historical traffic sequence and the first feature matrix extracted therefrom are used as the main sequence; for each time step in the sequence, a comprehensive feature vector is constructed; the comprehensive feature vector includes the traffic index value and the spatiotemporal features of the time step, and the low-dimensional dense feature matrix corresponding to the time step is spliced into each time step feature to obtain the two-dimensional matrix; The two-dimensional matrix also integrates temporal features, spatial features, and environmental features; the temporal features include the encoded hour, day of the week, and holiday flags; the spatial features include the road segment ID code, the average flow rate of adjacent road segments, POI density, and road type; and the environmental features include weather codes, temperature, and event flags.
7. The traffic flow prediction method according to claim 1, characterized in that: The step of inputting the two-dimensional matrix into a transformer network and outputting a traffic data flow prediction model includes: The transformer network uses the Adam optimizer to guide the training of the model, and the mean square error loss function is used as the evaluation indicator of the model performance.
8. A traffic flow prediction device, characterized in that: The device comprises: A data preprocessing module is used to obtain raw data related to traffic flow from multiple data sources and preprocess the raw data to obtain preprocessed data; the raw data includes historical traffic series, weather data, whether it is a holiday, road section information, and timestamp; A model building module is used to divide the preprocessed data into a training set, a validation set, and a test set in chronological order; set parameters of the random forest; sample data and features from the training set to generate multiple training samples, and use the training samples to build and train a decision tree to obtain a trained random forest model; A feature dimensionality reduction module is used to obtain the leaf node index of the training sample in each decision tree and convert the leaf node index of each training sample into a high-dimensional sparse feature matrix; apply a dimensionality reduction algorithm to the high-dimensional sparse feature matrix to convert it into a low-dimensional dense feature matrix; The model generation module is used to concatenate the low-dimensional dense feature matrix with the historical traffic sequence to obtain a two-dimensional matrix; the two-dimensional matrix is input into the transformer network, the traffic data flow prediction model is output, and the model is adjusted using the validation set and tested using the test set to obtain the final prediction model.
9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the program, the steps of the traffic flow prediction method according to any one of claims 1 to 7 are implemented.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the traffic flow prediction method according to any one of claims 1 to 7 are implemented.
Citation Information
Patent Citations
Sorting algorithms for high dimensional data
CN107273909A
GBDT learning method for online prediction task
CN112200316A
Goods quantity prediction method and system based on gradient boosting decision tree and neural network
CN114169612A
Highway traffic flow prediction method based on Transform and graph attention network
CN116092294A
Traffic flow prediction model determination method, device and apparatus, and readable storage medium
CN116432810A
Cited By
Traffic flow characteristic processing method and device
CN120632800A
Traffic equipment data processing method and device
CN120724296A
Traffic flow prediction method, device, equipment and product
CN121811640A