-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear_regression.py
More file actions
61 lines (41 loc) · 1.29 KB
/
linear_regression.py
File metadata and controls
61 lines (41 loc) · 1.29 KB
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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import random
def linear_regression(x, y):
# Average for X & Y
mean_x, mean_y = np.mean(x), np.mean(y)
total_xy = np.sum((x-mean_x)*(y-mean_y))
total_xx = np.sum(x*(x-mean_x))
slope = total_xy / total_xx
b = mean_y - (slope*mean_x)
return (slope, b)
def plot_regression(x, y, b):
plt.scatter(x, y, color="blue", marker="o", s=30)
y_pred = b[0]*x + b[1]
plt.plot(x, y_pred, lw=4, c='green')
plt.xlabel('X-Independent_variable SAT Exam score')
plt.ylabel('Y-Dependent_variable GPA Grade point average')
plt.show()
def main():
# Dataset
data = pd.read_csv('sample_linear_regression.csv')
data.describe()
x = data['SAT']
y = data['GPA']
# Plot Dataset
plt.scatter(x, y)
plt.xlabel('SAT')
plt.ylabel('GPA')
plt.show()
b = linear_regression(x, y)
print("Values from linear regression slope = {} and y = {}"
.format(b[0], b[1]))
# Predictions
sample_sat = x[random.randint(0, 84)]
gpa_prediction = b[0]*sample_sat + b[1]
print("Prediction for a SAT score of {} it will receive a GPA score of {}"
.format(sample_sat, gpa_prediction))
plot_regression(x, y, b)
if __name__ == '__main__':
main()