@author:wepon
@blog:http://blog.csdn.net/u012162613/article/details/42784403
今天在deeplearning.net上看theano tutorial,发现一个numpy-100-exercise,介绍numpy一些基本用法的,不过不是很具体,我利用闲暇时间照着敲了一些,权且当作翻译吧,增加函数的原型和详细介绍。持续更新。
>>> import numpy as np
>>> print np.__version__ >>> np.__config__.show()
>>> z=np.zeros((2,3)) >>> print z [[ 0. 0. 0.] [ 0. 0. 0.]]
>>>z[1,2]=1 >>> print z [[ 0. 0. 0.] [ 0. 0. 1.]]
>>> z=np.arange(1,101) %1~100范围,注意不包括101 >>> print z
>>> Z = np.arange(9).reshape(3,3) >>> print Z [[0 1 2] [3 4 5] [6 7 8]]
>>> nz=np.nonzero([1,2,3,0,0,4,0]) >>> nz (array([0, 1, 2, 5]),)
>>> z=np.eye(3) >>> print z [[ 1. 0. 0.] [ 0. 1. 0.] [ 0. 0. 1.]]
>>> z=np.diag([1,2,3,4],k=0) %k=0,以[1,2,3,4]为对角线 >>> print z [[1 0 0 0] [0 2 0 0] [0 0 3 0] [0 0 0 4]] >>> z=np.diag([1,2,3,4],k=1) %k=1,[1,2,3,4]在对角线上一行 >>> print z [[0 1 0 0 0] [0 0 2 0 0] [0 0 0 3 0] [0 0 0 0 4] [0 0 0 0 0]] >>> z=np.diag([1,2,3,4],k=-1) %k=-1,[1,2,3,4]在对角线下一行 >>> print z [[0 0 0 0 0] [1 0 0 0 0] [0 2 0 0 0] [0 0 3 0 0] [0 0 0 4 0]]
>>> Z = np.random.random((3,3)) >>> print Z [[ 0.95171484 0.61394126 0.38864802] [ 0.41943918 0.9398714 0.31608202] [ 0.9993507 0.91717093 0.73002723]]
>>> z=np.zeros((8,8),dtype=int) >>> z[1::2,::2]=1 %1、3、5、7行&&0、2、4、6列的元素置为1 >>> print z [[0 0 0 0 0 0 0 0] [1 0 1 0 1 0 1 0] [0 0 0 0 0 0 0 0] [1 0 1 0 1 0 1 0] [0 0 0 0 0 0 0 0] [1 0 1 0 1 0 1 0] [0 0 0 0 0 0 0 0] [1 0 1 0 1 0 1 0]] >>> z[::2,1::2]=1 >>> print z [[0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0] [0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0] [0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0] [0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0]]
>>> z=np.random.random((10,10)) >>> zmin,zmax=z.min(),z.max() >>> print zmin,zmax 0.014230501632 0.99548760299
>>> z=np.tile(np.array([[0,1],[0,1]]),(4,4)) >>> print z [[0 1 0 1 0 1 0 1] [0 1 0 1 0 1 0 1] [0 1 0 1 0 1 0 1] [0 1 0 1 0 1 0 1] [0 1 0 1 0 1 0 1] [0 1 0 1 0 1 0 1] [0 1 0 1 0 1 0 1] [0 1 0 1 0 1 0 1]]
>>> Z = np.random.random((5,5)) >>> Zmax,Zmin = Z.max(), Z.min() >>> Z = (Z - Zmin)/(Zmax - Zmin) >>> print Z [[ 0. 0.32173291 0.17607851 0.6270374 0.95000808] [ 0.49153473 0.70465605 0.61930085 0.00303294 1. ] [ 0.4680561 0.88742782 0.29899683 0.80704789 0.12300414] [ 0.05094248 0.23065875 0.82776775 0.07873239 0.50644422] [ 0.27417053 0.78679222 0.517819 0.5649124 0.4716856 ]]
>>> z=np.dot(np.ones((5,3)),np.ones((3,2))) >>> print z [[ 3. 3.] [ 3. 3.] [ 3. 3.] [ 3. 3.] [ 3. 3.]]
>>> Z = np.zeros((5,5)) >>> Z += np.arange(5) >>> print Z [[ 0. 1. 2. 3. 4.] [ 0. 1. 2. 3. 4.] [ 0. 1. 2. 3. 4.] [ 0. 1. 2. 3. 4.] [ 0. 1. 2. 3. 4.]]
>>> Z = np.linspace(0,10,11,endpoint=True, retstep=False) >>> print Z [ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.]
>>> Z = np.random.random(10) >>> Z.sort() >>> print Z [ 0.15978787 0.28050494 0.35865916 0.40047826 0.45141311 0.4828367 0.66133575 0.66775779 0.69278544 0.98095989]
A = np.random.randint(0,2,5) B = np.random.randint(0,2,5) equal = np.allclose(A,B) print equal
>>> Z = np.random.random(30) >>> m = Z.mean() >>> print m 0.362299527973 >>> A = np.random.randint(0,2,5) >>> B = np.random.randint(0,2,5) >>> equal = np.allclose(A,B) >>> print equal False
原文地址:http://blog.csdn.net/u012162613/article/details/42784403