![]() |
この記事は曖昧さ回避です |
モデリング(modelling)とは、モデル (model, 基準となるべき物)を組み立てることを意味する英単語である。転じて以下の意味で使われている。
- 科学的モデリング(Scientific modelling): 数理モデルを構築すること。
- 心理学的モデリング(Psychological modeling)
- 3DCGの分野に於いて、3次元モデルを作ること。
- 物理的立体造形物を作ること。
「統計モデリング」の概要
統計学・機械学習に於けるモデリングとは、予測モデル(関数)を構築することである。例えば、広告費から売上げを予測したい場合、既知(過去)の広告費と売上げの関係のデータがあれば、これを用いて広告費xから売上げを予測する関数f(x)を構築することができる。以下、単回帰分析を用いたモデリング(予測モデルの構築)の実演を示す。
- Google Colaboratory
を開く
- 「ファイル」メニューの「ノートブックを新規作成」をクリック
- 「+ コード」ボタンをクリックしてセルを用意
- 下記のコードをセルにコピペ
!pip install japanize-matplotlib !wget -nc https://www.juse-p.co.jp/files/download/8/9305-6_Excel2007-2.zip !unzip -u -O Windows-31J 9305-6_Excel2007-2.zip %matplotlib inline import matplotlib.pyplot as plt import japanize_matplotlib import numpy as np import pandas as pd from sklearn.linear_model import LinearRegression import os.path print() xlabel, ylabel = "広告費(万円)", "売上げ(万円)" print("「{}」と「{}」の関係のデータ (欠損値を含む行を除く)".format(xlabel, ylabel)) df = pd.read_excel(os.path.join(".", "Excel2007ファイル", "第2章Excel2007", "第2章単回帰分析_ソルバー.xlsx"), sheet_name="データ", index_col=0).dropna() display(df) # 説明変数 X = df[xlabel].values.reshape((-1, 1)) # 目的変数 y = df[ylabel].values # 予測モデルの定義 model = LinearRegression() # モデリング model.fit(X, y) # 可視化: 予測モデルのグラフ表現と関数表現 ax = df.plot.scatter(xlabel, ylabel) ax.set_title("$r={:,.3f}$".format(df.corr().iloc[0, -1])) # 相関係数 ax.set_xlim(0.0, 500.0) ax.set_ylim(0.0, 5000.0) ax.set_xlabel("$x$: " + ax.get_xlabel()) ax.set_ylabel("$f(x)$: " + ax.get_ylabel()) major_formatter = plt.FuncFormatter(lambda x, pos: "{:,.1f}".format(float(x))) ax.xaxis.set_major_formatter(major_formatter) ax.yaxis.set_major_formatter(major_formatter) for text, xy in df.iterrows(): ax.annotate(text, xy) f = model.predict x = np.array([X.min(), X.max()]).reshape((-1, 1)) ax.plot(x, f(x), color='red', label="$f(x)={beta1:,.3f}x{beta0:+,.3f}$\n$R^2={rsquared:,.3f}$".format( # 回帰係数(傾き) beta1=model.coef_[0], # 切片 beta0=model.intercept_, # 決定係数 rsquared=model.score(X, y))) ax.legend() plt.show()
- 「▶」ボタンをクリック
関連動画
関連静画
関連商品
関連コミュニティ
関連項目
- 0
- 0pt