0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

マルチクラス分類のラベルエンコーディングと相関係数の分析

Posted at

はじめに

データ分析の勉強をしています。
分類先が複数あるマルチクラス分類の問題に取り掛かる際、まず相関関係が知りたくなるのでその方法を記載しました。

対象の試験データ

対象はこのようなデータです。
自動車のclassを分類する課題で、class以外の情報からclassの情報を判定する必要があります。

in
print(train_data)
out
       id  class buying  maint  doors persons lug_boot safety
0       0  unacc    low    med      3       2    small    low
1       3    acc    low   high      3    more    small    med
2       7  unacc  vhigh   high  5more       2    small    med
3      11    acc   high   high      3    more      big    med
4      12  unacc   high   high      3       2      med   high
..    ...    ...    ...    ...    ...     ...      ...    ...

要素の種類の調査

ユニークなものを集めて、データがどんな要素で構成されているのかを調査します。
これをすると欠損値や特異点の有無も確認できそうです。今回は無さそうです。

in
#調査対処の要素の定義
columns = ['class', 'buying', 'maint', 'doors', 'persons', 'lug_boot', 'safety']

#要素を収集してパターンを確認
unique_elements = {}
for col in columns:
    unique_elements[col] = train_data[col].unique()
    print(unique_elements[col])
out
['unacc' 'acc' 'vgood' 'good']
['low' 'vhigh' 'high' 'med']
['med' 'high' 'low' 'vhigh']
['3' '5more' '2' '4']
['2' 'more' '4']
['small' 'big' 'med']
['low' 'med' 'high']

要素に対してマッピングを作成

数値に置き換える際、相関係数を見るには順序性も大事なので、classに対して影響がありそうな順に並び変えて番号を振ります。

in
# 各カラムに対するマッピングを定義する
mapping = {
    'class': {'unacc': 0, 'acc': 1, 'good': 2, 'vgood': 3},
    'buying': {'vhigh': 0, 'high': 1, 'med': 2, 'low': 3},
    'maint': {'vhigh': 0, 'high': 1, 'med': 2, 'low': 3},
    'doors': {'2': 0, '3': 1, '4': 2, '5more': 3},
    'persons': {'2': 0, '4': 1, 'more': 2},
    'lug_boot': {'small': 0, 'med': 1, 'big': 2},
    'safety': {'low': 0, 'med': 1, 'high': 2}
}

ラベルエンコーディング

要素を数値に置き換えます。
元のデータを残しつつデータの変換をします。表示させて確認します。

in
# マッピングを元にデータを数値に変換する
mapped_train_data = train_data.copy()
for col in mapping:
    mapped_train_data[col] = train_data[col].map(mapping[col])

print(train_data)
print(mapped_train_data)
out
       id  class buying  maint  doors persons lug_boot safety
0       0  unacc    low    med      3       2    small    low
1       3    acc    low   high      3    more    small    med
2       7  unacc  vhigh   high  5more       2    small    med
3      11    acc   high   high      3    more      big    med
4      12  unacc   high   high      3       2      med   high
..    ...    ...    ...    ...    ...     ...      ...    ...

       id  class  buying  maint  doors  persons  lug_boot  safety
0       0      0       3      2      1        0         0       0
1       3      1       3      1      1        2         0       1
2       7      0       0      1      3        0         0       1
3      11      1       1      1      1        2         2       1
4      12      0       1      1      1        0         1       2
..    ...    ...     ...    ...    ...      ...       ...     ...

相関係数の出力と分析

相関関係の強さを調べます。
相関係数は絶対値が1に近いほど相関関係が強いので、結果からsafetyの相関関係が強く、doorsの相関関係は弱いように見えます。

in
# id列とclass列を除いた特徴量列のみを選択
features = mapped_train_data.drop(['id', 'class'], axis=1)
# class列と各特徴量列との相関係数を計算する
correlations = features.corrwith(mapped_train_data['class'])

print("class列との相関係数:")
print(correlations)
out
class列との相関係数:
buying      0.292611
maint       0.235106
doors       0.031257
persons     0.351530
lug_boot    0.163265
safety      0.444428

おわりに

データ分析について少しは勉強していたつもりでしたが、いざ課題に取り組むと手も足も出なくて悲しくなりました。
まず関係性を見て、そこから考えるのがいいかなと思いました。

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?