Introduction To Machine Learning Algorithms: Linear Regression
Introduction To Machine Learning Algorithms: Linear Regression
Linear Regression
Cost Function
The cost function helps us to %gure out the best possible values
for a_0 and a_1 which would provide the best %t line for the
data points. Since we want the best values for a_0 and a_1, we
convert this search problem into a minimization problem where
we would like to minimize the error between the predicted
value and the actual value.
Gradient Descent
The next important concept needed to understand linear
regression is gradient descent. Gradient descent is a method of
updating a_0 and a_1 to reduce the cost function(MSE). The
idea is that we start with some values for a_0 and a_1 and then
we change these values iteratively to reduce the cost. Gradient
descent helps us on how to change the values.
Gradient Descent
The partial derivates are the gradients and they are used to
update the values of a_0 and a_1. Alpha is the learning rate
which is a hyperparameter that you must specify. A smaller
learning rate could get you closer to the minima but takes more
time to reach the minima, a larger learning rate converges
sooner but there is a chance that you could overshoot the
minima.
Code
Let’s get to the code. We have two choices, we can either use the
scikit learn library to import the linear regression model and
use it directly or we can write our own regression model based
on the equations above. Instead of choosing one among the
two, let’s do both :)
Let’s start with the easiest of the two methods, i.e using scikit
learn library to build our linear regression model.
❤
1
import pandas as pd
2
import numpy as np
3
4
df_train = pd.read_csv('/Users/rohith/Documents/Datasets/Linea
5
df_test = pd.read_csv('/Users/rohith/Documents/Datasets/Linear
6
7
x_train = df_train['x']
8
y_train = df_train['y']
9
x_test = df_test['x']
10
y_test = df_test['y']
11
12
x_train = np.array(x_train)
13
y_train = np.array(y_train)
14
x_test = np.array(x_test)
15
y_test = np.array(y_test)
16
17
x_train = x_train.reshape(-1,1)
18
x_test = x_test.reshape(-1,1)
view raw
LR_SK_1.py hosted with by GitHub
❤
1
from sklearn.linear_model import LinearRegression
2
from sklearn.metrics import r2_score
3
4
clf = LinearRegression(normalize=True)
5
clf.fit(x_train,y_train)
6
y_pred = clf.predict(x_test)
7
print(r2_score(y_test,y_pred))
view raw
LR_SK_2.py hosted with by GitHub
Now, let’s build our own linear regression model from the
equations above. We will be using only numpy library for the
computations and the R2 score for metrics.
❤
1
## Linear Regression
2
import numpy as np
3
4
n = 700
5
alpha = 0.0001
6
7
a_0 = np.zeros((n,1))
8
a_1 = np.zeros((n,1))
9
10
epochs = 0
11
while(epochs < 1000):
12
y = a_0 + a_1 * x_train
13
error = y - y_train
14
mean_sq_er = np.sum(error**2)
15
mean_sq_er = mean_sq_er/n
16
a_0 = a_0 - alpha * 2 * np.sum(error)/n
17
a_1 = a_1 - alpha * 2 * np.sum(error * x_train)/n
18
epochs += 1
19
if(epochs%10 == 0):
20
print(mean_sq_er)
view raw
LR_NP_1.py hosted with by GitHub
We initialize the value 0.0 for a_0 and a_1. For 1000 epochs we
calculate the cost, and using the cost we calculate the gradients,
and using the gradients we update the values of a_0 and a_1.
After 1000 epochs, we would’ve obtained the best values for
a_0 and a_1 and hence, we can formulate the best %t straight
line.
❤
1
import matplotlib.pyplot as plt
2
3
y_prediction = a_0 + a_1 * x_test
4
print('R2 Score:',r2_score(y_test,y_prediction))
5
6
y_plot = []
7
for i in range(100):
8
y_plot.append(a_0 + a_1 * i)
9
plt.figure(figsize=(10,10))
10
plt.scatter(x_test,y_test,color='red',label='GT')
11
plt.plot(range(len(y_plot)),y_plot,color='black',label =
12
plt.legend()
13
plt.show()
view raw
LR_NP_2.py hosted with by GitHub