Hyperparameter Tuning
Hyperparameter Tuning
target
0 0
1 0
2 0
3 0
4 0
Manual Search
# Choose the model (SVM in this case) with specific hyperparameters
model = SVC(C=100, kernel='rbf', gamma=10)
SVC(C=100, gamma=10)
y_predict = model.predict(X_test)
y_predict
array([1, 2, 2, 1, 1, 0, 1, 2, 1, 1, 2, 0, 0, 0, 0, 1, 2, 1, 1, 2, 0,
2,
0, 2, 2, 2, 2, 2, 0, 0, 2, 2, 1, 0, 0, 2, 1, 0, 0, 2, 2, 1, 1,
0,
0, 1, 1, 2, 1, 2, 1, 2, 1, 0, 2, 1, 0, 0, 2, 1, 2, 2, 0, 0, 1,
0,
1, 2, 0, 1, 2, 2, 2, 2, 1, 1, 2, 1, 0, 1, 2, 0, 0, 1, 1, 0, 2,
0,
0, 2])
accuracy = accuracy_score(y_test,y_predict)
accuracy
0.9
GridSearchCV
# Define the hyperparameter grid for GridSearchCV
param_grid_gridsearch = {
'C': [0.1, 1, 10, 100],
'kernel': ['linear', 'rbf', 'poly'],
'gamma': [0.01, 0.1, 1, 'auto']
}
# Perform GridSearchCV
grid_search = GridSearchCV(model_gridsearch,
param_grid=param_grid_gridsearch, scoring='accuracy', cv=5)
grid_search.fit(X_train, y_train)
GridSearchCV(cv=5, estimator=SVC(),
param_grid={'C': [0.1, 1, 10, 100],
'gamma': [0.01, 0.1, 1, 'auto'],
'kernel': ['linear', 'rbf', 'poly']},
scoring='accuracy')
0.9777777777777777
Hyperparameter Tuning for Multiple Models
Manual Search
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
array([1, 0, 2, 1, 1, 0, 1, 2, 1, 1, 2, 0, 0, 0, 0, 1, 2, 1, 1, 2, 0,
2,
0, 2, 2, 2, 2, 2, 0, 0, 0, 0, 1, 0, 0, 2, 1, 0, 0, 0, 2, 1, 1,
0,
0, 1, 2, 2, 1, 2, 1, 2, 1, 0, 2, 1, 0, 0, 0, 1, 2, 0, 0, 0, 1,
0,
1, 2, 0, 1, 2, 0, 1, 2, 1, 1, 2, 1, 0, 1, 2, 0, 0, 1, 2, 0, 2,
0,
0, 1])
0.9777777777777777
RandomForestClassifier(max_depth=10, n_estimators=50)
# Predict the test set
y2_predict = model2.predict(X_test)
y2_predict
array([1, 0, 2, 1, 1, 0, 1, 2, 1, 1, 2, 0, 0, 0, 0, 1, 2, 1, 1, 2, 0,
2,
0, 2, 2, 2, 2, 2, 0, 0, 0, 0, 1, 0, 0, 2, 1, 0, 0, 0, 2, 1, 1,
0,
0, 1, 1, 2, 1, 2, 1, 2, 1, 0, 2, 1, 0, 0, 0, 1, 2, 0, 0, 0, 1,
0,
1, 2, 0, 1, 2, 0, 2, 2, 1, 1, 2, 1, 0, 1, 2, 0, 0, 1, 2, 0, 2,
0,
0, 2])
0.9666666666666667
array([2, 0, 2, 2, 2, 0, 2, 2, 2, 2, 2, 0, 0, 0, 0, 2, 2, 2, 2, 2, 0,
2,
0, 2, 2, 2, 2, 2, 0, 0, 0, 0, 2, 0, 0, 2, 2, 0, 0, 0, 2, 2, 2,
0,
0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 0, 0, 0, 2, 2, 0, 0, 0, 2,
0,
2, 2, 0, 2, 2, 0, 2, 2, 2, 2, 2, 2, 0, 2, 2, 0, 0, 2, 2, 0, 2,
0,
0, 2])
0.6777777777777778
Using GridSearchCV
# Define models
models = {
'SVM': SVC(),
'Random Forest': RandomForestClassifier(),
'Logistic Regression': LogisticRegression()
}
import warnings
# Ignore all warnings
warnings.simplefilter("ignore")
Thank You
Email: [email protected]
ORCID: 0009-0000-2684-7611
YouTube: https://bit.ly/GmathStats