<jupyter_start><jupyter_text>🏁 Wrap-up quiz**This quiz requires some programming to be answered.**Open the dataset `house_prices.csv` with the following command:```pyimport pandas as pdames_housing = pd.read_csv("../datasets/house_prices.csv", na_values="?")ames_housing = ames_housing.drop(columns="Id")target_name = "SalePrice"data, target = ames_housing.drop(columns=target_name), ames_housing[target_name]target = (target > 200_000).astype(int)````ames_housing` is a pandas dataframe. The column "SalePrice" contains thetarget variable. Note that we instructed pandas to treat the character "?" as amarker for cells with missing values also known as "null" values.Furthermore, we ignore the column named "Id" because unique identifiers areusually useless in the context of predictive modeling.We did not encounter any regression problem yet. Therefore, we will convert theregression target into a classification target to predict whether or not anhouse is expensive. "Expensive" is defined as a sale price greater than$200,000.  Question 1```{admonition} QuestionUse the `data.info()` and ` data.head()` commands to examine the columns ofthe dataframe. The dataset contains:- a) numerical features- b) categorical features- c) missing data_Select several answers_```+++<jupyter_code>import pandas as pd
ames_housing = pd.read_csv("../datasets/house_prices.csv", na_values="?")
ames_housing = ames_housing.drop(columns="Id")

target_name = "SalePrice"
data, target = ames_housing.drop(columns=target_name), ames_housing[target_name]
target = (target > 200_000).astype(int)

ames_housing
target<jupyter_output><empty_output><jupyter_text>Question 2```{admonition} QuestionHow many features are available to predict whether or not an house isexpensive?- a) 79- b) 80- c) 81_Select a single answer_```+++<jupyter_code>data.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 1460 entries, 0 to 1459
Data columns (total 79 columns):
 #   Column         Non-Null Count  Dtype  
