numpy.mean和numpy.random.multivariate_normal(依據均值和協方差生成資料,提醒:計算協方差別忘了轉置)
>> import numpy as np
>>> A1_mean = [1, 1]
>>> A1_cov = [[2, .99], [1, 1]]
>>> A1 = np.random.multivariate_normal(A1_mean, A1_cov, 10) #依據指定的均值和協方差生成資料
>>> A1
array([[-1.72475813, 0.33681971],
[ 0.78643798, 0.76700529],
[ 0.61538183, -0.75786666],
[ 2.85758498, 2.55947038],
[ 1.78292279, 0.75539859],
[ 1.51245811, 2.2377212 ],
[ 1.86063512, 0.89370386],
[ 0.40500526, 0.83009172],
[ 1.39342622, 1.66581794],
[-1.75143864, -0.39855419]])
>>> np.mean(A1) #求全體數的均值
0.83136316789824638
>>> np.mean(A1,axis=0) #按列求均值(每列為一組),和預設有點差距
array([ 0.77376555, 0.88896078])
>>> np.mean(A1,axis=1)#按行求均值(每行為一組)
array([-0.69396921, 0.77672163, -0.07124242, 2.70852768, 1.26916069,1.87508966, 1.37716949, 0.61754849, 1.52962208, -1.07499641])
>>> np.cov(A1.T) #轉置後求協方差,和預設的差不多
array([[ 2.2502378 , 1.08232076],
[ 1.08232076, 1.10267326]])
>> np.cov(A1).shape #沒有轉置,就是10*10的矩陣了
(10, 10)
>>> np.cov(A1)
array([[ 2.12505159e+00, -2.00310018e-02, -1.41552934e+00,-3.07293225e-01, -1.05916056e+00, 7.47593157e-01,-9.96702035e-01, 4.38174408e-01, 2.80778370e-01,1.39453830e+00],
[ -2.00310018e-02, 1.88814725e-04, 1.33429563e-02,2.89658432e-03, 9.98377972e-03, -7.04690648e-03,9.39503788e-03, -4.13028670e-03, -2.64665199e-03,-1.31450922e-02],
[ -1.41552934e+00, 1.33429563e-02, 9.42905719e-01,2.04692712e-01, 7.05523031e-01, -4.97983225e-01,6.63918454e-01, -2.91874668e-01, -1.87030762e-01,-9.28923268e-01],
[ -3.07293225e-01, 2.89658432e-03, 2.04692712e-01,4.44361569e-02, 1.53159982e-01, -1.08105757e-01,1.44128163e-01, -6.33622388e-02, -4.06019746e-02,-2.01657302e-01],
[ -1.05916056e+00, 9.98377972e-03, 7.05523031e-01,1.53159982e-01, 5.27902989e-01, -3.72612687e-01,4.96772636e-01, -2.18393309e-01, -1.39944543e-01,-6.95060753e-01],
[ 7.47593157e-01, -7.04690648e-03, -4.97983225e-01,-1.08105757e-01, -3.72612687e-01, 2.63003275e-01,-3.50639779e-01, 1.54149758e-01, 9.87778314e-02,4.90598577e-01],
[ -9.96702035e-01, 9.39503788e-03, 6.63918454e-01,1.44128163e-01, 4.96772636e-01, -3.50639779e-01,4.67478036e-01, -2.05514692e-01, -1.31692037e-01,6.54073135e-01],
[ 4.38174408e-01, -4.13028670e-03, -2.91874668e-01,-6.33622388e-02, -2.18393309e-01, 1.54149758e-01,-2.05514692e-01, 9.03492470e-02, 5.78950160e-02,2.87546427e-01],
[ 2.80778370e-01, -2.64665199e-03, -1.87030762e-01,-4.06019746e-02, -1.39944543e-01, 9.87778314e-02,-1.31692037e-01, 5.78950160e-02, 3.70986254e-02,1.84257263e-01],
[ 1.39453830e+00, -1.31450922e-02, -9.28923268e-01,-2.01657302e-01, -6.95060753e-01, 4.90598577e-01,-6.54073135e-01, 2.87546427e-01, 1.84257263e-01,9.15148164e-01]])
>>>
本文轉自http://www.cnblogs.com/qqhfeng/p/5294583.html