Tune Hyperparameters for Classification Machine Learning Algorithms

Last Updated on December 13, 2019Machine learning algorithms have hyperparameters that allow you to tailor the behavior of the algorithm to your specific dataset.

Hyperparameters are different from parameters, which are the internal coefficients or weights for a model found by the learning algorithm.

Unlike parameters, hyperparameters are specified by the practitioner when configuring the model.

Typically, it is challenging to know what values to use for the hyperparameters of a given algorithm on a given dataset, therefore it is common to use random or grid search strategies for different hyperparameter values.

The more hyperparameters of an algorithm that you need to tune, the slower the tuning process.

Therefore, it is desirable to select a minimum subset of model hyperparameters to search or tune.

Not all model hyperparameters are equally important.

Some hyperparameters have an outsized effect on the behavior, and in turn, the performance of a machine learning algorithm.

As a machine learning practitioner, you must know which hyperparameters to focus on to get a good result quickly.

In this tutorial, you will discover those hyperparameters that are most important for some of the top machine learning algorithms.

Let’s get started.

Hyperparameters for Classification Machine Learning AlgorithmsPhoto by shuttermonkey, some rights reserved.

We will take a closer look at the important hyperparameters of the top machine learning algorithms that you may use for classification.

We will look at the hyperparameters you need to focus on and suggested values to try when tuning the model on your dataset.

The suggestions are based both on advice from textbooks on the algorithms and practical advice suggested by practitioners, as well as a little of my own experience.

The seven classification algorithms we will look at are as follows:We will consider these algorithms in the context of their scikit-learn implementation (Python); nevertheless, you can use the same hyperparameter suggestions with other platforms, such as Weka and R.

A small grid searching example is also given for each algorithm that you can use as a starting point for your own classification predictive modeling project.

Note: if you have had success with different hyperparameter values or even different hyperparameters than those suggested in this tutorial, let me know in the comments below.

I’d love to hear about it.

Let’s dive in.

Logistic regression does not really have any critical hyperparameters to tune.

Sometimes, you can see useful differences in performance or convergence with different solvers (solver).

Regularization (penalty) can sometimes be helpful.

Note: not all solvers support all regularization terms.

The C parameter controls the penality strength, which can also be effective.

For the full list of hyperparameters, see:The example below demonstrates grid searching the key hyperparameters for LogisticRegression on a synthetic binary classification dataset.

Some combinations were omitted to cut back on the warnings/errors.

Running the example prints the best result as well as the results from all combinations evaluated.

Ridge regression is a penalized linear regression model for predicting a numerical value.

Nevertheless, it can be very effective when applied to classification.

Perhaps the most important parameter to tune is the regularization strength (alpha).

A good starting point might be values in the range [0.

1 to 1.

0]For the full list of hyperparameters, see:The example below demonstrates grid searching the key hyperparameters for RidgeClassifier on a synthetic binary classification dataset.

Running the example prints the best result as well as the results from all combinations evaluated.

The most important hyperparameter for KNN is the number of neighbors (n_neighbors).

Test values between at least 1 and 21, perhaps just the odd numbers.

It may also be interesting to test different distance metrics (metric) for choosing the composition of the neighborhood.

For a fuller list see:It may also be interesting to test the contribution of members of the neighborhood via different weightings (weights).

For the full list of hyperparameters, see:The example below demonstrates grid searching the key hyperparameters for KNeighborsClassifier on a synthetic binary classification dataset.

Running the example prints the best result as well as the results from all combinations evaluated.

The SVM algorithm, like gradient boosting, is very popular, very effective, and provides a large number of hyperparameters to tune.

Perhaps the first important parameter is the choice of kernel that will control the manner in which the input variables will be projected.

There are many to choose from, but linear, polynomial, and RBF are the most common, perhaps just linear and RBF in practice.

If the polynomial kernel works out, then it is a good idea to dive into the degree hyperparameter.

Another critical parameter is the penalty (C) that can take on a range of values and has a dramatic effect on the shape of the resulting regions for each class.

A log scale might be a good starting point.

For the full list of hyperparameters, see:The example below demonstrates grid searching the key hyperparameters for SVC on a synthetic binary classification dataset.

Running the example prints the best result as well as the results from all combinations evaluated.

The most important parameter for bagged decision trees is the number of trees (n_estimators).

Ideally, this should be increased until no further improvement is seen in the model.

Good values might be a log scale from 10 to 1,000.

For the full list of hyperparameters, see:The example below demonstrates grid searching the key hyperparameters for BaggingClassifier on a synthetic binary classification dataset.

Running the example prints the best result as well as the results from all combinations evaluated.

The most important parameter is the number of random features to sample at each split point (max_features).

You could try a range of integer values, such as 1 to 20, or 1 to half the number of input features.

Alternately, you could try a suite of different default value calculators.

Another important parameter for random forest is the number of trees (n_estimators).

Ideally, this should be increased until no further improvement is seen in the model.

Good values might be a log scale from 10 to 1,000.

For the full list of hyperparameters, see:The example below demonstrates grid searching the key hyperparameters for BaggingClassifier on a synthetic binary classification dataset.

Running the example prints the best result as well as the results from all combinations evaluated.

Also called Gradient Boosting Machine (GBM) or named for the specific implementation, such as XGBoost.

The gradient boosting algorithm has many parameters to tune.

There are some parameter pairings that are important to consider.

The first is the learning rate, also called shrinkage or eta (learning_rate) and the number of trees in the model (n_estimators).

Both could be considered on a log scale, although in different directions.

Another pairing is the number of rows or subset of the data to consider for each tree (subsample) and the depth of each tree (max_depth).

These could be grid searched at a 0.

1 and 1 interval respectively, although common values can be tested directly.

For more detailed advice on tuning the XGBoost implementation, see:For the full list of hyperparameters, see:The example below demonstrates grid searching the key hyperparameters for GradientBoostingClassifier on a synthetic binary classification dataset.

Running the example prints the best result as well as the results from all combinations evaluated.

This section provides more resources on the topic if you are looking to go deeper.

In this tutorial, you discovered the top hyperparameters and how to configure them for top machine learning algorithms.

Do you have other hyperparameter suggestions?.Let me know in the comments below.

Do you have any questions?.Ask your questions in the comments below and I will do my best to answer.

with just a few lines of scikit-learn codeLearn how in my new Ebook: Machine Learning Mastery With PythonCovers self-study tutorials and end-to-end projects like: Loading data, visualization, modeling, tuning, and much more.

Skip the Academics.

Just Results.

.

. More details

Leave a Reply