Last active
January 8, 2024 01:44
-
-
Save smzn/3d17c9562b22dc7dcc9758544f58b4f3 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
# 行と列の両方に存在するステーション名を抽出 | |
common_stations = set(transition_matrix.index) & set(transition_matrix.columns) | |
# 行と列の両方に含まれるステーションのみに行列を制限 | |
filtered_matrix = transition_matrix.loc[common_stations, common_stations] | |
# filtered_matrix の各行の合計を計算 | |
row_sums = filtered_matrix.sum(axis=1) | |
# 行和が0でないステーションのリストを取得 | |
non_zero_stations = row_sums[row_sums != 0].index | |
# 行和が0でないステーションに対応する行と列のみを保持 | |
filtered_matrix = filtered_matrix.loc[non_zero_stations, non_zero_stations] | |
# 各行の合計を新しい列として追加 | |
filtered_matrix['Row Sum'] = row_sums | |
filtered_matrix.to_csv(path + 'transition_count_matrix.csv', index=True) | |
# 結果を表示 | |
filtered_matrix |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment