NYC Taxi Trip Data: A Deep Dive

30/12/2019

Rating: 4.89 (9694 votes)

Understanding the NYC Taxi Trip Duration Dataset

The hustle and bustle of New York City are intrinsically linked to its iconic yellow cabs. For years, taxi services have been a cornerstone of urban mobility, and understanding the intricacies of their operations is vital for efficiency and customer satisfaction. One of the key challenges for any taxi company is predicting the duration of a trip. This knowledge is crucial for optimising cab assignments, managing driver schedules, and providing accurate estimated arrival times to passengers. The NYC Taxi Trip Duration Dataset offers a rich source of information for delving into these operational aspects. This article will explore this dataset, employing various data analysis techniques to uncover valuable insights into the factors that influence taxi trip durations in the Big Apple.

What models are used in NYC taxi fare prediction?
DevCon5 Community Meet up NYC Taxi Fare Prediction with 7 models (Linear Regression, Random Forest, XGBoost, LightGBM, CatBoost, KNN, and Decision Tree) The models used range from simple linear regression to more complex ensemble methods such as boosting algorithms.
Table

Data Loading and Initial Exploration

To begin our analysis, we first need to import the necessary Python libraries for data manipulation, analysis, and visualisation. These include pandas for data handling, numpy for numerical operations, matplotlib.pyplot and seaborn for plotting, and datetime for date and time manipulations. We then load the NYC Taxi Trip Duration dataset into a pandas DataFrame, which we'll refer to as df.

import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns import datetime sns.set() df = pd.read_csv('nyc_taxi_trip_duration.csv')

A fundamental step in any data analysis project is to get a preliminary understanding of the data. We examine the shape of the DataFrame to know the number of rows and columns, the columns to understand the available features, their data types, and the first few rows using df.head() to see the raw data structure.

The dataset comprises 729,322 rows and 11 columns. The columns can be broadly categorised into customer/vendor information, trip details, and the target variable, trip_duration.

Dataset Columns:

  • id: A unique identifier for each trip.
  • vendor_id: A code indicating the service provider.
  • pickup_datetime: The date and time when the taxi meter was engaged.
  • dropoff_datetime: The date and time when the taxi meter was disengaged.
  • passenger_count: The number of passengers in the vehicle, as entered by the driver.
  • pickup_longitude: The longitude coordinate of the pickup location.
  • pickup_latitude: The latitude coordinate of the pickup location.
  • dropoff_longitude: The longitude coordinate of the dropoff location.
  • dropoff_latitude: The latitude coordinate of the dropoff location.
  • store_and_fwd_flag: Indicates if the trip record was stored due to a lack of server connection (Y=store and forward; N=not a store and forward trip).
  • trip_duration: The target variable, representing the duration of the trip in seconds.

Initial inspection reveals that pickup_datetime and dropoff_datetime are stored as objects and will require conversion to a datetime format for effective time-based analysis. The store_and_fwd_flag is a categorical variable.

Descriptive Statistics

To gain a quantitative understanding of the data, we generate descriptive statistics for the numerical columns using df.describe(). This provides insights into measures like count, mean, standard deviation, minimum, maximum, and quartiles.

Key observations from the descriptive statistics include:

  • No missing values in numerical columns.
  • Passenger count typically ranges from 1 to 9, with most trips involving 1 or 2 passengers.
  • Trip duration exhibits a wide range, from 1 second to approximately 538 hours (1,939,736 seconds). This wide spread suggests the presence of significant outliers that will need to be addressed.

We also check the non-numerical columns using df[non_num_cols].count() to confirm the absence of missing values. As expected, there are no missing values in these columns either.

Data Preprocessing: Date and Time Conversion

For meaningful temporal analysis, we convert the pickup_datetime and dropoff_datetime columns from object type to datetime objects:

df['pickup_datetime'] = pd.to_datetime(df['pickup_datetime']) df['dropoff_datetime'] = pd.to_datetime(df['dropoff_datetime'])

Univariate Analysis: Exploring Individual Variables

Univariate analysis focuses on understanding the distribution of each variable independently.

Passenger Count Distribution

A histogram of passenger_count shows that the majority of taxi rides are taken by 1 or 2 passengers. Larger groups are less common.

Day of the Week Analysis

To analyse patterns related to days of the week, we extract the day name from the datetime columns:

df['pickup_day'] = df['pickup_datetime'].dt.day_name() df['dropoff_day'] = df['dropoff_datetime'].dt.day_name()

Frequency counts and bar plots reveal that Fridays and Thursdays tend to have the most taxi pickups and drop-offs, while Mondays have the fewest. This suggests potential correlations between the day of the week and travel demand.

Time of Day Analysis

To analyse temporal patterns, we can categorise trips into time zones: morning, midday, evening, and late night. We also extract the hour of the day for a more granular view.

The distributions of pickups and drop-offs across these time zones indicate that the evening hours are the busiest, while the morning hours see the least activity. Similarly, histograms of pickup and drop-off hours show peak activity during the late afternoon and early evening.

Store and Forward Flag Distribution

The store_and_fwd_flag shows a significant imbalance, with the vast majority of trips recorded with 'N' (not a store and forward trip). This suggests that most trips had a stable connection to the vendor's server.

Trip Duration Distribution

The distribution of trip_duration is highly skewed to the right, indicating the presence of extreme outliers. A box plot further confirms this, showing a single data point far beyond the typical range.

To address this, we remove the most extreme outlier (the maximum value) to mitigate its influence on subsequent analysis:

df = df[df.trip_duration != df.trip_duration.max()]

