-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linear Regression
98 lines (77 loc) · 2.39 KB
/
Linear Regression
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
'''Linear Regression (Using Logic)'''
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = (20.0,10.0)
# Reading Data
data = pd.read_excel('headbrain.xlsx')
# Collecting X and Y
X= data['Head Size'].values
Y= data['Brain Weight'].values
# Mean X and Y
mean_x = np.mean(X)
mean_y = np.mean(Y)
# Total number of values
n = len(X)
# Using the formula, calculate Slope and c
numer = 0
denom = 0
for i in range(n):
numer += ((X[i] - mean_x)*(Y[i] - mean_y))
denom += (X[i] - mean_x)**2
m = numer/denom
c = mean_y - (m * mean_x)
# Plotting values and Regression line
max_x = np.max(X) + 100
min_x = np.min(X) - 100
# Calculating line values of x and y
x = np.linspace(min_x, max_x, 1000)
y = m*x + c
# Plotting Line
plt.plot(x, y, color = '#58b970', label = 'Regression Line')
# Plotting Scatter Points
plt.scatter(X, Y, c = '#ef5423', label = 'Scatter Plot')
plt.xlabel('Head Size')
plt.ylabel('Brain Weight')
plt.legend()
plt.show()
# Calculation of R2 score
ss_t = 0
ss_r = 0
for i in range(n):
y_pred = m*X[i] + c
ss_t = (Y[i] - mean_y)**2
ss_r = (Y[i] - y_pred)**2
r2 = 1 - (ss_r/ss_t)
print(r2)
''' Linear Regression (Using Scikit Learn) '''
# Example 1
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
plt.rcParams['figure.figsize'] = (20.0,10.0)
# Reading Data
data = pd.read_excel('headbrain.xlsx')
# Collecting X and Y
X= data['Head Size'].values
Y= data['Brain Weight'].values
# Cannot use Rank 1 matrix in scikit learn
X = X.reshape((237,1))
# Creating Model
reg = LinearRegression()
# Fitting training Data
reg = reg.fit(X, Y)
# Y Prediction
Y_pred = reg.predict(X)
m = reg.coef_
# Plot outputs
plt.scatter(X, Y, color='black')
plt.plot(X, Y_pred, color='blue', linewidth=3)
plt.xticks(())
plt.yticks(())
plt.show()
# Calculating R2 Score
r2_score = reg.score(X, Y)
print(r2_score)