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)