Python Data processing--numpy_3

Source: Internet
Author: User

Through the previous two times of study, basically to numpy have a certain understanding, so, then further to NumPy study. At the same time, finally, with an interesting example, deepen the understanding of numpy.

ImportNumPy as Npxarr= Np.array ([1.1, 1.2, 1.3, 1.4, 1.5]) Yarr= Np.array ([2.1, 2.2, 2.3, 2.4, 2.5]) Cond=Np.array ([True, False, True, True, false])#if the value in cond is T, select the value of Xarr, otherwise select from Yarr. This mode is: x if condition else y (condition is equal to x, select X, otherwise y)result = [(xifCElsey) forx, Y, CinchZip (Xarr, Yarr, cond)]#Print Result" "the above formula can be replaced by a function: Np.where (,,,). The first parameter is a decision, and the result of this decision is output according to the following two parameters. Where the second is the true result output of the first argument, and the third parameter is the first false result output. " "result=Np.where (Cond,xarr,yarr)#Print Result fromNumpy.randomImportRandnarr= RANDN (bis)#The value greater than 0 becomes 2, and the value less than 0 becomes-2result = Np.where (arr > 0, 2, 2)#Print Result#only change the value greater than 0 to 2, the other constantresult = Np.where (arr > 0, 2, arr)#Print Result" "np.where (rond1 & rond2, 0, Np.where (rond1, 1, Np.where (Rond2, 2, 3)))" "Ax= Np.random.randn (5, 4)#Print AxA =ax[0,:]#calculate the mean of each row using Axis = 1 1 for the row#print Ax.mean (Axis=1)#print A.mean ()b =ax[:,0]#calculate the mean of each column using Axis = 0 0 for columns#print Ax.mean (axis=0)#print B.mean ()ay = Np.array ([[0,1,2],               [3,4,5],               [6,7,8]])#The number of the first and last number of each column is calculated, and the return is still an array. 0 Delegate Columns#print ay.cumsum (0)#calculates the product of the first and last number of rows, and returns an array. 1 Representative Row#print Ay.cumprod (1)#The number of positive numbers in AX is calculated, and the Boolean values are coerced to 1 (True) and 0 (False). #print (Ax > 0). sum ()
ImportNumPy as NPA= Np.arange (10) Np.save ("Some_array", a) b= Np.load ("Some_array.npy")#Print B#load txt and comma delimited file (CSV) mode. Save in Np.savetxt way#ab = Np.loadtxt ("Array_ex.txt", delimiter= ",")#linear algebra#the establishment of a one-dimensional array consists of 3 1. Np.ones (3) x= Np.array ([[+], [4,5,6]]) y= Np.array ([[6, 23],[-1, 7],[8, 9]])#calculates the product of a two array. Dot () functionX.dot (y) Np.dot (x, y) Np.dot (x, Np.ones (3)) fromNumpy.randomImportRandn fromNumpy.linalgImportINV, QRx= Randn (5, 5) Mat=X.t.dot (X)#calculates the inverse of an arrayINV (MAT) Mat.dot (INV (MAT))#calculate QR DecompositionQ, r =QR (MAT)#Print R

Finally, take the example of random walk, use numpy to deepen understanding of it.

ImportRandomImportNumPy as NP fromNumpy.randomImportRANDINTB= Np.random.randint (0,2)#The randint in the numpy cannot take the right-hand value, that is, the example (0,2) cannot be taken to 2A = Random.randint (0,2)#The randint in random is the value that can be taken to the right end, (0,2) is the random value in 0,1,2#Random Walk (normal edition)Position =0walk=[Position]steps= 10 forIinchxrange (steps):#This sentence is actually a logical judgment sentence, Random.randint is the logical judgment condition, compares with 0. Standard statement: A If condition else B. The judging condition is greater than 0, select a, and vice versa, select B. Step = 1ifRandom.randint (0,1)Else-1position+=step walk.append (position)#Print Walk#Random Walk (ascending version)Nsteps = 10draws= Np.random.randint (0,2, size=nsteps) Steps= Np.where (Draws > 0, 1, 1)#turns the result into an arrayWalk =steps.cumsum ()#Print Walk#only arrays can be used this waywalk.min () Walk.max ( )#Judging from 0 to 2 steps, how long it takes, how many times. (Np.abs (walk) >= 2). Argmax ()#Multiple random walksnwalks = 100nsteps= 100draws= Np.random.randint (0,2, size=(Nwalks, nsteps)) steps= Np.where (Draws > 0, 1, 1)#calculates the cumulative sum of each row. "1" stands for Row, "0" for columnWalks = Steps.cumsum (1) Walks.min () Walks.max ()#computes a Boolean value greater than 20 or-20 (true,false)Np.abs (walks) >= 20#calculate a Boolean value greater than 20 or-20 in each rowHITS20 = (Np.abs (walks) >=). Any (1)#calculate the line that reaches 20 or-20, how manyhits20.sum ()#calculates the number of walks accumulated for rows up to 20 or-20WALKS[HITS20]#calculate the number of steps for the first stroll to 20 or-20 for each line that reaches 20 or-20Crossing_times = (Np.abs (WALKS[HITS20]) >=). Argmax (1)#calculates the mean of the first walk to 20 or-20 of each row that reaches 20 or-20PrintCrossing_times.mean ()

Python Data processing--numpy_3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.