where函數(shù)和常見的數(shù)據(jù)分布

np.where 學(xué)習(xí),主要參考https://www.cnblogs.com/massquantity/p/8908859.html

一、numpy.where()?有兩種用法:

1. np.where(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]], ? ? ? ?

?[[1,2], [3,4]],[[9,8], [7,6]])

array([[1, 8],

? ? ? [3, 4]])

2.np.where(condition)

只有條件 (condition),沒有x和y,則輸出滿足條件 (即非0) 元素的坐標(biāo) (等價于numpy.nonzero)。這里的坐標(biāo)以tuple的形式給出,通常原數(shù)組有多少維,輸出的tuple中就包含幾個數(shù)組,分別對應(yīng)符合條件元素的各維坐標(biāo)。

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

復(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]), ?#表示是第幾個數(shù)組

array([2, 2, 2, 0, 0, 0, 1, 1, 1, 2, 2, 2, 0, 0, 0, 1, 1, 1, 2, 2, 2]), ?#數(shù)組的行

array([0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2])) ?#數(shù)組的列

# 符合條件的元素為? ? ??

?[ 6,? 7,? 8]],

? ? ? [[ 9, 10, 11],

? ? ? [12, 13, 14],

? ? ? [15, 16, 17]],

? ? ? [[18, 19, 20],

? ? ? [21, 22, 23],

? ? ? [24, 25, 26]]]

二、正太分布

np.random.standard_normal(hang,lie)

.mean()均值 ?std()標(biāo)準(zhǔn)差

三、伯努利分布

np.random.binormal(1,p)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容