<jupyter_start><jupyter_text>Dimensionality Reduction and PCA<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
sns.set_style('whitegrid')
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Data preaparation<jupyter_code>default = pd.read_csv('../data/credit_card_default.csv', index_col="ID")
default.rename(columns=lambda x: x.lower(), inplace=True)
default.rename(columns={'pay_0':'pay_1','default payment next month':'default'}, inplace=True)
# Base values: female, other_education, not_married
default['grad_school'] = (default['education'] == 1).astype('int')
default['university'] = (default['education'] == 2).astype('int')
default['high_school'] = (default['education'] == 3).astype('int')
default.drop('education', axis=1, inplace=True)

default['male'] = (default['sex']==1).astype('int')
default.drop('sex', axis=1, inplace=True)

default['married'] = (default['marriage'] == 1).astype('int')
default.drop('marriage', axis=1, inplace=True)

# For pay_n features if >0 then it means the customer was delayed on that month
pay_features = ['pay_' + str(i) for i in range(1,7)]
for p in pay_features:
    default[p] = (default[p] > 0).astype(int)<jupyter_output><empty_output><jupyter_text>Take a look at the bill amount features<jupyter_code>bill_amt_features = ['bill_amt'+str(i) for i in range(1,7)]
sns.pairplot(default, vars=bill_amt_features, hue='default',
             plot_kws={'s':2});
default[bill_amt_features].corr()<jupyter_output><empty_output><jupyter_text>Performing PCA on bill amount features<jupyter_code>from sklearn.decomposition import PCA
bill_amt_pca = PCA()
bill_amt_pca.fit(default[bill_amt_features])
explainded_variance= pd.Series(100*bill_amt_pca.explained_variance_ratio_, 
                               index=['bill_amt_comp_'+str(i) for i in range(1,7)])
fig, ax = plt.subplots(figsize=(8,5))
explainded_variance.plot(kind='barh', ax=ax);
ax.set_title('Explained Variance, PCA of Bill Amount Features');
fig, ax = plt.subplots(figsize=(8,5))
explainded_variance.cumsum().plot()
ax.set_title('Cumulative Explained Variance, PCA of Bill Amount Features');<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1541970802919708, 'fraction_numerical': 0.011861313868613138, 'mean_word_length': 7.6, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '246719', 'license': 'MIT', 'n_tokens_mistral': 804, 'n_tokens_neox': 775, 'n_words': 150, 'path': 'Section3/Dimensionality Reduction and PCA.ipynb', 'repo_name': 'PacktPublishing/Advanced-Predictive-Techniques-with-Scikit-Learn-and-TensorFlow', 'size': '2192'}