text_plain_output_1.png
from collections import Counter

import matplotlib.pyplot as plt # visualization

import numpy as np  # linear algebra

import pandas as pd  # read and wrangle dataframes

import seaborn as sns # statistical visualizations and aesthetics

df = pd.read_csv('../input/glass/glass.csv')
features = df.columns[:-1].tolist()

df.dtypes

for feat in features:
    skew = df[feat].skew()

def outlier_hunt(df):
    """
    Takes a dataframe df of features and returns a list of the indices
    corresponding to the observations containing more than 2 outliers. 
    """
    outlier_indices = []
    for col in df.columns.tolist():
        Q1 = np.percentile(df[col], 25)
        Q3 = np.percentile(df[col], 75)
        IQR = Q3 - Q1
        outlier_step = 1.5 * IQR
        outlier_list_col = df[(df[col] < Q1 - outlier_step) | (df[col] > Q3 + outlier_step)].index
        outlier_indices.extend(outlier_list_col)
    outlier_indices = Counter(outlier_indices)
    multiple_outliers = list((k for k, v in outlier_indices.items() if v > 2))
    return multiple_outliers

corr = df[features].corr()

outlier_indices = outlier_hunt(df[features])
df = df.drop(outlier_indices).reset_index(drop=True)

for feat in features:
    skew = df[feat].skew()

features_boxcox = []
for feature in features:
    bc_transformed, _ = boxcox(df[feature] + 1)
    features_boxcox.append(bc_transformed)
features_boxcox = np.column_stack(features_boxcox)
df_bc = pd.DataFrame(data=features_boxcox, columns=features)
df_bc['Type'] = df['Type']

for feature in features:
    fig, ax = plt.subplots(1, 2, figsize=(7, 3.5))
    ax[0].hist(df[feature], color='blue', bins=30, alpha=0.3, label='Skew = %s' % str(round(df[feature].skew(), 3)))
    ax[0].set_title(str(feature))
    ax[0].legend(loc=0)
    ax[1].hist(df_bc[feature], color='red', bins=30, alpha=0.3, label='Skew = %s' % str(round(df_bc[feature].skew(), 3)))
    ax[1].set_title(str(feature) + ' after a Box-Cox transformation')
    ax[1].legend(loc=0)
    plt.show()