Created
June 27, 2019 10:48
-
-
Save unkcpz/5463ea676c58ba2f8054a3d3a93f962c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
# fitting equation: y = (a-d)/(1+(x/c)^b)+d | |
# genrate the data set | |
a,b,c,d = 1,2,3,4 | |
x = np.arange(0,10,0.01) | |
y = (a-d)/(1+(x/c)**b)+d + 0.1*np.random.random(len(x)) | |
fuc = lambda a,b,c,d,x:(a-d)/(1+(x/c)**b)+d | |
err_fuc = lambda a,b,c,d,x,y: np.linalg.norm(fuc(a,b,c,d,x) - y) | |
# initialize parameters | |
a,b,c,d = np.random.random((4,)) | |
error = err_fuc(a,b,c,d,x,y) | |
eta = 0.001 | |
for iter in range(1000): | |
eps = 1e-6 | |
par_a = (err_fuc(a+eps,b,c,d,x,y)-error)/eps | |
par_b = (err_fuc(a,b+eps,c,d,x,y)-error)/eps | |
par_c = (err_fuc(a,b,c+eps,d,x,y)-error)/eps | |
par_d = (err_fuc(a,b,c,d+eps,x,y)-error)/eps | |
a,b,c,d = a-eta*par_a,b-eta*par_b,c-eta*par_c,d-eta*par_d #gradient descent algorithm | |
error = err_fuc(a,b,c,d,x,y) | |
print(a,b,c,d) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment