TypeError:UFUNC的循环不支持没有可呼叫的EXP方法的类型Float的参数0

发布于 2025-02-12 12:32:49 字数 2485 浏览 0 评论 0原文

这是我的数据集,

Id      B          C      
1       0.784      -1.6745
2       2.123      -2.8934

这是我尝试

import numpy as np
df.apply(lambda x: np.exp(x)/(1+np.exp(x)))

错误消息的内容

---------------------------------------------------------------------------
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

Here's my dataset

Id      B          C      
1       0.784      -1.6745
2       2.123      -2.8934

Here's what I try

import numpy as np
df.apply(lambda x: np.exp(x)/(1+np.exp(x)))

The error message

---------------------------------------------------------------------------
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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯然 2025-02-19 12:32:49

看起来像数据类型问题。

df = pd.DataFrame({'B':[0.784,2.123], 'C':[-1.6745,-2.8934]})
print(df.info())

信息:

<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

应用:

df.apply(lambda x: np.exp(x)/(1+np.exp(x)))
print(df)

结果:

       B       C
0  0.784 -1.6745
1  2.123 -2.8934

复制错误:

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)))

Looks like a data type issue.

df = pd.DataFrame({'B':[0.784,2.123], 'C':[-1.6745,-2.8934]})
print(df.info())

Info:

<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

Apply:

df.apply(lambda x: np.exp(x)/(1+np.exp(x)))
print(df)

Result:

       B       C
0  0.784 -1.6745
1  2.123 -2.8934

Reproduce error:

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)))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文