-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathnonlinear_regression.py
More file actions
289 lines (227 loc) · 8.22 KB
/
nonlinear_regression.py
File metadata and controls
289 lines (227 loc) · 8.22 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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
from linear_algebra import LinearAlgebra
from gauss_newton import GaussNewton
class NonLinearRegression(object):
def __init__(self, Xin, Yin):
self.Xin = Xin
self.Yin = Yin
def polynomial(self, order):
"""
:param order:
:return: list of coefficient - [B0, B1, B3 .. etc].
Length of coefficient list is same as order
"""
degree_of_x = order
sigma_X = []
sigma_XY = []
x_count = 1
coefficient_list = []
algebra_obj = LinearAlgebra(len(self.Xin))
while x_count <= degree_of_x * 2:
temp = 0
for x in self.Xin:
temp = temp + pow(float(x), x_count)
sigma_X.append(temp)
x_count += 1
y_count = 0
while y_count <= degree_of_x:
temp = 0
number_of_x = 0
for y in self.Yin:
temp = temp + float(y) * (pow(float(self.Xin[number_of_x]), y_count))
number_of_x = number_of_x + 1
sigma_XY.append(temp)
y_count += 1
arr = []
sigma_X.insert(0, float(len(self.Xin)))
for i in range(degree_of_x + 1):
temp = []
for j in range(degree_of_x + 1):
temp.append(sigma_X[j + i])
arr.append(temp)
N = degree_of_x + 1
algebra_obj.N = N
adj = []
inverse = []
for i in range(N):
adj.append([])
inverse.append([])
for j in range(N):
adj[i].append([])
inverse[i].append([])
sigma_XY_Transpose = []
for i in range(N):
sigma_XY_Transpose.append([])
for j in range(1):
sigma_XY_Transpose[i].append(0)
for i in range(N):
sigma_XY_Transpose[i][0] = sigma_XY[i]
print('\n Transpose of different sigma xy \n')
for transpose in sigma_XY_Transpose:
print(transpose)
print('\n')
print('\n Array after finding and putting x and y values in equations \n')
for a in arr:
print(a)
print('\n')
algebra_obj.get_adjoint(arr, adj)
print('\n Array Adjoint \n')
for adjoint in adj:
print(adjoint)
print('\n')
algebra_obj.get_matrix_inverse(arr, inverse)
print('\n Inverse of Array after finding x and y values \n')
for inv in inverse:
print(inv)
print('\n')
for i in range(N):
coefficient_list.append([])
for j in range(1):
coefficient_list[i].append(0)
for i in range(len(inverse)):
for j in range(len(sigma_XY_Transpose[0])):
for k in range(len(sigma_XY_Transpose)):
coefficient_list[i][j] += inverse[i][k] * sigma_XY_Transpose[k][j]
# clean up the coefficient list
return [coefficient[0] for coefficient in coefficient_list]
#return coefficient_list
def sinusoidal(self, order):
"""
:param order:
:return: list of coefficient - [B0, B1, B3 .. etc].
Length of coefficient list is same as order
"""
# check the order
if order != 4:
return 'Permissible valued of order for sinusoidal model is 4'
from sympy import symbols,sin
symbol_list = []
init_coeff = []
for i in range(order):
symbol_list.append(symbols('m{}'.format(i), real=True))
# initialized the coefficient to default 1.0.
for i in range(order):
init_coeff.append(1.0)
x = symbols('x', real=True)
gauss_obj = GaussNewton(order, self.Xin, self.Yin)
model = symbol_list[0] * sin(symbol_list[1] * x + symbol_list[2]) + symbol_list[3]
coefficient = gauss_obj.gauss_newton_method(init_coeff, model, symbol_list, x)
return coefficient
def exponential(self, order):
"""
:param order:
:return: list of coefficient - [B0, B1, B3 .. etc].
Length of coefficient list is same as order
"""
# check the order
if order != 2:
return 'Permissible valued of order for sinusoidal model is 2'
from sympy import symbols, exp
symbol_list = []
init_coeff = []
for i in range(order):
symbol_list.append(symbols('m{}'.format(i), real=True))
# initialized the coefficient to default 1.0.
for i in range(order):
init_coeff.append(1.0)
x = symbols('x', real=True)
gauss_obj = GaussNewton(order, self.Xin, self.Yin)
model = symbol_list[0] * (1.0 - exp(-symbol_list[1] * x))
coefficient = gauss_obj.gauss_newton_method(init_coeff, model, symbol_list, x)
return coefficient
def polynomial_using_gauss_newton(self, order):
from sympy import symbols
symbol_list = []
init_coeff = []
for i in range(order):
symbol_list.append(symbols('m{}'.format(i), real=True))
# initialized the coefficient to default 1.0.
for i in range(order):
init_coeff.append(1.0)
x = symbols('x', real=True)
gauss_obj = GaussNewton(order, self.Xin, self.Yin)
model = symbol_list[0]
for order in range(1, order):
model = model + symbol_list[order] * x ** order
coefficient = gauss_obj.gauss_newton_method(init_coeff, model, symbol_list, x)
return coefficient
def power(self, order):
"""
:param order:
:return: list of coefficient - [B0, B1, B3 .. etc].
Length of coefficient list is same as order
"""
pass
def exponentialTransformation(self, order):
"""
:param order:
:return: list of coefficient - [B0, B1].
Length of coefficient list is same as order
"""
print('Inside exp')
# check the order
if order != 1:
return 'Permissible value for order in exponentialTransformation model is 1'
# below method returns number after decimal point
def num_after_point(a): #a is float or string value
s = str(a)
if not '.' in s:
return 0
return len(s) - s.index('.') - 1
import numpy
#import linearRegression
num_after_decimal = num_after_point(self.Yin[0])
#Convert Yin into LogYin with round upto decimal as input values
lnYin = [round(numpy.log(float(y)),num_after_decimal) for y in self.Yin]
#round Xin values upto decimal same as input values
round_Xin = [round(float(x),num_after_decimal) for x in self.Xin]
#Call linearRegression function here--------------
self.Xin = round_Xin
self.Yin = lnYin
coefficient1 = self.polynomial(1)
coefficient_list = []
i = 0
for coeff in coefficient1:
if i == 0:
#convert B0 to original form
coefficient_list.append(round(numpy.exp(float(coeff)),num_after_decimal))
i += 1
else:
coefficient_list.append(round(float(coeff),num_after_decimal))
return coefficient_list
#Nonlinear to linear transformation using scaling factor
#For scaling negative data points in the dataset (Xin,Yin)
def findMin(inp):
min = 0
for i in inp:
if i < min and i < 0:
min = i
return min
def scaleFactor(Xin,Yin):
if len(x)==len(y):
xMin = findMin(Xin)
yMin = findMin(Yin)
k = xMin
if yMin<xMin:
k = yMin
return mod_number(k)
return 0
def mod_number(x):
if x < 0:
strVal = str(x)
substrVal = strVal[1:]
return int(substrVal)
return 0
def applyScaleFactorToDataPoints():
k = scaleFactor(x,y)
#print('Scale Factor:',k)
if k > 0:
i=0;
while i < len(x):
self.Xin[i]+=k
i+=1
#print(self.Xin)
i=0
while i < len(y):
self.Yin[i]+=k
i+=1
#print(self.Yin)