開始使用免費開始

使用類別變數建模

在前面的練習中,你已經配適一個以 color 與 width 為解釋變數的羅吉斯迴歸模型,但當時是把 color 視為連續變數。這個練習中,你會把 color 視為類別變數;在建立模型矩陣時,color 會被以 0/1 編碼成 3 個變數。

回顧一下,dmatrix() 的預設編碼會使用第一組作為參考組。若要將模型矩陣顯示為資料框,請在 dmatrix() 中將引數 return_type 設為 'dataframe'。

color 變數有以下自然順序:
1:medium light
2:medium
3:medium dark
4:dark

crab 資料集已預先載入至工作區。

本練習屬於課程

Generalized Linear Models in Python

檢視課程

動手互動練習

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

# Construct model matrix
model_matrix = ____('C(____, ____(____))' , data = ____, 
                       return_type = 'dataframe')

# Print first 5 rows of model matrix dataframe
print(____.____)

# Fit and print the results of a glm model with the above model matrix configuration
model = ____('____ ~ ____(____, ____(____))', data = ____, 
            family = ____).____

print(____.____)
編輯並執行程式碼