• <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 数组的条件操作

        Conditional operations on numpy arrays(numpy 数组的条件操作)

          • <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 数组的条件操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                    <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'>

                            <tbody id='BeFOx'></tbody>
                          <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>