• <bdo id='JdFaw'></bdo><ul id='JdFaw'></ul>
    1. <small id='JdFaw'></small><noframes id='JdFaw'>

    2. <tfoot id='JdFaw'></tfoot>
    3. <legend id='JdFaw'><style id='JdFaw'><dir id='JdFaw'><q id='JdFaw'></q></dir></style></legend>
      <i id='JdFaw'><tr id='JdFaw'><dt id='JdFaw'><q id='JdFaw'><span id='JdFaw'><b id='JdFaw'><form id='JdFaw'><ins id='JdFaw'></ins><ul id='JdFaw'></ul><sub id='JdFaw'></sub></form><legend id='JdFaw'></legend><bdo id='JdFaw'><pre id='JdFaw'><center id='JdFaw'></center></pre></bdo></b><th id='JdFaw'></th></span></q></dt></tr></i><div id='JdFaw'><tfoot id='JdFaw'></tfoot><dl id='JdFaw'><fieldset id='JdFaw'></fieldset></dl></div>

        numpy 数组的条件操作

        时间:2023-08-30

          • <legend id='1x7PI'><style id='1x7PI'><dir id='1x7PI'><q id='1x7PI'></q></dir></style></legend>

            • <tfoot id='1x7PI'></tfoot>
                <tbody id='1x7PI'></tbody>
              <i id='1x7PI'><tr id='1x7PI'><dt id='1x7PI'><q id='1x7PI'><span id='1x7PI'><b id='1x7PI'><form id='1x7PI'><ins id='1x7PI'></ins><ul id='1x7PI'></ul><sub id='1x7PI'></sub></form><legend id='1x7PI'></legend><bdo id='1x7PI'><pre id='1x7PI'><center id='1x7PI'></center></pre></bdo></b><th id='1x7PI'></th></span></q></dt></tr></i><div id='1x7PI'><tfoot id='1x7PI'></tfoot><dl id='1x7PI'><fieldset id='1x7PI'></fieldset></dl></div>
              1. <small id='1x7PI'></small><noframes id='1x7PI'>

                • <bdo id='1x7PI'></bdo><ul id='1x7PI'></ul>
                  本文介绍了numpy 数组的条件操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我是 NumPy 的新手,在 numpy 数组上运行一些条件语句时遇到了问题.假设我有 3 个如下所示的 numpy 数组:

                  I'm new to NumPy, and I've encountered a problem with running some conditional statements on numpy arrays. Let's say I have 3 numpy arrays that look like this:

                  一个:

                  [[0, 4, 4, 2],
                   [1, 3, 0, 2],
                   [3, 2, 4, 4]]
                  

                  b:

                  [[6, 9, 8, 6],
                   [7, 7, 9, 6],
                   [8, 6, 5, 7]]
                  

                  和,c:

                  [[0, 0, 0, 0],
                   [0, 0, 0, 0],
                   [0, 0, 0, 0]]
                  

                  我有一个a和b的条件语句,我想用b的值(如果a和b的条件都满足的话)来计算c的值:

                  I have a conditional statement for a and b in which I would like to use the value of b (if the conditions of a and b are met) to calculate the value of c:

                  c[(a > 3) & (b > 8)]+=b*2
                  

                  我收到一条错误消息:

                  Traceback (most recent call last):
                    File "<interactive input>", line 1, in <module>
                  ValueError: non-broadcastable output operand with shape (1,) doesn't match the broadcast shape (3,4)
                  

                  知道我该如何做到这一点吗?

                  Any idea how I can accomplish this?

                  我希望 c 的输出如下所示:

                  I would like the output of c to look as follows:

                  [[0, 18, 0, 0],
                   [0, 0, 0, 0],
                   [0, 0, 0, 0]]
                  

                  推荐答案

                  可以使用numpy.where:

                  np.where((a > 3) & (b > 8), c + b*2, c)
                  #array([[ 0, 18,  0,  0],
                  #       [ 0,  0,  0,  0],
                  #       [ 0,  0,  0,  0]])
                  

                  或算术:

                  c + b*2 * ((a > 3) & (b > 8))
                  #array([[ 0, 18,  0,  0],
                  #       [ 0,  0,  0,  0],
                  #       [ 0,  0,  0,  0]])
                  

                  这篇关于numpy 数组的条件操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Pandas - 带条件公式的 Groupby 下一篇:不同版本 Python 的条件 shebang 行

                  相关文章

                    <legend id='BeFOx'><style id='BeFOx'><dir id='BeFOx'><q id='BeFOx'></q></dir></style></legend>

                      <bdo id='BeFOx'></bdo><ul id='BeFOx'></ul>

                    <small id='BeFOx'></small><noframes id='BeFOx'>

                    <i id='BeFOx'><tr id='BeFOx'><dt id='BeFOx'><q id='BeFOx'><span id='BeFOx'><b id='BeFOx'><form id='BeFOx'><ins id='BeFOx'></ins><ul id='BeFOx'></ul><sub id='BeFOx'></sub></form><legend id='BeFOx'></legend><bdo id='BeFOx'><pre id='BeFOx'><center id='BeFOx'></center></pre></bdo></b><th id='BeFOx'></th></span></q></dt></tr></i><div id='BeFOx'><tfoot id='BeFOx'></tfoot><dl id='BeFOx'><fieldset id='BeFOx'></fieldset></dl></div>

                    1. <tfoot id='BeFOx'></tfoot>