Assigment Regression
Assigment Regression
Please install scikit learn and matplotlib. We will use scikit learn for data analysis and matplotlib for
plotting. Pandas will be used for reading the data.
1 Linear Regression
Regression analysis is a conceptually simple method for investigating functional relationships among
variables. These variables are known as dependent/response variable (x and independent/predictor
variable (y).
y = a1 x1 + a2 x2 + ................ + an xn + b (1)
Pn
xi yi − ni=1 xi ni=1 yi
P P
n
a= i=1
(2)
n ni=1 xi 2 − ( ni=1 xi )2
P P
Pn Pn
i=1 yi − a i=1 xi
b= (3)
n
Please consider a dataset of marks distribution of a class as given below;
1
Listing 1: marks_btech.dat
The least square regression line for the set of n data points is given by y = ax + b
2
Listing 2: linear_regression
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import linear_model
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
df= pd.read_table("marks_Btech.dat", sep="\s+")
X=df.iloc[:,1:2].values
print X
y=df.iloc[:,7].values
print y
print X_train
print X_test
print y_train
print y_test
reg = linear_model.LinearRegression()
reg.fit(X_train,y_train)
print reg.coef_
print reg.intercept_
y_pred= reg.predict(X_test)
y = a1 x1 + a2 x2 + ... + an xn + b (4)
Suppose, we want to calculate the linear regression between the marks scored in several/rest of the
subjects and computer science. We will perform multi variable linear regression;
3
Listing 3: Multi_linear
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import linear_model
from sklearn.model_selection import train_test_split
df= pd.read_table("marks_Btech.dat", sep="\s+")
X=df.iloc[:,1:-1].values
print X, len(X)
y=df.iloc[:,7].values
print y, len(y)
reg = linear_model.LinearRegression()
reg.fit(X_train,y_train)
print reg.coef_
y_pred= reg.predict(X_test)
print y_test
print reg.intercept_
print y_pred
2 Polynomial regression
y = a0 + a1 x + a2 x2 + ... + an xn + b (5)
The data consist of n observations of the dependent variable y on independent variable x.
In the program given below, we use polynomial regression of order 1 to 6 to study the regression
between marks scored by the students in maths and computer science.
4
Figure 2: Linear regression
Listing 4: Poly_regression
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import linear_model
from sklearn.model_selection import train_test_split
df= pd.read_table("marks_Btech.dat", sep="\s+")
X=df.iloc[:,1:2].values
print X
y=df.iloc[:,7].values
print y
reg = linear_model.LinearRegression()
for i in range(1,6):
poly_reg= PolynomialFeatures(degree=i)
x_poly= poly_reg.fit_transform(X)
reg.fit(x_poly, y)
print('Degree of Equation:', i )
print('Coefficient:', reg.coef_ )
print('Intercept:', reg.intercept_ )
print('Accuracy Score', reg.score(poly_reg.fit_transform(X_test),y_test))
5
3 Assignment
• Please use matplotlib to plot the y_pred as function of x_test using polynomial regression of
order 1 to 6 and compare them in the same plot.
• In the program written above, we perform single variable polynomial regression. Please perform
multivariable polynomial regression to predict y, which is the marks scored in computer science
as a function of marks scored in all the subjects.
4.1 Ridge
Ridge regression addresses some of the problems of ordinary least squares by imposing a penalty on
the size of coefficients. The ridge coefficients minimize a penalized residual sum of squares,
Here, λ is a complexity parameter that controls the amount of shrinkage: the larger the value of λ, the
greater the amount of shrinkage and thus the coefficients become more robust to collinearity.
Listing 5: Ridge
6
Figure 3: Ridge and LASSO
4.2 LASSO
LASSO (Least Absolute Shrinkage Selector Operator), is a similar technique to ridge. LASSO selects
only some feature while reduces the coefficients of others to zero. This property is known as feature
selection and which is absent in case of ridge. In LASSO instead of adding squares of β, we will add
absolute value of β.
min(kY − X(β)k22 + λ kβk1 ) (7)
β
1. It is generally used when we have more number of features, because it automatically does feature
selection.
It is useful in some contexts due to its tendency to prefer solutions with fewer parameter values,
effectively reducing the number of variables upon which the given solution is dependent. For this
reason, the LASSO and its variants are fundamental to the field of compressed sensing.
Listing 6: Lasso
7
4.3 ENet
Elastic net regression is basically a hybrid of ridge and lasso regression. This combination allows for
learning a sparse model, where few of the weights are non-zero like LASSO, while still maintaining
the regularization (keeping the same number of features, but reduce the magnitude of the coefficients)
properties of ridge.
Listing 7: ENet
enet=ElasticNet(alpha=3.0, fit_intercept=True)
enet.fit(X_train, y_train)
print enet.coef_
y_pred = enet.predict(X_test)
print y_test, y_pred
print enet.intercept_
8
Listing 8: Multi_linear
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import linear_model
from sklearn.model_selection import train_test_split
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
df= pd.read_table("marks_Btech.dat", sep="\s+")
X=df.iloc[:,1:-1].values
print X, len(X)
y=df.iloc[:,7].values
print y, len(y)
print X.shape
reg = linear_model.LinearRegression()
reg.fit(X_train,y_train)
print reg.coef_
y_pred= reg.predict(X_test)
print y_test
print reg.intercept_
print y_pred
5 Assignment
• Please also go through (i) support vector machine regression (ii) Kernel Ridge regression.