{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "import numpy as np\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Parameter selection, Validation, and Testing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Most models have parameters that influence how complex a model they can learn. Remember using `KNeighborsRegressor`.\n", "If we change the number of neighbors we consider, we get a smoother and smoother prediction:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the above figure, we see fits for three different values of ``n_neighbors``.\n", "For ``n_neighbors=2``, the data is overfit, the model is too flexible and can adjust too much to the noise in the training data. For ``n_neighbors=20``, the model is not flexible enough, and can not model the variation in the data appropriately.\n", "\n", "In the middle, for ``n_neighbors = 5``, we have found a good mid-point. It fits\n", "the data fairly well, and does not suffer from the overfit or underfit\n", "problems seen in the figures on either side. What we would like is a\n", "way to quantitatively identify overfit and underfit, and optimize the\n", "hyperparameters (in this case, the polynomial degree d) in order to\n", "determine the best algorithm.\n", "\n", "We trade off remembering too much about the particularities and noise of the training data vs. not modeling enough of the variability. This is a trade-off that needs to be made in basically every machine learning application and is a central concept, called bias-variance-tradeoff or \"overfitting vs underfitting\"." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Hyperparameters, Over-fitting, and Under-fitting\n", "\n", "Unfortunately, there is no general rule how to find the sweet spot, and so machine learning practitioners have to find the best trade-off of model-complexity and generalization by trying several hyperparameter settings. Hyperparameters are the internal knobs or tuning parameters of a machine learning algorithm (in contrast to model parameters that the algorithm learns from the training data -- for example, the weight coefficients of a linear regression model); the number of *k* in K-nearest neighbors is such a hyperparameter.\n", "\n", "Most commonly this \"hyperparameter tuning\" is done using a brute force search, for example over multiple values of ``n_neighbors``:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from sklearn.model_selection import cross_val_score, KFold\n", "from sklearn.neighbors import KNeighborsRegressor\n", "# generate toy dataset:\n", "x = np.linspace(-3, 3, 100)\n", "rng = np.random.RandomState(42)\n", "y = np.sin(4 * x) + x + rng.normal(size=len(x))\n", "X = x[:, np.newaxis]\n", "\n", "cv = KFold(shuffle=True)\n", "\n", "# for each parameter setting do cross-validation:\n", "for n_neighbors in [1, 3, 5, 10, 20]:\n", " scores = cross_val_score(KNeighborsRegressor(n_neighbors=n_neighbors), X, y, cv=cv)\n", " print(\"n_neighbors: %d, average score: %f\" % (n_neighbors, np.mean(scores)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is a function in scikit-learn, called ``validation_plot`` to reproduce the cartoon figure above. It plots one parameter, such as the number of neighbors, against training and validation error (using cross-validation):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from sklearn.model_selection import validation_curve\n", "n_neighbors = [1, 3, 5, 10, 20, 50]\n", "train_scores, test_scores = validation_curve(KNeighborsRegressor(), X, y, param_name=\"n_neighbors\",\n", " param_range=n_neighbors, cv=cv)\n", "plt.plot(n_neighbors, train_scores.mean(axis=1), label=\"train accuracy\")\n", "plt.plot(n_neighbors, test_scores.mean(axis=1), label=\"test accuracy\")\n", "plt.ylabel('Accuracy')\n", "plt.xlabel('Number of neighbors')\n", "plt.xlim([50, 0])\n", "plt.legend(loc=\"best\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " Note that many neighbors mean a \"smooth\" or \"simple\" model, so the plot uses a reverted x axis.\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If multiple parameters are important, like the parameters ``C`` and ``gamma`` in an ``SVM`` (more about that later), all possible combinations are tried:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from sklearn.model_selection import cross_val_score, KFold\n", "from sklearn.svm import SVR\n", "\n", "# each parameter setting do cross-validation:\n", "for C in [0.001, 0.01, 0.1, 1, 10]:\n", " for gamma in [0.001, 0.01, 0.1, 1]:\n", " scores = cross_val_score(SVR(C=C, gamma=gamma), X, y, cv=cv)\n", " print(\"C: %f, gamma: %f, average score: %f\" % (C, gamma, np.mean(scores)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As this is such a very common pattern, there is a built-in class for this in scikit-learn, ``GridSearchCV``. ``GridSearchCV`` takes a dictionary that describes the parameters that should be tried and a model to train.\n", "\n", "The grid of parameters is defined as a dictionary, where the keys are the parameters and the values are the settings to be tested.\n", "\n", "To inspect training score on the different folds, the parameter ``return_train_score`` is set to ``True``." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from sklearn.model_selection import GridSearchCV\n", "param_grid = {'C': [0.001, 0.01, 0.1, 1, 10], 'gamma': [0.001, 0.01, 0.1, 1]}\n", "\n", "grid = GridSearchCV(SVR(), param_grid=param_grid, cv=cv, verbose=3, return_train_score=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One of the great things about GridSearchCV is that it is a *meta-estimator*. It takes an estimator like SVR above, and creates a new estimator, that behaves exactly the same - in this case, like a regressor.\n", "So we can call ``fit`` on it, to train it:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "grid.fit(X, y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What ``fit`` does is a bit more involved then what we did above. First, it runs the same loop with cross-validation, to find the best parameter combination.\n", "Once it has the best combination, it runs fit again on all data passed to fit (without cross-validation), to built a single new model using the best parameter setting." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then, as with all models, we can use ``predict`` or ``score``:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "grid.predict(X)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can inspect the best parameters found by ``GridSearchCV`` in the ``best_params_`` attribute, and the best score in the ``best_score_`` attribute:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(grid.best_score_)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(grid.best_params_)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But you can investigate the performance and much more for each set of parameter values by accessing the `cv_results_` attributes. The `cv_results_` attribute is a dictionary where each key is a string and each value is array. It can therefore be used to make a pandas DataFrame." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "type(grid.cv_results_)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(grid.cv_results_.keys())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "\n", "cv_results = pd.DataFrame(grid.cv_results_)\n", "cv_results.head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv_results_tiny = cv_results[['param_C', 'param_gamma', 'mean_test_score']]\n", "cv_results_tiny.sort_values(by='mean_test_score', ascending=False).head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is a problem with using this score for evaluation, however. You might be making what is called a multiple hypothesis testing error. If you try very many parameter settings, some of them will work better just by chance, and the score that you obtained might not reflect how your model would perform on new unseen data.\n", "Therefore, it is good to split off a separate test-set before performing grid-search. This pattern can be seen as a training-validation-test split, and is common in machine learning:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can do this very easily by splitting of some test data using ``train_test_split``, training ``GridSearchCV`` on the training set, and applying the ``score`` method to the test set:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from sklearn.model_selection import train_test_split\n", "X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)\n", "\n", "param_grid = {'C': [0.001, 0.01, 0.1, 1, 10], 'gamma': [0.001, 0.01, 0.1, 1]}\n", "cv = KFold(n_splits=10, shuffle=True)\n", "\n", "grid = GridSearchCV(SVR(), param_grid=param_grid, cv=cv)\n", "\n", "grid.fit(X_train, y_train)\n", "grid.score(X_test, y_test)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also look at the parameters that were selected:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "grid.best_params_" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Some practitioners go for an easier scheme, splitting the data simply into three parts, training, validation and testing. This is a possible alternative if your training set is very large, or it is infeasible to train many models using cross-validation because training a model takes very long.\n", "You can do this with scikit-learn for example by splitting of a test-set and then applying GridSearchCV with ShuffleSplit cross-validation with a single iteration:\n", "\n", "" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "from sklearn.model_selection import train_test_split, ShuffleSplit\n", "\n", "X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)\n", "\n", "param_grid = {'C': [0.001, 0.01, 0.1, 1, 10], 'gamma': [0.001, 0.01, 0.1, 1]}\n", "single_split_cv = ShuffleSplit(n_splits=1)\n", "\n", "grid = GridSearchCV(SVR(), param_grid=param_grid, cv=single_split_cv, verbose=3)\n", "\n", "grid.fit(X_train, y_train)\n", "grid.score(X_test, y_test)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is much faster, but might result in worse hyperparameters and therefore worse results." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "clf = GridSearchCV(SVR(), param_grid=param_grid)\n", "clf.fit(X_train, y_train)\n", "clf.score(X_test, y_test)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " EXERCISE:\n", " \n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "# %load solutions/14_grid_search.py" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.4" } }, "nbformat": 4, "nbformat_minor": 2 }