-
Notifications
You must be signed in to change notification settings - Fork 0
/
sleep_grades_regression.py
70 lines (50 loc) · 1.94 KB
/
sleep_grades_regression.py
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
#
#
# Regression and Classification programming exercises
#
#
#
# In this exercise we will be taking a small data set and computing a linear function
# that fits it, by hand.
#
# the data set
import numpy as np
sleep = [5,6,7,8,10]
scores = [65,51,75,75,86]
def compute_regression(sleep,scores):
# First, compute the average amount of each list
avg_sleep = np.mean(sleep)
avg_scores = np.mean(scores)
print "avg_sleep ", avg_sleep
print "avg_scores ", avg_scores
# Then normalize the lists by subtracting the mean value from each entry
normalized_sleep = np.subtract(sleep, avg_sleep)
normalized_scores = np.subtract(scores, avg_scores)
print "normalized_sleep ", normalized_sleep
print "normalized_scores ", normalized_scores
# Compute the slope of the line by taking the sum over each student
# of the product of their normalized sleep times their normalized test score.
# Then divide this by the sum of squares of the normalized sleep times.
sleep_times_score = np.multiply(normalized_sleep, normalized_scores)
print "sleep_times_score ", sleep_times_score
sleep_squares = np.square(normalized_sleep)
print"sleep_squares ", sleep_squares
sum_sleep_times_score = np.sum(sleep_times_score)
sum_sleep_squares = np.sum(sleep_squares)
slope = sum_sleep_times_score/sum_sleep_squares
print "slope ", slope
# Finally, We have a linear function of the form
# y - avg_y = slope * ( x - avg_x )
# Rewrite this function in the form
# y = m * x + b
# Then return the values m, b
m = slope
b = avg_scores - (slope * avg_sleep)
print "b ", b
return m,b
if __name__=="__main__":
m,b = compute_regression(sleep,scores)
print "Your linear model is y={}*x+{}".format(m,b)
for i in np.arange(2, 6):
coeffs = np.polyfit(scores, sleep, i)
print "coeffs for degree {} : {}".format(i, coeffs)