Roberta2024 commited on
Commit
5c0cabb
·
verified ·
1 Parent(s): bbb0a94

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -37
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import streamlit as st
2
  import pandas as pd
3
- import matplotlib.pyplot as plt
4
  import seaborn as sns
5
  from sklearn.ensemble import RandomForestClassifier
6
  from xgboost import XGBClassifier
@@ -38,26 +38,14 @@ def calculate_importances(file):
38
  feature_names = X.columns
39
 
40
  # Prepare DataFrame
41
- rf_importance = {'Feature': feature_names, 'Random Forest': rf_importances}
42
- xgb_importance = {'Feature': feature_names, 'XGBoost': xgb_importances}
43
- cart_importance = {'Feature': feature_names, 'CART': cart_importances}
44
-
45
- # Create DataFrames
46
- rf_df = pd.DataFrame(rf_importance)
47
- xgb_df = pd.DataFrame(xgb_importance)
48
- cart_df = pd.DataFrame(cart_importance)
49
-
50
- # Merge DataFrames
51
- importance_df = rf_df.merge(xgb_df, on='Feature').merge(cart_df, on='Feature')
52
 
53
  # Correlation Matrix
54
  corr_matrix = heart_df.corr()
55
 
56
- # Save to Excel
57
- file_name = 'feature_importances.xlsx'
58
- importance_df.to_excel(file_name, index=False)
59
-
60
- return file_name, importance_df, corr_matrix, rf_importances, feature_names
61
 
62
  # Streamlit interface
63
  st.title("Feature Importance Calculation")
@@ -67,30 +55,25 @@ uploaded_file = st.file_uploader("Upload heart.csv file", type=['csv'])
67
 
68
  if uploaded_file is not None:
69
  # Process the file and get results
70
- excel_file, importance_df, corr_matrix, rf_importances, feature_names = calculate_importances(uploaded_file)
71
-
72
- # Display a preview of the DataFrame
73
- st.write("Feature Importances (Preview):")
74
- st.dataframe(importance_df.head())
75
 
76
- # Provide a link to download the Excel file
77
- with open(excel_file, "rb") as file:
78
- btn = st.download_button(
79
- label="Download Excel File",
80
- data=file,
81
- file_name=excel_file,
82
- mime="application/vnd.ms-excel"
83
- )
84
-
85
- # Plot and display the Correlation Matrix
86
  st.write("Correlation Matrix:")
87
  plt.figure(figsize=(10, 8))
88
  sns.heatmap(corr_matrix, annot=True, fmt=".2f", cmap="coolwarm", cbar=True)
89
  st.pyplot(plt)
90
 
91
- # Plot and display the Feature Importance (Random Forest)
92
  st.write("Random Forest Feature Importance:")
93
- fig, ax = plt.subplots()
94
- sns.barplot(x=rf_importances, y=feature_names, ax=ax)
95
- ax.set_title('Random Forest Feature Importances')
96
- st.pyplot(fig)
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import pandas as pd
3
+ import plotly.express as px
4
  import seaborn as sns
5
  from sklearn.ensemble import RandomForestClassifier
6
  from xgboost import XGBClassifier
 
38
  feature_names = X.columns
39
 
40
  # Prepare DataFrame
41
+ rf_importance = pd.DataFrame({'Feature': feature_names, 'Importance': rf_importances})
42
+ xgb_importance = pd.DataFrame({'Feature': feature_names, 'Importance': xgb_importances})
43
+ cart_importance = pd.DataFrame({'Feature': feature_names, 'Importance': cart_importances})
 
 
 
 
 
 
 
 
44
 
45
  # Correlation Matrix
46
  corr_matrix = heart_df.corr()
47
 
48
+ return rf_importance, xgb_importance, cart_importance, corr_matrix
 
 
 
 
49
 
50
  # Streamlit interface
51
  st.title("Feature Importance Calculation")
 
55
 
56
  if uploaded_file is not None:
57
  # Process the file and get results
58
+ rf_importance, xgb_importance, cart_importance, corr_matrix = calculate_importances(uploaded_file)
 
 
 
 
59
 
60
+ # Display the correlation matrix as a heatmap (static for now)
 
 
 
 
 
 
 
 
 
61
  st.write("Correlation Matrix:")
62
  plt.figure(figsize=(10, 8))
63
  sns.heatmap(corr_matrix, annot=True, fmt=".2f", cmap="coolwarm", cbar=True)
64
  st.pyplot(plt)
65
 
66
+ # Plot and display Random Forest Feature Importances with Plotly
67
  st.write("Random Forest Feature Importance:")
68
+ fig_rf = px.bar(rf_importance, x='Importance', y='Feature', orientation='h', title="Random Forest Feature Importances")
69
+ st.plotly_chart(fig_rf)
70
+
71
+ # Plot and display XGBoost Feature Importances with Plotly
72
+ st.write("XGBoost Feature Importance:")
73
+ fig_xgb = px.bar(xgb_importance, x='Importance', y='Feature', orientation='h', title="XGBoost Feature Importances")
74
+ st.plotly_chart(fig_xgb)
75
+
76
+ # Plot and display CART (Decision Tree) Feature Importances with Plotly
77
+ st.write("CART (Decision Tree) Feature Importance:")
78
+ fig_cart = px.bar(cart_importance, x='Importance', y='Feature', orientation='h', title="CART (Decision Tree) Feature Importances")
79
+ st.plotly_chart(fig_cart)