text_html_output_1.png text_plain_output_1.png
import matplotlib.pyplot as plt

import pandas as pd

import seaborn as sns

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import shutil
from sklearn import preprocessing
from sklearn.preprocessing import StandardScaler
import statsmodels.api as sm
from scipy.stats import skew
from sklearn.preprocessing import OneHotEncoder
sns.set()
pd.set_option('display.max_columns', None)

pth_train = '../input/house-prices-advanced-regression-techniques/train.csv'
pth_test = '../input/house-prices-advanced-regression-techniques/test.csv'
raw_train = pd.read_csv(pth_train)
raw_test = pd.read_csv(pth_test)

categorical_nominal_cols = ['MSSubClass', 'MSZoning', 'Street', 'Alley', 'LotShape', 'LandContour', 'Utilities', 'LotConfig', 'LandSlope', 'Neighborhood', 'Condition1', 'Condition2', 'BldgType', 'HouseStyle', 'RoofStyle', 'RoofMatl', 'Exterior1st', 'Exterior2nd', 'MasVnrType', 'Foundation', 'Heating', 'Electrical', 'Functional', 'GarageType', 'MiscFeature', 'SaleType', 'SaleCondition']
categorical_ordinal_cols = ['OverallQual', 'OverallCond', 'YearBuilt', 'YearRemodAdd', 'ExterQual', 'ExterCond', 'BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2', 'HeatingQC', 'KitchenQual', 'FireplaceQu', 'GarageYrBlt', 'GarageFinish', 'GarageQual', 'GarageCond', 'PavedDrive', 'PoolQC', 'Fence']
categorical_bool_cols = ['CentralAir']

categorical_ordinal2encode = {}
categorical_ordinal2encode['ExterQual'] = {'Po': 0, 'Fa': 1, 'TA': 2, 'Gd': 3, 'Ex': 4}
categorical_ordinal2encode['ExterCond'] = categorical_ordinal2encode['ExterQual'].copy()
categorical_ordinal2encode['BsmtQual'] = {'NA': 0, 'Po': 1, 'Fa': 2, 'TA': 3, 'Gd': 4, 'Ex': 5}
categorical_ordinal2encode['BsmtCond'] = categorical_ordinal2encode['BsmtQual'].copy()
categorical_ordinal2encode['BsmtExposure'] = {'NA': 0, 'No': 1, 'Mn': 2, 'Av': 3, 'Gd': 4}
categorical_ordinal2encode['BsmtFinType1'] = {'NA': 0, 'Unf': 1, 'LwQ': 2, 'Rec': 3, 'BLQ': 4, 'ALQ': 5, 'GLQ': 6}
categorical_ordinal2encode['BsmtFinType2'] = categorical_ordinal2encode['BsmtFinType1'].copy()
categorical_ordinal2encode['HeatingQC'] = categorical_ordinal2encode['ExterQual'].copy()
categorical_ordinal2encode['KitchenQual'] = categorical_ordinal2encode['HeatingQC'].copy()
categorical_ordinal2encode['FireplaceQu'] = categorical_ordinal2encode['BsmtQual'].copy()
categorical_ordinal2encode['GarageFinish'] = {'NA': 0, 'Unf': 1, 'RFn': 2, 'Fin': 3}
categorical_ordinal2encode['GarageQual'] = categorical_ordinal2encode['BsmtQual'].copy()
categorical_ordinal2encode['GarageCond'] = categorical_ordinal2encode['BsmtQual'].copy()
categorical_ordinal2encode['PavedDrive'] = {'N': 0, 'P': 1, 'Y': 2}
categorical_ordinal2encode['PoolQC'] = {'NA': 0, 'Fa': 1, 'TA': 2, 'Gd': 3, 'Ex': 4}
categorical_ordinal2encode['Fence'] = {'NA': 0, 'MnWw': 1, 'GdWo': 2, 'MnPrv': 3, 'GdPrv': 4}

total_col = 4
total_row = len(categorical_ordinal_cols) // total_col
if len(categorical_ordinal_cols) % total_col > 0:
    total_row += 1
idx = 0
fig, axs = plt.subplots(total_row, total_col, figsize=(15, total_row * 4))
for i in range(total_row):
    for j in range(total_col):
        if idx < len(categorical_ordinal_cols):
            title = categorical_ordinal_cols[idx]
            if title in categorical_ordinal2encode:
                vc = raw_train[title].value_counts().reset_index()
                vc.rename(columns={'index': 'code'}, inplace=True)
                vc['index'] = vc['code'].copy()
                vc['index'] = vc['index'].map(categorical_ordinal2encode[title])
                vc.set_index('index', inplace=True)
                vc = vc.sort_index()
                sns.barplot(data=vc, x='code', y=title, color='orange', ax=axs[i][j])
            else:
                vc = raw_train[title].value_counts().sort_index()
                sns.barplot(x=vc.index, y=vc, color='orange', ax=axs[i][j])
            axs[i][j].set_ylabel('frequency')
            axs[i][j].set_xlabel('level')
            axs[i][j].set_title(title)
            idx += 1
plt.tight_layout()
plt.show()