在 python 拟合给定函数中生成随机数据
Generating random data in python fitting given function
我想在python中生成数据,就像是一些实验点一样。我想让噪声呈指数下降,噪声和误差呈正态分布。像这张图,但是指数:noisy polynomial data。
如果我简单地采用指数曲线并向其添加一些高斯噪声并产生这样的随机误差,是否可以?
import numpy as np
errors = np.random.normal(0,1,100)
或者可以用更智能的方式来完成?
这就是问题的解决方法。我不知道这样做是否正确,但我还是这样做了:
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as st
import random
from scipy.optimize import curve_fit
#number of data points
n = 50
#function
def func(data):
return 10*np.exp(-0.5*data)
def fit(data, a, b):
return a*np.exp(b*data)
#define interval
a = 0
b = 4
#generate random data grid
x = []
for i in range(0, n):
x.append(random.uniform(a, b))
x.sort()
#noise-free data points
yclean = []
for i in range(0, n):
yclean.append(func(x[i]))
#define mean, standard deviation, sample size for 0 noise and 1 errors
mu0 = 0
sigma0 = 0.4
mu1 = 0.5
sigma1 = 0.02
#generate noise
noise = st.norm.rvs(mu0, sigma0, size = n)
y = yclean + noise
yerr = st.norm.rvs(mu1, sigma1, size = n)
#now x and y is your data
#define analytic x and y
xan = np.linspace(a, b, n)
yan = []
for i in range(0, n):
yan.append(func(xan[i]))
#now estimate fit parameters
#initial guesses
x0 = [1.0, 1.0]
#popt are list of optimal coefficients, pcov is covariation matrix
popt, pcov = curve_fit(fit, x, y, x0, yerr)
fity = []
for i in range(0, n):
fity.append(fit(xan[i], *popt))
print 'function used to generate is 10 * exp( -0.5 * x )'
print 'fit function is', popt[0], '* exp(', popt[1], '* x )'
#plotting data and analytical function
plt.rc("figure", facecolor="w")
plt.rc('text', usetex=True)
plt.rc('font', family='serif',size = 16)
plt.title("Data", fontsize=20)
plt.errorbar(x, y, yerr, fmt='o')
plt.plot(xan, yan, 'r')
plt.plot(xan, fity, 'g')
plt.show()
我想在python中生成数据,就像是一些实验点一样。我想让噪声呈指数下降,噪声和误差呈正态分布。像这张图,但是指数:noisy polynomial data。 如果我简单地采用指数曲线并向其添加一些高斯噪声并产生这样的随机误差,是否可以?
import numpy as np
errors = np.random.normal(0,1,100)
或者可以用更智能的方式来完成?
这就是问题的解决方法。我不知道这样做是否正确,但我还是这样做了:
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as st
import random
from scipy.optimize import curve_fit
#number of data points
n = 50
#function
def func(data):
return 10*np.exp(-0.5*data)
def fit(data, a, b):
return a*np.exp(b*data)
#define interval
a = 0
b = 4
#generate random data grid
x = []
for i in range(0, n):
x.append(random.uniform(a, b))
x.sort()
#noise-free data points
yclean = []
for i in range(0, n):
yclean.append(func(x[i]))
#define mean, standard deviation, sample size for 0 noise and 1 errors
mu0 = 0
sigma0 = 0.4
mu1 = 0.5
sigma1 = 0.02
#generate noise
noise = st.norm.rvs(mu0, sigma0, size = n)
y = yclean + noise
yerr = st.norm.rvs(mu1, sigma1, size = n)
#now x and y is your data
#define analytic x and y
xan = np.linspace(a, b, n)
yan = []
for i in range(0, n):
yan.append(func(xan[i]))
#now estimate fit parameters
#initial guesses
x0 = [1.0, 1.0]
#popt are list of optimal coefficients, pcov is covariation matrix
popt, pcov = curve_fit(fit, x, y, x0, yerr)
fity = []
for i in range(0, n):
fity.append(fit(xan[i], *popt))
print 'function used to generate is 10 * exp( -0.5 * x )'
print 'fit function is', popt[0], '* exp(', popt[1], '* x )'
#plotting data and analytical function
plt.rc("figure", facecolor="w")
plt.rc('text', usetex=True)
plt.rc('font', family='serif',size = 16)
plt.title("Data", fontsize=20)
plt.errorbar(x, y, yerr, fmt='o')
plt.plot(xan, yan, 'r')
plt.plot(xan, fity, 'g')
plt.show()