溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

numpy.where怎么用

發(fā)布時間:2021-12-15 15:17:10 來源:億速云 閱讀:331 作者:小新 欄目:大數(shù)據(jù)

這篇文章給大家分享的是有關(guān)numpy.where怎么用的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

numpy.where怎么用

函數(shù)功能簡單說明:

首先condition、x和y三個多維數(shù)組的維度必須可以廣播.然后返回一個多維數(shù)組,這個多維數(shù)組的值根據(jù)condition來確定,如果condition的值是True,那么從x中取值,否則從y中取值.

實驗代碼展示:

Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.>>> import numpy as np>>> a = np.arange(10)>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])>>> np.where(a < 5, a, 20200910*a)array([        0,         1,         2,         3,         4, 101004550,   121205460, 141406370, 161607280, 181808190])>>> np.where(a < 5, a, 10*a)array([ 0,  1,  2,  3,  4, 50, 60, 70, 80, 90])>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])>>> >>> >>> np.where(  [[True, False],[True, True]],   [[1, 2],[3, 4]],   [[9, 8], [7, 6]])array([[1, 8],   [3, 4]])>>> >>> >>> x, y = np.ogrid[:3, :4]>>> x
array([[0],   [1],   [2]])>>> y
array([[0, 1, 2, 3]])>>> np.where(x < y, x, 10 + y)  # both x and 10+y are broadcastarray([[10,  0,  0,  0],   [10, 11,  1,  1],   [10, 11, 12,  2]])>>> >>> >>> >>> a = np.array([[0, 1, 2],  [0, 2, 4],  [0, 3, 6]])>>> a
array([[0, 1, 2],   [0, 2, 4],   [0, 3, 6]])>>> np.where(a < 4, a, -1)  # -1 is broadcastarray([[ 0,  1,  2],   [ 0,  2, -1],   [ 0,  3, -1]])>>> >>> >>>

感謝各位的閱讀!關(guān)于“numpy.where怎么用”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI