[Python] 결측치 시각화 missingno 사용하기

2020. 7. 23. 20:00분석 Python/Visualization

728x90

결측치에 대해서 시각적으로 보고 싶을 때 missingno가 쓸만한 것 같다.

해당 패키지를 자주 쓸것 같아서 간단하게 함수화를 해봤다.

보통 Train , Test를 나누니 그것을 기준으로 그려보는 것을 해봤다.

import missingno as msno
import matplotlib.pyplot as plt
def msno_vis_train_test(train , test , graph_type = "bar" , fig_kws = {"figsize" : (20,5)}) :
    fig , ax = plt.subplots(1,2,figsize=fig_kws.get("figsize", (20,5)) )
    axes = ax.flatten()
    if graph_type == "bar" :
        msno.bar(train, ax=axes[0])
        msno.bar(test, ax=axes[1])
    elif graph_type == "matrix" :
        msno.matrix(train, ax=axes[0])
        msno.matrix(test, ax=axes[1])
    elif graph_type == "dendrogram" :
        msno.dendrogram(train, ax=axes[0])
        msno.dendrogram(test, ax=axes[1])
    elif graph_type == "heatmap" :
        msno.heatmap(train, ax=axes[0], cbar=False)
        msno.heatmap(test, ax=axes[1])
    else :
        raise Exception(f"not supported graph type : {graph_type}, \n only supported by bar, matrix , dendrogram, heatmap")
    axes[0].set_title(fig_kws.get("train_title" , "train"), 
                      fontsize=fig_kws.get("title_font_size" , 30))
    axes[1].set_title(fig_kws.get("test_title" , "test"), 
                      fontsize=fig_kws.get("title_font_size" , 30))
    plt.show()

 

msno_vis_train_test(train ,test , "bar")
msno_vis_train_test(train ,test , "matrix")
msno_vis_train_test(train ,test , "heatmap")
msno_vis_train_test(train ,test , "dendrogram")

이쁘군

 

728x90