Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,13 +6,13 @@ import matplotlib.pyplot as plt
|
|
| 6 |
from sklearn.model_selection import train_test_split
|
| 7 |
from sklearn.impute import SimpleImputer
|
| 8 |
from sklearn.preprocessing import LabelEncoder, StandardScaler
|
| 9 |
-
from sklearn.ensemble import RandomForestClassifier
|
| 10 |
-
from sklearn.linear_model import LogisticRegression
|
| 11 |
-
from sklearn.svm import SVC
|
| 12 |
-
from sklearn.neighbors import KNeighborsClassifier
|
| 13 |
-
from sklearn.tree import DecisionTreeClassifier
|
| 14 |
from sklearn.naive_bayes import GaussianNB
|
| 15 |
-
from sklearn.metrics import classification_report, accuracy_score
|
| 16 |
from scipy import stats
|
| 17 |
|
| 18 |
# File uploader
|
|
@@ -78,16 +78,8 @@ if uploaded_file is not None:
|
|
| 78 |
X = df_cleaned[features]
|
| 79 |
y = df_cleaned[target]
|
| 80 |
|
| 81 |
-
#
|
| 82 |
-
|
| 83 |
-
X = df_cleaned[features]
|
| 84 |
-
y = df_cleaned[target]
|
| 85 |
-
|
| 86 |
-
# Check if the target is continuous (for regression) or categorical (for classification)
|
| 87 |
-
if y.dtype == 'O' or len(y.unique()) <= 10: # Treat as classification if target is categorical or has <= 10 unique values
|
| 88 |
-
is_classification = True
|
| 89 |
-
else:
|
| 90 |
-
is_classification = False
|
| 91 |
|
| 92 |
# Ensure there is enough data before proceeding with train-test split
|
| 93 |
if len(X) == 0 or len(y) == 0:
|
|
@@ -100,31 +92,43 @@ if uploaded_file is not None:
|
|
| 100 |
# Store results in a dictionary
|
| 101 |
results = []
|
| 102 |
|
| 103 |
-
#
|
|
|
|
| 104 |
if is_classification:
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
for name,
|
| 115 |
-
|
| 116 |
-
y_pred =
|
| 117 |
-
|
| 118 |
-
# Accuracy and Classification Report
|
| 119 |
accuracy = accuracy_score(y_test, y_pred)
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
# Append results
|
| 123 |
-
results.append([name, accuracy, classification_report_output])
|
| 124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
# Display results in a table
|
| 126 |
st.subheader("Model Performance Results")
|
| 127 |
-
results_df = pd.DataFrame(results, columns=["Model", "Accuracy", "
|
| 128 |
|
| 129 |
# Bold the headers
|
| 130 |
st.markdown(f"**Model Performance Results**")
|
|
|
|
| 6 |
from sklearn.model_selection import train_test_split
|
| 7 |
from sklearn.impute import SimpleImputer
|
| 8 |
from sklearn.preprocessing import LabelEncoder, StandardScaler
|
| 9 |
+
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
|
| 10 |
+
from sklearn.linear_model import LogisticRegression, LinearRegression, Ridge
|
| 11 |
+
from sklearn.svm import SVC, SVR
|
| 12 |
+
from sklearn.neighbors import KNeighborsClassifier, KNeighborsRegressor
|
| 13 |
+
from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor
|
| 14 |
from sklearn.naive_bayes import GaussianNB
|
| 15 |
+
from sklearn.metrics import classification_report, accuracy_score, mean_squared_error
|
| 16 |
from scipy import stats
|
| 17 |
|
| 18 |
# File uploader
|
|
|
|
| 78 |
X = df_cleaned[features]
|
| 79 |
y = df_cleaned[target]
|
| 80 |
|
| 81 |
+
# Determine if the target is continuous or categorical
|
| 82 |
+
is_classification = len(y.unique()) <= 10 # If target has fewer than or equal to 10 unique values, treat as classification
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
# Ensure there is enough data before proceeding with train-test split
|
| 85 |
if len(X) == 0 or len(y) == 0:
|
|
|
|
| 92 |
# Store results in a dictionary
|
| 93 |
results = []
|
| 94 |
|
| 95 |
+
# Model Selection and Evaluation
|
| 96 |
+
models = []
|
| 97 |
if is_classification:
|
| 98 |
+
model_choices = [
|
| 99 |
+
("Random Forest", RandomForestClassifier(n_estimators=50)),
|
| 100 |
+
("Logistic Regression", LogisticRegression(max_iter=1000)),
|
| 101 |
+
("SVM", SVC()),
|
| 102 |
+
("K-Nearest Neighbors", KNeighborsClassifier(n_neighbors=5)),
|
| 103 |
+
("Decision Tree", DecisionTreeClassifier()),
|
| 104 |
+
("Naive Bayes", GaussianNB())
|
| 105 |
+
]
|
| 106 |
+
|
| 107 |
+
for name, model in model_choices:
|
| 108 |
+
model.fit(X_train, y_train)
|
| 109 |
+
y_pred = model.predict(X_test)
|
|
|
|
|
|
|
| 110 |
accuracy = accuracy_score(y_test, y_pred)
|
| 111 |
+
results.append([name, accuracy, None])
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
+
else:
|
| 114 |
+
model_choices = [
|
| 115 |
+
("Random Forest", RandomForestRegressor(n_estimators=50)),
|
| 116 |
+
("Linear Regression", LinearRegression()),
|
| 117 |
+
("SVR", SVR()),
|
| 118 |
+
("K-Nearest Neighbors", KNeighborsRegressor(n_neighbors=5)),
|
| 119 |
+
("Decision Tree", DecisionTreeRegressor()),
|
| 120 |
+
("Ridge Regression", Ridge())
|
| 121 |
+
]
|
| 122 |
+
|
| 123 |
+
for name, model in model_choices:
|
| 124 |
+
model.fit(X_train, y_train)
|
| 125 |
+
y_pred = model.predict(X_test)
|
| 126 |
+
mse = mean_squared_error(y_test, y_pred)
|
| 127 |
+
results.append([name, None, mse])
|
| 128 |
+
|
| 129 |
# Display results in a table
|
| 130 |
st.subheader("Model Performance Results")
|
| 131 |
+
results_df = pd.DataFrame(results, columns=["Model", "Accuracy" if is_classification else "Accuracy (N/A)", "Mean Squared Error" if not is_classification else "MSE (N/A)"])
|
| 132 |
|
| 133 |
# Bold the headers
|
| 134 |
st.markdown(f"**Model Performance Results**")
|