100-Way NumPy Exercise
@author: Wepon
@blog: http://blog.csdn.net/u012162613/article/details/42784403
Today in deeplearning.net see Theano Tutorial, found a numpy-100-exercise, introduced numpy some basic usage, but not very specific, I use leisure time to knock some, right vote as translation, Add prototypes and detailed descriptions of the functions. Continuously updated.
First, beginner 10 way1. Import the NumPy package into the Python environment and name it NP
>>> Import NumPy as NP
2. View NumPy version and configuration information
>>> print np.__version__>>> np.__config__.show ()
3, create 0 vector, zeros function
>>> Z=np.zeros ((2,3)) >>> print z[[0. 0. 0.] [0. 0. 0.]
4. Set the second row of the above 0 vector to the third column element to 1. Note that the ranks of Python are starting from 0.
>>>z[1,2]=1>>> print z[[0. 0. 0.] [0. 0. 1.]
5. Arange function, create a vector in the given range.
>>> Z=np.arange (1,101) %1~100 range, note 101>>> print Z is not included
6. Reshape function, transform array to matrix
>>> Z = Np.arange (9). Reshape (3,3) >>> print z[[0 1 2] [3 4 5] [6 7 8]]
7, nonzero function, looking for the subscript of non-0 elements
>>> Nz=np.nonzero ([1,2,3,0,0,4,0]) >>> NZ (Array ([0, 1, 2, 5])
8. Eye function, generating unit vector
>>> Z=np.eye (3) >>> print z[[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]
9, diag function, diagonal diagonal.
>>> Z=np.diag ([1,2,3,4],k=0) %k=0, to [1,2,3,4] for diagonal >>> 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] on the diagonal line >>> 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] on the diagonal line >>> 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]]
10. Random function of randomness module, generate stochastic number
>>> Z = Np.random.random ((3,3)) >>> print z[[0.95171484 0.61394126 0.38864802] [0.41943918< c6/>0.9398714 0.31608202] [0.9993507 0.91717093 0.73002723]
Second, entry-level 10-channel1. Create a 8*8 "checkerboard" Matrix.
>>> Z=np.zeros ((8,8), dtype=int) >>> z[1::2,::2]=1 % 1, 3, 5, 7 rows &&0, 2, 4, 6 column elements are set to 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]]
2, Min (), Max () function
>>> Z=np.random.random ((10,10)) >>> zmin,zmax=z.min (), Z.max () >>> print zmin, zmax0.014230501632 0.99548760299
3, Function tile (a,reps), reps that is the number of repetitions, not only can be a number, but also can be an array. For example, constructing a checkerboard matrix:
>>> Z=np.tile (Np.array ([[[0,1],[0,1]]), (+) >>> 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]]
4, normalization, the matrix normalized to 0~1, that is, the smallest becomes 0, the largest becomes 1, the minimum and the maximum ratio of scaling.
>>> 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]]
5, Matrix point multiplication
>>> Z=np.dot (Np.ones ((5,3)), Np.ones ((3,2))) >>> print z[[3. 3.] [3. 3.] [3. 3.] [3. 3.] [3. 3.]
6, matrix addition, 5*5 matrix +1*5 vector, equivalent to each row plus 1*5 matrix
>>> 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.]
7, Linspace function, generate a given number of uniform distribution in a given interval.
Function prototype Linspace (start, Stop, num=50, Endpoint=true, Retstep=false)
>>> Z = Np.linspace (0,10,11,endpoint=true, retstep=false) >>> print z[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.]
Generates 11 numbers evenly distributed between 0~10, including 0 and 1.
If Endpoint=false, then 10 is not included.
If Retstep=false, the interval of every two numbers in a uniform interval is returned.
8, sort function. Call the random function in the random module to generate 10 random numbers, and then sort.
>>> Z = Np.random.random (Ten) >>> z.sort () >>> print z[0.15978787 0.28050494 0.35865916 0.40047826 0.45141311 0.4828367 0.66133575 0.66775779 0.69278544 0.98095989]
9, Allclose function, determine whether the two array within the error range is equal
Function Prototypes Allclose (A, B, rtol=1e-05, atol=1e-08), if absolute (A-a) <= (Atol + rtol * Absolute (b)) are equal.
A = Np.random.randint (0,2,5) B = np.random.randint (0,2,5) equal = Np.allclose (A, b) Print equal
10. Mean function, averaging
>>> Z = np.random.random (+) >>> m = Z.mean () >>> print m0.362299527973>>> A = Np.rando M.randint (0,2,5) >>> B = Np.random.randint (0,2,5) >>> equal = Np.allclose (A, b) >>> print Equalfalse
Note: Randint (min,max,num) generates an array of size num with a range of values Min~max
----------------------------2015/1/15-------------------------------
100-Way NumPy Exercise