-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnonlinearreg.py
94 lines (78 loc) · 3.32 KB
/
nonlinearreg.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 28 20:29:56 2021
@author: deses
"""
import matplotlib.pyplot as plt
import numpy as np
import scipy
from sklearn.metrics import r2_score
def func1(x, a, b):
return a * np.log(x )+b
def func2(data,a,b):#alternative model
return np.exp(np.log(data)*a)*b
def logreg1(data, colors='#2CBDFE', graph=False):
# non linear regression using ordinary least squares from scipy
xData,yData= data[:,0],data[:,1]
popt, pcov = scipy.optimize.curve_fit(func1, xData, yData, maxfev=10000)#,p0=[0.1,0.1]
# print (np.sqrt(np.diag(pcov)))#standar deviation of the parameters
# print(popt)
x = np.linspace(1, max(xData),100000)
#calculating R2
y_pred = func1(xData, *popt)
# print('R-squared='+str(r2_score(yData, y_pred)))
milestones =[[1,268],[1,22]]
if graph:
plt.figure(5)
plt.scatter(xData, yData,c='#2CBDFE', label='Dev milestones', marker='o', alpha=(0.5))
plt.scatter( milestones[0],milestones[1],c='red', marker='o')
plt.plot(x, func1(x,popt[0],popt[1]),color=colors , label="Log Fit",alpha=(0.9))
plt.xlabel("Human (days after fertilization)")
plt.ylabel("Rat (days after fertilization)")
#plt.legend(loc='upper left')
plt.ylim(ymin=0)
plt.show()
return popt
def logreg2(data):
# non linear regression using ordinary least squares from scipy
xData,yData= data[:,0],data[:,1]
popt, pcov = scipy.optimize.curve_fit(func1, xData, yData, maxfev=10000)#,p0=[0.1,0.1]
#print (popt)
x = np.linspace(1, max(xData),100000)
#calculating R2
y_pred = func1(xData, *popt)
# print('R-squared='+str(r2_score(yData, y_pred)))
milestones =[[1,280],[1,22]]
array= np.stack((x, func1(x,popt[0],popt[1])) , axis=1)
plt.figure(1)
plt.scatter(xData, yData,c='#2CBDFE', label='Dev milestones', marker='o', alpha=(0.5))
plt.scatter( milestones[0],milestones[1], marker='o')
plt.plot(x, func1(x,popt[0],popt[1]), '#2CBDFE', label="Log Fit",alpha=(0.9))
plt.xlabel("Human (days after fertilization)")
plt.ylabel("Rat (days after fertilization)")
#plt.legend(loc='upper left')
plt.ylim(ymin=0)
plt.show()
return array
def logreg3alt(data, colors='#2CBDFE', graph=False):
# non linear regression using ordinary least squares from scipy
xData,yData= data[:,0],data[:,1]
popt, pcov = scipy.optimize.curve_fit(func2, xData, yData, maxfev=10000)#,p0=[0.1,0.1]
# print (np.sqrt(np.diag(pcov)))#standar deviation of the parameters
# print(popt)
x = np.linspace(1, max(xData),100000)
#calculating R2
y_pred = func2(xData, *popt)
# print('R-squared='+str(r2_score(yData, y_pred)))
milestones =[[1,280],[1,22]]
if graph:
plt.figure(9)
plt.scatter(xData, yData,c='#2CBDFE', label='Dev milestones', marker='o', alpha=(0.5))
plt.scatter( milestones[0],milestones[1],c='red', marker='o')
plt.plot(x, func2(x,popt[0],popt[1]),color=colors , label="Log Fit",alpha=(0.9))
plt.xlabel("Human (days after fertilization)")
plt.ylabel("Rat (days after fertilization)")
#plt.legend(loc='upper left')
plt.ylim(ymin=0)
plt.show()
return popt