佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > np.where()[0] 和 np.where()[1]的具體使用

np.where()[0] 和 np.where()[1]的具體使用

熱門標簽:呼叫中心市場需求 客戶服務(wù) Win7旗艦版 硅谷的囚徒呼叫中心 電話運營中心 語音系統(tǒng) 企業(yè)做大做強 百度AI接口

本文主要介紹了np.where()[0] 和 np.where()[1]的具體使用,以及np.where()的具體用法,廢話不多說,具體如下:

import numpy as np
 
a = np.arange(12).reshape(3,4)
print('a:', a)
print('np.where(a > 5):', np.where(a > 5))
print('a[np.where(a > 5)]:', a[np.where(a > 5)])
print('np.where(a > 5)[0]:', np.where(a > 5)[0])
print('np.where(a > 5)[1]:', np.where(a > 5)[1])
print(a[np.where(a > 5)[0], np.where(a > 5)[1]])
a: [[ 0 1 2 3]
 [ 4 5 6 7]
 [ 8 9 10 11]]
np.where(a > 5): (array([1, 1, 2, 2, 2, 2]), array([2, 3, 0, 1, 2, 3]))
a[np.where(a > 5)]: [ 6 7 8 9 10 11]
np.where(a > 5)[0]: [1 1 2 2 2 2]
np.where(a > 5)[1]: [2 3 0 1 2 3]
[ 6 7 8 9 10 11]

np.where()[0] 表示行索引,np.where()[1]表示列索引

numpy.where() 有兩種用法:

1. np.where(condition, x, y)

滿足條件(condition),輸出x,不滿足輸出y。

如果是一維數(shù)組,相當(dāng)于[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]) # 0為False,所以第一個輸出-1
>>> np.where(aa > 5,1,-1)
array([-1, -1, -1, -1, -1, -1, 1, 1, 1, 1])

>>> np.where([[True,False], [True,True]],  # 官網(wǎng)上的例子
  [[1,2], [3,4]],
       [[9,8], [7,6]])
array([[1, 8],
  [3, 4]])

上面這個例子的條件為[[True,False], [True,False]],分別對應(yīng)最后輸出結(jié)果的四個值。第一個值從[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的形式給出,通常原數(shù)組有多少維,輸出的tuple中就包含幾個數(shù)組,分別對應(yīng)符合條件元素的各維坐標。

>>> a = np.array([2,4,6,8,10])
>>> np.where(a > 5)  # 返回索引
(array([2, 3, 4]),)  
>>> a[np.where(a > 5)]   # 等價于 a[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] 。

下面看個復(fù)雜點的例子:

>>> a = np.arange(27).reshape(3,3,3)
>>> a
array([[[ 0, 1, 2],
    [ 3, 4, 5],
    [ 6, 7, 8]],

    [[ 9, 10, 11],
    [12, 13, 14],
    [15, 16, 17]],

    [[18, 19, 20],
    [21, 22, 23],
    [24, 25, 26]]])

>>> 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]],

   [[ 9, 10, 11],
    [12, 13, 14],
    [15, 16, 17]],

   [[18, 19, 20],
    [21, 22, 23],
    [24, 25, 26]]]

所以np.where會輸出每個元素的對應(yīng)的坐標,因為原數(shù)組有三維,所以tuple中有三個數(shù)組。

需要注意的一點是,輸入的不能直接是list,需要轉(zhuǎn)為array或者為array才行。比如range(10)和np.arange(10)后者返回的是數(shù)組,使用np.where才能達到效果。

到此這篇關(guān)于np.where()[0] 和 np.where()[1]的具體使用的文章就介紹到這了,更多相關(guān)np.where()[0] 和 np.where()[1]內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • Numpy中的數(shù)組搜索中np.where方法詳細介紹

標簽:海南 安康 山西 濟南 喀什 山西 崇左 長沙

巨人網(wǎng)絡(luò)通訊聲明:本文標題《np.where()[0] 和 np.where()[1]的具體使用》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    宁蒗| 兰溪市| 岗巴县| 阳新县| 贺州市| 郎溪县| 法库县| 福海县| 绵竹市| 江西省| 高台县| 凤凰县| 正镶白旗| 元阳县| 五常市| 甘谷县| 武平县| 德江县| 本溪| 腾冲县| 剑河县| 怀安县| 喀喇沁旗| 上蔡县| 确山县| 桂东县| 瑞昌市| 建昌县| 察哈| 宣威市| 兖州市| 青龙| 雅安市| 平昌县| 天台县| 贡觉县| 磴口县| 封开县| 吕梁市| 仙游县| 山东省|