-
Notifications
You must be signed in to change notification settings - Fork 0
/
NonLinearRegressionTest.py
57 lines (39 loc) · 1.31 KB
/
NonLinearRegressionTest.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
import numpy as np
from scipy.optimize import leastsq
def getData():
return [[-0.042780748663101636, -0.0040771571786609945, -0.00506567946276074],
[0.042780748663101636, -0.0044771571786609945, -0.10506567946276074],
[0.542780748663101636, -0.005771571786609945, 0.30506567946276074],
[-0.342780748663101636, -0.0304077157178660995, 0.90506567946276074]]
data = np.array(getData())
coefficient = data[:,0:2]
dependent = data[:,-1]
def model(p, x):
a,b,c = p
u = x[:,0]
v = x[:,1]
return(a*u**2 + b*v + c)
def residuals(p, y, x):
a,b,c = p
err = y - model(p,x)
return err
p0 = np.array([2, 3, 4]) #arbitary guess
p = leastsq(residuals, p0, args = (dependent, coefficient))[0]
def f(p,x):
return p[0]*x[0] + p[1]*x[1] + p[2]
def getPredictionPoints():
predictionPoints = []
for x in coefficient:
predicionPoint = (f(p,x))
predictionPoints.append(predicionPoint)
return predictionPoints
def getActualPoints():
return dependent
def getResiduals():
predictionPoints = getPredictionPoints()
actualPoints = getActualPoints()
residualPoints = []
for i in range(len(predictionPoints)):
print(predictionPoints[i])
residualPoints.append(predictionPoints[i] - actualPoints[i])
return residualPoints