-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbestFitSlope.py~
40 lines (28 loc) · 1.07 KB
/
bestFitSlope.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
from statistics import mean
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
style.use('fivethirtyeight')
#a regression line is just like a straight line (same as best fit line)
xs = np.array([1,2,3,4,5,6], dtype=np.float64)
ys = np.array([5,4,6,5,6,7], dtype=np.float64)
#finding m for best fit slope and y for intercept in eq y = mx + b
def best_fit_slopeIntercept(xs,ys):
m = ( (mean(xs) * mean(ys)) - mean(xs*ys) ) / ( mean(xs)**2 - mean(xs**2) )
b = mean(ys) - m*mean(xs)
return m,b
m,b = best_fit_slopeIntercept(xs,ys)
print(m,b)
#do mx+b for each x in xs
regressionLine = [(m*x)+b for x in xs]
#going to predict the y value where x = 8
predictX = 8
predictY = (m*predictX)+b
plt.scatter(xs,ys)
plt.scatter(predictX,predictY, color='red')
plt.plot(xs, regressionLine)
plt.show()
# using coefficient of determination or squared error to see how good our prediction is
#sqaured error is the dist between our point and our line squared
#squared since we want pos values and if point is above line then it would be a neg value
print("done")