前言

本文旨在解决Python运行时出现的这样的报错:
TypeError: ufunc ‘add’ output (typecode ‘O’) could not be coerced to provided output parameter (typecode ‘d’) according to the casting rule ‘‘same_kind’’

1 问题描述

这个错误是怎么产生的呢?
就是在对numpy的dnarray数组使用运算符+=的时候(比如y+=c),如果数组复杂到一定程度,这样的自加运算就会报如上的错。

2 解决方案

百度上基本找不到什么有用的方案,所以这里才简单记录到这个博客,以免后来者走弯路。

实际上,我发现英语还是太重要了,得扎实学,不然错误都不知道是什么。

2.1 首先基本理解错误的意思

ufunc是什么?
python科学计算之numpy——ufunc函数 https://blog.csdn.net/unixtch/article/details/78531585
这里介绍的还是很清楚的。

'add' output 这里指的自然就是+=的输出了

typecode 'O' 这里表示自加的输出类型是‘O’,我感觉是object类型

could not be coerced to provided output parameter (typecode 'd') 不能强制从’Object’类型转换成‘dnarray’类型。

2.2 找资料

  • TypeError: Cannot cast ufunc add output from dtype(‘float64’) to dtype(‘uint8’) with casting rule ‘same_kind’ #7225 https://github.com/numpy/numpy/issues/7225

这里写得挺好的,还是比较靠谱的(因为来源于numpy的github网站中的issue,非常给力)

从这里就可以明白,numpy是因为你在用+=运算的时候,认为你的操作不安全,转换类型不一样,所以报错。

你要想继续运行,就得改用:
np.add(a, b, out=a, casting="unsafe")
这样的形式。(实际上,我把自加y+=c换成y=y+c 或者y=np.add(y, c)都可以,都不会报错。)

  • TypeError generated when using inplace operations on numpy arrays? https://stackoverflow.com/questions/12588986/typeerror-generated-when-using-inplace-operations-on-numpy-arrays
    这个更加给力了,非常切题。

其问题描述:

对应给出的答案:

故在此算是解决问题了。供参考。

小结

此外,还参考了:

  • ufunc cannot be cast with rule same_kind #6464 https://github.com/numpy/numpy/issues/6464 ——这里描述的是/=运算的问题,和+=类似。
  • Universal functions (ufunc) https://docs.scipy.org/doc/numpy/reference/ufuncs.html#ufunc ——这里讲的是numpy中的各种ufunc (姑且翻译为通用函数吧,universal functions)
  • numpy.add https://docs.scipy.org/doc/numpy/reference/generated/numpy.add.html#numpy.add ——这里则是介绍了numpy的其中一个通用函数add的具体参数之类的。

总言之,解决问题是费时间的,如果再熟练英语一点,可能会更快些。因为我感觉python这种编程语言,如果出错了,英文文献才是第一手资料,中文很多翻译过来的存在一定的失真(不准确性)和滞后,而这种失真和滞后性往往又是不可避免的。

所以可以说是很扎心了。

python问题解决:TypeError: ufunc 'add' output (typecode 'O') could not be coerced to provided output para相关推荐

  1. python TypeError: ufunc 'subtract' did not contain a loop with signature matching types dtype('S32')

    参考: TypeError: ufunc 'add' did not contain a loop with signature matching types: http://stackoverflo ...

  2. TypeError: ufunc did not contain a loop with signature matching types dtype('U32')

    原因:object类型和int类型相加减 解决方法: 此时的object类型可能是'12.3'这样str格式的数字,如果要运算必须进行格式转换:  可采用如下方法(pd.to_numeric()): ...

  3. 解决numpy报错UFuncTypeError: Cannot cast ufunc ‘add‘ output from dtype(‘x‘) to dtype(‘x‘)

    解决方法 把代码中的a += b改为a = a+b就可以了: 比如: import numpy as npa = np.ones((2,3), dtype=int) b = np.random.ran ...

  4. Python 3 集合方法 add( )

    Python 3 集合方法 add( ) Python 3.8.5 语法格式: set.add(elem) 描述: 将元素 elem 添加到集合中. 参数说明: elem -- 必需参数,要添加的元素 ...

  5. 生成浮点数列表:Python range():TypeError: ‘float‘ object cannot be interpreted as an integer

    生成浮点数列表:Python range():TypeError: 'float' object cannot be interpreted as an integer 目录 Python range ...

  6. 成功解决TypeError: ufunc 'sqrt' not supported for the input types, and the inputs could not be safely co

    成功解决TypeError: ufunc 'sqrt' not supported for the input types, and the inputs could not be safely co ...

  7. TypeError: ufunc ‘multiply‘ did not contain a loop with signature matching types dtype(‘S32‘) dtype(

    目录 错误: 解决: 错误: TypeError: ufunc 'multiply' did not contain a loop with signature matching types dtyp ...

  8. 解决python中TypeError: not enough arguments for format string

    解决python中TypeError: not enough arguments for format string for i in range(1,10):​ for j in range(1,i ...

  9. Uncaught TypeError: Cannot add property 0, object is not extensible 引起的思考

    默认情况下,对象都是可以扩展的,即对象可以添加新的属性和方法.使用Object.preventExtensions().Object.seal()和Object.freeze()方法都可以标记对象为不 ...

  10. TypeError: ufunc ‘isnan‘ not supported for the input types, and the inputs could not be...可能的原因记录

    TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerc ...

最新文章

  1. python转cython_用Cython加速Python到“起飞”(推荐)
  2. python套接字编程_Python网络编程 Python套接字编程
  3. 「2017 山东一轮集训 Day5」距离
  4. ACM 推荐blog汇总及OJ
  5. 数据中台实战(四):商品分析(产品设计篇)
  6. mysql_rollback_MySQL的rollback--事务回滚
  7. Codeforces 1338E JYPnation (图论)
  8. 学号20145220《信息安全系统设计基础》第12周学习总结
  9. 在线JSON转flow工具
  10. [软件人生]关于认知,能力的思考——中国城市里的无知现象片段
  11. 手机腾讯网mt框架简介
  12. LVS之ipvsadm命令
  13. 解决cmd中文输入法看不到待选文字
  14. 计算机中的字体从哪里来,电脑里的汉字是怎么造出来的
  15. 计算机文档字体替换,word2007进行字体替换的两种方法
  16. VMware Ubuntu18.04与WIN7共享目录
  17. 哔哩哔哩前端笔试(卷1)
  18. UDT长度的含义是什么?
  19. 围住一只猫猫需要几步?【多猫预警】
  20. 「微信群合影2.3.0」- 新增高清头像

热门文章

  1. deepfacelab训练多久_DeepFaceLab进阶:H128,DF,SAE模型有何不同?哪个最好?
  2. 关于跨境电商shopee平台,你了解多少?
  3. excel怎么批量插行_excel如何批量在文字前加上固定文字
  4. 河南省第三届金盾信安杯网络安全大赛部分wp
  5. 2018 百度机器学习算法工程师面试
  6. ANSYS workench 物体受力分析
  7. 平方根python_python如何求平方根
  8. LaTeX beamer中minipage添加脚注(footnote)
  9. 应用Matlab小波变换工具箱进行图像压缩
  10. 智能陈桥五笔7.8试用编号是多少_学霸的学习方法,听多少都不嫌多