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

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

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

    2. <i id='jh0ND'><tr id='jh0ND'><dt id='jh0ND'><q id='jh0ND'><span id='jh0ND'><b id='jh0ND'><form id='jh0ND'><ins id='jh0ND'></ins><ul id='jh0ND'></ul><sub id='jh0ND'></sub></form><legend id='jh0ND'></legend><bdo id='jh0ND'><pre id='jh0ND'><center id='jh0ND'></center></pre></bdo></b><th id='jh0ND'></th></span></q></dt></tr></i><div id='jh0ND'><tfoot id='jh0ND'></tfoot><dl id='jh0ND'><fieldset id='jh0ND'></fieldset></dl></div>
    3. <legend id='jh0ND'><style id='jh0ND'><dir id='jh0ND'><q id='jh0ND'></q></dir></style></legend>
    4. 在由切片和两个布尔数组索引的 numpy 数组中设置值

      时间:2023-09-01

        <legend id='KCZr1'><style id='KCZr1'><dir id='KCZr1'><q id='KCZr1'></q></dir></style></legend>
          <tbody id='KCZr1'></tbody>
            • <bdo id='KCZr1'></bdo><ul id='KCZr1'></ul>

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

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

              1. 本文介绍了在由切片和两个布尔数组索引的 numpy 数组中设置值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有两个 numpy 数组:

                I have two numpy arrays:

                a = np.arange(100*100).reshape(100,100)
                b = np.random.rand(100, 100)
                

                我还有一个切片元组来提取数组的某个部分:

                I also have a tuple of slices to extract a certain piece of the array:

                slice_ = (slice(5, 10), slice(5, 10))
                

                然后我有一组布尔索引来选择这个切片的某个部分:

                I then have a set of boolean indices to select a certain part of this slice:

                indices = b[slice_] > 0.5
                

                如果我想将这些索引设置为不同的值,我可以轻松做到:

                If I want to set these indices to a different value I can do it easily:

                a[slice_][indices] = 42
                

                但是,如果我创建另一组布尔索引来选择索引数组的特定部分:

                However, if I create another set of boolean indices to select a specific part of the indexed array:

                high_indices = a[slice_][indices] > 700
                

                然后尝试在这些索引处设置数组的值:

                and then try and set the value of the array at these indices:

                a[slice_][indices][high_indices] = 42 # Doesn't do anything!
                

                我想也许我需要将两个索引数组合并在一起,但它们的形状不同:indices 的形状为 (5, 5)high_indices 的形状为 (12,).

                I thought maybe I needed to AND the two index arrays together, but they are different shape: indices has a shape of (5, 5) and high_indices has a shape of (12,).

                我想我在试图做一些相对简单的事情时让自己陷入了一个可怕的混乱之中.如何使用这两个布尔数组进行索引,以便可以设置数组的值?

                I think I've got myself into a terrible muddle here trying to do something relatively simple. How can I index using these two boolean arrays in such a way that I can set the values of the array?

                推荐答案

                切片一个 numpy 数组返回一个视图,但布尔索引返回一个数组的副本.因此,当您第一次在 a[slice_][indices][high_indices] 中使用布尔索引对其进行索引时,您会得到一个副本,并将值 42 分配给副本而不是数组 <代码>一个.您可以通过链接布尔索引来解决问题:

                Slicing a numpy array returns a view, but boolean indexing returns a copy of an array. So when you indexed it first time with boolean index in a[slice_][indices][high_indices], you got back a copy, and the value 42 is assigned to a copy and not to the array a. You can solve the problem by chaining the boolean index:

                a[slice_][(a[slice_] > 700) & (b[slice_] > 0.5)] = 42
                

                这篇关于在由切片和两个布尔数组索引的 numpy 数组中设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:在python中存储布尔值以节省内存的最佳方法 下一篇:Python bool() 函数能否为无效参数引发异常?

                相关文章

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

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

                    • <bdo id='Yxk9Z'></bdo><ul id='Yxk9Z'></ul>