Professional Documents
Culture Documents
the course!
LINEAR C L AS S I F I E R S I N P Y T
HON
newsgroups = sklearn.datasets.fetch_20newsgroups_vectorized()
X, y = newsgroups.data, newsgroups.target
X.shape
(11314, 130107)
y.shape
(11314,)
knn =
KNeighborsClassifier(n_neighbors=1)
knn.fit(X,y)
y_pred = knn.predict(X)
0.99991
knn.fit(X_train, y_train)
knn.score(X_test, y_test)
0.66242
lr = LogisticRegression()
lr.fit(X_train, y_train)
lr.predict(X_test)
lr.score(X_test, y_test)
0.972
lr.predict_proba(wine.data[:1])
wine = sklearn.datasets.load_wine()
from sklearn.svm import LinearSVC
svm = LinearSVC()
svm.fit(wine.data, wine.target)
svm.score(wine.data, wine.target)
0.893
1.
THON
14
array([0, 1, 2])
x@y
y = np.arange(3,6)
y
14
LINEAR CLASSIFIERS IN
Linear classifier
prediction
raw model output = coefficients ⋅ features + intercept
Linear classifier prediction : comp u te raw model outp ut , check
the sign
if positive, predict one class
LINEAR CLASSIFIERS IN
How LogisticRegression makes
predictions
raw model output = coefficients ⋅ features + intercept
lr = LogisticRegression()
lr.fit(X,y)
lr.predict(X)[10]
lr.predict(X)[20]
LINEAR CLASSIFIERS IN
How LogisticRegression makes predictions
cont.)@ X[10] + lr.intercept_ # raw model
(lr.coef_
output
array([-33.78572166])
array([ 0.08050621])
LINEAR CLASSIFIERS IN
The ra w model
output
LINEAR CLASSIFIERS IN
The ra w model
output
LINEAR CLASSIFIERS IN
The ra w model
output
LINEAR CLASSIFIERS IN
Let's practice!
L I N EA R C L A S S I F I ER S I NP Y
THON
What is a
loss
function?
L I N EA R C L A S S I F I ER S I N
Michael Gelbart
PYTHON
Instructor, The University of British
Col u mbia
Least squares: the squared
loss
scikit - learn ' s LinearRegression minimi zes a loss:
n
∑(
tru
e
ith
targ
et
val
ue
−
pre
dict
ed
ith
targ LINEAR CLASSIFIERS IN
Classification errors: the 0-1
loss
Squared loss not appropriate for classi fi cation problems
( more on this later).
A nat u ral loss for classi fi cation problem is the n u mber of
errors.
This is the 0-1 loss: it's 0 for a correct prediction and 1 for
an incorrect prediction .
LINEAR CLASSIFIERS IN
Minimizing a
loss
from scipy.optimize import minimize
minimize(np.square, 0).x
array([0.])
minimize(np.square, 2).x
array([-1.88846401e-08])
LINEAR CLASSIFIERS IN
Let's practice!
L I N EA R C L A S S I F I ER S I NP Y
THON
Loss
function
diagrams
L I N EA R C L A S S I F I ER S I
LINEAR CLASSIFIERS IN
0-1 loss
diagram
LINEAR CLASSIFIERS IN
Linear regression loss
diagram
LINEAR CLASSIFIERS IN
Logistic loss
diagram
LINEAR CLASSIFIERS IN
Hinge loss
diagram
LINEAR CLASSIFIERS IN
Hinge loss
diagram
LINEAR CLASSIFIERS IN
Let's practice!
L I N EA R C L A S S I F I ER S I NP Y
THON
Logistic
regression and
regularization
L I N EA R C L A S S I F I ER S I NP Y T H O N
LINEAR CLASSIFIERS IN
Regularized logistic
regression
LINEAR CLASSIFIERS IN
How does regularization affect training
accuracy?
lr_weak_reg = LogisticRegression(C=100)
lr_strong_reg = LogisticRegression(C=0.01)
lr_weak_reg.fit(X_train, y_train)
lr_strong_reg.fit(X_train, y_train)
lr_weak_reg.score(X_train, y_train)
lr_strong_reg.score(X_train, y_train)
1.0
0.92
LINEAR CLASSIFIERS IN
How does regularization affect test
acc urac y?
lr_weak_reg.score(X_test, y_test)
0.86
lr_strong_reg.score(X_test, y_test)
0.88
more reg u lari z ation : ( almost always) higher test acc u rac y
LINEAR CLASSIFIERS IN
L1 vs. L2
reg ulari
Lasso zation
= linear regression wit h L1
lr_L1 = LogisticRegression(penalty='l1')
lr_L2 = LogisticRegression() # penalty='l2'
by default
lr_L1.fit(X_train, y_train)
lr_L2.fit(X_train,
y_train)
plt.plot(lr_L1.coef_.flatten())
LINEAR CLASSIFIERS IN
L2 vs. L1
regularization
LINEAR CLASSIFIERS IN
Let's practice!
L I N EA R C L A S S I F I ER S I NP Y
THON
Logistic
regression and
probabilities
L I N EA R C L A S S I F I ER S I NP Y T H O N
model intercept : [-
0.64]
LINEAR CLASSIFIERS IN
Logistic regression
probabilities
Witho u t reg u lari z ation
( C = 108):
model intercept : [-
0.64]
LINEAR CLASSIFIERS IN
Logistic regression
probabilities
Witho u t reg u lari z ation With reg u lari z ation ( C =
( C = 108): 1):
LINEAR CLASSIFIERS IN
How are these probabilities
computed?
logistic regression predictions : sign of ra w model o u tp u t logistic
regression probabilities : "squashed" raw model o u tp u t
LINEAR CLASSIFIERS IN
Let's practice!
L I N EA R C L A S S I F I ER S I NP Y
THON
M ulti-class
logistic
regression
L I N EA R C L A S S I F I ER S I NP Y T H O N
lr2.fit(X, y==2)
lr2.decision_function(X)[0]
lr.fit(X, y)
6.124
lr.predict(X)[0]
LINEAR CLASSIFIERS IN
One-vs- "Multinomial" or "softmax":
rest:
fi t a binar y classifier for fi t a single classifier for all
each class classes
LINEAR CLASSIFIERS IN
Model coefficients for multi-
class
# one-vs-rest by default lr_mn =
lr_ovr = LogisticRegression() LogisticRegression( multi
_class="multinomial",
lr_ovr.fit(X,y) solver="lbfgs")
lr_mn.fit(X,y)
lr_ovr.coef_.shape lr_mn.coef_.shape
(3,13) (3,13)
lr_ovr.intercept_.shape lr_mn.intercept_.shape
(3,) (3,)
LINEAR CLASSIFIERS IN
Let's practice!
L I N EA R C L A S S I F I ER S I NP Y
THON
Support Vectors
LINEAR C L AS S I F I E R S I N P Y T
HON
transformed feature =
(original feature)2
transformed feature =
(original feature)2
transformed feature =
(original feature)2
linear_model.LogisticRegression
svm.LinearSVC and
svm.SVC
logreg = SGDClassifier(loss='log')
linsvm = SGDClassifier(loss='hinge')
→ Machine learning
→ → Supervised learning
→ → → Classiification
→ → → → Linear classiifiers (this course)