ValueError: operands could not be broadcast together with shapes、numpy广播错误

问题

# 不符合numpy的广播规则,触发了计算错误。两个向量或者矩阵维度不同,没法进行按位的运算;

M = np.ones((3, 2))
a = np.arange(3)M+a

解决方案

#查看np.newaxis进行维度扩增后的shape

a[:, np.newaxis].shape
(3, 1)

# 使用np.newaxis进行维度扩增,之后就符合了numpy的广播规则,就可以进行运算

M + a[:, np.newaxis]
array([[ 1.,  1.],[ 2.,  2.],[ 3.,  3.]])

还要注意,虽然我们在这里一直关注+运算符,但这些广播规则适用于任何二进制UFUNC。例如,下面是logaddexp(a,b)函数,它计算log(exp(a)+exp(b))的精度比简单方法高。

np.logaddexp(M, a[:, np.newaxis])******************************************************************************array([[ 1.31326169,  1.31326169],[ 1.69314718,  1.69314718],[ 2.31326169,  2.31326169]])

完整错误日志

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-219-f2cda6cbe81d> in <module>2 a = np.arange(3)3
----> 4 M+aValueError: operands could not be broadcast together with shapes (3,2) (3,)

参考:Computation on Arrays: Broadcasting

参考:Python Data Science Handbook

参考:Numpy的广播机制

ValueError: operands could not be broadcast together with shapes、numpy广播错误相关推荐

  1. ValueError: operands could not be broadcast together with shapes解决

    ValueError: operands could not be broadcast together with shapes (1,2) (1,3) import numpy as np a=np ...

  2. 【python问题系列--4】ValueError: operands could not be broadcast together with shapes (100,3) (3,1)...

    背景:dataMatrix是(100,3)的列表,labelMat是(1,100)的列表,weights是(3,1)的数组,属性如下代码所示: >>> import types &g ...

  3. ValueError: operands could not be broadcast together with shapes (320,320,4) (3,) (320,320,4)

    problem: ValueError: operands could not be broadcast together with shapes (320,320,4) (3,) (320,320, ...

  4. 报错ValueError: operands could not be broadcast together with shapes (448,448) with (224,224)

    这个错误发生原因是因为图片的尺寸与weight 不匹配,不能做乘积,所以需要改变图片尺寸. 使用如下代码:img = cv2.resize(img, (448, 448)) 加在img = cv2.r ...

  5. 【Python学习记录】Numpy广播机制(broadcast)

    ✨ 博客主页:小小马车夫的主页 ✨ 所属专栏:Python学习记录 文章目录 一.什么是Numpy广播机制 二.Numpy广播应用 三.Numpy广播规则 一.什么是Numpy广播机制 在Numpy. ...

  6. operands could not be broadcast together with remapped shapes

    operands could not be broadcast together with remapped shapes 报错代码: point_num = 12try:labels=np.pad( ...

  7. einsum() operands do not broadcast with remapped shapes [original->remapped]报错解决方法

    RuntimeError: einsum() operands do not broadcast with remapped shapes [original->remapped]: [4, 6 ...

  8. NumPy 广播(Broadcast)与pandas基础知识

    文章目录 NumPy 广播(Broadcast) 控制遍历顺序 修改数组中元素的值 使用外部循环 广播迭代 Numpy 数组操作 修改数组形状 umpy.ndarray.flat umpy.ndarr ...

  9. 成功解决IndexError: shape mismatch: indexing arrays could not be broadcast together with shapes (100,)

    成功解决IndexError: shape mismatch: indexing arrays could not be broadcast together with shapes (100,) 目 ...

  10. Numpy中出现“ValueError: zero-size array to reduction operation maximum which has no identity”错误的调试笔记

    1 问题描述 在写作目标检测的代码时,遇到了这样的一个错误, File "/home/-/-py", line 121, in - array = array/ array.max ...

最新文章

  1. jQuery操作input改变value属性值
  2. Win2008 R2 WEB 服务器设置之禁用不必要的服务和关闭端口
  3. DayDayUp:《机器崛起前传》第二十二章【蹒跚而来】读后感(文章源自网络)—听课笔记
  4. Day12 CSS简单用法
  5. 如何基于DataWorks构建数据中台?
  6. ASP.NET Core中为指定类添加WebApi服务功能
  7. Android之BaseRecyclerViewAdpater(3.0.4版本)当页面第二次滑到底部的时候没有触发OnLoadMoreListener监听的onLoadMore函数
  8. Mr. Kitayuta‘s Technology CodeForces - 505D(并查集+拓扑排序或dfs找环) 题解
  9. linux 命令find
  10. 从零开始学做微信小程序,看这些就够了!
  11. 关于无序分类变量量化的处理思考
  12. SEO思维的优化源于生活
  13. 在线二进制转文本字符工具
  14. Windows上搭建PHP开发环境
  15. 如何让函数只执行一次
  16. 砍价永远差一刀?拼多多法庭上回复:小数点后有6位......
  17. 按Right-BICEP要求的测试用例
  18. 亲历骗子通过闲鱼、江苏猎宝网络科技股份有限公司进行诈骗!
  19. iOS底层原理:weak的实现原理
  20. docker-Alpine添加字体支持功能

热门文章

  1. xp系统打印机服务器报错,WinXP系统打印机显示Spoolsv.exe 应用程序错误的解决方法...
  2. IE源代码摘抄,基于泄漏的IE5.0(持续更新)
  3. python微信群管理开禁言_怎么设置群禁言(微信群怎么禁言)
  4. autocad ios 虚线_autocad中画箭头、虚线绘制方法介绍
  5. Java创建Shape类
  6. Ubuntu在物理机系统安装和teamviewer安装过程问题和解决的记录
  7. C6678多核DSP——CMD文件介绍
  8. 用阿里云商标注册流程步骤,以及有哪些商标延伸服务
  9. SageMath的使用
  10. Prolog 语言入门教程