我想在python中生成数据,就好像它是一些实验点。我希望噪声指数减少,噪声和正常分布的误差。像这张图片,但是指数:noisy polynomial data。 如果我只是采用指数曲线并为其添加一些高斯噪声并且还会产生像这样的随机误差,那会没关系
import numpy as np
errors = np.random.normal(0,1,100)
或者它可以以更智能的方式完成?
答案 0 :(得分:0)
这是问题的解决方案。我不知道这是否正确,但我仍然这样做:
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()