{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Pipelines\n",
"\n",
"Pipelines are just series of steps you perform on data in `sklearn`. (The `sklearn` [guide to them is here.](https://scikit-learn.org/stable/modules/compose.html))\n",
"\n",
"A \"typical\" pipeline in ML projects \n",
"1. [Preprocesses the data](https://scikit-learn.org/stable/modules/preprocessing.html) to clean and tranform variables \n",
"1. Possibly selects a subset of variables from among the features [to avoid overfitting](03a_ML_obj_and_tradeoff) (see also [this](https://scikit-learn.org/stable/modules/feature_selection.html))\n",
"1. Runs [a model](03e_whichModel) on those cleaned variables \n",
"\n",
"```{tip}\n",
"You can set up pipelines with `make_pipeline`.\n",
"```\n",
"\n",
"## Intro to pipes\n",
"\n",
"```{margin}\n",
" \n",
"```\n",
"\n",
"For example, here is a simple pipeline:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.pipeline import make_pipeline \n",
"from sklearn.impute import SimpleImputer\n",
"from sklearn.linear_model import Ridge\n",
"\n",
"ridge_pipe = make_pipeline(SimpleImputer(),Ridge(1.0))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You put a series of steps inside `make_pipeline`, separated by commas.\n",
"\n",
"The pipeline object (printed out below) is a list of steps, where each step has a name (e.g. \"simpleimputer\" ) and a task associated with that name (e.g. \"SimpleImputer()\")."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Pipeline(steps=[('simpleimputer', SimpleImputer()), ('ridge', Ridge())])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ridge_pipe"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```{tip}\n",
"You can `.fit()` and `.predict()` pipelines like any model, and they can be used in `cross_validate` too!\n",
"```\n",
"\n",
"Using it is the same as using any estimator! After I load the data we've been using [from the last two pages](04d_crossval) below (hidden), we can fit and predict like on the [\"one model intro\" page](04c_onemodel):"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"tags": [
"hide-input"
]
},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"from sklearn.linear_model import Ridge\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.model_selection import KFold, cross_validate\n",
"\n",
"url = 'https://github.com/LeDataSciFi/ledatascifi-2022/blob/main/data/Fannie_Mae_Plus_Data.gzip?raw=true'\n",
"fannie_mae = pd.read_csv(url,compression='gzip').dropna()\n",
"y = fannie_mae.Original_Interest_Rate\n",
"fannie_mae = (fannie_mae\n",
" .assign(l_credscore = np.log(fannie_mae['Borrower_Credit_Score_at_Origination']),\n",
" l_LTV = np.log(fannie_mae['Original_LTV_(OLTV)']),\n",
" )\n",
" .iloc[:,-11:] \n",
" )\n",
"\n",
"rng = np.random.RandomState(0) # this helps us control the randomness so we can reproduce results exactly\n",
"X_train, X_test, y_train, y_test = train_test_split(fannie_mae, y, random_state=rng)\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([5.95256433, 4.20060942, 3.9205946 , ..., 4.06401663, 5.30024985,\n",
" 7.32600213])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ridge_pipe.fit(X_train,y_train)\n",
"ridge_pipe.predict(X_test)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Those are the same numbers as before - good! \n",
"\n",
"We can use this pipeline in our cross validation in place of the estimator:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.9030537085469961"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cross_validate(ridge_pipe,X_train,y_train,\n",
" cv=KFold(5), scoring='r2')['test_score'].mean()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Preprocessing in pipes\n",
"\n",
"```{warning}\n",
"(Virtually) All preprocessing should be done in the pipeline! \n",
"```\n",
"\n",
"[This is the link you should start with to see how you might clean and preprocess data.](https://scikit-learn.org/stable/modules/preprocessing.html) Key preprocessing steps include\n",
"- Filling in missing values (imputation) or dropping those observations\n",
"- Standardization\n",
"- Encoding categorical data\n",
"\n",
"With real-world data, you'll have many data types. So the preprocessing steps you apply to one column won't necessarily be what the next column needs. \n",
"\n",
"I use [ColumnTransformer](https://scikit-learn.org/stable/auto_examples/compose/plot_column_transformer_mixed_types.html#sphx-glr-auto-examples-compose-plot-column-transformer-mixed-types-py) to assemble my preprocessing portion of my full pipeline, and it allows me to process different variables differently.\n",
"\n",
"---\n",
"\n",
"**The generic steps to preprocess in a pipeline:**\n",
"1. Set up a pipeline for numerical data\n",
"1. Set up a pipeline for categorical variables\n",
"1. Set up the ColumnTransformer:\n",
" - `ColumnTransformer()` is a function, so it needs the parentheses \"()\"\n",
" - The first argument inside it is a list (so now it is `ColumnTransformer([])`)\n",
" - Each element in that list is a tuple that has three parts: \n",
" - name of the step (you decide the name), \n",
" - estimator/pipeline to use on that step, \n",
" - and which variables to use it on\n",
" - **Put the pipeline for each variable type as its own tuple inside `ColumnTransformer([])`**\n",
"1. Use the `ColumnTransformer` set as the first step inside your glorious estimation pipeline. \n",
"\n",
"---\n",
"\n",
"So, let me put this together: \n",
"\n",
"```{tip}\n",
"This is good pseudo!\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.preprocessing import OneHotEncoder \n",
"from sklearn.compose import ColumnTransformer, make_column_selector\n",
"\n",
"#############\n",
"# Step 1: how to deal with numerical vars\n",
"# pro-tip: you might set up several numeric pipelines, because\n",
"# some variables might need very different treatment!\n",
"#############\n",
"\n",
"numer_pipe = make_pipeline(SimpleImputer()) \n",
"# this deals with missing values (somehow?)\n",
"# you might also standardize the vars in this numer_pipe\n",
"\n",
"#############\n",
"# Step 2: how to deal with categorical vars\n",
"#############\n",
"\n",
"cat_pipe = make_pipeline(OneHotEncoder(drop='first'))\n",
"\n",
"# notes on this cat pipe:\n",
"# OneHotEncoder is just one way to deal with categorical vars\n",
"# drop='first' is necessary if the model is regression\n",
"\n",
"#############\n",
"# Step 3: combine the subparts\n",
"#############\n",
"\n",
"preproc_pipe = ColumnTransformer( \n",
" [ # arg 1 of ColumnTransformer is a list, so this starts the list\n",
" # a tuple for the numerical vars: name, pipe, which vars to apply to\n",
" (\"num_impute\", numer_pipe, ['l_credscore','TCMR']),\n",
" # a tuple for the categorical vars: name, pipe, which vars to apply to\n",
" (\"cat_trans\", cat_pipe, ['Property_state'])\n",
" ]\n",
" , remainder = 'drop' # you either drop or passthrough any vars not modified above\n",
")\n",
"\n",
"#############\n",
"# Step 4: put the preprocessing into an estimation pipeline\n",
"#############\n",
"\n",
"new_ridge_pipe = make_pipeline(preproc_pipe,Ridge(1.0))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The data loaded above has no categorical variables, so I'm going to reload the data and keep new variables to illustrate what we can do: \n",
"- `'TCMR','l_credscore'` are numerical\n",
"- `'Property_state'` is categorical\n",
"- `'l_LTV'` is in the data, but should be dropped (because of `remainder='drop'`)\n",
"\n",
"So here is the raw data:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"tags": [
"hide-input"
]
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" TCMR | \n",
" Property_state | \n",
" l_credscore | \n",
" l_LTV | \n",
"
\n",
" \n",
" \n",
" \n",
" 4326 | \n",
" 4.651500 | \n",
" IL | \n",
" 6.670766 | \n",
" 4.499810 | \n",
"
\n",
" \n",
" 15833 | \n",
" 4.084211 | \n",
" TN | \n",
" 6.652863 | \n",
" 4.442651 | \n",
"
\n",
" \n",
" 66753 | \n",
" 3.675000 | \n",
" MO | \n",
" 6.635947 | \n",
" 4.442651 | \n",
"
\n",
" \n",
" 23440 | \n",
" 3.998182 | \n",
" MO | \n",
" 6.548219 | \n",
" 4.553877 | \n",
"
\n",
" \n",
" 4155 | \n",
" 4.651500 | \n",
" CO | \n",
" 6.602588 | \n",
" 4.442651 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" TCMR Property_state l_credscore l_LTV\n",
"4326 4.651500 IL 6.670766 4.499810\n",
"15833 4.084211 TN 6.652863 4.442651\n",
"66753 3.675000 MO 6.635947 4.442651\n",
"23440 3.998182 MO 6.548219 4.553877\n",
"4155 4.651500 CO 6.602588 4.442651"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" count | \n",
" mean | \n",
" std | \n",
" min | \n",
" 25% | \n",
" 50% | \n",
" 75% | \n",
" max | \n",
"
\n",
" \n",
" \n",
" \n",
" TCMR | \n",
" 7938.0 | \n",
" 3.36 | \n",
" 1.29 | \n",
" 1.50 | \n",
" 2.21 | \n",
" 3.00 | \n",
" 4.45 | \n",
" 6.66 | \n",
"
\n",
" \n",
" l_credscore | \n",
" 7938.0 | \n",
" 6.60 | \n",
" 0.07 | \n",
" 6.27 | \n",
" 6.55 | \n",
" 6.61 | \n",
" 6.66 | \n",
" 6.72 | \n",
"
\n",
" \n",
" l_LTV | \n",
" 7938.0 | \n",
" 4.51 | \n",
" 0.05 | \n",
" 4.25 | \n",
" 4.49 | \n",
" 4.50 | \n",
" 4.55 | \n",
" 4.57 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" count mean std min 25% 50% 75% max\n",
"TCMR 7938.0 3.36 1.29 1.50 2.21 3.00 4.45 6.66\n",
"l_credscore 7938.0 6.60 0.07 6.27 6.55 6.61 6.66 6.72\n",
"l_LTV 7938.0 4.51 0.05 4.25 4.49 4.50 4.55 4.57"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"url = 'https://github.com/LeDataSciFi/ledatascifi-2022/blob/main/data/Fannie_Mae_Plus_Data.gzip?raw=true'\n",
"fannie_mae = pd.read_csv(url,compression='gzip').dropna()\n",
"y = fannie_mae.Original_Interest_Rate\n",
"fannie_mae = (fannie_mae\n",
" .assign(l_credscore = np.log(fannie_mae['Borrower_Credit_Score_at_Origination']),\n",
" l_LTV = np.log(fannie_mae['Original_LTV_(OLTV)']),\n",
" )\n",
" [['TCMR', 'Property_state', 'l_credscore', 'l_LTV']]\n",
" )\n",
"\n",
"rng = np.random.RandomState(0) # this helps us control the randomness so we can reproduce results exactly\n",
"X_train, X_test, y_train, y_test = train_test_split(fannie_mae, y, random_state=rng)\n",
"\n",
"display(X_train.head())\n",
"display(X_train.describe().T.round(2))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We could `.fit()` and `.transform()` using the `preproc_pipe` from step 3 (or just `.fit_transform()` to do it in one command) to see how it transforms the data. But the output is tough to use:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<7938x53 sparse matrix of type ''\n",
"\twith 23792 stored elements in Compressed Sparse Row format>"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"preproc_pipe.fit_transform(X_train)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So I added a convenience function (`df_after_transform`) to the [community codebook](https://github.com/LeDataSciFi/ledatascifi-2022/tree/main/community_codebook) to show the dataframe after the ColumnTransformer step.\n",
"\n",
"Notice\n",
"- The `l_LTV` column is gone!\n",
"- The property state variable is now 50+ variables (one dummy for each state, and a few territories)\n",
"- The numerical variables aren't changed (there are no missing variables, so the imputation does nothing)\n",
"\n",
"This is the transformed data:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" l_credscore | \n",
" TCMR | \n",
" Property_state_AL | \n",
" Property_state_AR | \n",
" Property_state_AZ | \n",
" Property_state_CA | \n",
" Property_state_CO | \n",
" Property_state_CT | \n",
" Property_state_DC | \n",
" Property_state_DE | \n",
" ... | \n",
" Property_state_SD | \n",
" Property_state_TN | \n",
" Property_state_TX | \n",
" Property_state_UT | \n",
" Property_state_VA | \n",
" Property_state_VT | \n",
" Property_state_WA | \n",
" Property_state_WI | \n",
" Property_state_WV | \n",
" Property_state_WY | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 6.670766 | \n",
" 4.651500 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" ... | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
"
\n",
" \n",
" 1 | \n",
" 6.652863 | \n",
" 4.084211 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" ... | \n",
" 0.0 | \n",
" 1.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
"
\n",
" \n",
" 2 | \n",
" 6.635947 | \n",
" 3.675000 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" ... | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
"
\n",
" \n",
" 3 | \n",
" 6.548219 | \n",
" 3.998182 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" ... | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
"
\n",
" \n",
" 4 | \n",
" 6.602588 | \n",
" 4.651500 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 1.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" ... | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
"
\n",
" \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
"
\n",
" \n",
" 7933 | \n",
" 6.650279 | \n",
" 1.556522 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" ... | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
"
\n",
" \n",
" 7934 | \n",
" 6.647688 | \n",
" 2.416364 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" ... | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
"
\n",
" \n",
" 7935 | \n",
" 6.507278 | \n",
" 6.054000 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" ... | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 1.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
"
\n",
" \n",
" 7936 | \n",
" 6.618739 | \n",
" 2.303636 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" ... | \n",
" 0.0 | \n",
" 0.0 | \n",
" 1.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
"
\n",
" \n",
" 7937 | \n",
" 6.639876 | \n",
" 4.971304 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" ... | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.0 | \n",
"
\n",
" \n",
"
\n",
"
7938 rows × 53 columns
\n",
"
"
],
"text/plain": [
" l_credscore TCMR Property_state_AL Property_state_AR \\\n",
"0 6.670766 4.651500 0.0 0.0 \n",
"1 6.652863 4.084211 0.0 0.0 \n",
"2 6.635947 3.675000 0.0 0.0 \n",
"3 6.548219 3.998182 0.0 0.0 \n",
"4 6.602588 4.651500 0.0 0.0 \n",
"... ... ... ... ... \n",
"7933 6.650279 1.556522 0.0 0.0 \n",
"7934 6.647688 2.416364 0.0 0.0 \n",
"7935 6.507278 6.054000 0.0 0.0 \n",
"7936 6.618739 2.303636 0.0 0.0 \n",
"7937 6.639876 4.971304 0.0 0.0 \n",
"\n",
" Property_state_AZ Property_state_CA Property_state_CO \\\n",
"0 0.0 0.0 0.0 \n",
"1 0.0 0.0 0.0 \n",
"2 0.0 0.0 0.0 \n",
"3 0.0 0.0 0.0 \n",
"4 0.0 0.0 1.0 \n",
"... ... ... ... \n",
"7933 0.0 0.0 0.0 \n",
"7934 0.0 0.0 0.0 \n",
"7935 0.0 0.0 0.0 \n",
"7936 0.0 0.0 0.0 \n",
"7937 0.0 0.0 0.0 \n",
"\n",
" Property_state_CT Property_state_DC Property_state_DE ... \\\n",
"0 0.0 0.0 0.0 ... \n",
"1 0.0 0.0 0.0 ... \n",
"2 0.0 0.0 0.0 ... \n",
"3 0.0 0.0 0.0 ... \n",
"4 0.0 0.0 0.0 ... \n",
"... ... ... ... ... \n",
"7933 0.0 0.0 0.0 ... \n",
"7934 0.0 0.0 0.0 ... \n",
"7935 0.0 0.0 0.0 ... \n",
"7936 0.0 0.0 0.0 ... \n",
"7937 0.0 0.0 0.0 ... \n",
"\n",
" Property_state_SD Property_state_TN Property_state_TX \\\n",
"0 0.0 0.0 0.0 \n",
"1 0.0 1.0 0.0 \n",
"2 0.0 0.0 0.0 \n",
"3 0.0 0.0 0.0 \n",
"4 0.0 0.0 0.0 \n",
"... ... ... ... \n",
"7933 0.0 0.0 0.0 \n",
"7934 0.0 0.0 0.0 \n",
"7935 0.0 0.0 0.0 \n",
"7936 0.0 0.0 1.0 \n",
"7937 0.0 0.0 0.0 \n",
"\n",
" Property_state_UT Property_state_VA Property_state_VT \\\n",
"0 0.0 0.0 0.0 \n",
"1 0.0 0.0 0.0 \n",
"2 0.0 0.0 0.0 \n",
"3 0.0 0.0 0.0 \n",
"4 0.0 0.0 0.0 \n",
"... ... ... ... \n",
"7933 0.0 0.0 0.0 \n",
"7934 0.0 0.0 0.0 \n",
"7935 0.0 0.0 0.0 \n",
"7936 0.0 0.0 0.0 \n",
"7937 0.0 0.0 0.0 \n",
"\n",
" Property_state_WA Property_state_WI Property_state_WV \\\n",
"0 0.0 0.0 0.0 \n",
"1 0.0 0.0 0.0 \n",
"2 0.0 0.0 0.0 \n",
"3 0.0 0.0 0.0 \n",
"4 0.0 0.0 0.0 \n",
"... ... ... ... \n",
"7933 0.0 0.0 0.0 \n",
"7934 0.0 0.0 0.0 \n",
"7935 0.0 1.0 0.0 \n",
"7936 0.0 0.0 0.0 \n",
"7937 0.0 0.0 0.0 \n",
"\n",
" Property_state_WY \n",
"0 0.0 \n",
"1 0.0 \n",
"2 0.0 \n",
"3 0.0 \n",
"4 0.0 \n",
"... ... \n",
"7933 0.0 \n",
"7934 0.0 \n",
"7935 0.0 \n",
"7936 0.0 \n",
"7937 0.0 \n",
"\n",
"[7938 rows x 53 columns]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from df_after_transform import df_after_transform\n",
"\n",
"df_after_transform(preproc_pipe,X_train)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" count | \n",
" mean | \n",
" std | \n",
" min | \n",
" 25% | \n",
" 50% | \n",
" 75% | \n",
" max | \n",
"
\n",
" \n",
" \n",
" \n",
" l_credscore | \n",
" 7938.0 | \n",
" 6.60 | \n",
" 0.07 | \n",
" 6.27 | \n",
" 6.55 | \n",
" 6.61 | \n",
" 6.66 | \n",
" 6.72 | \n",
"
\n",
" \n",
" TCMR | \n",
" 7938.0 | \n",
" 3.36 | \n",
" 1.29 | \n",
" 1.50 | \n",
" 2.21 | \n",
" 3.00 | \n",
" 4.45 | \n",
" 6.66 | \n",
"
\n",
" \n",
" Property_state_AL | \n",
" 7938.0 | \n",
" 0.02 | \n",
" 0.12 | \n",
" 0.00 | \n",
" 0.00 | \n",
" 0.00 | \n",
" 0.00 | \n",
" 1.00 | \n",
"
\n",
" \n",
" Property_state_AR | \n",
" 7938.0 | \n",
" 0.01 | \n",
" 0.10 | \n",
" 0.00 | \n",
" 0.00 | \n",
" 0.00 | \n",
" 0.00 | \n",
" 1.00 | \n",
"
\n",
" \n",
" Property_state_AZ | \n",
" 7938.0 | \n",
" 0.03 | \n",
" 0.17 | \n",
" 0.00 | \n",
" 0.00 | \n",
" 0.00 | \n",
" 0.00 | \n",
" 1.00 | \n",
"
\n",
" \n",
" Property_state_CA | \n",
" 7938.0 | \n",
" 0.07 | \n",
" 0.25 | \n",
" 0.00 | \n",
" 0.00 | \n",
" 0.00 | \n",
" 0.00 | \n",
" 1.00 | \n",
"
\n",
" \n",
" Property_state_CO | \n",
" 7938.0 | \n",
" 0.03 | \n",
" 0.16 | \n",
" 0.00 | \n",
" 0.00 | \n",
" 0.00 | \n",
" 0.00 | \n",
" 1.00 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" count mean std min 25% 50% 75% max\n",
"l_credscore 7938.0 6.60 0.07 6.27 6.55 6.61 6.66 6.72\n",
"TCMR 7938.0 3.36 1.29 1.50 2.21 3.00 4.45 6.66\n",
"Property_state_AL 7938.0 0.02 0.12 0.00 0.00 0.00 0.00 1.00\n",
"Property_state_AR 7938.0 0.01 0.10 0.00 0.00 0.00 0.00 1.00\n",
"Property_state_AZ 7938.0 0.03 0.17 0.00 0.00 0.00 0.00 1.00\n",
"Property_state_CA 7938.0 0.07 0.25 0.00 0.00 0.00 0.00 1.00\n",
"Property_state_CO 7938.0 0.03 0.16 0.00 0.00 0.00 0.00 1.00"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"display(df_after_transform(preproc_pipe,X_train)\n",
" .describe().T.round(2)\n",
" .iloc[:7,:]) # only show a few variables for space..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Working with pipes\n",
"\n",
"- Using pipes is the same as any model: `.fit()` and `.predict()`, put into CVs\n",
"- When modelling, you should spend time interrogating model predictions, plotting and printing. Does the model struggle predicting certain observations? Does it excel at some?\n",
"- You'll want to tweak parts of your pipeline. The next pages cover how we can do that."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.8.12"
}
},
"nbformat": 4,
"nbformat_minor": 4
}