-
Notifications
You must be signed in to change notification settings - Fork 0
/
sleep_and_scores.py
57 lines (39 loc) · 1.51 KB
/
sleep_and_scores.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
#
#
# 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)
# Then normalize the lists by subtracting the mean value from each entry
normalized_sleep = sleep - avg_sleep
normalized_scores = scores - avg_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.
#slope = np.sum(normalized_sleep * normalized_scores) / sum(normalized_sleep * normalized_sleep)
slope = sum(normalized_sleep*normalized_scores) / sum(normalized_sleep * normalized_sleep)
#(np.sum(scores * normalized_sleep)) / np.sum(np.square(normalized_sleep))
print (slope)
# Finally, We have a linear function of the form
# y - avg_y = slope * ( x - avg_x )
# y = slope * ( x - avg_x ) + avg_score
# Rewrite this function in the form
# y = m * x + b
# Then return the values m, b
m = slope
b = avg_scores - (slope * avg_sleep)
return m,b
if __name__=="__main__":
m,b = compute_regression(sleep,scores)
print "Your linear model is y={}*x+{}".format(m,b)