<jupyter_start><jupyter_text>Credit One  Preprocesamiento del Set  Inicialización de librerias<jupyter_code>import pandas as pd
import numpy as np
import pandas as pd
from pandas import Series, DataFrame
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
from IPython.display import display # Allows the use of display() for DataFrames
from bokeh.plotting import figure, output_file, show<jupyter_output><empty_output><jupyter_text>Carga de información<jupyter_code>credit = pd.read_csv('D:\Personal\Cenfotec\Modulo 5\Fuente\default of credit card clients.csv', header =1)<jupyter_output><empty_output><jupyter_text>Se renombran las columnas para mejor entendimiento<jupyter_code>credit=credit.rename(columns={"PAY_0": "PAY_April", "PAY_2": "PAY_May", "PAY_3": "PAY_Jun", "PAY_4": "PAY_Jul", "PAY_5": "PAY_Aug", "PAY_6": "PAY_Set"})
credit=credit.rename(columns={"BILL_AMT1": "BILL_AMT_Set", "BILL_AMT2": "BILL_AMT_Aug", "BILL_AMT3": "BILL_AMT_Jul", "BILL_AMT4": "BILL_AMT_Jun", "BILL_AMT5": "BILL_AMT_May", "BILL_AMT6": "BILL_AMT_Abril"})
credit=credit.rename(columns={"PAY_AMT1": "PAY_AMT_Set", "PAY_AMT2": "PAY_AMT_Aug", "PAY_AMT3": "PAY_AMT_Jul", "PAY_AMT4": "PAY_AMT_Jun", "PAY_AMT5": "PAY_AMT_May", "PAY_AMT6": "PAY_AMT_Abril"})
credit=credit.rename(columns={"default payment next month": "DEFAULT"})<jupyter_output><empty_output><jupyter_text>Al ser un módelo de Clasificación, Se cambian variables a catagoricas<jupyter_code>credit=credit.astype({'SEX':'category','AGE':'category','EDUCATION':'category','MARRIAGE':'category','PAY_April':'category','PAY_May':'category','PAY_Jun':'category','PAY_Jul':'category','PAY_Aug':'category','PAY_Set':'category','DEFAULT':'category'})<jupyter_output><empty_output><jupyter_text>Se modifican los valores por su prosa en busqueda de un mejor entendimiento de la información.  Modificar Record de pagos<jupyter_code>credit['PAY_April']=credit['PAY_April'].replace({-2: "No consumption"})
credit['PAY_April']=credit['PAY_April'].replace({-1: "Paid in full"})
credit['PAY_April']=credit['PAY_April'].replace({0: "The use of revolving credit"})

credit['PAY_Jun']=credit['PAY_Jun'].replace({-2: "No consumption"})
credit['PAY_Jun']=credit['PAY_Jun'].replace({-1: "Paid in full"})
credit['PAY_Jun']=credit['PAY_Jun'].replace({0: "The use of revolving credit"})

credit['PAY_Jul']=credit['PAY_Jul'].replace({-2: "No consumption"})
credit['PAY_Jul']=credit['PAY_Jul'].replace({-1: "Paid in full"})
credit['PAY_Jul']=credit['PAY_Jul'].replace({0: "The use of revolving credit"})

credit['PAY_Aug']=credit['PAY_Aug'].replace({-2: "No consumption"})
credit['PAY_Aug']=credit['PAY_Aug'].replace({-1: "Paid in full"})
credit['PAY_Aug']=credit['PAY_Aug'].replace({0: "The use of revolving credit"})

credit['PAY_Set']=credit['PAY_Set'].replace({-2: "No consumption"})
credit['PAY_Set']=credit['PAY_Set'].replace({-1: "Paid in full"})
credit['PAY_Set']=credit['PAY_Set'].replace({0: "The use of revolving credit"})

credit['PAY_May']=credit['PAY_May'].replace({-2: "No consumption"})
credit['PAY_May']=credit['PAY_May'].replace({-1: "Paid in full"})
credit['PAY_May']=credit['PAY_May'].replace({0: "The use of revolving credit"})<jupyter_output><empty_output><jupyter_text>Modificar valores de Genero<jupyter_code>credit['SEX']=credit['SEX'].replace({1: "Male"})
credit['SEX']=credit['SEX'].replace({2: "Female"})<jupyter_output><empty_output><jupyter_text>Modificar valores de Educación<jupyter_code>credit['EDUCATION']=credit['EDUCATION'].replace({1: "Graduate school"})
credit['EDUCATION']=credit['EDUCATION'].replace({2: "University"})
credit['EDUCATION']=credit['EDUCATION'].replace({3: " high school"})
credit['EDUCATION']=credit['EDUCATION'].replace({0: "others"})
credit['EDUCATION']=credit['EDUCATION'].replace({4: "others"})
credit['EDUCATION']=credit['EDUCATION'].replace({5: "others"})
credit['EDUCATION']=credit['EDUCATION'].replace({6: "others"})<jupyter_output><empty_output><jupyter_text>Modificar Valores de Estado Civil<jupyter_code>credit['MARRIAGE']=credit['MARRIAGE'].replace({1: "Married"})
credit['MARRIAGE']=credit['MARRIAGE'].replace({2: "Single"})
credit['MARRIAGE']=credit['MARRIAGE'].replace({3: "Divorce"})
credit['MARRIAGE']=credit['MARRIAGE'].replace({0: "others"})
credit['DEFAULT']=credit['DEFAULT'].replace({1: "Yes"})
credit['DEFAULT']=credit['DEFAULT'].replace({0: "No"})<jupyter_output><empty_output><jupyter_text>Se imprime el dataset para la revisión del mismo<jupyter_code>display(credit)<jupyter_output><empty_output><jupyter_text>Revisión de Datos Faltantes<jupyter_code>total = credit.isnull().sum().sort_values(ascending = False)
percent = (credit.isnull().sum()/credit.isnull().count()*100).sort_values(ascending = False)
pd.concat([total, percent], axis=1, keys=['Total', 'Percent']).transpose()<jupyter_output><empty_output><jupyter_text>Matriz de Correlación<jupyter_code>import seaborn as sns

