如何着手解决:'operands could not be broadcast together with shapes '

How to go about solving: 'operands could not be broadcast together with shapes '

上下文:

我的代码(目标)解释:

如果我有这样的代码就可以工作:

color=np.append(np.insert(listCoords.index.hour, 0, 0), 23)),

我想从该列表中指定满足我解释的两个条件的点并将它们标记为 红色

如果我这样做,代码也有效:

color=np.where(((listCoords.index.hour >= 1) & (listCoords.index.hour < 5)) & (listCoords['Threat'] == '3'), 'red', 'blue'),

但我不希望一般图是蓝色的,我希望它们的颜色与它们被检测到的时间相关。

我的缩进代码片段:

color=np.where(((listCoords.index.hour >= 1) & (listCoords.index.hour < 5)) & (listCoords['Threat'] == '3'), 'red', np.append(np.insert(listCoords.index.hour, 0, 0), 23)),

但这会输出错误消息: - 我不明白这条消息或如何解决它。

Traceback (most recent call last):
  File "/Users/Me/Desktop/DSP_Frontend/app.py", line 509, in update_graph
    color=np.where(((listCoords.index.hour >= 1) & (listCoords.index.hour < 5)) & (listCoords['Threat'] == '3'), 'red', np.append(np.insert(listCoords.index.hour, 0, 0), 23)), #
  File "<__array_function__ internals>", line 6, in where

ValueError: operands could not be broadcast together with shapes (241,) () (243,)

My question: How can I solve this issue?

我的整体代码上下文:

