Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# streamlit_app.py
|
2 |
+
|
3 |
+
import streamlit as st
|
4 |
+
import pandas as pd
|
5 |
+
import seaborn as sns
|
6 |
+
import matplotlib.pyplot as plt
|
7 |
+
from sklearn.model_selection import train_test_split
|
8 |
+
from sklearn.preprocessing import StandardScaler
|
9 |
+
from sklearn.ensemble import VotingClassifier, StackingClassifier
|
10 |
+
from sklearn.linear_model import LogisticRegression
|
11 |
+
from sklearn.tree import DecisionTreeClassifier
|
12 |
+
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
|
13 |
+
from sklearn.svm import SVC
|
14 |
+
from sklearn.metrics import accuracy_score, confusion_matrix, roc_curve, auc, classification_report
|
15 |
+
|
16 |
+
# Title and description
|
17 |
+
st.title("Classification Model Comparison: Stacking and Voting Classifiers")
|
18 |
+
st.write("""
|
19 |
+
### Predict target goals using different ensemble techniques
|
20 |
+
This application compares the performance of Stacking and Voting classifiers on the provided dataset.
|
21 |
+
""")
|
22 |
+
|
23 |
+
# File upload
|
24 |
+
uploaded_file = st.file_uploader("Upload your CSV file", type=["csv"])
|
25 |
+
|
26 |
+
if uploaded_file is not None:
|
27 |
+
df = pd.read_csv(uploaded_file)
|
28 |
+
|
29 |
+
st.write("### Raw Data")
|
30 |
+
st.write(df)
|
31 |
+
|
32 |
+
# Correlation Matrix
|
33 |
+
corrMatrix = df.corr()
|
34 |
+
|
35 |
+
# Plot heatmap
|
36 |
+
st.write("### Correlation Heatmap")
|
37 |
+
plt.figure(figsize=(25, 10))
|
38 |
+
color_palette = sns.color_palette("viridis", as_cmap=True)
|
39 |
+
ax = sns.heatmap(corrMatrix, vmin=-1, vmax=1, center=0, cmap=color_palette, annot=True, fmt=".2f", linewidths=0.5, square=True, cbar_kws={"shrink": 0.75})
|
40 |
+
plt.title('Correlation Heatmap', fontsize=20, pad=20)
|
41 |
+
plt.xticks(rotation=45, ha='right')
|
42 |
+
plt.yticks(rotation=0)
|
43 |
+
st.pyplot(plt)
|
44 |
+
|
45 |
+
# Replace target variable
|
46 |
+
df['Target_goal'] = df['Target_goal'].replace({1: 0, 2: 1})
|
47 |
+
|
48 |
+
# Define features and target variable
|
49 |
+
X = df.drop(columns=['Target_goal'])
|
50 |
+
y = df['Target_goal']
|
51 |
+
|
52 |
+
# Split the data
|
53 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
54 |
+
|
55 |
+
# Standardize the data
|
56 |
+
scaler = StandardScaler()
|
57 |
+
X_train = scaler.fit_transform(X_train)
|
58 |
+
X_test = scaler.transform(X_test)
|
59 |
+
|
60 |
+
# Define base models for stacking and voting
|
61 |
+
estimators = [
|
62 |
+
('lr', LogisticRegression()),
|
63 |
+
('dt', DecisionTreeClassifier()),
|
64 |
+
('rf', RandomForestClassifier()),
|
65 |
+
('gb', GradientBoostingClassifier()),
|
66 |
+
('svc', SVC(probability=True))
|
67 |
+
]
|
68 |
+
|
69 |
+
# Stacking Classifier
|
70 |
+
stacking_clf = StackingClassifier(estimators=estimators, final_estimator=LogisticRegression())
|
71 |
+
stacking_clf.fit(X_train, y_train)
|
72 |
+
y_pred_stack = stacking_clf.predict(X_test)
|
73 |
+
y_pred_stack_proba = stacking_clf.predict_proba(X_test)[:, 1]
|
74 |
+
|
75 |
+
# Voting Classifier
|
76 |
+
voting_clf = VotingClassifier(estimators=estimators, voting='soft')
|
77 |
+
voting_clf.fit(X_train, y_train)
|
78 |
+
y_pred_vote = voting_clf.predict(X_test)
|
79 |
+
y_pred_vote_proba = voting_clf.predict_proba(X_test)[:, 1]
|
80 |
+
|
81 |
+
# Evaluation
|
82 |
+
st.write("### Accuracy Scores")
|
83 |
+
accuracy_stack = accuracy_score(y_test, y_pred_stack)
|
84 |
+
accuracy_vote = accuracy_score(y_test, y_pred_vote)
|
85 |
+
st.write(f'Stacking Classifier Accuracy: {accuracy_stack:.2f}')
|
86 |
+
st.write(f'Voting Classifier Accuracy: {accuracy_vote:.2f}')
|
87 |
+
|
88 |
+
# Classification Reports
|
89 |
+
st.write("### Classification Reports")
|
90 |
+
st.write("#### Stacking Classifier")
|
91 |
+
st.text(classification_report(y_test, y_pred_stack))
|
92 |
+
|
93 |
+
st.write("#### Voting Classifier")
|
94 |
+
st.text(classification_report(y_test, y_pred_vote))
|
95 |
+
|
96 |
+
# Confusion Matrix
|
97 |
+
st.write("### Confusion Matrix for Stacking Classifier")
|
98 |
+
conf_matrix_stack = confusion_matrix(y_test, y_pred_stack)
|
99 |
+
plt.figure(figsize=(6, 5))
|
100 |
+
sns.heatmap(conf_matrix_stack, annot=True, fmt='d', cmap='Blues')
|
101 |
+
plt.title('Stacking Classifier Confusion Matrix')
|
102 |
+
plt.xlabel('Predicted')
|
103 |
+
plt.ylabel('Actual')
|
104 |
+
st.pyplot(plt)
|
105 |
+
|
106 |
+
st.write("### Confusion Matrix for Voting Classifier")
|
107 |
+
conf_matrix_vote = confusion_matrix(y_test, y_pred_vote)
|
108 |
+
plt.figure(figsize=(6, 5))
|
109 |
+
sns.heatmap(conf_matrix_vote, annot=True, fmt='d', cmap='Blues')
|
110 |
+
plt.title('Voting Classifier Confusion Matrix')
|
111 |
+
plt.xlabel('Predicted')
|
112 |
+
plt.ylabel('Actual')
|
113 |
+
st.pyplot(plt)
|
114 |
+
|
115 |
+
# ROC Curve and AUC
|
116 |
+
fpr_stack, tpr_stack, _ = roc_curve(y_test, y_pred_stack_proba)
|
117 |
+
roc_auc_stack = auc(fpr_stack, tpr_stack)
|
118 |
+
|
119 |
+
fpr_vote, tpr_vote, _ = roc_curve(y_test, y_pred_vote_proba)
|
120 |
+
roc_auc_vote = auc(fpr_vote, tpr_vote)
|
121 |
+
|
122 |
+
plt.figure(figsize=(10, 6))
|
123 |
+
plt.plot(fpr_stack, tpr_stack, color='blue', lw=2, label='Stacking Classifier (AUC = %0.2f)' % roc_auc_stack)
|
124 |
+
plt.plot(fpr_vote, tpr_vote, color='red', lw=2, label='Voting Classifier (AUC = %0.2f)' % roc_auc_vote)
|
125 |
+
plt.plot([0, 1], [0, 1], color='gray', lw=1, linestyle='--')
|
126 |
+
plt.xlim([0.0, 1.0])
|
127 |
+
plt.ylim([0.0, 1.05])
|
128 |
+
plt.xlabel('False Positive Rate')
|
129 |
+
plt.ylabel('True Positive Rate')
|
130 |
+
plt.title('ROC Curve')
|
131 |
+
plt.legend(loc="lower right")
|
132 |
+
st.pyplot(plt)
|