File size: 9,606 Bytes
ba67510 |
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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
from sklearn.linear_model import LinearRegression, SGDRegressor, Ridge, Lasso, ElasticNet
from sklearn.ensemble import RandomForestRegressor, AdaBoostRegressor, GradientBoostingRegressor, HistGradientBoostingRegressor
from sklearn.neighbors import KNeighborsRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn.svm import SVR
from xgboost import XGBRegressor, XGBRFRegressor
from sklearn.neural_network import MLPRegressor
from lightgbm import LGBMRegressor
from sklearn.naive_bayes import GaussianNB
from sklearn.model_selection import GridSearchCV
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
import streamlit as st
import evaluationer
from sklearn.metrics import root_mean_squared_error
from sklearn.linear_model import LogisticRegression, SGDClassifier, RidgeClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier, GradientBoostingClassifier, HistGradientBoostingClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC
from xgboost import XGBClassifier, XGBRFClassifier
from sklearn.neural_network import MLPClassifier
from lightgbm import LGBMClassifier
from sklearn.naive_bayes import MultinomialNB, CategoricalNB
param_grids_class = {
"Logistic Regression": {
'penalty': ['l1', 'l2', 'elasticnet', 'none'],
'C': [0.01, 0.1, 1, 10],
'solver': ['lbfgs', 'liblinear', 'saga']
},
"SGD Classifier": {
'loss': ['hinge', 'log', 'modified_huber', 'squared_hinge'],
'penalty': ['l2', 'l1', 'elasticnet'],
'alpha': [0.0001, 0.001, 0.01],
'max_iter': [1000, 5000, 10000]
},
"Ridge Classifier": {
'alpha': [0.1, 1, 10, 100]
},
"Random Forest Classifier": {
'n_estimators': [100, 200, 300],
'max_depth': [None, 10, 20, 30],
'min_samples_split': [2, 5, 10],
'min_samples_leaf': [1, 2, 4]
},
"AdaBoost Classifier": {
'n_estimators': [50, 100, 200],
'learning_rate': [0.01, 0.1, 1]
},
"Gradient Boosting Classifier": {
'n_estimators': [100, 200, 300],
'learning_rate': [0.01, 0.1, 0.2],
'max_depth': [3, 5, 7]
},
"Hist Gradient Boosting Classifier": {
'learning_rate': [0.01, 0.1, 0.2],
'max_depth': [None, 10, 20],
'min_samples_leaf': [20, 50, 100]
},
"K Neighbors Classifier": {
'n_neighbors': [3, 5, 7],
'weights': ['uniform', 'distance'],
'metric': ['euclidean', 'manhattan']
},
"Decision Tree Classifier": {
'max_depth': [None, 10, 20, 30],
'min_samples_split': [2, 5, 10],
'min_samples_leaf': [1, 2, 4]
},
"SVC": {
'C': [0.1, 1, 10],
'kernel': ['linear', 'poly', 'rbf'],
'degree': [3, 4, 5],
'gamma': ['scale', 'auto']
},
"XGB Classifier": {
'n_estimators': [100, 200, 300],
'learning_rate': [0.01, 0.1, 0.2],
'max_depth': [3, 5, 7]
},
"XGBRF Classifier": {
'n_estimators': [100, 200, 300],
'learning_rate': [0.01, 0.1, 0.2],
'max_depth': [3, 5, 7]
},
"MLP Classifier": {
'hidden_layer_sizes': [(50,), (100,), (50, 50)],
'activation': ['tanh', 'relu'],
'solver': ['adam', 'sgd'],
'alpha': [0.0001, 0.001, 0.01],
'learning_rate': ['constant', 'adaptive']
},
"LGBM Classifier": {
'n_estimators': [100, 200, 300],
'learning_rate': [0.01, 0.1, 0.2],
'max_depth': [-1, 10, 20]
},
"Multinomial Naive Bayes": {
'alpha': [0.1, 0.5, 1.0]
},
"Categorical Naive Bayes": {
'alpha': [0.1, 0.5, 1.0]
}
}
param_grids_reg = {
"Linear Regression": {},
"SGD Regressor": {
'loss': ['squared_loss', 'huber'],
'penalty': ['l2', 'l1', 'elasticnet'],
'alpha': [0.0001, 0.001, 0.01],
'max_iter': [1000, 5000, 10000]
},
"Ridge Regressor": {
'alpha': [0.1, 1, 10, 100],
'solver': ['auto', 'svd', 'cholesky', 'lsqr']
},
"Lasso Regressor": {
'alpha': [0.1, 1, 10, 100]
},
"ElasticNet Regressor": {
'alpha': [0.1, 1, 10, 100],
'l1_ratio': [0.1, 0.5, 0.9]
},
"Random Forest Regressor": {
'n_estimators': [100, 200, 300],
'max_depth': [None, 10, 20, 30],
'min_samples_split': [2, 5, 10],
'min_samples_leaf': [1, 2, 4]
},
"AdaBoost Regressor": {
'n_estimators': [50, 100, 200],
'learning_rate': [0.01, 0.1, 1]
},
"Gradient Boosting Regressor": {
'n_estimators': [100, 200, 300],
'learning_rate': [0.01, 0.1, 0.2],
'max_depth': [3, 5, 7]
},
"Hist Gradient Boosting Regressor": {
'learning_rate': [0.01, 0.1, 0.2],
'max_depth': [None, 10, 20],
'min_samples_leaf': [20, 50, 100]
},
"K Neighbors Regressor": {
'n_neighbors': [3, 5, 7],
'weights': ['uniform', 'distance'],
'metric': ['euclidean', 'manhattan']
},
"Decision Tree Regressor": {
'max_depth': [None, 10, 20, 30],
'min_samples_split': [2, 5, 10],
'min_samples_leaf': [1, 2, 4]
},
"SVR": {
'C': [0.1, 1, 10],
'kernel': ['linear', 'poly', 'rbf'],
'degree': [3, 4, 5],
'gamma': ['scale', 'auto']
},
"XGB Regressor": {
'n_estimators': [100, 200, 300],
'learning_rate': [0.01, 0.1, 0.2],
'max_depth': [3, 5, 7]
},
"XGBRF Regressor": {
'n_estimators': [100, 200, 300],
'learning_rate': [0.01, 0.1, 0.2],
'max_depth': [3, 5, 7]
},
"MLP Regressor": {
'hidden_layer_sizes': [(50,), (100,), (50, 50)],
'activation': ['tanh', 'relu'],
'solver': ['adam', 'sgd'],
'alpha': [0.0001, 0.001, 0.01],
'learning_rate': ['constant', 'adaptive']
},
"LGBM Regressor": {
'n_estimators': [100, 200, 300],
'learning_rate': [0.01, 0.1, 0.2],
'max_depth': [-1, 10, 20]
},
"Gaussian Naive Bayes": {
'var_smoothing': [1e-9, 1e-8, 1e-7]
}
}
# Define the regressors
regressors = {
"Linear Regression": LinearRegression(),
"SGD Regressor": SGDRegressor(),
"Ridge Regressor": Ridge(),
"Lasso Regressor": Lasso(),
"ElasticNet Regressor": ElasticNet(),
"Random Forest Regressor": RandomForestRegressor(),
"AdaBoost Regressor": AdaBoostRegressor(),
"Gradient Boosting Regressor": GradientBoostingRegressor(),
"Hist Gradient Boosting Regressor": HistGradientBoostingRegressor(),
"K Neighbors Regressor": KNeighborsRegressor(),
"Decision Tree Regressor": DecisionTreeRegressor(),
"SVR": SVR(),
"XGB Regressor": XGBRegressor(),
"XGBRF Regressor": XGBRFRegressor(),
"MLP Regressor": MLPRegressor(),
"LGBM Regressor": LGBMRegressor(),
"Gaussian Naive Bayes": GaussianNB()
}
classifiers = {
"Logistic Regression": LogisticRegression(),
"SGD Classifier": SGDClassifier(),
"Ridge Classifier": RidgeClassifier(),
"Random Forest Classifier": RandomForestClassifier(),
"AdaBoost Classifier": AdaBoostClassifier(),
"Gradient Boosting Classifier": GradientBoostingClassifier(),
"Hist Gradient Boosting Classifier": HistGradientBoostingClassifier(),
"K Neighbors Classifier": KNeighborsClassifier(),
"Decision Tree Classifier": DecisionTreeClassifier(),
"SVC": SVC(),
"XGB Classifier": XGBClassifier(),
"XGBRF Classifier": XGBRFClassifier(),
"MLP Classifier": MLPClassifier(),
"LGBM Classifier": LGBMClassifier(),
"Multinomial Naive Bayes": MultinomialNB(),
"Categorical Naive Bayes": CategoricalNB()
}
def perform_grid_search(model,model_name,X_train,X_test,y_train,y_test,eva):
if eva == "reg":
regressor = regressors[model_name]
param_grid_reg = param_grids_reg[model_name]
grid_search = GridSearchCV(estimator=regressor, param_grid=param_grid_reg, cv=5, scoring='neg_mean_squared_error')
grid_search.fit(X_train,y_train)
st.write(f"Best Parameters for {model_name}: {grid_search.best_params_}")
st.write(f"Best Score for {model_name}: {grid_search.best_score_}")
best_model = grid_search.best_estimator_
y_pred = best_model.predict(X_test)
evaluationer.evaluation("best hyperparams",X_train,X_test,y_train,y_test,model,root_mean_squared_error,eva)
elif eva == "class":
classifier = classifiers[model_name]
param_grid_class = param_grids_class[model_name]
grid_search = GridSearchCV(estimator=classifier, param_grid=param_grid_class, cv=5, scoring='accuracy')
grid_search.fit(X_train,y_train)
st.write(f"Best Parameters for {model_name}: {grid_search.best_params_}")
st.write(f"Best Score for {model_name}: {grid_search.best_score_}")
best_model = grid_search.best_estimator_
y_pred = best_model.predict(X_test)
evaluationer.evaluation("best hyperparams",X_train,X_test,y_train,y_test,model,root_mean_squared_error,eva)
|