基本的numpy用法
shape函数:numpy.core.fromnumeric中的函数,它的功能是查看矩阵或者数组的维数。
>>> e = eye(3 ) # 3 *3 的单位矩阵
>>> e
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]] )
>>> e.shape
(3 , 3 )
#
>>> b =array([1 ,2 ,3 ,4 ])
>>> b.shape
(4 ,)
>>> b.shape[0 ]
4
np.random.shuffle:调整数组或者列表的顺序,返回值为None
numpy.random .shuffle (x ) : Modify a sequence in -place by shuffling its contents.
Parameters: x :The array or list to be shuffled.
Returns: None
##例子----------------------------
z = np.array (np.arange (10 ))%3
print z
print np.random .shuffle (z )
print z
输出:
[0 1 2 0 1 2 0 1 2 0 ]
None
[1 2 2 2 1 0 0 0 0 1 ]
np.random.multivariate_normal:多维正态分布的采样函数
功能:从参数为mean(均值,决定样本的维度),cov(协方差矩阵,对称非负矩阵) 的正太分布中采样,获得样本
参数:
mean:1 -D array_like, of length N,1 维的数组,长度代表样本的维度
cov:协方差矩阵,非负对称矩阵(N*N)
size:决定输出样本的形式,例如 m*n*N,表示输出为m*n的矩阵,每个元素是个样本(1 *N 的向量)
Returns:
out : ndarray
#
mean = (1 , 2 )
cov = [[1, 0], [0, 1]]
x = np.random.multivariate_normal(mean, cov, (3 , 3 ))
print x.shape
print x
#输出
(3 L, 3 L, 2 L)
[[[-0.12592769 2.24209966]
[ 0.97144764 1.49548991]
[ 2.93351284 1.41213514]]
[[ 2.40776703 2.81647166]
[ 1.31714065 1.46549801]
[ 2.71169081 1.87501286]]
[[ 2.38085976 2.36607448]
[ 0.88649646 1.47525901]
[ 1.43455155 2.45520959]] ]
print np.zeros(5 ,dtype=np.int)
print np.zeros((2 ,1 ))
#输出
[0 0 0 0 0 ]
[[ 0.]
[ 0.]]
numpy.argwhere:发现数组中满足某个条件的索引
x = np.arange(6 ).reshape(2 ,3 )
print x
print np.argwhere(x>2 )
# 输出
[[0 1 2]
[3 4 5]]
[[1 0] #索引(1,0),(1,1),(1,2)
[1 1]
[1 2]]