轉換變數

使用 Python 中的 statsmodels 進行回歸入門

Maarten Van den Broeck

Content Developer at DataCamp

鱸魚資料集

perch = fish[fish["species"] == "Perch"]
print(perch.head())
   species  mass_g  length_cm
55   Perch     5.9        7.5
56   Perch    32.0       12.5
57   Perch    40.0       13.8
58   Perch    51.5       15.0
59   Perch    70.0       15.7

歐洲鱸,_Perca fluviatilis_

使用 Python 中的 statsmodels 進行回歸入門

不是線性關係

sns.regplot(x="length_cm",
            y="mass_g",
            data=perch,
            ci=None)

plt.show()

鱸魚質量對長度的散佈圖與趨勢線。魚變長時,重量上升快於線性,呈向上彎曲。

使用 Python 中的 statsmodels 進行回歸入門

白鯿 vs. 鱸魚

歐洲白鯿。白鯿身形較扁。

歐洲鱸。鱸魚身形較圓。

使用 Python 中的 statsmodels 進行回歸入門

繪製質量 vs. 長度立方

perch["length_cm_cubed"] = perch["length_cm"] ** 3
sns.regplot(x="length_cm_cubed",
            y="mass_g",
            data=perch,
            ci=None)
plt.show()

鱸魚質量對長度立方的散佈圖與趨勢線。轉換後,大多數點貼近趨勢線。

使用 Python 中的 statsmodels 進行回歸入門

建模:質量 vs. 長度立方

perch["length_cm_cubed"] = perch["length_cm"] ** 3

mdl_perch = ols("mass_g ~ length_cm_cubed", data=perch).fit()
mdl_perch.params
Intercept         -0.117478
length_cm_cubed    0.016796
dtype: float64
使用 Python 中的 statsmodels 進行回歸入門

預測:質量 vs. 長度立方

explanatory_data = pd.DataFrame({"length_cm_cubed": np.arange(10, 41, 5) ** 3,
                                 "length_cm": np.arange(10, 41, 5)})
prediction_data = explanatory_data.assign(
  mass_g=mdl_perch.predict(explanatory_data))
print(prediction_data)
   length_cm_cubed  length_cm       mass_g
0             1000         10    16.678135
1             3375         15    56.567717
2             8000         20   134.247429
3            15625         25   262.313982
4            27000         30   453.364084
5            42875         35   719.994447
6            64000         40  1074.801781
使用 Python 中的 statsmodels 進行回歸入門

繪製質量 vs. 長度立方

fig = plt.figure()
sns.regplot(x="length_cm_cubed", y="mass_g",
            data=perch, ci=None)
sns.scatterplot(data=prediction_data,
                x="length_cm_cubed", y="mass_g",
                color="red", marker="s")

鱸魚質量對長度立方的散佈圖與趨勢線,並標註由 predict() 計算的點。這些點完全貼合趨勢線。

fig = plt.figure()
sns.regplot(x="length_cm", y="mass_g",
            data=perch, ci=None)
sns.scatterplot(data=prediction_data,
                x="length_cm", y="mass_g",
                color="red", marker="s")

鱸魚質量對長度的散佈圖與趨勢線,並標註由 predict() 計算的點。這些點不沿著趨勢線,但符合資料點的彎曲。

使用 Python 中的 statsmodels 進行回歸入門

Facebook 廣告資料集

廣告如何運作

  1. 付費給 Facebook 投放廣告。
  2. 人們看到廣告(「impressions」)。
  3. 部分看到的人會點擊。

 

  • 936 列
  • 每列代表 1 則廣告
spent_usd n_impressions n_clicks
1.43 7350 1
1.82 17861 2
1.25 4259 1
1.29 4133 1
4.77 15615 3
... ... ...
使用 Python 中的 statsmodels 進行回歸入門

圖形過於擁擠

sns.regplot(x="spent_usd",
            y="n_impressions",
            data=ad_conversion,
            ci=None)

曝光次數對廣告花費的散佈圖與趨勢線。多數資料點擠在圖的左下角。

使用 Python 中的 statsmodels 進行回歸入門

平方根對平方根

ad_conversion["sqrt_spent_usd"] = np.sqrt(
  ad_conversion["spent_usd"])

ad_conversion["sqrt_n_impressions"] = np.sqrt(
  ad_conversion["n_impressions"])

sns.regplot(x="sqrt_spent_usd",
            y="sqrt_n_impressions",
            data=ad_conversion,
            ci=None)

曝光次數平方根對廣告花費平方根的散佈圖與趨勢線。現在資料點在圖中分布更平均。

使用 Python 中的 statsmodels 進行回歸入門

建模與預測

mdl_ad = ols("sqrt_n_impressions ~ sqrt_spent_usd", data=ad_conversion).fit()
explanatory_data = pd.DataFrame({"sqrt_spent_usd": np.sqrt(np.arange(0, 601, 100)),
                                 "spent_usd": np.arange(0, 601, 100)})
prediction_data = explanatory_data.assign(sqrt_n_impressions=mdl_ad.predict(explanatory_data),
                          n_impressions=mdl_ad.predict(explanatory_data) ** 2)
print(prediction_data)
   sqrt_spent_usd  spent_usd  sqrt_n_impressions  n_impressions
0        0.000000          0           15.319713   2.346936e+02
1       10.000000        100          597.736582   3.572890e+05
2       14.142136        200          838.981547   7.038900e+05
3       17.320508        300         1024.095320   1.048771e+06
4       20.000000        400         1180.153450   1.392762e+06
5       22.360680        500         1317.643422   1.736184e+06
6       24.494897        600         1441.943858   2.079202e+06
使用 Python 中的 statsmodels 進行回歸入門

一起來練習吧!

使用 Python 中的 statsmodels 進行回歸入門

Preparing Video For Download...