Skip to content

Instantly share code, notes, and snippets.

@smzn
Created January 8, 2024 02:01
Show Gist options
  • Save smzn/0e6cf3771b5c407db4d244ed632eb885 to your computer and use it in GitHub Desktop.
Save smzn/0e6cf3771b5c407db4d244ed632eb885 to your computer and use it in GitHub Desktop.
推移確率行列の作成
# Adjust the code to exclude the last column (row sum) in the normalization process
# We only use the numerical columns except the last one for division
numerical_part_excluding_sum = filtered_matrix.iloc[:, 1:-1] # Exclude the first (string) column and the last (row sum) column
row_sums_excluding_last_column = numerical_part_excluding_sum.sum(axis=1)
# Divide each cell by the row sum to normalize the rows, excluding the last column
normalized_matrix_excluding_sum = numerical_part_excluding_sum.div(row_sums_excluding_last_column, axis=0)
# Replace NaN values with 0
normalized_matrix_excluding_sum = normalized_matrix_excluding_sum.fillna(0)
# Re-add the station names as the first column
transition_probability_matrix = pd.concat([filtered_matrix.iloc[:, 0], normalized_matrix_excluding_sum], axis=1)
# 推移確率行列をCSVファイルに保存
transition_probability_matrix.to_csv(path + 'transition_probability_matrix.csv', index=True)
# Display the first few rows of the transition probability matrix excluding the row sum column
transition_probability_matrix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment