import numpy as np
from functools import partial
println = partial(print,sep="\n"+"*"*50+"\n")
arr = np.random.randn(7) * 5
remainder, whole_part = np.modf(arr)
println(arr,remainder,whole_part)
#where 語句
arr = np.random.randn(4,4)
result = np.where(arr>0,2,arr)
println(arr,result)
#布爾值數組
arr = np.random.randn(100)
#第一個打印值是數組大於零的值的和,第二個打印值是大於零的個數,arr>0得出的是布爾數組
println(arr[arr>0].sum(),(arr>0).sum())
println((arr>0).any(),(arr>0).all())
#模擬隨機漫步
nwalks = 5000 #樣本數
nsteps = 1000
draws = np.random.randint(0,2,size=(nwalks,nsteps)) #0或1
steps = np.where(draws>0,1,-1) #1或-1
walks = steps.cumsum(1) #步數相加
print(walks)
#想算出走到30或-30的時間,先把未走到的樣本剔除
index_hit30 = (np.abs(walks)>=30).any(1)
hit30 = walks[index_hit30]
#第一次走到30或-30的時間
hit30_time = (np.abs(hit30)>=30).argmax(1)
println(index_hit30,hit30_time.mean())
#最值及排序
data = np.sin(np.arange(20).reshape(4,-1))
max_index = np.argmax(data,axis=0)
println(data,max_index,data[max_index,range(data.shape[1])])
sort_index = np.argsort(data[0])
sort_index
data[0,sort_index]