{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Combine predictors using stacking\n\n.. currentmodule:: sklearn\n\nStacking refers to a method to blend estimators. In this strategy, some\nestimators are individually fitted on some training data while a final\nestimator is trained using the stacked predictions of these base estimators.\n\nIn this example, we illustrate the use case in which different regressors are\nstacked together and a final linear penalized regressor is used to output the\nprediction. We compare the performance of each individual regressor with the\nstacking strategy. Stacking slightly improves the overall performance.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Authors: The scikit-learn developers\n# SPDX-License-Identifier: BSD-3-Clause" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Download the dataset\n\nWe will use the `Ames Housing`_ dataset which was first compiled by Dean De Cock\nand became better known after it was used in Kaggle challenge. It is a set\nof 1460 residential homes in Ames, Iowa, each described by 80 features. We\nwill use it to predict the final logarithmic price of the houses. In this\nexample we will use only 20 most interesting features chosen using\nGradientBoostingRegressor() and limit number of entries (here we won't go\ninto the details on how to select the most interesting features).\n\nThe Ames housing dataset is not shipped with scikit-learn and therefore we\nwill fetch it from `OpenML`_.\n\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n\nfrom sklearn.datasets import fetch_openml\nfrom sklearn.utils import shuffle\n\n\ndef load_ames_housing():\n df = fetch_openml(name=\"house_prices\", as_frame=True)\n X = df.data\n y = df.target\n\n features = [\n \"YrSold\",\n \"HeatingQC\",\n \"Street\",\n \"YearRemodAdd\",\n \"Heating\",\n \"MasVnrType\",\n \"BsmtUnfSF\",\n \"Foundation\",\n \"MasVnrArea\",\n \"MSSubClass\",\n \"ExterQual\",\n \"Condition2\",\n \"GarageCars\",\n \"GarageType\",\n \"OverallQual\",\n \"TotalBsmtSF\",\n \"BsmtFinSF1\",\n \"HouseStyle\",\n \"MiscFeature\",\n \"MoSold\",\n ]\n\n X = X.loc[:, features]\n X, y = shuffle(X, y, random_state=0)\n\n X = X.iloc[:600]\n y = y.iloc[:600]\n return X, np.log(y)\n\n\nX, y = load_ames_housing()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Make pipeline to preprocess the data\n\nBefore we can use Ames dataset we still need to do some preprocessing.\nFirst, we will select the categorical and numerical columns of the dataset to\nconstruct the first step of the pipeline.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from sklearn.compose import make_column_selector\n\ncat_selector = make_column_selector(dtype_include=object)\nnum_selector = make_column_selector(dtype_include=np.number)\ncat_selector(X)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "num_selector(X)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then, we will need to design preprocessing pipelines which depends on the\nending regressor. If the ending regressor is a linear model, one needs to\none-hot encode the categories. If the ending regressor is a tree-based model\nan ordinal encoder will be sufficient. Besides, numerical values need to be\nstandardized for a linear model while the raw numerical data can be treated\nas is by a tree-based model. However, both models need an imputer to\nhandle missing values.\n\nWe will first design the pipeline required for the tree-based models.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from sklearn.compose import make_column_transformer\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.pipeline import make_pipeline\nfrom sklearn.preprocessing import OrdinalEncoder\n\ncat_tree_processor = OrdinalEncoder(\n handle_unknown=\"use_encoded_value\",\n unknown_value=-1,\n encoded_missing_value=-2,\n)\nnum_tree_processor = SimpleImputer(strategy=\"mean\", add_indicator=True)\n\ntree_preprocessor = make_column_transformer(\n (num_tree_processor, num_selector), (cat_tree_processor, cat_selector)\n)\ntree_preprocessor" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then, we will now define the preprocessor used when the ending regressor\nis a linear model.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from sklearn.preprocessing import OneHotEncoder, StandardScaler\n\ncat_linear_processor = OneHotEncoder(handle_unknown=\"ignore\")\nnum_linear_processor = make_pipeline(\n StandardScaler(), SimpleImputer(strategy=\"mean\", add_indicator=True)\n)\n\nlinear_preprocessor = make_column_transformer(\n (num_linear_processor, num_selector), (cat_linear_processor, cat_selector)\n)\nlinear_preprocessor" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Stack of predictors on a single data set\n\nIt is sometimes tedious to find the model which will best perform on a given\ndataset. Stacking provide an alternative by combining the outputs of several\nlearners, without the need to choose a model specifically. The performance of\nstacking is usually close to the best model and sometimes it can outperform\nthe prediction performance of each individual model.\n\nHere, we combine 3 learners (linear and non-linear) and use a ridge regressor\nto combine their outputs together.\n\n
Although we will make new pipelines with the processors which we wrote in\n the previous section for the 3 learners, the final estimator\n :class:`~sklearn.linear_model.RidgeCV()` does not need preprocessing of\n the data as it will be fed with the already preprocessed output from the 3\n learners.