AttributeError: ‘float‘ object has no attribute ‘shape‘

项目场景:

在一个DataFrame中,使用df.corr()计算每一列之间的协方差。
总共有8列,500行。

print(train_data_val.dtypes)
#每一列的dtype为object

问题描述

报了以下错误:

AttributeError                            Traceback (most recent call last)
/tmp/ipykernel_159/3488124376.py in <module>
     23 print(train_data_val['market1501_rank'])
     24 
---> 25 print(train_data_val['cplfw_rank'].corr(train_data_val['market1501_rank']))
     26 print(train_data_val.corr())
     27 

/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/pandas/core/series.py in corr(self, other, method, min_periods)
   2333         if method in ["pearson", "spearman", "kendall"] or callable(method):
   2334             return nanops.nancorr(
-> 2335                 this.values, other.values, method=method, min_periods=min_periods
   2336             )
   2337 

/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/pandas/core/nanops.py in _f(*args, **kwargs)
     69             try:
     70                 with np.errstate(invalid="ignore"):
---> 71                     return f(*args, **kwargs)
     72             except ValueError as e:
     73                 # we want to transform an object array

/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/pandas/core/nanops.py in nancorr(a, b, method, min_periods)
   1350 
   1351     f = get_corr_func(method)
-> 1352     return f(a, b)
   1353 
   1354 

/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/pandas/core/nanops.py in func(a, b)
   1371 
   1372         def func(a, b):
-> 1373             return np.corrcoef(a, b)[0, 1]
   1374 
   1375         return func

<__array_function__ internals> in corrcoef(*args, **kwargs)

/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/numpy/lib/function_base.py in corrcoef(x, y, rowvar, bias, ddof)
   2549         warnings.warn('bias and ddof have no effect and are deprecated',
   2550                       DeprecationWarning, stacklevel=3)
-> 2551     c = cov(x, y, rowvar)
   2552     try:
   2553         d = diag(c)

<__array_function__ internals> in cov(*args, **kwargs)

/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/numpy/lib/function_base.py in cov(m, y, rowvar, bias, ddof, fweights, aweights)
   2454             w *= aweights
   2455 
-> 2456     avg, w_sum = average(X, axis=1, weights=w, returned=True)
   2457     w_sum = w_sum[0]
   2458 

<__array_function__ internals> in average(*args, **kwargs)

/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/numpy/lib/function_base.py in average(a, axis, weights, returned)
    413 
    414     if returned:
--> 415         if scl.shape != avg.shape:
    416             scl = np.broadcast_to(scl, avg.shape).copy()
    417         return avg, scl

AttributeError: 'float' object has no attribute 'shape'

解决方案:

将DataFrame数据转换为float16.

train_data_val = train_data_val.astype('float16')

便可以解决该问题。

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
"AttributeError: 'float' object has no attribute 'shape'"错误是由于在尝试访问一个浮点数对象的'shape'属性时发生的。浮点数对象是没有shape属性的,因为shape属性是用于多维数组的。 该错误通常发生在使用NumPy库中的数组或矩阵时。如果您希望使用shape属性来获取数组的维度信息,您需要确保您正在处理的对象是一个数组而不是一个单独的浮点数。 解决此错误的方法是确保您正在处理的对象是一个数组。您可以使用NumPy库中的相关函数将浮点数转换为数组,例如`np.array()`函数。以下是一个示例代码来解决该错误: ```python import numpy as np # 将浮点数转换为数组 my_float = 3.14 my_array = np.array(my_float) # 检查数组的形状 print(my_array.shape) ``` 在这个示例中,我们首先将浮点数 `3.14` 转换为一个数组 `my_array`,然后使用 `shape` 属性来获取数组的形状信息。输出将是一个空的元组 `()`,表示这是一个零维数组。 总结一下,当您在处理浮点数对象时遇到`AttributeError: 'float' object has no attribute 'shape'`错误时,您需要确保您正在处理的对象是一个数组而不是单独的浮点数,并使用相应的函数将其转换为数组。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [AttributeError:‘floatobject has no attribute ‘astype](https://blog.csdn.net/m0_51195818/article/details/126912616)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Python中出现‘floatobject has no attribute ‘strip‘的解决方式](https://blog.csdn.net/m0_50553633/article/details/123988569)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

看远方的星是否听得见

求打赏求问问题~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值