開始使用免費開始

找出最正面與最負面的單字

在這個練習中,你要解讀套用在電影評論情緒資料集上的邏輯斯迴歸係數。模型物件已為你建立並完成訓練,儲存在變數 lr 中。

另外,各個特徵所對應的單字已載入變數 vocab。例如,因為 vocab[100] 是「think」,代表特徵 100 對應於該電影評論中單字「think」出現的次數。

本練習屬於課程

Python 中的線性分類器

檢視課程

練習說明

  • 找出對應於前 5 大係數的單字。
  • 找出對應於最小的 5 個係數的單字。

動手互動練習

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

# Get the indices of the sorted cofficients
inds_ascending = np.argsort(lr.coef_.flatten()) 
inds_descending = inds_ascending[::-1]

# Print the most positive words
print("Most positive words: ", end="")
for i in range(5):
    print(____, end=", ")
print("\n")

# Print most negative words
print("Most negative words: ", end="")
for i in range(5):
    print(____, end=", ")
print("\n")
編輯並執行程式碼