Machine Learning: Practice 2
Machine Learning: Practice 2
Practice 2
April 7, 2021
1 Perceptron
2 Linear Regression
4 Logistic Regression
Problem
Using the perceptron model to classify the species of flowers (’setosa’ or
’versicolor’) based on the sepal and petal width.
w = [w0 , w1 , w2 ]
Net input function (z)
z = w0 + w1 ∗ x1 + w2 ∗ x2 = w T x
Unit step function (φ(·))
(
1 if z ≥ 0,
φ(z) =
−1 otherwise.
w = w + ∆w
∆wi = η ∗ (y − ŷ ) ∗ xi
where:
I η: learning rate
I y : the true class label
I ŷ : the predicted class label
Examples
∆w0 = η ∗ (y − ŷ )
∆w1 = η ∗ (y − ŷ ) ∗ x1
∆w2 = η ∗ (y − ŷ ) ∗ x2
Hyperparameters
eta → the learning rate
max iter → the maximum number of epochs
random state → to make the reproducible results
Parameters
w → the weights of model
errors → to store the error in each epoch
Methods
fit(X , y ) → to train the model
predict(X ) → to predict the output value
net input(X ) → to combine the features with the weights
class Peceptron :
def init (self, eta = 0.01, max iter = 20, random state = 1):
self.eta = eta
self.max iter = max iter
self.random state = random state
self.w = None
self.errors = [ ]
Hyperparameters
eta
max iter
random state
Parameters
coef
intercept
Methods
fit(X , y )
predict(X )
Quan Minh Phan & Ngoc Hoang Luong (University of Information
MachineTechnology
Learning (UIT)) April 7, 2021 13 / 74
Practice
>> ppn = Perceptron (eta = 0.001, max iter = 30, random state = 1)
ppn.fit(X train, y train)
1 Perceptron
2 Linear Regression
4 Logistic Regression
Cost function:
1 X (i)
J(w ) = (y − φ(z (i) ))2
2
i
∂J X (i)
=− (y (i) − φ(z (i) ))xj
∂wj
i
∂J X (i)
∆wj = −η =η (y (i) − φ(z (i) ))xj
∂wj
i
(
wj + η ∗ X T .dot((y − φ(z)) j ∈ [1, . . . , n]
wj =
wj + η ∗ sum(y − φ(z)) j =0
Normal Equation
from sklearn.linear model import LinearRegression
Parameters Methods
intercept fit(X, y)
coef predict(X)
Gradient Descent
w := w + ∆w
∆w = η i (y (i) − φ(z (i) )x i
P
Normal Equation
w = (X T X )−1 X T y
Gradient Descent
>> reg GD = LinearRegression GD(eta=0.001, max iter=20,
random state=1)
reg GD.fit(X train, y train)
Normal Equation
>> reg NE = LinearRegression()
reg NE.fit(X train, y train)
Example
X = [258.0, 270.0, 294.0, 320.0, 342.0, 368.0, 396.0, 446.0, 480.0, 586.0]
y = [236.4, 234.4, 252.8, 298.6, 314.2, 342.2, 360.8, 368.0, 391.2, 390.8]
Syntax
from sklearn.preprocessing import PolynomialFeatures
Linear regression
>> lr = LR()
lr.fit(X train, y train)
1 Perceptron
2 Linear Regression
4 Logistic Regression
w = w + ∆w
∆wi = η ∗ (y − ŷ ) ∗ xi
where:
I η: learning rate
I y : the true class label
I ŷ : the predicted class label
Examples
∆w0 = η ∗ (y − ŷ )
∆w1 = η ∗ (y − ŷ ) ∗ x1
∆w2 = η ∗ (y − ŷ ) ∗ x2
Hyperparameters
eta
max iter
random state
Parameters
w
costs
Methods
fit(X , y )
predict(X )
net input(X )
activation(X )
Quan Minh Phan & Ngoc Hoang Luong (University of Information
MachineTechnology
Learning (UIT)) April 7, 2021 65 / 74
Implement (code from scratch)
class Adaline:
def init (self, eta = 0.01, max iter = 50, random state = 1):
self.eta = eta
self.max iter = max iter
self.random state = random state
self.w = None
self.costs = [ ]
1 Perceptron
2 Linear Regression
4 Logistic Regression
Hyperparameters
eta
max iter
random state
Parameters
w
costs
Methods
fit(X , y )
predict(X )
net input(X )
activation(X )
Quan Minh Phan & Ngoc Hoang Luong (University of Information
MachineTechnology
Learning (UIT)) April 7, 2021 70 / 74
Implement (code from scratch)
class LogisticRegression:
def init (self, eta = 0.01, max iter = 50, random state = 1):
self.eta = eta
self.max iter = max iter
self.random state = random state
self.w = None
self.costs = [ ]
Syntax (import)
from sklearn.linear model import LogisticRegression
Examples
>> from sklearn.linear model import LogisticRegression as
LogisticRegression
>> clf LR lib = LogisticRegression (random state=1)
clf LR lib.fit(X train, y train)
>> y pred lib1 = clf LR lib.predict(X test)