"NumPy Foundation" 100-way NumPy Exercise--Advanced article

Source: Internet
Author: User

"NumPy Foundation" 100-way NumPy Exercise--Advanced article

@author: Wepon

@blog: http://blog.csdn.net/u012162613/article/details/42846777


Choose from numpy-100, as a familiar numpy practice. NumPy is just a numerical calculation of the toolkit, in the actual implementation of the algorithm to be familiar with the numpy is effective, so I do not intend to continue to write, to this article, the basic grammar has been enough, and then in practice to summarize the results may be better. and encountered problems refer to NumPy official website documentation.

Previous two articles:

"NumPy Foundation" 100 numpy Exercises--beginner and introductory articles

"NumPy Foundation" 100 NumPy Practice--apprentice Chapter




1, read the data in the file, such as a example.txt, the data is as follows:

1,2,3,45,,, 67,8,,9
The code to read into the file:
>>> Z = Np.genfromtxt ("Example.txt", delimiter= ",") >>> print z[[1. 2.3. 4.][5. Nan nan 6.] [7.8. Nan 9.]]

The first parameter of the Note:np.genfromtxt () function represents the path name of the file, delimiter is the delimiter, and in our Exampl.txt the delimiter is comma "," and is set to a comma. By the way, many data files are given in CSV suffix format, and CSV is the comma delimiter file.



2. Advanced features of NumPy: Generator. Create an array with the generator function.

>>> def generate (): ... for x in Xrange (TEN): ... yield x ... >>> Z = np.fromiter (Generate (), Dtype=float,co Unt=-1) >>> Print z[0. 1.2. 3.4. 5.6. 7.8. 9.]

NOTE: The yield keyword makes generate () the generator, which is the advanced feature of NumPy.


3, Bincount () function, the number of occurrences of each number in the output array. The subscript for output represents the number in the original array, and the value of output is the number of occurrences of that count, for example:

>>> z=[0,2,4,8,8]>>> Np.bincount (Z) Array ([1, 0, 1, 0, 1, 0, 0, 0, 2])

You can see that 8 appears two times.


4, Bincount (x, Weights=none, Minlength=none), where weights represents weights, usage is as follows:

>>> X = [1,2,3,4,5,6]>>> I = [1,3,9,3,4,1]>>> F = Np.bincount (i,x) >>> print f[0. 7.0. 6.5. 0.0. 0.0. 6.8

Note: With I as input, X is the weight. Analysis: f[0]=0, because I did not appear in the 0,f[1]=7, because I in 1 appeared two times, and their weight is 1, 6, so f[1]=1*1+1*6



5, considering a (w,h,3) image of (dtype=ubyte), compute the number of unique colors
# Author:nadav horeshw,h = 16,16i = Np.random.randint (0,2, (h,w,3)). Astype (np.ubyte) F = i[...,0]*256*256 + i[...,1]*256 +I [..., 2]n = Len (Np.unique (F)) print Np.unique (I)




6, a four-dimensional array, the next two dimensions for the units, calculate their sum, such as a 1*2*3*4 array, then the 3*4 dimension in the back, output 1*2 sum, for example:
>>> A = Np.random.randint (0,10, (1,2,3,4)) >>> print a[[[[2 7 9 7][6 6 8 2][0 0 9 3]][[5 4 1 4][5 7 9 7][8 4 1 4]]]]>>> a.reshape (A.shape[:-2] + ( -1,)) array ([[[[[2], 7, 9, 7, 6, 6, 8, 2, 0, 0, 9, 3],[5, 4, 1, 4, 5, 7, 9, 7 , 8, 4, 1, 4]]) >>> sum = A.reshape (A.shape[:-2] + ( -1,)). SUM (axis=-1) >>> print sum[[59 59]]



7, considering a one-dimensional vector D, how to compute means of subsets of D using a vector s of same size describing s Ubset indices?

>>> D = Np.random.uniform (0,1,100) >>> S = Np.random.randint (0,10,100) >>> d_sums = Np.bincount (S, weights=d) >>> d_counts = Np.bincount (s) >>> print d_sums[3.37619132 6.13452126 3.84121952 5.27577033 4.459793237.268070496.00231146 6.72050881 4.1281527 6.55666661]>>> print D_counts[6 12 8 7 7 12]>>> D_means = d_sums/d_counts>>> print d_means[0.56269855 0.5112101 0.38412195 0 .65947129 0.637113320.559082350.46171627 0.5600424 0.5897361 0.54638888]



8, in the array [1, 2, 3, 4, 5] in the middle of two digits inserted two 0
>>> Z = Np.array ([1,2,3,4,5]) >>> NZ = 3>>> Z0 = Np.zeros (Len (z) + (len (z)-1) * (NZ)) >>> Z0[::NZ+1] = z>>> print z0[1. 0.0. 0.2. 0.0. 0.3. 0.0. 0.4. 0.0. 0.5.]


9, how to multiply two-dimensional matrix and three-dimensional matrix?
>>> A = Np.ones ((3,3,2)) >>> B = 2*np.ones ((3,3)) >>> Print A * b[:,:,none][[[2. 2.][2. 2.][2. 2.]][[2. 2.][2. 2.][2. 2.]][[2. 2.][2. 2.][2. 2.]]]>>> B[:,:,none]array ([[[2.],[2.],[2.]],[[2.],[2.],[2.]],[[2.],[2.],[2.]])



10, how to exchange two lines of the matrix? Like the 12th line of exchange.
>>> A = Np.arange (+). Reshape (5,5) >>> print a[[0 1 2 3 4][5 6 7 8 9][10 11 12 13 14][15 16 17 18 19][2 0 24]]>>> A[[0,1]]array ([[0, 1, 2, 3, 4],[5, 6, 7, 8, 9]]) >>> A[[1,0]]array ([[5, 6, 7, 8, 9],[0 , 1, 2, 3, 4]]) >>> a[[0,1]] = a[[1,0]]>>> Print a[[5 6 7 8 9][0 1 2 3 4][10 11 12 13 14][15 16 17 18 1 9][20 21 22 23 24]]



"NumPy Foundation" 100-way NumPy Exercise--Advanced article

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.