-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
193 lines (156 loc) · 8.12 KB
/
app.py
File metadata and controls
193 lines (156 loc) · 8.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import streamlit as st
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression, LogisticRegression
from sklearn.svm import SVR, SVC
from sklearn.tree import DecisionTreeRegressor, DecisionTreeClassifier
from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier
from sklearn.metrics import mean_squared_error, r2_score, accuracy_score, precision_score, recall_score, f1_score
from sklearn.preprocessing import LabelEncoder, StandardScaler, PolynomialFeatures
from sklearn.pipeline import make_pipeline
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
import xgboost as xgb
import matplotlib.pyplot as plt
st.set_page_config(
page_title="Model Selection"
)
st.title("ML Model Selection Web App")
st.subheader("by [Junaid Saleem](https://github.com/JunaidSalim)")
uploadedFile = st.file_uploader("Upload your dataset (CSV)", type=["csv"])
if uploadedFile:
data = pd.read_csv(uploadedFile)
st.write("Dataset Preview:")
st.write(data.head())
st.write("Dataset Statistics:")
st.write(data.describe())
summary_df = pd.DataFrame({
'Missing Values': data.isnull().sum(),
'Data Types': data.dtypes
})
st.write("Dataset Info:")
st.write(summary_df)
st.write("Feature Distribution:")
numericColumns = data.select_dtypes(include=['float64', 'int64']).columns
if len(numericColumns) == 0:
st.write("No numeric columns available for visualization.")
else:
columnCount = 0
while columnCount < len(numericColumns):
remainingColumns = len(numericColumns) - columnCount
numCols = min(4, remainingColumns)
fig, axs = plt.subplots(nrows=1, ncols=numCols, figsize=(15, 3))
if numCols == 1:
axs = [axs]
for i in range(numCols):
col = numericColumns[columnCount]
axs[i].hist(data[col], bins=20, color='blue', edgecolor='black')
axs[i].set_title(f"{col}")
columnCount += 1
st.pyplot(fig)
plt.close(fig)
st.write("Remove Columns:")
columnsToRemove = st.multiselect("Select columns to remove", data.columns)
if columnsToRemove:
data = data.drop(columns=columnsToRemove)
targetColumn = st.selectbox("Select the target column", data.columns, index=len(data.columns) - 1)
st.write("Handle Missing Values:")
missingMethod = st.radio("Select method:", ['Mean', 'Median', 'Mode', 'Remove'])
for column in data.columns:
if data[column].isnull().sum() > 0:
if missingMethod == 'Mean':
data[column].fillna(data[column].mean(), inplace=True)
elif missingMethod == 'Median':
data[column].fillna(data[column].median(), inplace=True)
elif missingMethod == 'Mode':
data[column].fillna(data[column].mode()[0], inplace=True)
elif missingMethod == 'Remove':
data = data.dropna(subset=[column])
st.write("Data Preprocessing Options:")
normalizeData = st.checkbox("Normalize Data")
oneHotEncode = st.checkbox("One-Hot Encode Categorical Features")
inputFeatures = data.drop(columns=[targetColumn])
targetFeature = data[targetColumn]
if targetFeature.dtype == 'object':
targetFeature = LabelEncoder().fit_transform(targetFeature)
if targetFeature.nunique() == 2:
uniqueClasses = targetFeature.unique()
targetFeature = targetFeature.map({uniqueClasses[0]: 0, uniqueClasses[1]: 1})
if oneHotEncode:
columnsToEncode = st.multiselect("Select columns to one-hot encode", inputFeatures.columns)
inputFeatures = pd.get_dummies(inputFeatures, columns=columnsToEncode)
if normalizeData:
scaler = StandardScaler()
inputFeatures = pd.DataFrame(scaler.fit_transform(inputFeatures), columns=inputFeatures.columns)
trainFeatures, testFeatures, trainLabels, testLabels = train_test_split(
inputFeatures, targetFeature, test_size=0.2, random_state=42
)
defaultTask = "Classification" if targetFeature.nunique() < 10 else "Regression"
taskType = st.selectbox("Choose a task (Default detected: {})".format(defaultTask), ["Regression", "Classification"], index=0 if defaultTask == "Regression" else 1)
hyperCols = st.columns(3)
if taskType == "Regression":
svrC = hyperCols[0].number_input("SVR: C", 0.01, 10.0, 1.0)
svrKernel = hyperCols[1].selectbox("SVR: Kernel", ["linear", "poly", "rbf", "sigmoid"])
dtMaxDepth = hyperCols[2].number_input("Decision Tree: Max Depth", 1, 50, 5)
rfNEstimators = hyperCols[0].number_input("Random Forest: Estimators", 10, 500, 100)
xgbLearningRate = hyperCols[1].number_input("XGBoost: Learning Rate", 0.01, 1.0, 0.1)
xgbNEstimators = hyperCols[2].number_input("XGBoost: Estimators", 10, 500, 100)
models = {
"Multiple Linear Regression": LinearRegression(),
"Polynomial Regression": make_pipeline(PolynomialFeatures(degree=2), LinearRegression()),
"SVR": SVR(C=svrC, kernel=svrKernel),
"Decision Tree Regression": DecisionTreeRegressor(max_depth=dtMaxDepth),
"Random Forest Regression": RandomForestRegressor(n_estimators=rfNEstimators),
"XGBoost Regression": xgb.XGBRegressor(learning_rate=xgbLearningRate, n_estimators=xgbNEstimators)
}
results = []
for modelName, model in models.items():
model.fit(trainFeatures, trainLabels)
predictions = model.predict(testFeatures)
mse = mean_squared_error(testLabels, predictions)
r2 = r2_score(testLabels, predictions)
results.append({
"Model": modelName,
"Mean Squared Error": mse,
"R² Score": r2
})
resultsDf = pd.DataFrame(results)
st.write("Regression Results:")
st.write(resultsDf)
elif taskType == "Classification":
knnNeighbors = hyperCols[0].number_input("KNN: Neighbors", 1, 50, 5)
svcC = hyperCols[1].number_input("SVC: C", 0.01, 10.0, 1.0)
svcKernel = hyperCols[2].selectbox("SVC: Kernel", ["linear", "poly", "rbf", "sigmoid"])
dtMaxDepth = hyperCols[0].number_input("Decision Tree: Max Depth", 1, 50, 5)
rfNEstimators = hyperCols[1].number_input("Random Forest: Estimators", 10, 500, 100)
xgbLearningRate = hyperCols[2].number_input("XGBoost: Learning Rate", 0.01, 1.0, 0.1)
xgbNEstimators = hyperCols[0].number_input("XGBoost: Estimators", 10, 500, 100)
models = {
"Logistic Regression": LogisticRegression(max_iter=1000),
"KNN": KNeighborsClassifier(n_neighbors=knnNeighbors),
"SVC": SVC(C=svcC, kernel=svcKernel),
"Naive Bayes": GaussianNB(),
"Decision Tree": DecisionTreeClassifier(max_depth=dtMaxDepth),
"Random Forest": RandomForestClassifier(n_estimators=rfNEstimators),
"XGBoost": xgb.XGBClassifier(learning_rate=xgbLearningRate, n_estimators=xgbNEstimators)
}
if targetFeature.nunique() > 2:
models = {key: model for key, model in models.items() if not isinstance(model, (LogisticRegression, SVC, xgb.XGBClassifier, CatBoostClassifier))}
results = []
for modelName, model in models.items():
model.fit(trainFeatures, trainLabels)
predictions = model.predict(testFeatures)
accuracy = accuracy_score(testLabels, predictions)
precision = precision_score(testLabels, predictions, average='weighted')
recall = recall_score(testLabels, predictions, average='weighted')
f1 = f1_score(testLabels, predictions, average='weighted')
results.append({
"Model": modelName,
"Accuracy": accuracy,
"Precision": precision,
"Recall": recall,
"F1 Score": f1
})
resultsDf = pd.DataFrame(results)
st.write("Classification Results:")
st.write(resultsDf)