Commit ·
560e80b
1
Parent(s): 6086a84
add heatmaps
Browse files
app.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import altair as alt
|
|
|
|
|
|
|
| 4 |
|
| 5 |
#st.set_page_config(layout="wide")
|
| 6 |
|
|
@@ -661,4 +663,93 @@ styled_df = df.style.set_table_styles(
|
|
| 661 |
}).format("{:.2%}")
|
| 662 |
|
| 663 |
# Display the styled DataFrame
|
| 664 |
-
st.markdown(styled_df.to_html(), unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import altair as alt
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import seaborn as sns
|
| 6 |
|
| 7 |
#st.set_page_config(layout="wide")
|
| 8 |
|
|
|
|
| 663 |
}).format("{:.2%}")
|
| 664 |
|
| 665 |
# Display the styled DataFrame
|
| 666 |
+
st.markdown(styled_df.to_html(), unsafe_allow_html=True)
|
| 667 |
+
|
| 668 |
+
# heatmap
|
| 669 |
+
|
| 670 |
+
num_video_df = pd.read_csv('num_video_df.tsv', sep='\t')
|
| 671 |
+
|
| 672 |
+
def render_vanilla_heatmap():
|
| 673 |
+
|
| 674 |
+
# Compute the correlation matrix
|
| 675 |
+
corr_matrix = num_video_df.corr()
|
| 676 |
+
|
| 677 |
+
# Specify the variable of interest (e.g., 'target_variable')
|
| 678 |
+
variable_of_interest = 'Level'
|
| 679 |
+
|
| 680 |
+
# Sort the variables based on correlation with the variable of interest
|
| 681 |
+
sorted_vars = corr_matrix[variable_of_interest].sort_values(ascending=False).index
|
| 682 |
+
|
| 683 |
+
# Reorder the correlation matrix
|
| 684 |
+
sorted_corr_matrix = corr_matrix.loc[sorted_vars, sorted_vars]
|
| 685 |
+
|
| 686 |
+
# Create a heatmap using seaborn with the sorted correlation matrix
|
| 687 |
+
plt.figure(figsize=(10, 8))
|
| 688 |
+
sns.heatmap(sorted_corr_matrix, annot=True, cmap='coolwarm', fmt=".3f")
|
| 689 |
+
|
| 690 |
+
# Display the heatmap
|
| 691 |
+
#plt.show()
|
| 692 |
+
st.pyplot(plt.gcf())
|
| 693 |
+
|
| 694 |
+
render_vanilla_heatmap()
|
| 695 |
+
|
| 696 |
+
def render_level_row_unordered():
|
| 697 |
+
|
| 698 |
+
# Compute the correlation matrix
|
| 699 |
+
corr_matrix = num_video_df.drop(['Proportion of determiners', 'Proportion of nouns', 'Proportion of wago', 'Proportion of gairaigo'], axis=1).corr()
|
| 700 |
+
|
| 701 |
+
# Specify the variable of interest (e.g., 'Level')
|
| 702 |
+
variable_of_interest = 'Level'
|
| 703 |
+
|
| 704 |
+
# Sort the variables based on correlation with the variable of interest
|
| 705 |
+
sorted_vars = corr_matrix[variable_of_interest].sort_values(ascending=False).index
|
| 706 |
+
|
| 707 |
+
# Remove 'Level' from the sorted variables to exclude the self-correlation
|
| 708 |
+
sorted_vars = sorted_vars.drop(variable_of_interest)
|
| 709 |
+
|
| 710 |
+
# Reorder the correlation matrix and exclude 'Level' column from the first row
|
| 711 |
+
first_row_matrix = corr_matrix.loc[[variable_of_interest], sorted_vars]
|
| 712 |
+
|
| 713 |
+
# Create a heatmap using seaborn with the single row of the correlation matrix
|
| 714 |
+
plt.figure(figsize=(10, 1)) # Adjust the figure size to make it more appropriate for a single row
|
| 715 |
+
sns.heatmap(first_row_matrix, annot=True, cmap='coolwarm', fmt=".3f", cbar_kws={'label': 'Correlation'})
|
| 716 |
+
|
| 717 |
+
# Display the heatmap
|
| 718 |
+
#plt.show()
|
| 719 |
+
st.pyplot(plt.gcf())
|
| 720 |
+
|
| 721 |
+
def render_level_col_ordered():
|
| 722 |
+
|
| 723 |
+
# Compute the correlation matrix
|
| 724 |
+
corr_matrix = num_video_df.drop(['Proportion of determiners', 'Proportion of nouns', 'Proportion of wago', 'Proportion of gairaigo'], axis=1).corr()
|
| 725 |
+
|
| 726 |
+
# Specify the variable of interest (e.g., 'Level')
|
| 727 |
+
variable_of_interest = 'Level'
|
| 728 |
+
|
| 729 |
+
# Get the correlations of the variable of interest
|
| 730 |
+
correlations = corr_matrix[variable_of_interest]
|
| 731 |
+
|
| 732 |
+
# Sort the variables based on the absolute value of the correlation with the variable of interest
|
| 733 |
+
sorted_vars = correlations.abs().sort_values(ascending=False).index
|
| 734 |
+
|
| 735 |
+
# Remove 'Level' from the sorted variables (to exclude the self-correlation)
|
| 736 |
+
sorted_vars = sorted_vars.drop(variable_of_interest)
|
| 737 |
+
|
| 738 |
+
# Reorder the correlation matrix, excluding the self-correlation
|
| 739 |
+
sorted_corr_matrix = corr_matrix.loc[[variable_of_interest], sorted_vars]
|
| 740 |
+
|
| 741 |
+
# Transpose the matrix to make it vertical
|
| 742 |
+
transposed_corr_matrix = sorted_corr_matrix.T
|
| 743 |
+
|
| 744 |
+
# Create a heatmap using seaborn with the transposed correlation matrix
|
| 745 |
+
plt.figure(figsize=(2, 3)) # Adjust the figure size to make it more appropriate for a vertical layout
|
| 746 |
+
sns.heatmap(transposed_corr_matrix, annot=True, cmap='coolwarm', fmt=".3f", cbar_kws={'label': 'Correlation'})
|
| 747 |
+
|
| 748 |
+
# Display the heatmap
|
| 749 |
+
#plt.show()
|
| 750 |
+
st.pyplot(plt.gcf())
|
| 751 |
+
|
| 752 |
+
if st.checkbox('Flip and sort'):
|
| 753 |
+
render_level_col_ordered()
|
| 754 |
+
else:
|
| 755 |
+
render_level_row_unordered()
|