開始使用免費開始

從多類別變數建立虛擬變數

已提供一個只有一個預測變數「country」的 basetable。請為「country」建立虛擬變數,讓「country」可以作為邏輯斯迴歸模型的預測變數來使用。

本練習屬於課程

Python 中級預測分析

檢視課程

練習說明

  • 建立一個 pandas dataframe dummies_country,其中包含「country」的虛擬變數。請確保避免多重共線性。
  • 將這些虛擬變數加入原本的 basetable
  • basetable 中移除原始變數「country」。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Create the dummy variable
dummies_country = ____.____(____["____"], ____=____)

# Add the dummy variable to the basetable
basetable = ____.____([____, ____], ____=____)

# Delete the original variable from the basetable
____ ____["____"]
print(basetable.head())
編輯並執行程式碼