LoginSignup
2
1

More than 5 years have passed since last update.

Python dmatrix() ダミー変数化

Posted at

formulaでダミー変数化したいとき

必要なモジュールを呼び出します

import pandas as pd
import patsy

サンプルのデータフレームを準備します

スクリーンショット 2018-01-04 18.08.00 2.png
こんなかんじです。

a, b, x1, x2の列をダミー変数化します。

(カテゴリカルな列のみ展開されます)
スクリーンショット 2018-01-04 18.08.00.png
dmatrix("formulaでどのカラムを展開するか指定", 展開前のデータフレーム名)
でダミー変数化を行うことができます。
return_type="dataframe" の指定をすることで、
展開後のデータをデータフレームの形にしておくことができます。

その他

詳しく知りたい方はこちら

Rでのダミー変数化、相互作用項の作成も書いたのでよければ見てください
その1
その2

2
1
0

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