Spaces:
Sleeping
Sleeping
Modelmetrics hub
Browse files
app.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
from sklearn.model_selection import train_test_split
|
4 |
+
from sklearn.ensemble import RandomForestClassifier
|
5 |
+
from sklearn.tree import DecisionTreeClassifier
|
6 |
+
from sklearn.neighbors import KNeighborsClassifier
|
7 |
+
from sklearn.svm import SVC
|
8 |
+
from sklearn.linear_model import LogisticRegression
|
9 |
+
from sklearn.metrics import accuracy_score
|
10 |
+
|
11 |
+
# Function to build and evaluate models
|
12 |
+
def build_and_evaluate_model(model, X_train, X_test, y_train, y_test):
|
13 |
+
try:
|
14 |
+
model.fit(X_train, y_train)
|
15 |
+
y_pred = model.predict(X_test)
|
16 |
+
# Convert y_pred to the same data type as y_test for comparison
|
17 |
+
y_pred = y_pred.astype(str) #Used in data manipulation
|
18 |
+
accuracy = accuracy_score(y_test.astype(str), y_pred)
|
19 |
+
return accuracy
|
20 |
+
except Exception as e:
|
21 |
+
return f"Not Applicable: {str(e)}"
|
22 |
+
|
23 |
+
# Streamlit app
|
24 |
+
def main():
|
25 |
+
# Custom CSS for styling the title
|
26 |
+
custom_css = """
|
27 |
+
<style>
|
28 |
+
.title-text {
|
29 |
+
font-size: 55px;
|
30 |
+
font-weight: bold;
|
31 |
+
color: #FF0A01; /* Dark Blue */
|
32 |
+
text-align: center;
|
33 |
+
margin-bottom: 0px;
|
34 |
+
}
|
35 |
+
.tagline-text {
|
36 |
+
font-size: 18px;
|
37 |
+
font-style: italic;
|
38 |
+
color: #2c3e50; /* White */
|
39 |
+
text-align: center;
|
40 |
+
margin-top:-10px;
|
41 |
+
margin-bottom: 20px;
|
42 |
+
}
|
43 |
+
</style>
|
44 |
+
"""
|
45 |
+
|
46 |
+
# Streamlit app
|
47 |
+
st.markdown(custom_css, unsafe_allow_html=True)
|
48 |
+
# Title and tagline
|
49 |
+
st.markdown("<p class='title-text'>ModelMetrics Hub</p>", unsafe_allow_html=True)
|
50 |
+
st.markdown("<p class='tagline-text'>Optimal Model Explorer</p>", unsafe_allow_html=True)
|
51 |
+
|
52 |
+
#st.title("ModelMetrics Hub \n _Optimal Model Explorer_")
|
53 |
+
st.sidebar.title("Model Selection")
|
54 |
+
model_names = ["Random Forest", "Decision Tree", "KNN", "SVM", "Logistic Regression"]
|
55 |
+
selected_models = st.sidebar.multiselect("Choose models to compare", model_names, default=model_names)
|
56 |
+
|
57 |
+
uploaded_file = st.file_uploader("Upload a CSV file", type=["csv"])
|
58 |
+
if uploaded_file is not None:
|
59 |
+
data = pd.read_csv(uploaded_file, encoding='latin-1') # Specify encoding here
|
60 |
+
st.write("Preview of the dataset:")
|
61 |
+
st.write(data.head())
|
62 |
+
|
63 |
+
feature_columns = st.multiselect("Select feature columns", data.columns.tolist())
|
64 |
+
target_column = st.selectbox("Select target column", data.columns.tolist())
|
65 |
+
|
66 |
+
if st.button("Run Models"):
|
67 |
+
X = data[feature_columns]
|
68 |
+
y = data[target_column]
|
69 |
+
|
70 |
+
y = y.astype(str)
|
71 |
+
|
72 |
+
# Perform one-hot encoding for categorical columns
|
73 |
+
categorical_cols = X.select_dtypes(include=['object']).columns.tolist()
|
74 |
+
X = pd.get_dummies(X, columns=categorical_cols, drop_first=True)
|
75 |
+
|
76 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
77 |
+
|
78 |
+
models = {
|
79 |
+
"Random Forest": RandomForestClassifier(n_estimators=100, random_state=42),
|
80 |
+
"Decision Tree": DecisionTreeClassifier(random_state=42),
|
81 |
+
"KNN": KNeighborsClassifier(n_neighbors=5),
|
82 |
+
"SVM": SVC(kernel='rbf', random_state=42),
|
83 |
+
"Logistic Regression": LogisticRegression(max_iter=1000, random_state=42)
|
84 |
+
}
|
85 |
+
|
86 |
+
results = {}
|
87 |
+
for model_name in selected_models:
|
88 |
+
accuracy = build_and_evaluate_model(models[model_name], X_train, X_test, y_train, y_test)
|
89 |
+
results[model_name] = accuracy
|
90 |
+
|
91 |
+
sorted_results = sorted(results.items(), key=lambda x: x[1], reverse=True) #key=lambda x: is a custom sorting order
|
92 |
+
|
93 |
+
st.subheader("Accuracy")
|
94 |
+
for model_name, accuracy in sorted_results:
|
95 |
+
st.write(f"{model_name}: {accuracy * 100:.2f}%")
|
96 |
+
|
97 |
+
if __name__ == "__main__":
|
98 |
+
main()
|