데이터 분석 - 실습

CYSSSSSSSSS·2023년 8월 19일
0

데이터분석

목록 보기
16/19

주제

신용대출 심사

  • 고객사는 ## 은행입니다. 신용평가 업무를 인공지능으로 전환하고자 여러분에게 모델링을 의뢰하였습니다.

  • 대출업무는

    • 은행 창구에서 신청을 받고
    • 본사의 심사부서에서는 신용평가를 통해 대출 신청에 대한 승인 여부를 결정해 왔습니다.
  • 현장의 요구

    • 경쟁사의 공격적인 대출상품 판매로, 본사에서는 자사 은행의 대출 실적이 줄어들고 있는 것에 부담을 느끼고 있습니다.
    • 그런데, 자사 은행에서는 신용평가 결과의 정확성에 의문을 품고 있으며, 신용평가 기준을 완화하여 가급적 대출승인 범위를 더 확대해 주기를 요구합니다.
  • 신용평가 업무를 인공지능으로 전환

    • 현장의 요구를 감안하여, 과거 사람이 하던 평가방식을 개선하고자 인공지능에 의한 예측 모델을 만들고, 정확도를 높이고자 합니다.

import

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

from statsmodels.graphics.mosaicplot import mosaic

import scipy.stats as spst

read_file

path = 'https://raw.githubusercontent.com/DA4BAM/dataset/master/credit_all.csv'
df = pd.read_csv(path)
df.loc[df['Payment'] == 4 , 'Payment'] = 3 # 신용등급 4 -> 3 으로 변경 
df.drop(['Telephone','ForeignWorker','Dependents'], axis = 1, inplace = True) # 열을 삭제
df.head()
  • 신용등급 변경 + 필요없는 열 삭제

범주/숫자 분류

범주형 변수

  • Creditability(target) , Account Balance , Payment , Purpose , Employment , SexMarital , CurrentAddress , MostValuable , AppartmentType , NoCredits , Occupation

숫자형 변수

  • CreditAmount , Age , Duration

단변량 분석(숫자)

def univariate_fuction(target , df , bins = 30):
display(df[[target]].describe().T)

    print('-' * 70)
    
    plt.figure(figsize = (8,16))
    plt.subplot(3,1,1)
    sns.histplot(x = target , data = df , bins = bins)
    plt.title(f'histogram of {target}')
    plt.xlabel(target)
    
    plt.subplot(3,1,2)
    sns.kdeplot(df[target])
    plt.title(f'kdechart of {target}')
    plt.xlabel(target)
    
    plt.subplot(3,1,3)
    sns.boxplot(df[target])
    plt.title(f'boxchart of {target}')
    plt.xlabel(target)
    
    plt.grid()
    plt.show()
  • 숫자형 단변량 분석은 , histplot(히스토그램) , kdeplot(밀도 차트) , boxplot(사분위수) 를 체크 하여 데이터를 분석한다.

단변량 분석 (범주)

def univariate_function2(target , df):
    print(f'{target} 의 빈도')
    display(df[target].value_counts())
    print('-' * 70)
    print(f'{target} 의 빈도율')
    display(df[target].value_counts(normalize = True))
    print('-' * 70)
    plt.figure(figsize = (8,16))
    plt.subplot(2,1,1)
    sns.countplot(x = target , data = df)
    plt.title(f'countplot of {target}')
    plt.grid()
    
    plt.subplot(2,1,2)
    value = df[target].value_counts()
    plt.pie(value ,labels = value.index,autopct = '%.2f%%')
  • 범주형 단변량 분석은 빈도와 빈도율 을 체크하고 그것을 통해 countplot 과 pie chart 를 통해 범주별 비율을 분석한다.

이변량 분석 (숫자 -> 범주)

def bivariate_function(var , target , df):
    plt.figure(figsize = (8,16))
    plt.subplot(2,1,1)
    sns.kdeplot(x = var , data = df , hue = target , common_norm=False)
    plt.grid()
    
    plt.subplot(2,1,2)
    sns.kdeplot(x = var , data = df , hue = target , multiple = 'fill')
    plt.axhline(df[target].mean() , color = 'r')
    plt.show()
  • 밀도 함수의 여러가지 조건과 , 범주형의 평균과 비교하여 영향이 있는지 판단해야 한다.

이변량 분석 (범주 -> 범주)

def bivariate_function2(var,target ,data):
    table = pd.crosstab(df[var] , df[target])
    print(table)
    mosaic(df , [var , target])
    plt.axhline(df[target].mean() , color = 'r')
    plt.show()
    
    print(spst.chi2_contingency(table))
    
profile
개발자 되고 싶어요

0개의 댓글