corr = credit.drop('ID',axis=1).corr()
f, ax = plt.subplots(figsize=(10, 10))

# Generate a custom diverging colormap
cmap = sns.diverging_palette(220, 10, as_cmap=True)

# Draw the heatmap with the mask and correct aspect ratio
sns.heatmap(corr, cmap=cmap, vmin=0,vmax=1, center=0,
            square=True, linewidths=.5)<jupyter_output><empty_output><jupyter_text>Se Dicretiza la variable Age, con la finalidad de simplificar los valores<jupyter_code>from sklearn.preprocessing import KBinsDiscretizer

A1_dis = KBinsDiscretizer(n_bins=4, encode='ordinal',
                           strategy = "kmeans").fit_transform(credit[['AGE']])

A1_dis = pd.DataFrame(A1_dis)
A1_dis = A1_dis.rename(columns = {0: 'AGE_D'})
credit[['AGE_D']] = A1_dis
header = credit.dtypes.index
print(header)
def draw_histograms(df, variables, n_rows, n_cols, n_bins):
    fig=plt.figure()
    for i, var_name in enumerate(variables):
        ax=fig.add_subplot(n_rows,n_cols,i+1)
        df[var_name].hist(bins=n_bins,ax=ax)
        ax.set_title(var_name)
    fig.tight_layout()  # Improves appearance a bit.
    plt.show()

fil = (credit.PAY_April > -2) | (credit.PAY_April == -1) | (credit.PAY_April == 0)
credit.loc[fil, 'PAY_April'] = 0
fil = (credit.PAY_May == -2) | (credit.PAY_May == -1) | (credit.PAY_May == 0)
credit.loc[fil, 'PAY_May'] = 0
fil = (credit.PAY_Jun == -2) | (credit.PAY_Jun == -1) | (credit.PAY_Jun == 0)
credit.loc[fil, 'PAY_Jun'] = 0
fil = (credit.PAY_Jul == -2) | (credit.PAY_Jul == -1) | (credit.PAY_Jul == 0)
credit.loc[fil, 'PAY_Jul'] = 0
fil = (credit.PAY_Aug == -2) | (credit.PAY_Aug == -1) | (credit.PAY_Aug == 0)
credit.loc[fil, 'PAY_Aug'] = 0
fil = (credit.PAY_Set == -2) | (credit.PAY_Set == -1) | (credit.PAY_Set == 0)
credit.loc[fil, 'PAY_Set'] = 0
late = credit[['PAY_April','PAY_May', 'PAY_Jun', 'PAY_Jul', 'PAY_Aug', 'PAY_Set']]
draw_histograms(late, late.columns, 2, 3, 10)
class_0 = credit.loc[credit['SEX'] == 'Male']["LIMIT_BAL"]
class_1 = credit.loc[credit['SEX'] == 'Female']["LIMIT_BAL"]
plt.figure(figsize = (14,6))
plt.title('Limite de Credito por Genero')
sns.set_color_codes("pastel")
sns.distplot(class_1,kde=True,bins=100, color="blue")
sns.distplot(class_0,kde=True,bins=100, color="green")
sns.legend(title='Gender:')
plt.show()
ax=sns.violinplot(x='EDUCATION',y='LIMIT_BAL',hue='SEX',split=True,data=credit)
ax.set_title('Distribución de limite por Nivel Edicativo')
ax=credit.groupby("EDUCATION")['DEFAULT']\
                                .value_counts(normalize=True)\
                                .unstack()\
                                .plot(kind='barh',stacked='True')
ax.set_title("Porcentaje de Clientes Manchados por Nivel Educativo")
ax.legend(title='Default',bbox_to_anchor=(1,1))
fig,ax=plt.subplots()
sns.distplot(credit.loc[credit.SEX=="Male",'AGE'].dropna(),
            hist=False, color='green',
            kde_kws={"shade":True},
            ax=ax, label='Male')
sns.distplot(credit.loc[credit.SEX=="Female",'AGE'].dropna(),
            hist=False, color='blue',
            kde_kws={"shade":True},
            ax=ax, label='Female')
ax.set_title("Distribución por Genero por Edad")
ax.legend(title='Gender:')
fig,ax=plt.subplots()
sns.distplot(credit.loc[credit.DEFAULT=="Yes",'AGE'].dropna(),
            hist=False, color='green',
            kde_kws={"shade":True},
            ax=ax, label='Yes')
sns.distplot(credit.loc[credit.DEFAULT=="No",'AGE'].dropna(),
            hist=False, color='blue',
            kde_kws={"shade":True},
            ax=ax, label='No')
ax.set_title("Distribución por Default por Edad")
ax.legend(title='Default:')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.188948723952236, 'fraction_numerical': 0.014399438070709435, 'mean_word_length': 6.8955637707948245, 'pattern_counts': {'":': 23, '<': 49, '<?xml version=': 0, '>': 50, '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': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '482525', 'license': 'Unlicense', 'n_tokens_mistral': 3551, 'n_tokens_neox': 3244, 'n_words': 510, 'path': 'Modelado/Exploracion Datos Deily Rojas.ipynb', 'repo_name': 'deilyrm92/CreditOne_Deily', 'size': '8542'}