-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbestFitSlope.py
71 lines (52 loc) · 2.03 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
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
from statistics import mean
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
import random
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)
def createDataset(howMany, variance, step=2, correlation=False):
val = 1
ys = []
for i in range(howMany):
y = val + random.randrange(-variance, variance)
ys.append(y)
if correlation and correlation == 'pos':
val+=step
elif correlation and correlation == 'neg':
val -= step
xs = [i for i in range(len(ys))]
return np.array(xs, dtype=np.float64), np.array(ys, dtype=np.float64)
#finding m for best fit slope and y for intercept in eq y = mx + b
def bestFitSlopeIntercept(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
def squaredErr(ysOrig, ysLine):
return sum((ysLine - ysOrig)**2)
def coefficientOfDetermination(ysOrig, ysLine):
yMeanLine = [mean(ysOrig) for y in ysOrig]
squaredErrRegr = squaredErr(ysOrig, ysLine)
squaredErrYMean = squaredErr(ysOrig, yMeanLine)
return 1 - (squaredErrRegr / squaredErrYMean)
xs,ys = createDataset(40, 40, 2, correlation = 'pos')
m,b = bestFitSlopeIntercept(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
rSquared = coefficientOfDetermination(ys, regressionLine)
print(rSquared)
plt.scatter(xs,ys)
plt.scatter(predictX,predictY, s=80 , 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
#want the r squared value to be pretty high ex 0.9
print("done")