Created
January 8, 2024 06:00
-
-
Save smzn/595463c6a37c92209cf516e2e9590219 to your computer and use it in GitHub Desktop.
平均利用時間行列
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Aligning the station names exactly with the transition probability matrix, excluding any extra stations | |
# First, we adjust the list of station names to exclude the extra station (if any) | |
aligned_stations = set(transition_probability_matrix.columns) - {'start_station_name'} | |
# Filter the station_stats dataframe to include only those rows where both the start and end stations are in the aligned_stations set | |
filtered_station_stats = station_stats[ | |
(station_stats['start_station_name'].isin(aligned_stations)) & | |
(station_stats['end_station_name'].isin(aligned_stations)) | |
] | |
# Creating a pivot table for the mean travel times, exactly aligned with the stations in the transition probability matrix | |
mean_travel_time_matrix = filtered_station_stats.pivot( | |
index='start_station_name', | |
columns='end_station_name', | |
values='mean_travel_time_min' | |
) | |
# Filling NaN values with 0, assuming no travel time for non-existent trips | |
mean_travel_time_matrix = mean_travel_time_matrix.fillna(0) | |
# Reindexing the matrix to include only the stations from the transition probability matrix, filling missing values with 0 | |
mean_travel_time_matrix = mean_travel_time_matrix.reindex( | |
index=aligned_stations, | |
columns=aligned_stations, | |
fill_value=0 | |
) | |
mean_travel_time_matrix.to_csv(path + 'mean_travel_time_matrix', index=True) | |
mean_travel_time_matrix |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment