Data Study to Predict New York City Taxi Demand
The skills the author demoed here can be learned through taking Data Science with Machine Learning bootcamp with NYC Data Science Academy.
2). Data Pre-processing (Python)
3). Exploratory Data Analysis (Tableau)
4). Modelling (Python)
5). Model Analysis (Python)
6). Prediction (Shiny App)
7). Conclusion and Future Work
Welcome to visit the raw code!
Data cleaning Python code
Modelling Python code
Prediction Python code
Data shows there are roughly 200 million taxi rides in New York City each year. Exploiting an understanding of taxi supply and demand could increase the efficiency of the city’s taxi system. In the New York city, people use taxi in a frequency much higher than any other cities of US. Instead of booking a taxi by phone one day ahead of time, New York taxi drivers pick up passengers on street.
The ability to predict taxi ridership could present valuable insights to city planners and taxi dispatchers in answering questions such as how to position cabs where they are most needed, how many taxis to dispatch, and how ridership varies over time. Our project focuses on predicting the number of taxi pickups given a one-hour time window and a location within New York City. The problem is formulated in terms of the following inputs and outputs:
2. Data Pre-processing
We use a joining dataset detailing all ~1 billion taxi trips (14G) in New York City from April and September in 2014, as provided by he NYC Taxi and Limousine Commission (TLC), including information of yellow, green and uber taxies. The data associates each taxi ride with information including date, time, and location of pickup and drop-off, trip distance, payment type, tip amount, total amount.
Also hourly weather information is incorporated in the big taxi dataset. A small number of taxi pickups in this dataset originate from well outside the New York City area. In order to constrain our problem to New York City as well as to reduce the size of our data given our limited computational resources, we only consider taxi trips that originate somewhere defined in the figure on the right.
2.1 Data cleaning
The information including taxi pick-up date, time, longitude and latitude coordinates is selected, which contains 1 billion observations, and thus is hard for visualization and modeling. To reduce dimension and data set, longitude and latitude variables are rounded to 3 decimals and condensed to 120,000 records.
We used a python library Geopy to find out zip code from corresponding latitude and longitude. The geopy is a client for several popular geocoding web services, which makes it easy for user to locate the coordinates of addresses, cities, countries, and landmarks across the globe using third-party geocoders and other data sources.
In order to put the raw data into the same form as our input to the problem, we group the raw taxi data by time (at the granularity of an hour) zip code, temperature and participation of rainfall, count the total number of pickups for each time-zip code-weather combination, and store these aggregated values as data points to be used for training and testing. For instance, one row in our aggregated pickups table is “2014-04- 01 00:00:00, 49.0, 0, 10001, 375”, representing 375 pickups in zip code 10001 on April 1, 2014 between 0 aM and 1 aM local time, 49.0 ºF and no rain. In total, our data set consists of 710,000 such data points.
3. Exploratory Data Analysis (Tableau)
After pre-processing the data, we did the exploratory data analysis by using Tableau.
3.1 Weather influence on Taxi Demand
Firstly , we are exploring how the taxi demand is affected by the weather, from two attributes: Temperature and Rainfall.
From the temperature graph, the overall trend of hourly pick up frequency is getting larger from middle 62ºF to the right and left sides. Meanwhile, the fluctuation is getting bigger from middle to the two directions as the temperature becoming colder or hotter. Thus, the temperature has great influence on the taxi demand, which is also very intuitive that people require more taxies to go out when the temperature are cold, for example below 36ºF. While, when the temperature getting hot, more and more people come to the city, including travelers, which increase the demand of taxies.
While, the influence from rainfall to the hourly taxi demand is counterintuitive. From the rainfall graph, the hourly taxi demand does not increase as the participation of rainfall increase. But, the fluctuation of hourly taxi demand is getting bigger as the participation of rainfall increase, which indicate that the hourly taxi demand may affected by rainfall, but may be not so much, or it is a combination with other features to affect the taxi demand.
3.2 Operation Area by Taxi Type
Secondly , we are looking for the relationship among the different types of taxi. From the heat map, Uber operation area focus more on Manhattan area. While the green taxi is not allowed to take passenger in Manhattan, so there is no green points in the heat map. Last but not least, yellow taxi has the most records and wide spread operation area.
3.3 Cyclical Trend of Taxi Demand
Furthermore, we group the trip information by weekday and hour to see how the taxi demand changes.
As you can see , Uber, yellow taxi, and green taxi follow the similar cyclical trend in both of the above plots. The total taxi demand peaks around Fridays and Saturdays, while bottoms out around Sundays and Mondays. Meanwhile, it peaks around 6-7 pm, and bottoms out around 4-5 am within a day.
It is more obvious by visualizing the hourly changes in animated maps below. The colour faded at the most around 5 am in the morning, as we discussed above.
4. Modelling (Python)
By filtering the zip code within New York City, our data set has 71,000 observations.
4.1 Feature Engineering
Below is a list of feature templates we use to extract features from each data point:
- Zipcode. We expect location to be highly predictive of taxi traffic.
- Hour of day ∈ [0, 23]. We expect overall NYC taxi ridership to follow a daily cycle.
- Day of week ∈ [0, 6]. We expect day of week to correlate with taxi traffic.
Temperature, measured in fahrenheit. We expect temperature to increase taxi ridership variance, since in a cold weather people require more taxies to go out and also in a hot weather more people come to the city, including travelers, which increase the demand of taxies.
Hourly rain precipitation, measured in an inch. We expect precipitation to increase taxi ridership, since in rainy weather people may prefer taking taxis to walking or taking public transportation.
Zip code treated as categorical variables. For categorical variables, a common practice is one-hot-coding. For zip code with 198 possible values, we create a group of 198 dummy variables. Suppose a record in the data takes one value for this variable, then the corresponding dummy variable is set to 1 while other dummies in the same group are all set to 0.
In order to evaluate the performance of our model, we split the data into a training set (80% of data set) and testing set (20% of data set) , where the training examples are all ordered chronologically before the testing examples. This configuration mimics the task of predicting future numbers of taxi pickups using only past data.
We chose RMSE to evaluate our prediction because it favors consistency and heavily penalizes predictions with a high deviation from the true number of pickups.
From the point of view of a taxi dispatcher, any large mistake in gauging taxi demand for a particular zip code could be costly ‒ imagine sending 600 taxis to a zip code that only truly requires 400. This misallocation results in many unutilized taxis crowded in the same place, and should be penalized more heavily than dispatching 6 taxis to a zone that only requires 4, or even dispatching 6 taxis to 100 different zones that only require 4 taxis each. RMSE most heavily penalizes such large misallocations and best represents the quality of our models’ predictions.
In comparing the results between our different models, we also report the R^2 value (coefficient of determination) in order to evaluate how well the models perform relative to the variance of the data set.
4.3 Regression Models
The multiple-linear regression model allows us to exploit linear patterns in the data set. This model is an appealing first choice because feature weights are easily interpretable and because it runs efficiently on large datasets. The result is showed below.
To improve multiple-linear regression and introduce regularization, ridge model is applied and the hyperparameters alpha used for ridge model are determined using Bayesian Optimization select parameter values. The result shows that ridge regression does not improve multiple-linear regression.
Why did we use Bayesian Optimization instead of grid-search for hyperparameters tuning process?
Bayesian Optimization: uses a Gaussian Process to model the surrogate, and typically optimizes the Expected Improvement, which is the expected probability that new trials will improve upon the current best observation. Gaussian Process is a distribution over functions. A sample from a Gaussian process is an entire function. Training a Gaussian Process involves fitting this distribution to the given data, so that it generates functions that are close to the observed data.
Using Gaussian process, one can compute the Expected Improvement of any point in the search space. The one gives the highest expected improvement will be tried next. Bayesian Optimization typically gives non-trivial, off-the-grid values for continuous hyperparameters (like the learning rate, regularization coefficient and so on) and was shown to beat human performance on some good benchmark datasets.
The tree regression model is capable of representing complex decision boundaries, thus complementing our other chosen models. Random Forest is chosen since it prevents overfitting and robust against outliers. And the hyperparameters max-features(number of splits at each tree) and n-estimators (number of tress) are determined using Bayesian Optimization select parameter values. Of the values we swept, our model performed best with max-features 14 and n-estimators of 500.
We listed the top 20 important features produced by random forest. As demonstrated in EDA, the hour, weekday and temperature features are important. Also some zip codes impact the modeling most. By further investigation, the true value of taxi demand in these zip codes area have high value and high variance.
XGBoost is an advanced gradient boosting algorithm. It is a highly sophisticated algorithm, powerful enough to deal with all sorts of irregularities of data. The tool is extremely flexible, which allows users to customize a wide range of hyper-parameters while training the mode, and ultimately to reach the optimal solution.
For our model, the booster parameters are tuned byBayesian Optimization to find the best combination of hyperparameters (listed in the table), where max_depth is the maximum depth of a tree, learning_rate determines the impact of each tree on the final outcome ignorer to avoid overfitting, n_estimators is the number of trees, gamma is the minimum loss reduction at each split, min_child_weight is the minimum sum of weights of all aberrations required at each split node, subsample is the fraction of observations randomly sampled for each tree and closample_bytree is the fraction of columns randomly sampled for each tree.
The result is showed below.
Ensemble modeling is the process of running two or more related but different analytical models and then synthesizing the results into a single score or spread in order to improve the accuracy of predictive analytics and data mining applications. We combined our two strong models: randomforest and xgboost for ensemble modeling and used linear regression.
The results of all the models are compared in the below table, and ensemble modeling did not further improve the prediction as expected. Overall xgboost performs best.
5. Model Data Analysis
In order to visualize how well the models (randomforest, xgboost, ensemble) perform, we plot the true versus predicted number of pickups for each data point in the test set in the Figure.
The scatter plots in the figure suggest that the three models perform well on the test set. Most predictions lie close to the true values. The data points straddle the unit slope line evenly, signifying that the models do not systematically underestimate or overestimate the number of taxi pickups. For three models, as expected, absolute prediction error increases as the true number of pickups increases. This effect can be visualized as a cone-shaped region extending outward from the origin within which the data points fall.
To take a deep look at how absolute prediction error varies, the data is separated to 3 subsets based on the true number of pickups: subset 1 with pickups greater and equal than 1000, subset 2 with pickups greater and equal than 100 and less than 1000, subset 3 with pickups less than 100. From this table, we can confirm that RMSE increases as the true number of pickups increases. Also xgboost performs best in different subsets.
The below figure shows the comparison between the prediction of 3 models and the true value by randomly pick 10 samples from each subset. At each individual value, different models give different performance. So we did the predictions for the next coming week using three models and the result is visualized, summarized and compared by shiny interactive application.
6. Prediction (shiny) Through Data
Hourly weather is predicted by sin function using information from weather channel. The predictions for the incoming week is presented in shiny app:
The web interactive application can help user to compare the number of pickups of three different models (random forest, xgboost, ensemble) and across different locations in a given time zone, and also visualize the trend of the number of pickups in a 24-hour cycle across different locations within New York City.
7. Conclusion and Future Work
Overall, our models for predicting taxi pickups in New York City performed well. The xgboost regression model performed best, likely due to its unique ability to capture complex feature dependencies. The decision tree regression model achieved a value of 35.01 for RMSE and 0.98 for R^2. Our results and error analysis for the most part supported our intuitions about the usefulness of our features, with the exception of the unexpected result that participation of rainfall feature is not important for model performance. A model could be useful to city planners and taxi dispatchers in determining where to position taxicabs and studying patterns in ridership. In the future, we will implement the 2 below models.
Neural Network Regression
Neural network regression: We may be able to achieve good results using a neural network regression, since neural networks can automatically tune and model feature interactions. Instead of manually determining which features to combine in order to capture feature interactions, we could let the learning algorithm perform this task.
One possible instance of features interacting in the real world could be that New Yorkers may take taxi rides near Central Park or when it is raining, but not when they are near Central Park and it is raining, since they may not visit the park in bad weather. Neural networks could be promising because they can learn nonlinearities automatically, such as this example of an XOR relationship between features. In addition to our three core regression models, we implemented a neural network regression model using the Python library PyBrain. However, we would need more time to give the neural network model due consideration, so we list it here as possible future work.
K-means Clustering: In order to find non-obvious patterns across data points, we could use unsupervised learning to cluster our training set. The clustering algorithm could use features such as the number of bars and restaurants in a given zone, or distance to the nearest subway station. The cluster in which each data point falls could then serve as an additional feature for our regression models, thereby exploiting similar characteristics between different zones for learning.
Data period: Apr-Sept, 2014
Yellow/green Cab: http://www.nyc.gov/html/tlc/html/about/trip_record_data.shtml
Weather: NOAA climate data website: http://www.ncdc.noaa.gov/cdo-web/
Data Visualisation Tableau Files: https://github.com/nycdatasci/bootcamp006_project/blob/master/Project5-Capstone/Big4/Tableau_file.zip