從二元類別變數建立虛擬變數
給定一個含有單一預測變數「gender」的 basetable。請為「gender」建立虛擬變數,讓它可以在羅吉斯迴歸模型中作為預測變數使用。
本練習屬於課程
Python 中級預測分析
練習說明
- 建立一個 pandas 的 dataframe
dummies_gender,其中包含「gender」的虛擬變數。請確保避免多重共線性。 - 將這些虛擬變數加入原始的
basetable。 - 從
basetable中移除原本的變數「gender」。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create the dummy variable
dummies_gender = pd.____(____["____"], drop_first=____)
# Add the dummy variable to the basetable
basetable = pd.concat([____, ____], axis=1)
# Delete the original variable from the basetable
del basetable["____"]
print(basetable.head())