return go.Figure(
        data=[
            # Data for all observations based on date and time
            Scattermapbox(
                lat=listCoords["Lat"],
                lon=listCoords["Lon"],
                mode="markers",
                hoverinfo="text + lat + lon",
                text=np.append(np.insert(listCoords.index.hour, 0, 0), 23), #listCoords.index.hour,
                marker=dict(
                    showscale=True,
                    # the color is decided by the time of detection.
                    # color=np.append(np.insert(listCoords.index.hour, 0, 0), 23),
                    color=np.where(((listCoords.index.hour >= 1) & (listCoords.index.hour < 5)) & (listCoords['Threat'] == '3'), 'red', 'blue'), #
                    opacity=np.where(((listCoords.index.hour >= 1) & (listCoords.index.hour < 5)) & (listCoords['Threat'] == '3'), 0.1, 0.6), 
                    size=np.where(((listCoords.index.hour >= 1) & (listCoords.index.hour < 5)) & (listCoords['Threat'] == '3'), 80, 7),
                    colorscale=[
                        [0, "#F4EC15"],
                        [0.04167, "#DAF017"],
                        [0.0833, "#BBEC19"],
                        [0.125, "#9DE81B"],
                        [0.1667, "#80E41D"],
                        [0.2083, "#66E01F"],
                        [0.25, "#4CDC20"],
                        [0.292, "#34D822"],
                        [0.333, "#24D249"],
                        [0.375, "#25D042"],
                        [0.4167, "#26CC58"],
                        [0.4583, "#28C86D"],
                        [0.50, "#29C481"],
                        [0.54167, "#2AC093"],
                        [0.5833, "#2BBCA4"],
                        [1.0, "#613099"],
                    ],
                    colorbar=dict(
                        title="Time of<br>Day",
                        x=0.93,
                        xpad=0,
                        nticks=24,
                        tickfont=dict(color="#d8d8d8"),
                        titlefont=dict(color="#d8d8d8"),
                        thicknessmode="pixels",
                    ),
                ),
            ),

--------------------编辑 1---------------- ----

我在我的代码中添加了以下行来检查形状:

    shape1 = (listCoords.index.hour >= 1) & (listCoords.index.hour < 5)
    shape2 = (listCoords['Threat'] == '3')
    shape3 =  np.append(np.insert(listCoords.index.hour, 0, 0), 23)

    print('shape 1: ', shape1, ' shape 2: ', shape2, ' shape 3: ', shape3)

这会输出以下内容:

shape 1:  [ True  True  True  True  True  True  True  True  True  True  True  True
  True  True  True  True  True False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False]  

shape 2:  Date/Time
2019-06-02 04:00:00    False
2019-06-02 04:05:00    False
2019-06-02 04:10:00    False
2019-06-02 04:15:00    False
2019-06-02 04:20:00    False
                       ...  
2019-06-02 16:25:00    False
2019-06-02 16:30:00    False
2019-06-02 16:35:00    False
2019-06-02 16:40:00    False
2019-06-02 16:45:00    False

Name: Threat, Length: 241, dtype: bool  
shape 3:  [ 0  4  4  4  4  4  4  4  4  4  4  4  4  4  4  4  4  4  5  5  5  5  5  5
      5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  5  6  6  6  6  6  6
      6  6  6  6  6  6  6  6  6  6  6  6  6  6  6  6  6  6  7  7  7  7  7  7
      7  7  7  7  7  7  7  7  7  7  8  8  8  8  8  8  8  8  8  8  8  8  8  8
      8  8  8  8  8  8  8  9  9  9  9  9  9  9  9  9  9  9  9  9  9  9  9  9
      9  9  9  9  9 10 10 10 10 10 10 10 10 10 10 11 11 11 11 11 11 12 12 12
     12 12 12 12 12 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13
     13 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14
     14 14 14 14 14 14 14 14 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15
     15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 16 16 16 16 16 16 16 16
     16 16 23]

很明显,形状 (241,) 和 (243,) 之间似乎存在显着差异

让我们专注于您的 where 问题,用一个更简单的案例:

一维数组,条件:

In [37]: x = np.arange(10)                                                                       
In [38]: cond = (x>3)&(x<8)                                                                      
In [39]: y = np.append(np.insert(x,0,-1),100)                                                    
In [40]: cond.shape                                                                              
Out[40]: (10,)
In [41]: y.shape                                                                                 
Out[41]: (12,)
In [42]: y                                                                                       
Out[42]: array([ -1,   0,   1,   2,   3,   4,   5,   6,   7,   8,   9, 100])

当我们尝试使用大小为 10 的 cond 和大小为 12 的值 y 时,我们收到了您的错误。

In [43]: np.where(cond, -1, y)                                                                   
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-43-13267573c792> in <module>
----> 1 np.where(cond, -1, y)

<__array_function__ internals> in where(*args, **kwargs)

ValueError: operands could not be broadcast together with shapes (10,) () (12,) 

我们可以将 cond 扩展到大小 12:

In [44]: cond1 = np.append(np.insert(cond,0,False),False)                                        
In [45]: cond1                                                                                   
Out[45]: 
array([False, False, False, False, False,  True,  True,  True,  True,
       False, False, False])
In [46]: np.where(cond1, -1, y)                                                                  
Out[46]: array([ -1,   0,   1,   2,   3,  -1,  -1,  -1,  -1,   8,   9, 100])
In [47]: _.shape                                                                                 
Out[47]: (12,)

注意结果的大小为 12

您的原始版本有效,因为它使用大小为 10 的 cond 和两个标量值:

In [48]: np.where(cond, -1, 100)                                                                 
Out[48]: array([100, 100, 100, 100,  -1,  -1,  -1,  -1, 100, 100])
In [49]:                                                                  

解决方案:

首先我删除了 append/insert 因为不需要 0 和 23。这解决了 'operands could not be broadcast together with shapes' 问题。这使第二个形状从 (243,) 回到 (241,)。

但现在应用了以下部分:

color=np.where((listCoords['Threat'] == '3'), 'red', listCoords.index.hour)

它输出了一个新错误:

ValueError: 
    Invalid element(s) received for the 'color' property of scattermapbox.marker
        Invalid elements include: ['4', '4', '4', '4', '4', '4', '4', '4', '4', '4']

    The 'color' property is a color and may be specified as:
      - A hex string (e.g. '#ff0000')
      - An rgb/rgba string (e.g. 'rgb(255,0,0)')
      - An hsl/hsla string (e.g. 'hsl(0,100%,50%)')
      - An hsv/hsva string (e.g. 'hsv(0,100%,100%)')
      - A named CSS color:
            aliceblue, antiquewhite, aqua, aquamarine, azure,
            beige, bisque, black, blanchedalmond, blue,
            blueviolet, brown, burlywood, cadetblue,
            chartreuse, chocolate, coral, cornflowerblue....

为了解决这个问题,我通过说 [0, "#FF0000"](意思是 0 等于红色)改变了色阶。

后来我说if('Threat == 3'给0)else给常规颜色

我的新代码(只更新了部分):

    red = 0
    mycolors = np.where((listCoords['Threat'] == '3'), red, listCoords.index.hour)
    print(mycolors)

  color=np.array(mycolors, int),
                colorscale=[
                    [0, "#FF0000"],
                    [0.04167, "#DAF017"],
                    [0.0833, "#BBEC19"],
                    [0.125, "#9DE81B"],
                    [0.1667, "#80E41D"],
                    [0.2083, "#66E01F"],
                    [0.25, "#4CDC20"],
                    [0.292, "#34D822"],
                    [0.333, "#24D249"],
                    [0.375, "#25D042"],
                    [0.4167, "#26CC58"],
                    [0.4583, "#28C86D"],
                    [0.50, "#29C481"],
                    [0.54167, "#2AC093"],
                    [0.5833, "#2BBCA4"],
                    [1.0, "#613099"],
                ],