Regression Analysis
Regression Analysis
robust
the
actual against
" The goal is to find the best-fit line (or hyperplane in higher The Normal Equation:
Linear dimensions) that is, the value of 0, that minimizes the error Linear
Regression between predicted and actual values.
ê= (xX)-xy
Regression "@ is the value of 0 that minimizes the cost function
"The cost function for Linear Regression is the MSE:
" yisthe vector of targetvalues containing y) toy(m)
[=1
# Generate Linear like data to test equation (1)
where hg is the linear regression hypothesis on a training set X.
" The Normal Equation: import numpy as np
" The value of that minimizes MSE can be obtained using a
closed formsolution. np.random.seed(1)
" Closed form solution: A mathematical equation that directly m= 100 #number of instances
gives results X= 2*np.random.rand(m, 1) # column vector
y= 4 +3*X+ np, Random.randn(m, 1) # column vector
Linear
Regression 14
Linear Computing ô
Regression from
sklearn.preprocessing import
X_b= add_dummy_feature(X) #addadd_dummy_feature
x, to each instance
10
theta_best = np.linalg.inv(X_b.T @X_b) @x_b.T @y
y8 "The actual function used
togenerate the data is y =4 +3x, +
Gaussian noise.
" Computed value of
>» theta_best
array([[ 4.21509616],
0.5 l0 2.0 [2.770113391]1)
" Actual values: 0 = 4and 01 = 3
01 = 2.77.
instead of 00 = 4.215 and
Example:
array([[4.21509], [9.755322]]) 8
Polynomial >>> from sklearn.preprocessing import Polynomial Features # Fit a LinearRegression model to this extended
Regression >» poly_features = PolynomialFeatures(degree=2, include_bias=False) Polynomial training data
>>> lin_reg = LinearRegression()
>>> X poly = poly_features.fit _transform(X) Regression
>>> lin_reg.fit(X_poly, y)
>>> X[o]
>>> lin_reg.intercept_, lin_reg.coef
array([-0.75275929]) (array([ 1.78134581]), array(tt 0.93366893, 0.56456263]))
>>> X_poly[o] 16
2
" PolynomialFeatures adds all combinations of features up to the The
Polynomial given degree.
10,
300
Regression "Example: For two features, say a and b, PolynomialFeatures with
degree=3 would add the features a, a³, b², b, and the
learning_ 1
Learning once.
2.0
traln
val Regularized
Linear
fit the training data
too well but fail to generalize to unseen data.
" Apenalty term is added to the loss function being
optimized.
" This penalty term imposes
1 t
15 Models constraints on the model's
parameters, encouraging simpler models and reducing
risk of overfitting. the
degre e 1.0 LASSO Types of Regularization:
polynomial 0.5 - Regression 1. L1 Regularization - Least Absolute Shrinkage and Selection
Operator Regression (Lasso): A regularized version of Linear
0.04m Regression
The model is overfitted
10 20 30 40 50 60 70 80 Adds the sum of the absolute values of the model
How to improve an overfitted Training set size parameters (llwlli) to the cost function.
model?
1. Input more training data until the
validation error reaches the training error. J(e) = MSE(0) + 2ail
3.0
a=0
a=0.1
3.5 a=0
a=le-07
sparse model.
Linear Linear 2.5
3.04
a=l
>>> from sklearn.linear_model import Lasso 2.5
Models >>> lasso_reg = Lasso(alpha=0.1) Models- 2.0
0.0
array([1.53788]) 0.0 0.5 1.0 1.5 2,.0 2.5 3.0 0.0 o.5 1.0 1.5 2.0 5 3.0
X1 X1
Regression "
performing feature effectively by the model, thus LASSO input variables. relevant
How Does
Regularization
" The penalty Encourage Sparsity? Regression
Reducing Overfitting: By ignoring unnecessary features,
the model becomes less complex, which helps prevent
makes the
values, resulting in some model prefer smaller
parameter values parameter
overfitting.
" Example: If a feature isn't very usefulbecoming zero.
predictions, L1 regularization will penalize itsforassociated
making
parameter so much that the
feature by setting its weight tomodel
zero.
decides to "drop" that
sklearn.Ridge(alpha=0.
linear_model1,import Ridge 3.5 a=0
a=10
Models: Y2.0
2.5 -
1.0
2.0
L5
0.0 0.0
0.0 0.5 l.0 1.s 20 2.5 3.0o.0 0.5 1.0 15 2.0 2.5 3.0
X1 X
that logistic,
predictions as follows:
outputs denoted
a o(-), is a
sigmoid function (i.e., if p<0.s
number between 0 and 1. S-shaped) l1 if p2 0.5
The Logistic Function: o(t)= 1 Logistic Regression Model Training and computing Cost Function:
1+exp(-t) " Objective of the training: Estimate the parameter vector so
that the model estimates high probabilities for positive instancas
1.00
(y=1) and low probabilities for negative instances (y=0).
0.75 o(t) =
0.50
" The cost function for a single training instance to obtain above is
given as:
($) if y=1
0.25
0.00
c(0) =log(1-) ify= 0
-10.0 -7.5 -5.0 -2.5 0.0 2.5 5.0 20.0
t
What Mmm
Regularjzed
Logistic Linea
Regression Models