Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import seaborn as sns
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
from sklearn.preprocessing import LabelEncoder
|
5 |
+
from sklearn.model_selection import train_test_split
|
6 |
+
from sklearn.ensemble import RandomForestClassifier
|
7 |
+
from sklearn.metrics import confusion_matrix, classification_report
|
8 |
+
import streamlit as st
|
9 |
+
|
10 |
+
def process_and_evaluate(file):
|
11 |
+
# Load the dataset
|
12 |
+
df = pd.read_csv(file)
|
13 |
+
|
14 |
+
# Encode categorical features
|
15 |
+
categorical_columns = df.select_dtypes(include=['object']).columns
|
16 |
+
label_encoders = {}
|
17 |
+
for col in categorical_columns:
|
18 |
+
le = LabelEncoder()
|
19 |
+
df[col] = le.fit_transform(df[col])
|
20 |
+
label_encoders[col] = le
|
21 |
+
|
22 |
+
# Define the target and features
|
23 |
+
target = 'target' # Assuming the target column is named 'target'
|
24 |
+
X = df.drop(columns=[target])
|
25 |
+
y = df[target]
|
26 |
+
|
27 |
+
# Split the data into training and testing sets
|
28 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
29 |
+
|
30 |
+
# Train a RandomForestClassifier
|
31 |
+
clf = RandomForestClassifier(random_state=42)
|
32 |
+
clf.fit(X_train, y_train)
|
33 |
+
|
34 |
+
# Predict on the test set
|
35 |
+
y_pred = clf.predict(X_test)
|
36 |
+
|
37 |
+
# Compute the confusion matrix
|
38 |
+
conf_matrix = confusion_matrix(y_test, y_pred)
|
39 |
+
|
40 |
+
# Generate the classification report
|
41 |
+
classification_rep = classification_report(y_test, y_pred)
|
42 |
+
|
43 |
+
return df, conf_matrix, classification_rep
|
44 |
+
|
45 |
+
# Streamlit interface
|
46 |
+
st.title("Heart Disease Prediction")
|
47 |
+
st.write("Upload a CSV file containing heart disease data to get the classification report.")
|
48 |
+
|
49 |
+
uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
|
50 |
+
|
51 |
+
if uploaded_file is not None:
|
52 |
+
df, conf_matrix, report = process_and_evaluate(uploaded_file)
|
53 |
+
|
54 |
+
# Display the classification report
|
55 |
+
st.subheader("Classification Report")
|
56 |
+
st.text(report)
|
57 |
+
|
58 |
+
# Plot the correlation matrix
|
59 |
+
st.subheader("Correlation Matrix")
|
60 |
+
corr_matrix = df.corr()
|
61 |
+
fig, ax = plt.subplots(figsize=(10, 8))
|
62 |
+
sns.heatmap(corr_matrix, annot=True, fmt=".2f", cmap="coolwarm", ax=ax)
|
63 |
+
st.pyplot(fig)
|
64 |
+
|
65 |
+
# Plot the confusion matrix
|
66 |
+
st.subheader("Confusion Matrix")
|
67 |
+
fig, ax = plt.subplots()
|
68 |
+
sns.heatmap(conf_matrix, annot=True, fmt="d", cmap="Blues", ax=ax)
|
69 |
+
ax.set_xlabel('Predicted')
|
70 |
+
ax.set_ylabel('Actual')
|
71 |
+
st.pyplot(fig)
|