Python 为什么我会得到';ufunc的循环不支持int';类型的参数0;numpy.exp的错误?
我有一个数据帧,我想对列中的行子集执行指数计算。我已经尝试了三种版本的代码,其中两种有效。但我不明白为什么一个版本会给我这个错误Python 为什么我会得到';ufunc的循环不支持int';类型的参数0;numpy.exp的错误?,python,numpy,exponential,Python,Numpy,Exponential,我有一个数据帧,我想对列中的行子集执行指数计算。我已经尝试了三种版本的代码,其中两种有效。但我不明白为什么一个版本会给我这个错误 import numpy as np 第1版(工作版) 第2版(工作版) 版本3(错误) 它显示以下错误: AttributeError Traceback (most recent call last) AttributeError: 'int' object has no attribute 'exp' T
import numpy as np
第1版(工作版)
第2版(工作版)
版本3(错误)
它显示以下错误:
AttributeError Traceback (most recent call last)
AttributeError: 'int' object has no attribute 'exp'
The above exception was the direct cause of the following exception:
TypeError Traceback (most recent call last)
<ipython-input-161-9d5afc93942c> in <module>()
----> 1 np.exp(pd_feature.loc[(pd_feature[col] > 0) & (pd_feature[col] < 700), col])
TypeError: loop of ufunc does not support argument 0 of type int which has no callable exp method
其数据类型为:
<class 'pandas.core.series.Series'>
知道我为什么会看到这个错误吗?非常感谢。我刚看到你的帖子,想回答你的问题 我猜出现问题的原因是一些numpy函数需要float类型参数的明确性,而使用
np.exp(test)
这样的代码将int数据放入参数中
解决办法可以是:
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)
你能检查一下它对你是否有效吗?
我很乐意提供帮助。test=pd.loc[(pd['a']>0)和(pd['a']<650),'a'].values
问题的根本原因在Yoshiaki的回答中是正确的
我猜出现问题的原因是一些numpy函数需要浮点型参数的明确性,而您使用的代码如np.exp(test)将int数据放入参数中
然而,他的解决方案对我不起作用,所以我稍微调整了一下,让它对我起作用
your_array = your_array.astype(float)
output = np.exp(your_array)
test
是一个对象
dtype数组,请尝试test.values.astype(float)
请参见,但忽略apply
的使用,并将log10
替换为exp
。这无助于修复问题中的错误。无论在什么地方,这都是可行的,原始代码也会起作用。如果您解释为什么这是首选解决方案,并解释它是如何工作的,这将更有帮助。我们想要教育,而不仅仅是提供代码。
test = pd.loc[(pd['a'] > 0) & (pd['a'] < 650), 'a']
0 600
2 600
42 600
43 600
47 600
60 600
67 600
Name: a, dtype: Int64
<class 'pandas.core.series.Series'>
data = {'a':[600, 600, 600, 600, 600, 600, 600], 'b': ['a', 'a', 'a', 'a', 'a', 'a', 'a']}
df = pd.DataFrame(data)
np.exp(df.loc[:,'a'])
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)
your_array = your_array.astype(float)
output = np.exp(your_array)