numpy.where (condition [, x , y ]) numpy.where() 有两种用法:
1. np.where(condition, x, y)
满足条件(condition),输出x,不满足输出y。 如果是一维数组,相当于[xv if c else yv for (c,xv,yv) in zip(condition,x,y)]
>>> aa = np.arange(10 )
>>> np.where(aa,1 ,-1 )
array([-1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ])
>>> np.where(aa > 5 ,1 ,-1 )
array([-1 , -1 , -1 , -1 , -1 , -1 , 1 , 1 , 1 , 1 ])
>>> np.where([[True ,False ], [True ,True ]], [[1 ,2 ], [3 ,4 ]], [[9 ,8 ], [7 ,6 ]]) array([[1 , 8 ], [3 , 4 ]])
上面这个例子的条件为[[True,False], [True,False]]
,分别对应最后输出结果的四个值。第一个值从[1,9]
中选,因为条件为True,所以是选1。第二个值从[2,8]
中选,因为条件为False,所以选8,后面以此类推。类似的问题可以再看个例子:
>>> a = 10
>>> np.where([[a > 5 ,a < 5 ], [a == 10 ,a == 7 ]],
[["chosen" ,"not chosen" ], ["chosen" ,"not chosen" ]],
[["not chosen" ,"chosen" ], ["not chosen" ,"chosen" ]])
array([[‘chosen’ , ‘chosen’ ], [‘chosen’ , ‘chosen’ ]], dtype=’<U10’ )
2. np.where(condition)
只有条件 (condition),没有x和y,则输出满足条件 (即非0) 元素的坐标 (等价于numpy.nonzero )。这里的坐标以tuple的形式给出,通常原数组有多少维,输出的tuple中就包含几个数组,分别对应符合条件元素的各维坐标。
>>> a = np.array([2 ,4 ,6 ,8 ,10 ])
>>> np.where(a > 5 )
(array([2 , 3 , 4 ]),)
>>> a[np.where(a > 5 )]
array([ 6 , 8 , 10 ])
>>> np.where([[0 , 1 ], [1 , 0 ]]) (array([0 , 1 ]), array([1 , 0 ]))
上面这个例子条件中[[0,1],[1,0]]
的真值为两个1,各自的第一维坐标为[0,1]
,第二维坐标为[1,0]
。 下面看个复杂点的例子:
>>> a = np.arange(27 ).reshape(3 ,3 ,3 )
>>> a
array([[[ 0 , 1 , 2 ],
[ 3 , 4 , 5 ],
[ 6 , 7 , 8 ]],
[[ <span class="hljs-number">9</span>, <span class="hljs-number">10</span>, <span class="hljs-number">11</span>],
[<span class="hljs-number">12</span>, <span class="hljs-number">13</span>, <span class="hljs-number">14</span>],
[<span class="hljs-number">15</span>, <span class="hljs-number">16</span>, <span class="hljs-number">17</span>]],
[[<span class="hljs-number">18</span>, <span class="hljs-number">19</span>, <span class="hljs-number">20</span>],
[<span class="hljs-number">21</span>, <span class="hljs-number">22</span>, <span class="hljs-number">23</span>],
[<span class="hljs-number">24</span>, <span class="hljs-number">25</span>, <span class="hljs-number">26</span>]]])
>>> np.where(a > 5 ) (array([0 , 0 , 0 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 2 ]), array([2 , 2 , 2 , 0 , 0 , 0 , 1 , 1 , 1 , 2 , 2 , 2 , 0 , 0 , 0 , 1 , 1 , 1 , 2 , 2 , 2 ]), array([0 , 1 , 2 , 0 , 1 , 2 , 0 , 1 , 2 , 0 , 1 , 2 , 0 , 1 , 2 , 0 , 1 , 2 , 0 , 1 , 2 ]))
[ 6 , 7 , 8 ]],
[[ <span class="hljs-number">9</span>, <span class="hljs-number">10</span>, <span class="hljs-number">11</span>],
[<span class="hljs-number">12</span>, <span class="hljs-number">13</span>, <span class="hljs-number">14</span>],
[<span class="hljs-number">15</span>, <span class="hljs-number">16</span>, <span class="hljs-number">17</span>]],
[[<span class="hljs-number">18</span>, <span class="hljs-number">19</span>, <span class="hljs-number">20</span>],
[<span class="hljs-number">21</span>, <span class="hljs-number">22</span>, <span class="hljs-number">23</span>],
[<span class="hljs-number">24</span>, <span class="hljs-number">25</span>, <span class="hljs-number">26</span>]]]</code></pre>
所以np.where会输出每个元素的对应的坐标,因为原数组有三维,所以tuple中有三个数组。 /
</div>
<div class="postDesc">posted @ <span id="post-date">2018-04-22 18:59</span> <a href="https://www.cnblogs.com/massquantity/">massquantity</a> 阅读(<span id="post_view_count">22398</span>) 评论(<span id="post_comment_count">3</span>) <a href="https://i.cnblogs.com/EditPosts.aspx?postid=8908859" rel="nofollow">编辑</a> <a href="#" onclick="AddToWz(8908859);return false;">收藏</a></div>
</div>
<script src="//common.cnblogs.com/highlight/9.12.0/highlight.min.js"></script><script>markdown_highlight();</script><script type="text/javascript">var allowComments=true,cb_blogId=371451,cb_entryId=8908859,cb_blogApp=currentBlogApp,cb_blogUserGuid='0d5c233c-76f4-4879-f544-08d49c352df2',cb_entryCreatedDate='2018/4/22 18:59:00';loadViewCount(cb_entryId);var cb_postType=1;</script>