Hyperparameter tuning

16 Oct, 2020
A Machine Learning model is defined as a mathematical model with a number of parameters that need to be learned from the data. By training a model with existing data, we are able to fit the model parameters.However, there is another kind of parameters, known as Hyperparameters, that cannot be directly learned from the regular training process. They are usually fixed before the actual training process begins. These parameters express important properties of the model such as its complexity or how fast it should learn.
Some examples of model hyperparameters include:
The penalty in Logistic Regression Classifier i.e. L1 or L2 regularizationThe learning rate for training a neural network.The C and sigma hyperparameters for support vector machines.The k in k-nearest neighbors.
The penalty in Logistic Regression Classifier i.e. L1 or L2 regularization
The learning rate for training a neural network.
The C and sigma hyperparameters for support vector machines.
The k in k-nearest neighbors.
The aim of this article is to explore various strategies to tune hyperparameter for Machine learning model.
Models can have many hyperparameters and finding the best combination of parameters can be treated as a search problem. Two best strategies for Hyperparameter tuning are:
GridSearchCV
RandomizedSearchCV
GridSearchCVIn GridSearchCV approach, machine learning model is evaluated for a range of hyperparameter values. This approach is called GridSearchCV, because it searches for best set of hyperparameters from a grid of hyperparameters values.
For example, if we want to set two hyperparameters C and Alpha of Logistic Regression Classifier model, with different set of values. The gridsearch technique will construct many versions of the model with all possible combinations of hyerparameters, and will return the best one.
As in the image, for C = [0.1, 0.2, 0.3, 0.4, 0.5] and Alpha = [0.1, 0.2, 0.3, 0.4].For a combination C=0.3 and Alpha=0.2, performance score comes out to be 0.726(Highest), therefore it is selected.
Following code illustrates how to use GridSearchCV
# Necessary importsfrom sklearn.linear_model import LogisticRegressionfrom sklearn.model_selection import GridSearchCV  # Creating the hyperparameter gridc_space = np.logspace(-5, 8, 15)param_grid = {'C': c_space}  # Instantiating logistic regression classifierlogreg = LogisticRegression()  # Instantiating the GridSearchCV objectlogreg_cv = GridSearchCV(logreg, param_grid, cv = 5)  logreg_cv.fit(X, y)  # Print the tuned parameters and scoreprint("Tuned Logistic Regression Parameters: {}".format(logreg_cv.best_params_)) print("Best score is {}".format(logreg_cv.best_score_))
Output:
Tuned Logistic Regression Parameters: {‘C’: 3.7275937203149381}Best score is 0.7708333333333334
Drawback : GridSearchCV will go through all the intermediate combinations of hyperparameters which makes grid search computationally very expensive. RandomizedSearchCVRandomizedSearchCV solves the drawbacks of GridSearchCV, as it goes through only a fixed number of hyperparameter settings. It moves within the grid in random fashion to find the best set hyperparameters. This approach reduces unnecessary computation.Following code illustrates how to use RandomizedSearchCV
# Necessary importsfrom scipy.stats import randintfrom sklearn.tree import DecisionTreeClassifierfrom sklearn.model_selection import RandomizedSearchCV  # Creating the hyperparameter grid param_dist = {"max_depth": [3, None],              "max_features": randint(1, 9),              "min_samples_leaf": randint(1, 9),              "criterion": ["gini", "entropy"]}  # Instantiating Decision Tree classifiertree = DecisionTreeClassifier()  # Instantiating RandomizedSearchCV objecttree_cv = RandomizedSearchCV(tree, param_dist, cv = 5)  tree_cv.fit(X, y)  # Print the tuned parameters and scoreprint("Tuned Decision Tree Parameters: {}".format(tree_cv.best_params_))print("Best score is {}".format(tree_cv.best_score_))
Output:
Tuned Decision Tree Parameters: {‘min_samples_leaf’: 5, ‘max_depth’: 3, ‘max_features’: 5, ‘criterion’: ‘gini’}Best score is 0.7265625
Machine Learning
Python
Machine Learning
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.