Even after outlier removal, the distribution remains right-skewed. To better understand this, we can categorise trip durations into bins:

  • Less than 5 hours
  • 5–10 hours
  • 10–15 hours
  • 15–20 hours
  • More than 20 hours

This binning helps in analysing longer trips more effectively.

Geographical Distribution

Histograms for pickup_longitude, pickup_latitude, dropoff_longitude, and dropoff_latitude show the geographical spread of taxi trips. The longitude distributions appear similar for pickup and dropoff, while latitude distributions show some differences. The concentration of points suggests the primary operational areas within New York City.

Who collects the NYC Taxi & Limousine data?
The data used in the attached datasets were collected and provided to the NYC Taxi and Limousine Commission (TLC) by technology providers authorized under the Taxicab & Livery Passenger Enhancement Programs (TPEP/LPEP). The trip data was not created by the TLC, and TLC makes no representations as to the accuracy of these data.

Vendor ID Distribution

The distribution of vendor_id is relatively even, indicating that both service providers have a significant presence in the dataset.

Bivariate Analysis: Relationships with Trip Duration

Bivariate analysis explores the relationships between pairs of variables, particularly focusing on how other factors correlate with trip_duration.

Trip Duration vs. Day of the Week

Bar plots showing the average trip duration per day of the week indicate that trips starting on Thursdays tend to have the longest average duration, while Mondays have the shortest. Further analysis of trip duration categories by day reveals that while most trips are short across all days, Thursdays and Fridays have a higher proportion of longer trips.

Trip Duration vs. Time of Day

Analysis by hour of the day shows that trips starting in the midday (around 14:00-17:00) tend to have the longest average durations. Early morning trips generally have the shortest durations.

Trip Duration vs. Passenger Count

A scatter plot reveals that passenger count has a negligible direct impact on trip duration. However, it's observed that very long trips are rarely taken by larger passenger groups (7 or 9 passengers).

Trip Duration vs. Vendor ID

Vendor 1 appears to handle a higher proportion of shorter trips, while Vendor 2 provides services for both short and longer duration trips, suggesting potential differences in their operational focus or fleet capabilities.

Trip Duration vs. Store and Forward Flag

The store_and_fwd_flag shows a clear pattern: the flag is almost exclusively associated with short duration trips. Long duration trips do not seem to utilise this store-and-forward mechanism.

Trip Duration vs. Geographical Location

When examining trip duration in relation to geographical coordinates, we observe:

  • Shorter trips (< 5 hours): Pickup and dropoff latitudes are more evenly distributed between 30° and 40°. Longitudes are spread between -80° and -65°, with a few outliers.
  • Longer trips (> 5 hours): Pickup and dropoff latitudes are concentrated between 40° and 42°. Longitudes are primarily clustered around -75°. This geographical clustering for longer trips likely corresponds to specific routes or areas within the city where longer journeys are more common.

Conclusion and Key Takeaways

The analysis of the NYC Taxi Trip Duration Dataset provides several key insights:

  • Trip durations vary significantly, from mere seconds to many hours, highlighting the need for robust prediction models.
  • Fridays and Thursdays are the busiest days for taxi services, and these days also see a higher incidence of longer trips.
  • Trip durations are generally longest when starting in the afternoon (14:00-17:00).
  • While passenger count doesn't directly correlate with duration, very long trips are less common for larger groups.
  • Vendor 2 appears to handle a broader spectrum of trip durations compared to Vendor 1.
  • The store_and_fwd_flag is an indicator of shorter trips.
  • Longer trips are geographically concentrated in specific latitude and longitude bands, suggesting defined travel corridors or destination patterns for extended journeys within New York City.

This comprehensive data analysis serves as a foundational step for building predictive models for taxi trip duration, offering valuable insights for taxi companies aiming to optimise their operations and enhance customer service.

Frequently Asked Questions (FAQ)

Q1: What is the primary goal of analysing the NYC Taxi Trip Duration Dataset?

A1: The primary goal is to understand the factors influencing taxi trip durations in New York City, enabling taxi companies to predict trip times more accurately, optimise cab assignments, and improve overall service efficiency.

Q2: Which factors most significantly impact taxi trip duration?

A2: Based on the analysis, the time of day (afternoon trips being longer), the day of the week (Thursdays and Fridays showing longer average durations), and geographical location (longer trips concentrating in specific areas) appear to be significant influencing factors.

Q3: Are there any outliers in the dataset, and how were they handled?

A3: Yes, the trip_duration column exhibited extreme outliers. The most significant outlier (the maximum value) was removed to prevent it from disproportionately affecting the analysis. Further analysis involved binning the durations to understand the distribution of shorter and longer trips.

Q4: What can be inferred about passenger behaviour from this dataset?

A4: Most trips are taken by 1 or 2 passengers. While passenger count doesn't directly correlate with duration, extremely long trips are less common for larger groups.

Q5: How do different vendors compare in terms of trip duration?

A5: Vendor 1 seems to focus more on shorter trips, whereas Vendor 2 handles both short and long-duration trips, indicating a potential difference in their service offerings or operational models.

Q6: What are the common models used for NYC taxi fare prediction?

A6: While this article focuses on trip duration analysis, common models used for taxi fare and duration prediction often include regression models, tree-based models like Random Forests and Gradient Boosting (e.g., XGBoost), and sometimes deep learning approaches, especially when incorporating complex spatial and temporal features.

If you want to read more articles similar to NYC Taxi Trip Data: A Deep Dive, you can visit the Transport category.

Go up