我正在使用scipy的curve_fit将函数拟合到一些数据,并收到以下错误;
Cannot cast array data from dtype('O') to dtype('float64') according to the rule 'safe'
Run Code Online (Sandbox Code Playgroud)
这将我指向代码中的这一行;
popt_r, pcov = curve_fit(
self.rightFunc, np.array(wavelength)[beg:end][edgeIndex+30:],
np.dstack(transmitted[:,:,c][edgeIndex+30:])[0][0],
p0=[self.m_right, self.a_right])
Run Code Online (Sandbox Code Playgroud)
rightFunc定义如下;
def rightFunc(self, x, m, const):
return np.exp(-(m*x + const))
Run Code Online (Sandbox Code Playgroud)
据我了解,“ O”类型是指python对象,但我看不到是什么导致了此错误。
完成错误:
为了深入探讨此问题,我有什么想法需要调查?
我编写了一个计算冥王星轨道并设置动画的程序,并开始使用类重写它,因为这似乎是将更多行星引入模拟的明智方法。即有一个定义物理的类,然后输入特定的行星数据以获得轨道数据。
class Planet(object):
m_sun = 1.989*(10**30)
G = 6.67*(10**-11)
dt = 1
coords = []
def __init__(self, x, y, vx, vy, m):
self.x = x
self.y = y
self.vx = vx
self.vy = vy
self.m = m
def genData(self):
while self.dt < 100000000:
r = ((self.x)**2 + (self.y)**2)**0.5
a = ((self.G*self.m_sun)/r**2)
ax = -a*((self.x)/r)
ay = -a*((self.y)/r)
self.vx = self.vx + ax*self.dt
self.vy = self.vy + ay*self.dt
self.x = self.x + self.vx*self.dt
self.y = self.y + self.vy*self.dt
coord = (self.x, …Run Code Online (Sandbox Code Playgroud)