Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
7 |
+
from sklearn.tree import DecisionTreeClassifier
|
8 |
+
from sklearn.model_selection import train_test_split
|
9 |
+
import numpy as np
|
10 |
+
|
11 |
+
# Function to process data and return feature importances
|
12 |
+
def calculate_importances(file):
|
13 |
+
# Read uploaded file
|
14 |
+
heart_df = pd.read_csv(file)
|
15 |
+
|
16 |
+
# Set X and y
|
17 |
+
X = heart_df.drop('target', axis=1)
|
18 |
+
y = heart_df['target']
|
19 |
+
|
20 |
+
# Split the data
|
21 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)
|
22 |
+
|
23 |
+
# Initialize models
|
24 |
+
rf_model = RandomForestClassifier(random_state=42)
|
25 |
+
xgb_model = XGBClassifier(use_label_encoder=False, eval_metric='logloss', random_state=42)
|
26 |
+
cart_model = DecisionTreeClassifier(random_state=42)
|
27 |
+
|
28 |
+
# Train models
|
29 |
+
rf_model.fit(X_train, y_train)
|
30 |
+
xgb_model.fit(X_train, y_train)
|
31 |
+
cart_model.fit(X_train, y_train)
|
32 |
+
|
33 |
+
# Get feature importances
|
34 |
+
rf_importances = rf_model.feature_importances_
|
35 |
+
xgb_importances = xgb_model.feature_importances_
|
36 |
+
cart_importances = cart_model.feature_importances_
|
37 |
+
|
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 |
+
# Save to Excel
|
54 |
+
file_name = 'feature_importances.xlsx'
|
55 |
+
importance_df.to_excel(file_name, index=False)
|
56 |
+
|
57 |
+
return file_name, importance_df.head()
|
58 |
+
|
59 |
+
# Streamlit interface
|
60 |
+
st.title("Feature Importance Calculation")
|
61 |
+
|
62 |
+
# File upload
|
63 |
+
uploaded_file = st.file_uploader("Upload heart.csv file", type=['csv'])
|
64 |
+
|
65 |
+
if uploaded_file is not None:
|
66 |
+
# Process the file and get results
|
67 |
+
excel_file, preview_df = calculate_importances(uploaded_file)
|
68 |
+
|
69 |
+
# Display a preview of the DataFrame
|
70 |
+
st.write("Feature Importances (Preview):")
|
71 |
+
st.dataframe(preview_df)
|
72 |
+
|
73 |
+
# Provide a link to download the Excel file
|
74 |
+
with open(excel_file, "rb") as file:
|
75 |
+
btn = st.download_button(
|
76 |
+
label="Download Excel File",
|
77 |
+
data=file,
|
78 |
+
file_name=excel_file,
|
79 |
+
mime="application/vnd.ms-excel"
|
80 |
+
)
|