python问题解决:TypeError: ufunc 'add' output (typecode 'O') could not be coerced to provided output para
前言
本文旨在解决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相关推荐
- 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 ...
- TypeError: ufunc did not contain a loop with signature matching types dtype('U32')
原因:object类型和int类型相加减 解决方法: 此时的object类型可能是'12.3'这样str格式的数字,如果要运算必须进行格式转换: 可采用如下方法(pd.to_numeric()): ...
- 解决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 ...
- Python 3 集合方法 add( )
Python 3 集合方法 add( ) Python 3.8.5 语法格式: set.add(elem) 描述: 将元素 elem 添加到集合中. 参数说明: elem -- 必需参数,要添加的元素 ...
- 生成浮点数列表:Python range():TypeError: ‘float‘ object cannot be interpreted as an integer
生成浮点数列表:Python range():TypeError: 'float' object cannot be interpreted as an integer 目录 Python range ...
- 成功解决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 ...
- 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 ...
- 解决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 ...
- Uncaught TypeError: Cannot add property 0, object is not extensible 引起的思考
默认情况下,对象都是可以扩展的,即对象可以添加新的属性和方法.使用Object.preventExtensions().Object.seal()和Object.freeze()方法都可以标记对象为不 ...
- 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 ...
最新文章
- python转cython_用Cython加速Python到“起飞”(推荐)
- python套接字编程_Python网络编程 Python套接字编程
- 「2017 山东一轮集训 Day5」距离
- ACM 推荐blog汇总及OJ
- 数据中台实战(四):商品分析(产品设计篇)
- mysql_rollback_MySQL的rollback--事务回滚
- Codeforces 1338E JYPnation (图论)
- 学号20145220《信息安全系统设计基础》第12周学习总结
- 在线JSON转flow工具
- [软件人生]关于认知,能力的思考——中国城市里的无知现象片段
- 手机腾讯网mt框架简介
- LVS之ipvsadm命令
- 解决cmd中文输入法看不到待选文字
- 计算机中的字体从哪里来,电脑里的汉字是怎么造出来的
- 计算机文档字体替换,word2007进行字体替换的两种方法
- VMware Ubuntu18.04与WIN7共享目录
- 哔哩哔哩前端笔试(卷1)
- UDT长度的含义是什么?
- 围住一只猫猫需要几步?【多猫预警】
- 「微信群合影2.3.0」- 新增高清头像
热门文章
- deepfacelab训练多久_DeepFaceLab进阶:H128,DF,SAE模型有何不同?哪个最好?
- 关于跨境电商shopee平台,你了解多少?
- excel怎么批量插行_excel如何批量在文字前加上固定文字
- 河南省第三届金盾信安杯网络安全大赛部分wp
- 2018 百度机器学习算法工程师面试
- ANSYS workench 物体受力分析
- 平方根python_python如何求平方根
- LaTeX beamer中minipage添加脚注(footnote)
- 应用Matlab小波变换工具箱进行图像压缩
- 智能陈桥五笔7.8试用编号是多少_学霸的学习方法,听多少都不嫌多