TypeError: unsupported operand type(s) for -: ‘str‘ and ‘float‘

这篇具有很好参考价值的文章主要介绍了TypeError: unsupported operand type(s) for -: ‘str‘ and ‘float‘。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

背景:

读一个文件中的数据,用来训练一个小模型,发现数据中有异常值,如下:

TypeError: unsupported operand type(s) for -: ‘str‘ and ‘float‘,python,python,人工智能

使用pandas读数据,然后对数值类型特征,进行归一化,报错:

def minmax_norm(df):
    return (df - df.min()) / (df.max() - df.min())

if __name__=='__main__':

    train_data_path = 'train_1205_shanghai.txt'
    test_data_path = 'test_1206_shanghai.txt'
    # load_data_to_df(path)
    col_name = ['a','b','c']

    train_data = pd.read_table(train_data_path, header=None)
    train_data.columns = col_name
    test_data = pd.read_table(test_data_path, header=None)
    test_data.columns = col_name

    # print(data.head(3)) 'avg_rider_done_ord_cnt'
    number_feat = ['a','b']
    for i in range(len(number_feat)):
        train_data[[number_feat[i]]] = minmax_norm(train_data[[number_feat[i]]])
        test_data[[number_feat[i]]] = minmax_norm(test_data[[number_feat[i]]])

报错:

Traceback (most recent call last):
  File "/Users/alsc/.conda/envs/algoTest/lib/python3.6/site-packages/pandas/core/ops/array_ops.py", line 143, in na_arithmetic_op
    result = expressions.evaluate(op, left, right)
  File "/Users/alsc/.conda/envs/algoTest/lib/python3.6/site-packages/pandas/core/computation/expressions.py", line 233, in evaluate
    return _evaluate(op, op_str, a, b)  # type: ignore
  File "/Users/alsc/.conda/envs/algoTest/lib/python3.6/site-packages/pandas/core/computation/expressions.py", line 68, in _evaluate_standard
    return op(a, b)
TypeError: unsupported operand type(s) for -: 'str' and 'float'

排查:

这个错误的意思:类型有错误,不能将str和float类型的数据进行相减‘-’。

划重点,终于知道了【for -:】是什么意思,就是在减号处,出现了类型不匹配的问题,想修复问题,就去减号附近看看有没有涉及到不同类型计算的。

解决:

删除数据中的异常值。文章来源地址https://www.toymoban.com/news/detail-523887.html

到了这里,关于TypeError: unsupported operand type(s) for -: ‘str‘ and ‘float‘的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【Python】成功解决TypeError: object of type ‘numpy.float64‘ has no len()

    【Python】成功解决TypeError: object of type ‘numpy.float64’ has no len() 🌈 个人主页:高斯小哥 🔥 高质量专栏:Matplotlib之旅:零基础精通数据可视化、Python基础【高质量合集】、PyTorch零基础入门教程👈 希望得到您的订阅和支持~ 💡 创作高质量博文(平均质量分92+),分享更多关于

    2024年04月17日
    浏览(38)
  • TypeError: ufunc ‘bitwise_and‘ not supported for the input types, and the inputs could not be safely

    这个错误是因为您使用了逻辑运算符来执行按位与(bitwise and)运算,而它不适用于浮点数类型的输入数据。 要比较两个浮点数是否在一个范围内,您可以使用逻辑运算符and,或者使用numpy库中的logical_and函数。具体地,您可以按照以下方式更改代码: 1.使用逻辑运算符and来替代

    2024年02月14日
    浏览(36)
  • 深度学习解决Unable to allocate 33.6 GiB for an array with shape (60000, 224, 224, 3) and data type float32

    深度学习时,常常要处理超大文件。因此,常常引起电脑故障。当电脑的内存16G,虚拟内存16G,读入34G的数组,发生错误:Unable to allocate 33.6 GiB for an array with shape (60000, 224, 224, 3) and data type float32。解决办法: 在win10设置-查找-高级设置-性能选项-虚拟内存-选自定义大小-按C盘

    2024年02月12日
    浏览(38)
  • 成功解决TypeError: ‘<‘ not supported between instances of ‘str‘ and ‘int‘

    成功解决TypeError: \\\'\\\' not supported between instances of \\\'str\\\' and \\\'int\\\' 目录 解决问题 解决思路 解决方法 TypeError: \\\'\\\' not supported between instances of \\\'str\\\' and \\\'int\\\' 类型错误:\\\'\\\'在\\\'str\\\'和\\\'int\\\'实例之间不支持

    2024年02月14日
    浏览(33)
  • 解决 TypeError: object of type ‘float‘ has no len() 问题 unittest单元测试框架 ddt data 数据驱动

    在unittest框架下,运用ddt和data模块进行数据驱动,脚本外存储数据时,报错。 对象数据类型不够存储。 excel文件中的数据单元格没有添加 \\\' 英文的单引号,把数字当成文本来处理。电话号为11超出float数据类型的存储范围,文本就当成了字符串数据类型来处理。 将单元格内添

    2024年02月16日
    浏览(33)
  • RuntimeError: Input type (unsigned char) and bias type (float) should be the same错误

    这个错误通常是由于输入数据类型与模型参数的类型不匹配导致的。在PyTorch中,当输入的张量类型与模型的参数类型不匹配时,PyTorch会尝试将它们转换为相同的类型,但是当它们的类型不可转换时,就会出现这个错误。 解决办法是确保输入的张量类型与模型的参数类型相同

    2024年02月15日
    浏览(32)
  • TypeError: loop of ufunc does not support argument 0 of type float which has no callable radians met

    TypeError: loop of ufunc does not support argument 0 of type float which has no callable radians method 经查询了解到: 查询得知问题是数据列不是数值类型,而 np.log() 函数需要输入数值数据。查看我的数据发现数据对象均为 object,因此需要将数据框中的 object 数据转换为 float 类型, 进行如下操作

    2024年02月04日
    浏览(43)
  • 【Python】成功解决TypeError: can only concatenate str (not “int“) to str

    【Python】成功解决TypeError: can only concatenate str (not “int”) to str 🌈 欢迎进入我的个人主页,我是高斯小哥!👈 🎓 博主档案: 广东某985本硕,SCI顶刊一作,深耕 深度学习 多年,熟练掌握PyTorch框架。 🔧 技术专长: 擅长处理各类深度学习任务,包括但不限于图像分类、图像

    2024年04月23日
    浏览(36)
  • jmeter errstr :“unsupported field type for multipart.FileHeader“

    在使用jmeter测试接口的时候,提示errstr :\\\"unsupported field type for multipart.FileHeader\\\"如图所示 这是因为我们 在HTTP信息头管理加content-type参数有问题 直接在HTTP请求中,勾选: use multipart/form-data for POST【中文:对post使用multipart/form-data for POST】,在Files Upload【中文:文件上传】中的文

    2024年02月12日
    浏览(33)
  • python 报错TypeError: ‘float‘ object is not callable

    python公式中少打了乘号“*”,如下图所示 一般是变量名与函数冲突,如本文中前面代码用到sum,后面直接用sum()函数同样报错,下图: 检查公式是否少打“*”号,python中对格式要求比较严格,不能直接用数学中省略符号的算式 调用函数,sum()函数用np.sum()函数 python报

    2024年02月10日
    浏览(32)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包