[キャンプAI技術を導く]3週目Day 2


今日の勉強内容


1. Seaborn


1. Seaborn


  • Matplotlibベースの統計的可視化ライブラリは、複数のカスタマイズと簡単な構文と簡潔な設計を備えています.

  • 可視化の目的と方法に基づいてAPIを分類する
  • Categorical API
  • Distribution API
  • Realational API
  • Regression API
  • Multiples API
  • Theme API
  • Categorical API


    Countplot

  • Seabornの分類APIにおける古典的な可視化方法
  • sns.countplot(x='column', data = datas, hue='split_column', order=sorted())
  • hueは、色に基づいてデータ分割基準を決定するために使用される.
  • 注文書に記載された順序で注文できます.
  • 統計量


    Count:データ量
    Mean:平均値
    Std:標準偏差
    四分位数:データの四等分観測値(25%,50%75%)

    Box Plot

  • の間の矩形はそれぞれ25%、medium、50%であり、
  • を表す.
  • 矩形間(25%〜75%)を四分位数範囲と呼ぶ.
  • 25%と75%の値のうち、それぞれ−1.5倍と1.5倍の値をヒゲ(枠)とし、範囲外の値をOutlierと呼ぶ.
  • fig, ax = plt.subplots(1,1, figsize=(10, 5))
    sns.boxplot(x='column1', y='column2', data=datas,
                hue='split_column', 
                order=sorted(),
                width=0.3,
                linewidth=2,
                fliersize=10, #outlier 점의 크기
                ax=ax)
    plt.show()

    Violin Plot

  • Box Plotと比較して、
  • は実際の分布を記述するのに適しています.
  • IQRを表す方法はいろいろあります.
  • bw:分布表示の具体値
  • cut:境界値
  • を設定する
  • inner:内部IQR表示法
  • fig, ax = plt.subplots(1,1, figsize=(12, 5))
    sns.violinplot(x='column1', data=datas, ax=ax,
                   bw=0.1,
                   cut=0,
                   inner='quartile'
                  )
    plt.show()

    Distribution API

  • hisplot:ヒストグラム
  • kdeplot : Kernel Densitiy Estimate
  • ecdfplot:積算密度関数
  • カーペット:直線の密度関数
  • を使用
    fig, axes = plt.subplots(2,2, figsize=(12, 10))
    axes = axes.flatten()
    sns.histplot(x='math score', data=student, ax=axes[0])
    sns.kdeplot(x='math score', data=student, ax=axes[1]) #곡선 보간
    sns.ecdfplot(x='math score', data=student, ax=axes[2])
    sns.rugplot(x='math score', data=student, ax=axes[3])

    Relation & Regression


    Scatter Plot

  • スタイル、色調、サイズ設定可能
  • fig, ax = plt.subplots(figsize=(7, 7))
    sns.scatterplot(x='column1', y='column2', data=data,
                    hue='race/ethnicity')             

    Line Plot

    fig, ax = plt.subplots(1, 1,figsize=(12, 7))
    sns.lineplot(x='year', y='Jan',data=flights_wide, ax=ax)

    Regplot

  • モードバッチに回帰線が追加されたバッチ.
  • orderを使用する場合は、2 D戻り線を使用するか、log線を使用することができます.
  • fig, ax = plt.subplots(figsize=(7, 7))
    sns.regplot(x='math score', y='reading score', data=student)
    fig, ax = plt.subplots(figsize=(7, 7))
    sns.regplot(x='reading score', y='writing score', data=student,
                logx=True)

    Matrix Plots


    Heatmap

  • 相関の可視化に代表的に使用される.
  • vminとvmaxで範囲を調整します.
  • fig, ax = plt.subplots(1,1 ,figsize=(7, 6))
    sns.heatmap(heart.corr(), ax=ax,
               vmin=-1, vmax=1)
  • 注記fmtを使用して実値の内容
  • を挿入する.
    fig, ax = plt.subplots(1,1 ,figsize=(10, 9))
    sns.heatmap(heart.corr(), ax=ax,
               vmin=-1, vmax=1, center=0,
                cmap='coolwarm',
                annot=True, fmt='.2f')