1
5

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 5 years have passed since last update.

pandasで数値の変数を、閾値を決めてカテゴリカルに変換する

Posted at

自分の備忘録として書いていこうと思います。

初記事からすごくピンポイントですが、機械学習の特徴量エンジニアリングとして、

・0~1は1,1~3は2
・3~10は3
・10~20は4 …………

 のように、特徴量を区分したい時ってあると思います。

すごく分かりやすい例だと、
ビューフォート風力階級

beaufort = [(0, 0, 0.3), (1, 0.3, 1.6), (2, 1.6, 3.4), (3, 3.4, 5.5), (4, 5.5, 8), (5, 8, 10.8), (6, 10.8, 13.9), 
          (7, 13.9, 17.2), (8, 17.2, 20.8), (9, 20.8, 24.5), (10, 24.5, 28.5), (11, 28.5, 33), (12, 33, 200)]

for item in beaufort:
    train.loc[(train['wind_speed']>=item[1]) & (train['wind_speed']<item[2]), 'beaufort_scale'] = item[0]


このサンプルコードは非常にシンプルでかつ強力、使い回しもしやすいので、
自分の引き出しに入れておきます!!

1
5
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
1
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?