線性分類器:預測方程式

Python 中的線性分類器

Michael (Mike) Gelbart

Instructor, The University of British Columbia

點積(Dot Products)

x = np.arange(3)
x
array([0, 1, 2])
y = np.arange(3,6)
y
array([3, 4, 5])
x*y
array([0, 4, 10])
np.sum(x*y)
14
x@y
14
  • x@y 稱為 xy 的點積,寫作 $x \cdot y$。
Python 中的線性分類器

線性分類器的預測

  • $\textrm{raw model output} = \textrm{coefficients} \cdot \textrm{features} + \textrm{intercept}$
  • 線性分類器預測:先算原始輸出,再看其「正負號」
    • 正值:預測其中一類
    • 負值:預測另一類
  • 對 logistic regression 與 linear SVM 都相同
    • fit 不同,但 predict 相同
Python 中的線性分類器

LogisticRegression 如何做預測

$\textrm{raw model output} = \textrm{coefficients} \cdot \textrm{features} + \textrm{intercept}$

lr = LogisticRegression()

lr.fit(X,y)

lr.predict(X)[10]
0
lr.predict(X)[20]
1
Python 中的線性分類器

LogisticRegression 如何做預測(續)

lr.coef_ @ X[10] + lr.intercept_ # raw model output
array([-33.78572166])
lr.coef_ @ X[20] + lr.intercept_ # raw model output
array([ 0.08050621])
Python 中的線性分類器

原始模型輸出

Python 中的線性分類器

原始模型輸出

Python 中的線性分類器

原始模型輸出

Python 中的線性分類器

一起來練習吧!

Python 中的線性分類器

Preparing Video For Download...