首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >TypeError: ufunc循环不支持浮点数类型的参数0,后者没有可调用的exp方法。

TypeError: ufunc循环不支持浮点数类型的参数0,后者没有可调用的exp方法。
EN

Stack Overflow用户
提问于 2022-07-04 03:27:45
回答 1查看 1.3K关注 0票数 0

这是我的数据集

代码语言:javascript
运行
复制
Id      B          C      
1       0.784      -1.6745
2       2.123      -2.8934

以下是我所尝试的

代码语言:javascript
运行
复制
import numpy as np
df.apply(lambda x: np.exp(x)/(1+np.exp(x)))

错误信息

代码语言:javascript
运行
复制
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
AttributeError: 'float' object has no attribute 'exp'

The above exception was the direct cause of the following exception:

TypeError                                 Traceback (most recent call last)
<ipython-input-43-288751cc2e1d> in <module>
----> 1 A.apply(lambda x: np.exp(x)/(1+np.exp(x)))

~/.local/lib/python3.6/site-packages/pandas/core/frame.py in apply(self, func, axis, raw, result_type, args, **kwds)
   7550             kwds=kwds,
   7551         )
-> 7552         return op.get_result()
   7553 
   7554     def applymap(self, func) -> "DataFrame":

~/.local/lib/python3.6/site-packages/pandas/core/apply.py in get_result(self)
    183             return self.apply_raw()
    184 
--> 185         return self.apply_standard()
    186 
    187     def apply_empty_result(self):

~/.local/lib/python3.6/site-packages/pandas/core/apply.py in apply_standard(self)
    274 
    275     def apply_standard(self):
--> 276         results, res_index = self.apply_series_generator()
    277 
    278         # wrap results

~/.local/lib/python3.6/site-packages/pandas/core/apply.py in apply_series_generator(self)
    303                 for i, v in enumerate(series_gen):
    304                     # ignore SettingWithCopy here in case the user mutates
--> 305                     results[i] = self.f(v)
    306                     if isinstance(results[i], ABCSeries):
    307                         # If we have a view on v, we need to make a copy because

<ipython-input-43-288751cc2e1d> in <lambda>(x)
----> 1 A.apply(lambda x: np.exp(x)/(1+np.exp(x)))

~/.local/lib/python3.6/site-packages/pandas/core/series.py in __array_ufunc__(self, ufunc, method, *inputs, **kwargs)
    724 
    725         inputs = tuple(extract_array(x, extract_numpy=True) for x in inputs)
--> 726         result = getattr(ufunc, method)(*inputs, **kwargs)
    727 
    728         name = names[0] if len(set(names)) == 1 else None

TypeError: loop of ufunc does not support argument 0 of type float which has no callable exp method
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-04 04:20:57

看起来是数据类型问题。

代码语言:javascript
运行
复制
df = pd.DataFrame({'B':[0.784,2.123], 'C':[-1.6745,-2.8934]})
print(df.info())

信息:

代码语言:javascript
运行
复制
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 2 entries, 0 to 1
Data columns (total 2 columns):
 #   Column  Non-Null Count  Dtype  
---  ------  --------------  -----  
 0   B       2 non-null      float64
 1   C       2 non-null      float64
dtypes: float64(2)
memory usage: 160.0 bytes

适用范围:

代码语言:javascript
运行
复制
df.apply(lambda x: np.exp(x)/(1+np.exp(x)))
print(df)

结果:

代码语言:javascript
运行
复制
       B       C
0  0.784 -1.6745
1  2.123 -2.8934

复制错误:

代码语言:javascript
运行
复制
df = pd.DataFrame({'B':[0.784,2.123], 'C':[-1.6745,-2.8934]}, dtype=object)
df.apply(lambda x: np.exp(x)/(1+np.exp(x)))
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72851483

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档