第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

numpy數(shù)組,如何選擇滿足多個(gè)條件的索引?

numpy數(shù)組,如何選擇滿足多個(gè)條件的索引?

猛跑小豬 2019-11-26 14:24:56
假設(shè)我有一個(gè)numpy數(shù)組x = [5, 2, 3, 1, 4, 5],y = ['f', 'o', 'o', 'b', 'a', 'r']。我要選擇與大于1小于5 的元素y相對(duì)應(yīng)的元素x。我試過了x = array([5, 2, 3, 1, 4, 5])y = array(['f','o','o','b','a','r'])output = y[x > 1 & x < 5] # desired output is ['o','o','a']但這不起作用。我該怎么做?python numpy的
查看完整描述

3 回答

?
冉冉說

TA貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超1個(gè)贊

如果添加括號(hào),則表達(dá)式有效:


>>> y[(1 < x) & (x < 5)]

array(['o', 'o', 'a'], 

      dtype='|S1')


查看完整回答
反對(duì) 回復(fù) 2019-11-26
?
GCT1015

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超4個(gè)贊

IMO OP實(shí)際上并不需要np.bitwise_and()(aka &),但實(shí)際上是需要的,np.logical_and()因?yàn)樗鼈冋诒容^邏輯值,例如True和False-請(qǐng)參閱此SO 邏輯與按位比較,以了解區(qū)別。


>>> x = array([5, 2, 3, 1, 4, 5])

>>> y = array(['f','o','o','b','a','r'])

>>> output = y[np.logical_and(x > 1, x < 5)] # desired output is ['o','o','a']

>>> output

array(['o', 'o', 'a'],

      dtype='|S1')

同樣的方法是np.all()通過axis適當(dāng)設(shè)置參數(shù)。


>>> output = y[np.all([x > 1, x < 5], axis=0)] # desired output is ['o','o','a']

>>> output

array(['o', 'o', 'a'],

      dtype='|S1')

通過數(shù)字:


>>> %timeit (a < b) & (b < c)

The slowest run took 32.97 times longer than the fastest. This could mean that an intermediate result is being cached.

100000 loops, best of 3: 1.15 μs per loop


>>> %timeit np.logical_and(a < b, b < c)

The slowest run took 32.59 times longer than the fastest. This could mean that an intermediate result is being cached.

1000000 loops, best of 3: 1.17 μs per loop


>>> %timeit np.all([a < b, b < c], 0)

The slowest run took 67.47 times longer than the fastest. This could mean that an intermediate result is being cached.

100000 loops, best of 3: 5.06 μs per loop

所以使用np.all()比較慢,但&和logical_and大致相同。


查看完整回答
反對(duì) 回復(fù) 2019-11-26
  • 3 回答
  • 0 關(guān)注
  • 2544 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)