简体   繁体   English

为什么我会收到 numpy.exp 的“ufunc 循环不支持 int 类型的参数 0”错误?

[英]Why do I get the 'loop of ufunc does not support argument 0 of type int' error for numpy.exp?

I have a dataframe and I'd like to perform exponential calculation on a subset of rows in a column.我有一个数据框,我想对列中的行子集执行指数计算。 I've tried three versions of code and two of them worked.我尝试了三个版本的代码,其中两个有效。 But I don't understand why one version gives me the error.但我不明白为什么一个版本会给我错误。

import numpy as np

I just saw your post and would like to answer.刚看到你的帖子,想回答一下。

I guess your problem occurs because some numpy functions require float type argument explicity, whereas your such use of the code as np.exp(test) puts int data into the argument.我猜您的问题发生是因为某些 numpy 函数需要显式地使用浮点类型参数,而您使用np.exp(test)等代码将int数据放入参数中。

Solution could be:解决方案可能是:

import numpy as np

your_array = your_array.float()
output = np.exp(your_array)

# OR

def exp_test(x)
  x.float()
  return np.exp(x)

output = exp_test(your_array)

Would you check if it works to you?你会检查它是否对你有用吗? I'd be glad to help.我很乐意帮忙。

The root cause of the problem was correct in Yoshiaki's answer问题的根本原因在 Yoshiaki 的回答中是正确的

I guess your problem occurs because some numpy functions require float type argument explicity, whereas your such use of the code as np.exp(test) puts int data into the argument.我猜你的问题发生是因为一些 numpy 函数需要显式地使用浮点类型参数,而你像 np.exp(test) 这样使用代码将 int 数据放入参数中。

However, his solution didn't work for me so I adjusted it a little bit and got it work for me然而,他的解决方案对我不起作用,所以我稍微调整了一下,让它对我有用

your_array = your_array.astype(float)
output = np.exp(your_array)

Although this question has already been adequately answered, I'd like to share my experience with this issue in the hope of shedding some more light on this sort of problems and what causes them.虽然这个问题已经得到了充分的回答,但我还是想分享一下我在这个问题上的经验,希望能对这类问题以及造成这些问题的原因有更多的了解。 From what I gathered, the problem is related to "numpy vs non-numpy datatypes".根据我收集的信息,问题与“numpy 与非 numpy 数据类型”有关。 Here's a minimal example:这是一个最小的例子:

import numpy as np

arr_float = np.array([1., 2., 3.], dtype=object)
arr_float64 = arr_float.astype(float)  # The solution proposed in other answers
np.exp(arr_float)  # This throws the TypeError
np.exp(arr_float64)  # This works!

There can be various reasons for ending up with a "float-looking" object-type array, likely related to the analyzed data being pulled from a DataFrame where it was stored with an incorrect type (due to the presence of inconvertible entries), or some back-and-forth transitions between numpy and another medium (like pandas).可能有多种原因导致“看起来像浮点数”的对象类型数组结束,可能与从以不正确类型存储的数据帧中提取的分析数据有关(由于存在不可转换的条目),或者numpy 和另一种媒体(如 pandas)之间的一些来回转换。

In conclusion - be careful with datatypes , floatnp.float64 !总之-小心数据类型floatnp.float64

test = pd.loc[(pd['a'] > 0) & (pd['a'] < 650), 'a'].values

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我会收到日志方法的“ufunc 循环不支持 numpy.ndarray 类型的参数 0”错误? - Why do I get the 'loop of ufunc does not support argument 0 of type numpy.ndarray' error for log method? nplog“TypeError:ufunc循环不支持int类型的参数0”后的PYTHON错误 - PYTHON Error after nplog "TypeError: loop of ufunc does not support argument 0 of type int " ufunc 的循环不支持浮点类型的参数 0,它没有可调用的 exp 方法 - loop of ufunc does not support argument 0 of type float which has no callable exp method TypeError:ufunc 的循环不支持浮点类型的参数 0,它没有可调用的 exp 方法 - TypeError: loop of ufunc does not support argument 0 of type float which has no callable exp method python3 np.exp(matrix1 * matrix2)中的错误-“ufunc循环不支持float类型的参数0,它没有可调用的exp方法” - Error in python3 np.exp(matrix1 * matrix2) - “loop of ufunc does not support argument 0 of type float which has no callable exp method” “ufunc循环不支持Mul类型的参数0”是什么意思? - What does "loop of ufunc does not support argument 0 of type Mul" mean? 为什么NumPy.exp与C中的exp显示不同的结果 - Why does NumPy.exp show a different result than exp in C ufunc 的到期循环中的错误不支持 str 类型的参数 0,该参数没有可调用的日志方法 - Error in due loop of ufunc does not support argument 0 of type str which has no callable log method numpy.exp()溢出 - Overflow with numpy.exp() numpy.exp() 中的溢出 - Overflow in numpy.exp()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM