#### Is this the correct way of fitting data generated from gaussian distributions in python?

``````import random
import matplotlib.pyplot as plt
import numpy as np

def rSquared(obs, predicted):
error = ((predicted - obs)**2).sum()
mean = error/len(obs)
return 1 - (mean/np.var(obs))

def generateData(a, b, c, d, e, xvals):
for x in xvals:
calcVal= a*x**4 + b*x**3 + c*x**2 + d*x + e
yvals.append(calcVal+ random.gauss(0, 35))
xvals = np.arange(-10, 11, 1)
yvals= []
a, b, c, d, e = 3, 1, 3, 4, 5
generateData(a, b, c, d, e, xvals)
for i in range (5):
model= np.polyfit(xvals, yvals, i)
estYvals = np.polyval(model, xvals)
print('R-Squared:', rSquared(yvals, estYvals))
plt.plot(xvals, yvals, 'r', label = 'Actual values')

plt.plot(xvals, estYvals, 'bo', label = 'Predicted values')
plt.xlabel('Variable x values')
plt.ylabel('Calculated Value of Polynomial')
plt.legend()
plt.show()
``````

1 个回复

``````model = np.polyfit(xvals, yvals, i) # i=4 to get the perfect fit to values with R-Square of 4 : 0.9999995005089268.
``````
1 如何从高斯分布中制作戒指

2 从高斯分布采样

3 如何在Python中拟合双高斯分布？

2015-10-16 20:10:55 1 6875   gaussian
4 将逆高斯分布拟合到R中的数据

2018-10-01 05:34:51 1 491   r/ mle
5 将输入数据拟合为高斯分布

7 如何在R中的这2个场景中生成高斯分布的数据？

8 如何基于包含python中的cartisian坐标的数据集生成高斯分布式噪声

2014-03-13 13:29:30 1 1451   gaussian
9 在 Python 中使用多个高斯分布拟合数据

10 将多元高斯分布拟合到给定的数据集