---  ------         --------------  -----  
 0   MSSubClass     1460 non-null   int64  
 1   MSZoning       1460 non-null   object 
 2   LotFrontage    1201 non-null   float64
 3   LotArea        1460 non-null   int64  
 4   Street         1460 non-null   object 
 5   Alley          91 non-null     object 
 6   LotShape       1460 non-null   object 
 7   LandContour    1460 non-null   object 
 8   Utilities      1460 non-null   object 
 9   LotConfig      1460 non-null   object 
 10  LandSlope      1460 non-null   object 
 11  Neighborhood   1460 non-null   object 
 12  Condition1     1460 non-null   object 
 13  Condition2     1460 non-null   object 
 14  BldgType       1460 non-null   object 
 15  HouseStyle     1460 non-null   object 
 16  OverallQual    1460 non-null   int64  
 17  OverallCond    1460 non-null   int64  
 18  YearBuil[...]<jupyter_text>Question 3```{admonition} QuestionHow many features are represented with numbers?- a) 0- b) 36- c) 42- d) 79_Select a single answer_Hint: you can use the method[`df.select_dtypes`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.select_dtypes.html)or the function[`sklearn.compose.make_column_selector`](https://scikit-learn.org/stable/modules/generated/sklearn.compose.make_column_selector.html)as shown in a previous notebook.```+++Refer to the [dataset description](https://www.openml.org/d/42165) regardingthe meaning of the dataset.<jupyter_code>data.select_dtypes('number')<jupyter_output><empty_output><jupyter_text>Question 4```{admonition} QuestionAmong the following columns, which columns express a quantitative numericalvalue (excluding ordinal categories)?- a) "LotFrontage"- b) "LotArea"- c) "OverallQual"- d) "OverallCond"- e) "YearBuilt"_Select several answers_```+++We consider the following numerical columns:```pynumerical_features = [  "LotFrontage", "LotArea", "MasVnrArea", "BsmtFinSF1", "BsmtFinSF2",  "BsmtUnfSF", "TotalBsmtSF", "1stFlrSF", "2ndFlrSF", "LowQualFinSF",  "GrLivArea", "BedroomAbvGr", "KitchenAbvGr", "TotRmsAbvGrd", "Fireplaces",  "GarageCars", "GarageArea", "WoodDeckSF", "OpenPorchSF", "EnclosedPorch",  "3SsnPorch", "ScreenPorch", "PoolArea", "MiscVal",]```<jupyter_code>numerical_features = [
  "LotFrontage", "LotArea", "MasVnrArea", "BsmtFinSF1", "BsmtFinSF2",
  "BsmtUnfSF", "TotalBsmtSF", "1stFlrSF", "2ndFlrSF", "LowQualFinSF",
  "GrLivArea", "BedroomAbvGr", "KitchenAbvGr", "TotRmsAbvGrd", "Fireplaces",
  "GarageCars", "GarageArea", "WoodDeckSF", "OpenPorchSF", "EnclosedPorch",
  "3SsnPorch", "ScreenPorch", "PoolArea", "MiscVal",
]<jupyter_output><empty_output><jupyter_text>Question 5```{admonition} QuestionWhat is the accuracy score obtained by 5-fold cross-validation of thispipeline?- a) ~0.5- b) ~0.7- c) ~0.9_Select a single answer_```+++ Now create a predictive model that uses these numerical columns as input data.Your predictive model should be a pipeline composed of a standard scaler, amean imputer (cf.[`sklearn.impute.SimpleImputer(strategy="mean")`](https://scikit-learn.org/stable/modules/generated/sklearn.impute.SimpleImputer.html))and a [`sklearn.linear_model.LogisticRegression`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html).<jupyter_code># to display nice model diagram
from sklearn import set_config
set_config(display='diagram')
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.impute import SimpleImputer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_validate


model = make_pipeline(StandardScaler(), SimpleImputer(strategy='mean'), LogisticRegression())
cv_result = cross_validate(model, data[numerical_features], target, cv=5)
cv_result<jupyter_output><empty_output><jupyter_text>Question 6Instead of solely using the numerical columns, let us build a pipeline thatcan process both the numerical and categorical features together as follows:- numerical features should be processed as previously;- the left-out columns should be treated as categorical variables using a  [`sklearn.preprocessing.OneHotEncoder`](https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OneHotEncoder.html);- prior to one-hot encoding, insert the  `sklearn.impute.SimpleImputer(strategy="most_frequent")` transformer to  replace missing values by the most-frequent value in each column.Be aware that you can pass a `Pipeline` as a transformer in a`ColumnTransformer`. We give a succinct example where we use a`ColumnTransformer` to select the numerical columns and process them (i.e.scale and impute). We additionally show that we can create a final modelcombining this preprocessor with a classifier.```pythonscaler_imputer_transformer = make_pipeline(StandardScaler(), SimpleImputer())preprocessor = ColumnTransformer(transformers=[    ("num-preprocessor", scaler_imputer_transformer, numerical_features)])model = make_pipeline(preprocessor, LogisticRegression())```Let us now define a substantial improvement or deterioration as an increase ordecrease of the mean generalization score of at least three times the standarddeviation of the cross-validated generalization score.```{admonition} QuestionWith this heterogeneous pipeline, the accuracy score:- a) worsens substantially- b) worsens slightly- c) improves slightly- d) improves substantially_Select a single answer_```<jupyter_code>from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.impute import SimpleImputer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_validate
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder


cat_features = [col for col in data.columns if col not in numerical_features]
from sklearn.compose import make_column_selector as selector

categorical_columns_selector = selector(dtype_include=object)
categorical_columns = categorical_columns_selector(data)

categorical_columns

scaler_imputer_transformer = make_pipeline(StandardScaler(), SimpleImputer(strategy='mean'))
cat_ohe_imputer_transformer = make_pipeline(SimpleImputer(strategy="most_frequent"),OneHotEncoder(handle_unknown='ignore'))
preprocessor = ColumnTransformer(transformers=[
    ("num-preprocessor", scaler_imputer_transformer, numerical_features),
    ("cat-preprocessor", cat_ohe_imputer_transformer, categorical_columns)
])
model = make_pipeline(preprocessor, LogisticRegression())
cv_result = cross_validate(model, data, target, cv=5)
cv_result
model<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09951174145547548, 'fraction_numerical': 0.021157870262729597, 'mean_word_length': 5.8440731901352425, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 27, 'https://': 6, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '121476', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 2800, 'n_tokens_neox': 2589, 'n_words': 738, 'path': 'jupyter-book/predictive_modeling_pipeline/module 1 - wrap-up quizz.ipynb', 'repo_name': 'castorfou/scikit-learn-mooc', 'size': '8602'}