Python 无法将数组数据从 dtype('O') 转换为 dtype('float64')
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39452792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot cast array data from dtype('O') to dtype('float64')
提问by jm22b
I am using scipy's curve_fit to fit a function to some data, and receive the following error;
我正在使用 scipy 的 curve_fit 将函数拟合到某些数据,并收到以下错误;
Cannot cast array data from dtype('O') to dtype('float64') according to the rule 'safe'
which points me to this line in my code;
这将我指向代码中的这一行;
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])
rightFunc is defined as follows;
rightFunc 定义如下;
def rightFunc(self, x, m, const):
return np.exp(-(m*x + const))
As I understand it, the 'O' type refers to a python object, but I can't see what is causing this error.
据我了解,'O' 类型指的是一个 python 对象,但我看不出是什么导致了这个错误。
Complete Error:
完全错误:
Any ideas for what I should investigate to get to the bottom of this?
关于我应该调查什么来深入了解这个问题的任何想法?
采纳答案by hpaulj
Typically these scipy
functions require parameters like:
通常,这些scipy
函数需要以下参数:
curvefit( function, initial_values, (aux_values,), ...)
where the tuple of aux_values
is passed through to your function
along with the current value of the main variable.
其中元组 of与主变量的当前值一起aux_values
传递给您function
。
Is the dstack
expression this aux_values
? Or a concatenation of several. It may need to be wrapped in a tuple
.
是dstack
表达这个aux_values
?或者几个的串联。它可能需要包装在tuple
.
(np.dstack(transmitted[:,:,c][edgeIndex+30:])[0][0],)
We may need to know exactly where this error arises, not just which line of your code does it. We need to know what value is being converted. Where is there an array with dtype object?
我们可能需要确切地知道此错误发生的位置,而不仅仅是代码的哪一行。我们需要知道正在转换什么值。哪里有一个带有 dtype 对象的数组?
回答by Adrine Correya
Just in case it could help someone else, I used numpy.array(wavelength,dtype='float64')
to force the conversion of objects in the list to numpy's float64. Works well for me.
以防万一它可以帮助其他人,我曾经numpy.array(wavelength,dtype='float64')
强制将列表中的对象转换为 numpy 的 float64。对我来说效果很好。
回答by MHO
Just to clarify, I had the same problem, did not see the right answers in the comments, before solving it on my own. So I just repeat them here:
澄清一下,我遇到了同样的问题,在我自己解决之前没有在评论中看到正确的答案。所以我在这里重复一遍:
I have resolved the issue. I was passing an array with one element to the p0 list, rather than the element itself. Thank you for your help – Jacobadtr Sep 12 at 17:51
An O dtype often results when constructing an array from a list of sublists that differ in size. If np.array(...) can't make a clean n-d array of numbers, it resorts to making an array of objects. – hpaulj Sep 12 at 17:15
我已经解决了这个问题。我将一个包含一个元素的数组传递给 p0 列表,而不是元素本身。感谢您的帮助 – Jacobadtr 9 月 12 日 17:51
当从大小不同的子列表列表构造数组时,经常会产生 O dtype。如果 np.array(...) 不能创建一个干净的 nd 数字数组,它会求助于创建一个对象数组。– hpaulj 9 月 12 日 17:15
That is, make sure that the tuple of parameters you pass to curve_fit can be properly casted to an numpy array
也就是说,确保您传递给 curve_fit 的参数元组可以正确地转换为 numpy 数组
回答by NickBraunagel
From here, apparently numpy struggles with index type. The proposed solution is:
从这里开始,显然 numpy 与索引类型斗争。建议的解决方案是:
One thing you can do is use np.intp as dtype whenever things have to do with indexing or are logically related to indexing/array sizes. This is the natural dtype for it and it will normally also be the fastest one.
您可以做的一件事是,只要与索引有关或与索引/数组大小在逻辑上相关,就将 np.intp 用作 dtype。这是它的自然 dtype,它通常也是最快的。
Does this help?
这有帮助吗?