{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Homework 8.1: Lasso and Ridge regression on the Advertising data set" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this homework you will perform lasso and ridge regression on the advertising data set.\n", "The following cell imports the data set (adjust the path as necessary)." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "\n", "adv = pd.read_csv('./datasets/Advertising.csv', index_col=0)\n", "X = adv.values[:,0:3]\n", "y = adv.values[:,3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Task**: Perform both lasso and ridge regression on the data.\n", "Read the documentation for the functions `RidgeCV` and `LassoCV` from `sklearn.linear_model`.\n", "These functions select the regularization parameter by cross-validation, as we did by hand in Problem 8.\n", "Use 5-fold cross-validation and the following range for $\\alpha$: `np.logspace(-4,4,100)`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Task**: Compute the $R^2$-scores for both lasso and ridge regression. Which coefficients are selected by the lasso?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Task**: At the end of Exercise 3 and in the lecture, we noticed that there may exist interactions or higher order variations among the predictor variables.\n", "Use the function `PolynomialFeatures` from `sklearn.preprocessing` to add quadratic terms as well as interaction terms between the predictors. How does this improve your $R^2$-score?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "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.6" } }, "nbformat": 4, "nbformat_minor": 2 }