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.
Outline:
1). Introduction
2). Data Preprocessing (Python)
3). Exploratory Data Analysis (Tableau)
4). Modelling (Python)
5). Model Analysisย (Python)
6). Prediction (Shiny App)
7). Conclusion and Future Work
8).ย Reference
Welcome to visit the raw code!
Data cleaning Python code
Modelling Python code
Prediction Python code
Shinyย Code
1. Introduction:
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 onehour time window and a location within New York City.ย The problem is formulated in terms of the following inputs and outputs:
2. Data Preprocessing
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,
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 pickup 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 thirdparty 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 timezip codeweather 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 โ201404 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 preprocessing 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 67 pm, and bottoms out around 45 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 onehotcoding. 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.
4.2 Evaluation
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
Multiplelinear regression
The multiplelinear 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.
Ridge regression
To improve multiplelinear 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 multiplelinear regression.
Why did we useย Bayesian Optimization instead of gridsearch 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 nontrivial, offthegrid 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.
Random Forest
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 maxfeatures(number of splits at each tree) and nestimators (number of tress) are determined using Bayesian Optimization select parameter values. Of the values we swept, our model performed best with maxfeatures 14 and nestimators 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
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 hyperparameters 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
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 coneshaped 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 24hour 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.
Kmeans Clustering
Kmeans Clustering: In order to find nonobvious 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.
8. Reference
Data period: AprSept, 2014
Data Source:
Uber: https://github.com/fivethirtyeight/ubertlcfoilresponse
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/cdoweb/
Data Visualisation Tableau Files:ย https://github.com/nycdatasci/bootcamp006_project/blob/master/Project5Capstone/Big4/Tableau_file.zip