【问题标题】:Python: I keep getting a broadcasting error, but I'm not sure how to fix itPython:我不断收到广播错误,但我不知道如何解决它
【发布时间】:2019-10-02 18:52:10
【问题描述】:

我正在尝试根据我的目的调整此链接中接受的答案代码: Plot gradient arrows over heatmap with plt

我正在做一个项目,该项目需要我以 .csv 文件的形式拍摄热图像,然后从 .csv 文件中获取数据以制作显示方向的箭头(通过 quiverplot streamplot 等)来自图像上最热点(最高像素值)的热流。我认为这可以使用图像的渐变来实现,但我不确定如何实现。

这是我的代码:

import matplotlib.pyplot as plt
import numpy as np
import math

directory = os.chdir(r'user_directory') #Set folder to look in
file = 'data.csv'
data = np.genfromtxt(file, delimiter = ',')

horizontal_min, horizontal_max, horizontal_stepsize = 0, 100, 0.3
vertical_min, vertical_max, vertical_stepsize = 0, 100, 0.5

horizontal_dist = horizontal_max-horizontal_min
vertical_dist = vertical_max-vertical_min

horizontal_stepsize = horizontal_dist / float(math.ceil(horizontal_dist/float(horizontal_stepsize)))
vertical_stepsize = vertical_dist / float(math.ceil(vertical_dist/float(vertical_stepsize)))

xv, yv = np.meshgrid(np.arange(horizontal_min, horizontal_max, horizontal_stepsize),
                 np.arange(vertical_min, vertical_max, vertical_stepsize))
xv+=horizontal_stepsize/2.0
yv+=vertical_stepsize/2.0

result_matrix = np.asmatrix(data)
yd, xd = np.gradient(result_matrix)

def func_to_vectorize(x, y, dx, dy, scaling=0.01):
    plt.arrow(x, y, dx*scaling, dy*scaling), fc="k", ec="k", head_width=0.06, 
head_length=0.1)

vectorized_arrow_drawing = np.vectorize(func_to_vectorize)

plt.imshow(np.flip(result_matrix,0), extent=[horizontal_min, horizontal_max, vertical_min, 
vertical_max])
vectorized_arrow_drawing(xv, yv, xd, yd, 0.1)
plt.colorbar()
plt.show()

这是我得到的错误:

ValueError: operands could not be broadcast together with shapes (200,335) (200,335) (100,100) (100,100) ()

编辑:回溯错误

ValueError Traceback(最近一次调用最后一次)
中的
     46
     47 plt.imshow(np.flip(result_matrix,0), 范围=[horizo​​ntal_min,



水平最大,垂直最小,垂直最大])
---> 48 vectorized_arrow_drawing(xv, yv, xd, yd, 0.1)
     49 plt.colorbar()
     50 plt.show()

~\Anaconda3\lib\site-packages\numpy\lib\function_base.py in __call__(self,
*args, **kwargs)
   1970 vargs.extend([kwargs[_n] for _n in names])
   1971年
-> 1972 返回 self._vectorize_call(func=func, args=vargs)
   1973年
   1974年定义_get_ufunc_and_otypes(自我,功能,参数):

~\Anaconda3\lib\site-packages\numpy\lib\function_base.py 在
_vectorize_call(self, func, args)
   2046 for a in args]
   2047
-> 2048 个输出 = ufunc(*输入)
   2049
   2050 如果 ufunc.nout == 1:

ValueError:操作数无法与形状一起广播 (100,168)
(100,168) (100,100) (100,100) ()

【问题讨论】:

  • 请包括错误的完整回溯,而不仅仅是最后一行,因为这有助于诊断问题
  • 对不起,我添加了其余的错误!

标签: python numpy vectorization gradient


【解决方案1】:

你真的需要xd, yd, xv, yv 都具有相同的形状(或所有都可以广播到相同的形状,但在功能上这是相同的东西)vectorize 才能工作。最简单的方法是:

xv, yv = np.meshgrid(np.linspace(horizontal_min, horizontal_max, data.shape[0]),
                 np.linspace(vertical_min, vertical_max, data.shape[1]))

如果您确实希望在一个方向上比另一个方向具有更高的分辨率,则另一种方法是使用scipy.interpolate.interp2dxdyd 插入xvyv 的尺寸。但这要复杂得多。

【讨论】:

    【解决方案2】:

    我猜这个错误是由于这对语句引起的:

    vectorized_arrow_drawing = np.vectorize(func_to_vectorize)
    ...
    vectorized_arrow_drawing(xv, yv, xd, yd, 0.1)
    

    即使我的猜测是正确的,您也应该发布更多错误消息。

    np.vectorize 使用 broadcasting 组合来自输入的值,并针对每个组合将一组标量值发送到 func_to_vectorize

    根据错误,5个参数有形状:

    (200,335) (200,335) (100,100) (100,100) ()
    

    () 数组是标量值 0.1。那应该没问题。但它不能将 (200,335) 数组与 (100,100) 数组一起使用。 xvyv 数组与 xdyd 数组不兼容。

    【讨论】:

    • 哦,好吧,有道理!你有什么建议可以使它们兼容吗?我将水平和垂直边界设置为 0-100,因为这是 .csv 图像的大小。这看起来是正确的做法吗?
    猜你喜欢
    • 2015-12-17
    • 2021-11-30
    • 2016-05-16
    • 1970-01-01
    • 1970-01-01
    • 2018-05-31
    • 2022-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多