-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinearFitting.py
More file actions
39 lines (32 loc) · 1.1 KB
/
Copy pathlinearFitting.py
File metadata and controls
39 lines (32 loc) · 1.1 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
import numpy as np
def linearFitting(_x: np, _y: np, xFrom:float=None, xTo:float=None):
x = _x.copy()
y = _y.copy()
if (xFrom != None) or (xTo != None):
fromIndex = None
toIndex = None
if xFrom != None:
for i, val in enumerate(x):
if val >= xFrom:
fromIndex = i
break
if xTo != None:
for i, val in enumerate(x):
if val > xTo:
toIndex = i-1
break
x = x[fromIndex:toIndex]
y = y[fromIndex:toIndex]
results = {}
coeffs = np.polyfit(x, y, 1)
# Polynomial Coefficients
results['polynomial'] = coeffs
# r-squared
p = np.poly1d(coeffs)
# fit values, and mean
yhat = p(x) # or [p(z) for z in x]
ybar = np.sum(y)/len(y) # or sum(y)/len(y)
ssreg = np.sum((yhat-ybar)**2) # or sum([ (yihat - ybar)**2 for yihat in yhat])
sstot = np.sum((y - ybar)**2) # or sum([ (yi - ybar)**2 for yi in y])
results['determination'] = ssreg / sstot
return results