Explanatory Data Analysis (EDA)#
Step1. 不必要な特徴量を特定し除外する#
すべてが欠損値であるカラムを特徴量から除外#
# Drop columns that has all NaN Values
train_data.dropna(how='all', axis=1, inplace=True)
1つの値のバリエーションしか持たないカラムを特徴量から除外#
# Drop columns having a single value
nunique = train_data.nunique(dropna=False)
assert nunique[label] > 1, f"Only single value in label column. So, cannot train."
cols_to_drop = train_data.columns[nunique == 1]
if (len(cols_to_drop) >= 1):
print(f"Drop columns having a single value: {cols_to_drop}")
train_data.drop(cols_to_drop, axis=1, inplace=True)
nunique.drop(labels=cols_to_drop, inplace=True)
レコードごとにでユニークなカラム(rowid など)を特徴量から除外#
# Drop unique identity columns such as rowid columns
# workaround for WM-1565: text only colunn can be identify columns
cols_to_drop = train_data.columns[nunique == train_data.shape[0]]
cols_to_drop = train_data[cols_to_drop].select_dtypes(exclude='object').columns
del nunique
if (len(cols_to_drop) >= 1):
print(f"Drop unique identity columns: {cols_to_drop}")
train_data.drop(cols_to_drop, axis=1, inplace=True)
assert len(train_data.columns) >= 2, f"Training data expected to have at least 2 columns: {train_data.columns}"
Step2. 相関や分布を見る簡単な EDA の実施#
correlation#
Number
カラム同士の correlation(相関)を計算し、テーブルおよびヒートマップで出力する。
# Show correlation
corr = get_corr(train_data)
corr
seniorcitizen |
tenure |
monthlycharges |
totalcharges |
|
---|---|---|---|---|
seniorcitizen |
1 |
0.023467 |
0.223863 |
0.109136 |
tenure |
0.023467 |
1 |
0.253703 |
0.828414 |
monthlycharges |
0.223863 |
0.253703 |
1 |
0.652161 |
totalcharges |
0.109136 |
0.828414 |
0.652161 |
1 |

Fig. 18 correlation heatmap の例#
distribution#
推論タイプを target_column
の値のバリエーションから類推する。また、problem_type
オプションが指定されている場合はそちらが優先される。
次に、2値/多値分類の場合は、target_column
のラベルごとのカウント数が、regression の場合はヒストグラムが表示される。
from autogluon.core.utils import infer_problem_type
inferred_problem_type = infer_problem_type(train_data[label])
problem_type = problem_type if problem_type is not None else inferred_problem_type
print(f"problem_type: {problem_type}")
if problem_type != inferred_problem_type:
print(f"Inferred problem_type: {inferred_problem_type}")
if problem_type in {'binary', 'multiclass'}:
sns_countplot(train_data[label], title=f"{label} distribution")
else: # regression or qunatile
sns_histplot(train_data[label], title=f"{label} distribution", plot_mean=True, kde=True)

Fig. 19 2値分類の場合のラベルの分布#

Fig. 20 多値分類の場合のラベルの分布#

Fig. 21 回帰の場合のラベルの分布(この例の場合、ラベルの値が0に近いところに集中しつつ、非常に値が大きいものも散見され、Scaling が必要な例となっている。)#