<jupyter_start><jupyter_text><jupyter_code>!apt-get -qq install tree
import os

import numpy as np
import pandas as pd

from getpass import getpass

from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_absolute_error

from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OneHotEncoder

from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
def access_kaggle():
    """
    Access Kaggle from Google Colab.
    If the /root/.kaggle does not exist then prompt for
    the username and for the Kaggle API key.
    Creates the kaggle.json access file in the /root/.kaggle/ folder. 
    """
    KAGGLE_ROOT = os.path.join('/root', '.kaggle')
    KAGGLE_PATH = os.path.join(KAGGLE_ROOT, 'kaggle.json')

    if '.kaggle' not in os.listdir(path='/root'):
        user = getpass(prompt='Kaggle username: ')
        key  = getpass(prompt='Kaggle API key: ')
        
        !mkdir $KAGGLE_ROOT
        !touch $KAGGLE_PATH
        !chmod 666 $KAGGLE_PATH
        with open(KAGGLE_PATH, mode='w') as f:
            f.write('{"username":"%s", "key":"%s"}' %(user, key))
            f.close()
        !chmod 600 $KAGGLE_PATH
        del user
        del key
        success_msg = "Kaggle is successfully set up. Good to go."
        print(f'{success_msg}')

access_kaggle()

!kaggle competitions download -c home-data-for-ml-course -p datasets/ml-course
!tree -sh ./
!cat -n datasets/ml-course/train.csv|head -2
df = pd.read_csv('datasets/ml-course/train.csv', sep=',', index_col=0)

df.columns = df.columns.map(lambda c: c.lower())
df.columns
df.info()
df.saleprice.isnull().sum()
y = df.saleprice

X = df.drop(['saleprice'], axis='columns')

train_x_full, valid_x_full, train_y, valid_y = train_test_split(X, y, test_size=0.2, random_state=42)
numerical_columns = [col for col in train_x_full.columns if
                        train_x_full[col].dtype in ['float64', 'int64']]

categorical_columns = [col for col in train_x_full.columns if
                        train_x_full[col].dtype == 'object' and
                        train_x_full[col].nunique() < 10]

selected_columns = categorical_columns + numerical_columns

train_x = train_x_full[selected_columns].copy()
valid_x = valid_x_full[selected_columns].copy()

train_x.shape, valid_x.shape
train_x.head()
imputers = [
    ('imputer', SimpleImputer()),
    ('imputer_median', SimpleImputer(strategy='median')),
    ('imputer_most_frequent', SimpleImputer(strategy='most_frequent')),
]

trees_in_the_forest = [5, 10, 20, 50]

models = [RandomForestRegressor(n_estimators=N, random_state=42) for N in trees_in_the_forest]

for imputer_name, imputer in imputers:
    numerical_transformer = imputer

    categorical_transformer = Pipeline(steps=[
            ('imputer', SimpleImputer(strategy='most_frequent')),
            ('one_hot_encoder', OneHotEncoder(sparse=False, handle_unknown='ignore')),
    ])

    preprocessor = ColumnTransformer(
        transformers=[
          # (name , transformer          , columns)
            ('num', numerical_transformer, numerical_columns),
            ('cat', categorical_transformer, categorical_columns),
        ]
    )
    print(f'{imputer_name} imputer:')
    print('-'*20)

    for model in models:
        pipe = Pipeline(
            steps=[
                ('preprocessor', preprocessor),
                ('model', model),
            ]
        )

        pipe.fit(train_x, train_y)
        preds = pipe.predict(valid_x)

        mae = mean_absolute_error(y_true=valid_y, y_pred=preds)
        print(f'{model}')
        print(f'---> MAE: {mae}')
        print()<jupyter_output>imputer imputer:
--------------------
RandomForestRegressor(bootstrap=True, ccp_alpha=0.0, criterion='mse',
                      max_depth=None, max_features='auto', max_leaf_nodes=None,
                      max_samples=None, min_impurity_decrease=0.0,
                      min_impurity_split=None, min_samples_leaf=1,
                      min_samples_split=2, min_weight_fraction_leaf=0.0,
                      n_estimators=5, n_jobs=None, oob_score=False,
                      random_state=42, verbose=0, warm_start=False)
---> MAE: 21061.74246575343

RandomForestRegressor(bootstrap=True, ccp_alpha=0.0, criterion='mse',
                      max_depth=None, max_features='auto', max_leaf_nodes=None,
                      max_samples=None, min_impurity_decrease=0.0,
                      min_impurity_split=None, min_samples_leaf=1,
                      min_samples_split=2, min_weight_fraction_leaf=0.0,
                      n_estimators=10, n_jobs=None, oob_score=False,
              [...]
{'fraction_non_alphanumeric': 0.1185577942735949, 'fraction_numerical': 0.014209968186638389, 'mean_word_length': 2.83726606997559, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '2207', 'license': 'MIT', 'n_tokens_mistral': 1635, 'n_tokens_neox': 1547, 'n_words': 309, 'path': 'Pipeline_multiple_imputers_and_models.ipynb', 'repo_name': 'chrismarkella/Kaggle-access-from-Google-Colab', 'size': '4715'}