利用python 學習資料分析 (學習三)
阿新 • • 發佈:2019-01-14
內容學習自:
Python for Data Analysis, 2nd Edition
就是這本
純英文學的很累,對不對取決於百度翻譯了
前情提要:
各種方法貼:
https://www.cnblogs.com/baili-luoyun/p/10250177.html
本節內容,numpy 結尾, 以及pands初入門
一:偽隨機函式
samples =np.random.normal(size=(3,3)) print(samples) samples1 =np.random.seed(1234) #調節隨機種子 print(samples1) >>>> [[ 0.91041087 0.99724402 -0.0681944 ] [ 0.48277059 -1.50446272 0.30656754] [-0.01929894 -0.27835256 -0.68331925]] None
2>:給上下限的數取整
samplse1 = np.random.randint(1,6) #給上下限的數取整數
print(samplse1)
.>>>.>>>>>
# 隨機整數
2
3>:產生正太分佈樣本
arr1 =np.random.randn(3,3) #產生正態分佈的樣本值 print(arr1) >>>>> [[ 0.55526507 -0.3952732 1.49921319] [-0.06943318 -0.05375604 -0.26636376] [-1.48909115 -0.00702559 0.15973251]]
4>:產生均勻分佈樣本
arr1 =np.random.rand(3,3) #產生均勻分佈的樣本質
print(arr1)
>>>
[[0.56847387 0.23533505 0.97136229]
[0.0049996 0.60753788 0.20997453]
[0.41777576 0.26264969 0.55777828]]
二:隨機漫步
1:單隨機漫步(普通方法)
import matplotlib.pylab as plt import random postition = 0 walk =[postition] steps =1000 for i in range(steps): step = 1 if random.randint(0,1) else -1 postition += step walk.append(postition) print(walk) plt.plot(walk[:100]) plt.show() >>>>>> [0, 1, 0, 1, 2, 1, 0, -1, 0, -1, -2, -3, -4, -5, -6, -5, -6, -5, -6, -7, -8, -7, -8, -7, -8, -7, -6, -5, -6, -7, -6, -7, -8, -7, -8, -9, -8, -7, -6, -5, -6, -5, -4, -3, -4, -3, -2, -3, -4, -5, -4, -5, -4, -5, -6, -5, -4, -3, -4, -5, -4, -3, -2, -3, -2, -1, -2, -1, -2, -3, -4, -5, -4, -3, -2, -3, -4, -3, -2, -1, 0, 1, 0, -1, 0, -1, 0, 1, 2, 3, 4, 5, 4, 3, 2, 3, 2, 1, 2, 1, 0, 1, 2, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 5, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1, 2, 3, 4, 3, 4, 5, 6, 5, 4, 5, 4, 5, 6, 5, 6, 5, 4, 3, 2, 1, 2, 1, 0, 1, 0, -1, 0, 1, 0, -1, -2, -1, -2, -1, 0, 1, 2, 3, 4, 5, 4, 5, 4, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 5, 6, 7, 6, 7, 6, 7, 8, 9, 10, 9, 8, 9, 8, 9, 10, 11, 10, 11, 12, 11, 10, 9, 10, 9, 8, 7, 8, 7, 6, 5, 4, 5, 4, 3, 2, 1, 0, 1, 0, 1, 2, 1, 0, -1, 0, -1, -2, -3, -2, -1, 0, 1, 0, 1, 0, -1, 0, 1, 0, -1, -2, -3, -2, -3, -2, -3, -4, -3, -2, -3, -4, -3, -2, -3, -4, -3, -4, -3, -2, -3, -4, -3, -4, -5, -6, -7, -6, -7, -8, -9, -10, -9, -8, -7, -8, -9, -10, -11, -12, -13, -14, -15, -14, -15, -14, -15, -14, -13, -12, -13, -14, -13, -14, -15, -16, -17, -18, -17, -18, -19, -18, -19, -20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -12, -13, -12, -13, -12, -11, -10, -9, -8, -9, -8, -7, -8, -9, -10, -9, -10, -11, -12, -13, -12, -13, -14, -13, -12, -13, -14, -15, -14, -13, -12, -11, -12, -13, -12, -13, -12, -13, -14, -13, -14, -15, -14, -15, -16, -17, -16, -15, -16, -15, -16, -15, -16, -17, -18, -17, -18, -19, -18, -17, -16, -15, -14, -15, -14, -15, -16, -17, -18, -19, -20, -21, -20, -21, -20, -19, -20, -19, -18, -19, -18, -17, -16, -17, -16, -15, -16, -15, -14, -15, -14, -15, -14, -15, -16, -15, -14, -15, -16, -17, -16, -15, -16, -15, -16, -17, -16, -15, -14, -13, -14, -15, -14, -13, -14, -15, -16, -17, -16, -15, -14, -13, -12, -13, -14, -15, -14, -15, -14, -13, -12, -11, -12, -11, -12, -13, -14, -13, -12, -13, -12, -13, -14, -13, -14, -15, -14, -15, -14, -13, -12, -13, -14, -15, -14, -15, -16, -15, -16, -17, -18, -17, -18, -17, -18, -17, -18, -17, -18, -19, -18, -19, -18, -17, -18, -19, -20, -19, -18, -19, -18, -17, -16, -15, -14, -15, -16, -15, -14, -15, -16, -15, -16, -15, -14, -13, -12, -11, -12, -11, -10, -11, -12, -13, -14, -13, -12, -11, -10, -11, -10, -11, -12, -13, -14, -15, -14, -15, -14, -15, -16, -17, -16, -17, -18, -17, -18, -17, -16, -15, -14, -15, -14, -15, -16, -15, -14, -15, -16, -17, -18, -19, -20, -21, -20, -19, -18, -19, -18, -17, -18, -19, -18, -19, -18, -19, -20, -19, -18, -19, -20, -19, -20, -19, -18, -19, -20, -21, -22, -21, -22, -23, -22, -21, -20, -21, -20, -21, -22, -21, -20, -19, -18, -19, -18, -19, -18, -17, -18, -19, -20, -21, -20, -21, -22, -23, -24, -23, -22, -21, -20, -19, -20, -19, -18, -17, -18, -17, -18, -19, -20, -21, -20, -19, -18, -19, -20, -21, -22, -23, -22, -23, -22, -21, -20, -21, -22, -23, -24, -25, -24, -25, -24, -25, -24, -25, -26, -25, -24, -25, -26, -25, -26, -27, -28, -29, -30, -29, -30, -31, -32, -33, -34, -33, -32, -33, -34, -33, -34, -33, -32, -31, -30, -29, -30, -31, -30, -31, -30, -29, -28, -27, -26, -27, -28, -27, -26, -25, -24, -25, -26, -25, -24, -25, -24, -23, -22, -23, -22, -21, -20, -21, -20, -21, -20, -19, -20, -19, -20, -21, -22, -21, -20, -19, -20, -19, -18, -19, -18, -17, -16, -15, -16, -17, -18, -19, -20, -21, -22, -23, -22, -23, -24, -25, -26, -27, -28, -29, -28, -27, -28, -27, -26, -25, -24, -25, -26, -27, -26, -27, -26, -25, -24, -23, -24, -25, -24, -25, -26, -25, -26, -27, -28, -27, -26, -27, -26, -25, -24, -25, -26, -25, -26, -27, -28, -27, -28, -27, -26, -27, -26, -27, -26, -25, -24, -25, -24, -23, -22, -21, -20, -21, -20, -21, -22, -21, -20, -21, -20, -19, -20, -21, -20, -19, -20, -21, -22, -23, -24, -23, -22, -21, -22, -23, -24, -25, -26, -25, -24, -23, -24, -23, -22, -23, -22, -23, -22, -21, -20, -21, -22, -23, -24, -23, -22, -21, -22, -21, -22, -21, -20, -19, -20, -19, -20, -21, -22, -21, -20, -19, -18, -19, -18, -17, -18, -19, -20, -19, -20, -21, -20, -19, -20, -19, -18, -19, -20, -19, -20, -19, -18, -19, -18, -17, -18, -17, -18, -19, -18, -17, -16, -15, -16, -15, -16, -15, -14, -15, -16, -17, -18, -19, -20, -19, -18, -17, -16, -17, -18, -19, -20, -21, -20, -19, -20, -21, -20, -21, -20, -21, -22, -21, -20, -19, -20, -21, -20, -19, -20, -21, -22, -21, -22, -23, -24, -23, -24, -23, -24, -23, -24, -25, -26, -27, -28, -27, -28, -29, -30, -29, -28, -27, -26, -27, -26, -27, -28, -29, -28, -27, -26, -27, -28, -29, -30, -29, -30, -31, -32, -31, -32, -31, -30, -29, -30, -31, -30, -29, -28, -29, -30, -29, -28, -27, -28, -27, -26, -25, -26, -25, -26, -25, -24, -25, -26, -27, -26, -27, -26, -25, -24, -25, -26, -25, -24, -25, -24, -23, -22, -23, -24]
2:利用numpy隨機漫步
1>:生產1個0 或者1 的1維矩陣,1000次
nsteps =1000
draws =np.random.randint(0,2,size=nsteps) #生成一個0 或者1 的1位矩陣,1000次
print(draws)
>>>>
[1 1 1 0 1 1 1 0 1 0 0 0 1 1 1 1 0 1 0 1 0 1 0 1 0 0 1 0 0 0 0 0 1 1 1 0 0.............. 0 1 1 1 0 1 1 1 1 1 1 0 1 0 0 1 0 1 1 1 0 0 0 1 0 1 0 0 0 0 1 0 0 1 1 0 1
1]
2>:進行三元運算累加
nsteps =1000
draws =np.random.randint(0,2,size=nsteps) #生成一個0 或者1 的1位矩陣,1000次
# print(draws)
steps =np.where(draws>0 ,1,-1) #三元運算
walk =steps.cumsum() #累加
print(walk)
plt.plot(walk)
plt.show()
>>>>>>>>>>
[ 1 0 -1 0 -1 0 -1 0 1 0 1 2 3 4 3 4 5 6
5 6 5 4 5 6 7 6 7 8 9 10 11 10 11 12 11 10
11 12 11 12 11 12 13 14 13 12 13 14 15 16 15 16 17 18
17 18 17 18 19 18 17 16 15 14 15 16 17 16 15 14 13 12
11 10 9 10 9 10 9 10 9 10 9 10 11 10 11 12 11 10
9 10 11 12 11 10 9 10 11 10 9 8 9 8 9 8 9 8
.......
2.1>最大值 ,最小值
l1 =walk.min() # 最小值
print(l1)
l2 =walk.max() #最大值
print(l2)
>>>>>>
-51
12
2.2>求距離0 到(+,-)10 距離最近的索引
l =(np.abs(walk)>=10).argmax() #求距離0 位置10個距離的索引
print(l)
\>>>>>>>>>>>>>>
35
3:模擬多次隨機漫步
nwalks =5000 #組數 nsteps =1000 #步數 draws =np.random.randint(0,2,size=(nwalks,nsteps)) # 生成1,0 內的隨機5000個漫步值 print(draws) steps =np.where(draws>0 ,1,-1) walks =steps.cumsum(1) print(walks) >>>> [[1 1 1 ... 1 0 1] [0 0 0 ... 0 1 1] [1 0 0 ... 0 1 0] ... [0 0 0 ... 1 0 0] [1 1 0 ... 0 1 1] [1 0 1 ... 0 0 1]] [[ 1 2 3 ... 54 53 54] [ -1 -2 -3 ... -50 -49 -48] [ 1 0 -1 ... -34 -33 -34] ... [ -1 -2 -3 ... -64 -65 -66] [ 1 2 1 ... -8 -7 -6] [ 1 0 1 ... -8 -9 -8]]