text_plain_output_1.png
from datetime import datetime

import matplotlib.pyplot as plt

import numpy as np

import seaborn as sns

categ_feat = sorted(list(entire.dtypes[entire.dtypes == 'object'].index))
num_feat = sorted(list(entire.dtypes[(entire.dtypes == 'int64') | (entire.dtypes == 'float64')].index))
int_feat = sorted(list(entire.dtypes[entire.dtypes == 'int64'].index))
features = categ_feat + num_feat

def plot_features(x_list, y_name, df, threshold=0.5):
    """
        Generate a figure with an axe per plot, each axe plotting y_name
        feature function of numerical or categorical features in x_list.
        Print the feature names and corresponding regression coefficients
        ordered by coefficient values
        
        Arguments:
            - x_list (list): list of feature names for x-axis
            - y_name (str): feature name for y-axis
            - df (pandas.DataFrame): dataset containing the data to plot
            - threshold (float): threshold value for regression coefficient
              printing
    """
    colors = sns.color_palette(n_colors=len(x_list))
    list_tup = []  # List of (feature_name, reg_coef)
    if not len(x_list)%2:
        row_nb = int(len(x_list)/2)
    else:
        row_nb = int(len(x_list)/2) + 1

    size_y = row_nb * 5
    fig = plt.figure(figsize=(15, size_y*1.05))
    i = 1
    for x_name in x_list:
        ax = fig.add_subplot(row_nb, 2, i)
        if df[x_name].dtypes == 'object':  # Categorical feature
            sns.boxplot(x=x_name, y=y_name, data=df, ax=ax)
        else:  # Numerical feature
            sns.regplot(x=x_name, y=y_name, data=df, ax=ax,
                        color=colors[i-1])
            corr = np.corrcoef(df[x_name], df[y_name])[0, 1]
            list_tup.append((x_name, corr))
            ax.set_title('Regression coef: {:.2f}'.format(corr))
        i += 1
    
    list_tup = [tup for tup in list_tup if tup[1] >= threshold]
    list_tup = sorted(list_tup, key=lambda x: x[1], reverse=True)
    
    for tup in list_tup:
        print("Regression coefficient for {0}:\t{1:.2f}"
              .format(tup[0], tup[1]))

entire.Utilities.value_counts()

entire['DateSold'] = entire['YrSold'].astype(str) + '/' + entire['MoSold'].astype(str)
entire['DateSold'] = entire['DateSold'].apply(lambda x: datetime.strptime(x, '%Y/%m'))
title = 'Evolution of SalePrice with DateSold'
gpby = entire[entire.SalePrice.notnull()].groupby('DateSold')
gpby['SalePrice'].agg(['median', 'mean', 'std']).plot(grid=True, title=title, figsize=(8, 6))