1
2

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.

【多変量回帰】多変量回帰をコードで書いてみる

Posted at

はじめに

多変量回帰をプログラミングします。使うデータは前にも使ったアボカドデータです。
https://qiita.com/iwasaki_kenichi/items/ea580fd9498ad6950a75

カテゴリカルデータの変換

実際に取扱うデータには、整数や実数以外にも、文字列が含まれる場合があります。
いわゆるカテゴリカルデータですが、それを変換するために下記を行うと数値に変換することができます。
文字列から数値(カテゴリ数値)に変換することで、文字列データを多変量回帰に利用することが可能です。

main1.py

df["変数名"] = pd.Categorical(df.変数名).codes

# コード
```python:main1.py

# ライブラリインポート
%matplotlib inline
import numpy as np
from pylab import *
import pandas as pd
import matplotlib.pyplot as plt
from scipy import stats

# データ読み込み
df = pd.read_csv("hogehoge.csv")

df["Categorical_type"] = pd.Categorical(df.type).codes
df["Region_type"] = pd.Categorical(df.region).codes

X = df[["XLarge Bags","Region_type"]]
Y = df[["AveragePrice"]]

X1 = sm.add_constant(X)
est = sm.OLS(Y,X1).fit()
est.summary()

 おわりに

これぐらいもササッと書けるようになりたい。

summaryで出てくる色々な変数の意味は
ここがわかりやすいです。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?