我一直在尝试编写代码来计算高斯函数的卡方分布,并且遇到了讨厌的类型错误,我不知道该如何克服。我可以解决类型错误的解决方案吗?我试图通过访问数组的每个元素并将这些元素转换为有问题的其他数据类型来避免这种情况,但这似乎无济于事,我还尝试使用非numpy数组来完成这项工作。>
def make_gauss(N = random.random(), sigma = random.random(), mu = random.random()):
return (lambda x: N/(sigma * (2*numpy.pi)**.5) *
numpy.e ** (-(x-mu)**2/(2 * sigma**2)))
def gauss_chi2_generate(gauss_observed_values = [], gauss_expected_values = []):
""" Computes the Gaussian for Chi2"""
############################################################################
t_s = 0
t_g_s = 0
gauss_observed_values.append(make_gauss())
gauss_expected_values.append(make_gauss())
g_o_v = np.array(gauss_observed_values)
g_e_v = np.array(gauss_expected_values)
z_e_o_v = zip(g_o_v, g_e_v)
#############################################################################
for i in range(0,12):
""" Equation for Chi-Squared calculation"""
t_g_s+=((g_o_v[i])-(g_e_v[i]))**2/ g_e_v[i]
print("Observed Values ", g_o_v)
print("Expected Values" , g_e_v)
df=len(g_o_v)-1
print("Our goodness of fit for our linear function", stats.chi2.cdf(t_g_s,df))
return t_g_s
输出:
Traceback (most recent call last):
File "ChiSquaredFixed.py", line 82, in <module>
gauss_chi2_generate()
File "ChiSquaredFixed.py", line 67, in gauss_chi2_generate
t_g_s+=((g_o_v[i])-(g_e_v[i]))**2/ g_e_v[i]
TypeError: unsupported operand type(s) for -: 'function' and 'function'