Python 3.x 我正试图将一个自定义函数适配到一些一维直方图数据中,但我得到了-TypeError:只有size-1数组可以转换为Python标量

Python 3.x 我正试图将一个自定义函数适配到一些一维直方图数据中,但我得到了-TypeError:只有size-1数组可以转换为Python标量,python-3.x,curve-fitting,Python 3.x,Curve Fitting,我想使用Python3中的curve_fit函数将函数拟合到分子动力学模拟中的一些数据。原始数据是一个40000行的列,指定了水中11个残基多肽(btw)的端到端距离。我想拟合这个数据子集的直方图。这个问题被张贴在论坛的其他地方,我试图在那里实现这些想法,但我一直没能让它起作用。以下是代码和一些输出: #take subset from 40000 frames ppII_etoe_wt = np.take(etoedist_wt_np, loc_ppII_wt) print(len(ppII_

我想使用Python3中的curve_fit函数将函数拟合到分子动力学模拟中的一些数据。原始数据是一个40000行的列,指定了水中11个残基多肽(btw)的端到端距离。我想拟合这个数据子集的直方图。这个问题被张贴在论坛的其他地方,我试图在那里实现这些想法,但我一直没能让它起作用。以下是代码和一些输出:

#take subset from 40000 frames
ppII_etoe_wt = np.take(etoedist_wt_np, loc_ppII_wt)
print(len(ppII_etoe_wt))
print(ppII_etoe_wt[:5])

1495
[[16.273 ]
 [27.0817]
 [30.5083]
 [26.2768]
 [17.6271]]

#make histogram bins and histograma 
binedges_1D = np.arange(4,33.01,0.2)
bincenters = 0.5*(binedges_1D[1:]+binedges_1D[:-1])  

ppII_etoe_wt_hist = np.histogram(ppII_etoe_wt, binedges_1D)

#fitting function
def fit_function(x, C, t):
    return C*(float(x**2)/(1-x**2)**9.0/2) * np.exp(-0.75*t/(1-x**2))

#convert histogram to numpy array
ppII_etoe_wt_hist = np.array(ppII_etoe_wt_hist[0])

print(np.shape(ppII_etoe_wt_hist))
print(type(ppII_etoe_wt_hist))
print(type(bincenters))
print(np.shape(bincenters))

(145,)
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
(145,)

#attempt to fit function
popt = curve_fit(fit_function, bincenters, ppII_etoe_wt_hist)
print(popt)

TypeError: only size-1 arrays can be converted to Python scalarsTh
#从40000帧中获取子集
ppII_etoe_wt=np.取(etoedist_wt_np,loc_ppII_wt)
印刷品(透镜(ppII_etoe_wt))
打印(ppII_etoe_wt[:5])
1495
[[16.273 ]
[27.0817]
[30.5083]
[26.2768]
[17.6271]]
#制作直方图箱和直方图
binedges_1D=np.arange(4,33.01,0.2)
bincenters=0.5*(binedges_1D[1::+binedges_1D[:-1])
ppII_etoe_wt_hist=np.直方图(ppII_etoe_wt,binedges_1D)
#拟合函数
def fit_功能(x、C、t):
返回C*(浮动(x**2)/(1-x**2)**9.0/2)*np.exp(-0.75*t/(1-x**2))
#将直方图转换为numpy数组
ppII_etoe_wt_hist=np.数组(ppII_etoe_wt_hist[0])
印刷品(np.形状(ppII_etoe_wt_hist))
打印(类型(ppII_etoe_wt_hist))
打印(类型(中心))
打印(np.形状(双中心))
(145,)
(145,)
#尝试拟合函数
popt=曲线拟合(拟合函数、双中心、ppII-etoe-wt-hist)
打印(popt)
TypeError:只有大小为1的数组才能转换为Python scalarsTh

如果可以,请链接到一个数据子集,例如,该子集包含每100个数据点(总共400个点),这将非常有用。