纳速健身

标题: [python error]‘float‘ object has no attribute ‘log‘ / loop of ufunc does not support argument 0 of type [打印本页]

作者: awagink    时间: 2021-1-17 11:43
标题: [python error]‘float‘ object has no attribute ‘log‘ / loop of ufunc does not support argument 0 of type
[python error]‘float‘ object has no attribute ‘log‘ / loop of ufunc does not support argument 0 of type

报错情况:
原来的对数转换(如下),前面调过一些数后就报错。

  1. np.log(data['Item']+1)
复制代码


(, 下载次数: 0)
AttributeError Traceback (most recent call last)
AttributeError: ‘float’ object has no attribute ‘log’

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


解决方法
  1. np.log(data['Item']+1)
复制代码


改成

  1. np.log((data['Item']+1).astype('float'))
复制代码


astype()函数
1astype()函数可用于转化dateframe某一列的数据类型

如下将dateframe某列的str类型转为int,注意astype()没有replace=True的用法,想要在原数据上修改,要写成如下形式。

注意只有当该列的字符串全是由纯数字构成时才可以这样写,如果混有字母,会报错:ValueError: invalid literal for int() with base 10:

利用int()函数转字符串也类似

参考:https://www.cnblogs.com/helloworldcc/p/9681546.html

  1. app_train[['uid','index']] = app_train[['uid','index']].astype('int')
复制代码

isdigit()用于判断一个字符串是否由纯数字构成,如果是返回True,否则False

参考:
https://www.runoob.com/python/att-string-isdigit.html
https://blog.csdn.net/a1809032425/article/details/82316672




欢迎光临 纳速健身 (https://www.nasue.com/) Powered by Discuz! X3.4