text_plain_output_2.png text_plain_output_1.png
from catboost import CatBoostClassifier, Pool

from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier

from sklearn.linear_model import LogisticRegression

from sklearn.model_selection import GridSearchCV, RandomizedSearchCV

from sklearn.neighbors import KNeighborsClassifier

from sklearn.svm import SVC

from sklearn.tree import DecisionTreeClassifier

from xgboost import XGBClassifier

models = {'Logistic Regression': LogisticRegression(random_state=42), 'Random Forest': RandomForestClassifier(random_state=42), 'Gradient Boosting': GradientBoostingClassifier(random_state=42), 'Support Vector Machines': SVC(random_state=42), 'K-Nearest': KNeighborsClassifier(), 'XGB': XGBClassifier(random_state=42), 'Cat': CatBoostClassifier(random_state=42), 'Decision Tree': DecisionTreeClassifier(random_state=42)}
params = {'Logistic Regression': {'C': [0.001, 0.01, 0.1, 1, 10, 100, 1000], 'solver': ['newton-cg', 'lbfgs', 'liblinear']}, 'Random Forest': {'n_estimators': [10, 50, 100, 250, 500], 'max_depth': [5, 10, 20]}, 'Gradient Boosting': {'n_estimators': [10, 50, 100, 250, 500], 'learning_rate': [0.001, 0.005, 0.0001, 0.0005]}, 'Support Vector Machines': {'C': [0.001, 0.01, 0.1, 1, 10, 100, 1000], 'kernel': ['linear', 'rbf']}, 'K-Nearest': {'n_neighbors': [3, 5, 7, 11, 21], 'weights': ['uniform', 'distance'], 'metric': ['euclidean', 'manhattan']}, 'XGB': {'max_depth': [5, 10, 20], 'n_estimators': [10, 50, 100, 250, 500], 'learning_rate': [0.001, 0.005, 0.0001, 0.0005]}, 'Cat': {'iterations': [50, 500, 5000], 'max_depth': [5, 10, 20], 'loss_function': ['Logloss', 'CrossEntropy', 'MultiClass'], 'learning_rate': [0.001, 0.005, 0.0001, 0.0005], 'eval_metric': ['MultiClass']}, 'Decision Tree': {'max_features': ['auto', 'sqrt', 'log2'], 'ccp_alpha': [0.1, 0.01, 0.001], 'max_depth': [5, 10, 20], 'criterion': ['gini', 'entropy']}}
results = []
for name, model in models.items():
    clf = RandomizedSearchCV(model, params[name], cv=5, n_jobs=-1, scoring='accuracy')
    clf.fit(X_train_ex, y_train_ex)
    results.append({'model': name, 'best_score': clf.best_score_, 'best_params': clf.best_params_})
for result in results:
    print(f"{result['model']}: Best score = {result['best_score']:.4f}, Best params = {result['best_params']}")
"\nLogistic Regression: Best score = 1.0000, Best params = {'solver': 'newton-cg', 'C': 1}\nRandom Forest: Best score = 1.0000, Best params = {'n_estimators': 50, 'max_depth': 10}\nGradient Boosting: Best score = 1.0000, Best params = {'n_estimators': 10, 'learning_rate': 0.005}\nSupport Vector Machines: Best score = 1.0000, Best params = {'kernel': 'rbf', 'C': 100}\nK-Nearest: Best score = 1.0000, Best params = {'weights': 'uniform', 'n_neighbors': 5, 'metric': 'manhattan'}\nXGB: Best score = 1.0000, Best params = {'n_estimators': 250, 'max_depth': 10, 'learning_rate': 0.0001}\nCat: Best score = 1.0000, Best params = {'max_depth': 10, 'loss_function': 'MultiClass', 'learning_rate': 0.005, 'iterations': 50, 'eval_metric': 'MultiClass'}\nDecision Tree: Best score = 1.0000, Best params = {'max_features': 'sqrt', 'max_depth': 5, 'criterion': 'gini', 'ccp_alpha': 0.001}\n"