"NumPy Foundation" 100 NumPy Practice--apprentice Chapter

Source: Internet
Author: User
Tags true true

"NumPy Foundation" 100 NumPy Practice--apprentice Chapter

@author: Wepon

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


Today, half an hour to sweep the Apprentice chapter of the 10 exercise, do not know how to translate apprentice (Apprentice ~ ~) This word, directly to Apprentice article as a topic. NumPy grammar straightforward like water ah, spend these time exercise a little wasted .... Anyway, in order to be more proficient in the later use of some ml of the package, the use of leisure time to sweep the basic grammar. Original address: https://github.com/rougier/numpy-100


1. Set the array property to read-only (read-only)

>>> Z = Np.zeros (Ten) >>> z.flags.writeable = false>>> Z[0] = 1Traceback (most recent call last): File "<stdin>", line 1, in <module>valueerror:assignment destination is read-only


2. Convert points in Cartesian coordinates (x, y) to Polar coordinate systems (R,T)
>>> Z = Np.random.random ((10,2)) >>> print z[[0.49094922 0.58585236][0.32265092 0.14445935][ 0.8078448 0.70284832][0.35341989 0.76888775][0.01212107 0.43668101][0.36924292 0.64075512][0.22349212 0.4561141 [0] 44836271 0.19591462][0.6639701 0.16595659][0.25559111 0.01741761]]>>> x, y = Z[:,0], Z[:,1]>>> R = Np.s QRT (x**2+y**2) >>> T = np.arctan2 (y,x)% seek the inverse tangent value >>> print r[0.76436518 0.35351396 1.07079829 0.84622337 0. 43684920.739531920.50792598 0.48929711 0.684396 0.2561839]>>> print t[0.87330531 0.42096163 0.71600756 1.13994582 1.54304621.048014031.11518737 0.41195346 0.24492773 0.06804118]


3. Get the subscript for the maximum/minimum value in the array, Argmax (), argmin () function

>>> Z = Np.random.random (Ten) >>> print z[0.90315764 0.06574957 0.86297424 0.46519603 0.01174512 0.60977 1880.52107975 0.6328012 0.12344019 0.05034712]>>> z.argmax () 0>>> Z.argmin (4)



4, Meshgrid () to produce lattice matrix, for three-dimensional surface of the sub-grid coordinates
>>> Z = Np.zeros ((10,10), [(' X ', float), (' Y ', float)]) >>> z[' x '], z[' y '] = Np.meshgrid (Np.linspace ( 0,1,10), Np.linspace (0,1,10)) >>> print Z


5, the various data types of the maximum/minimum value, mainly 8-bit, 32-bit, 64-bit integer, 32-bit, 64-bit floating-point type
>>> for Dtype in [Np.int8, Np.int32, Np.int64]: ... print np.iinfo (dtype). Min ... Print Np.iinfo (dtype). Max ... -128127-21474836482147483647-92233720368547758089223372036854775807>>> for Dtype in [Np.float32, Np.float64]:... print Np.finfo (dtype). Min ... Print Np.finfo (dtype). Max ... print np.finfo (dtype). EPS ... -3.40282e+383.40282e+381.19209e-07-1.79769313486e+ 3081.79769313486e+3082.22044604925e-16


6. Generate a matrix in which each element is composed of coordinates (x, y) and color (r,g,b)
>>> Z = Np.zeros, [(' Position ', [(' X ', float, 1),... (' Y ', float, 1)]),... (' Color ', [(' R ', float, 1),... (' G ', float, 1),... (' B ', float, 1)])  >>> Print z[((0.0, 0.0), (0.0, 0.0, 0.0)) ((0.0, 0.0), (0.0, 0.0, 0.0)) ((0.0, 0.0), (0.0, 0.0, 0.0)) ((0.0, 0.0), (0.0, 0.0, 0.0)) ((0.0, 0.0), (0.0, 0.0, 0.0)) ((0.0, 0.0), (0.0, 0.0, 0.0)) ((0.0, 0.0), (0.0, 0.0, 0.0)) ((0.0, 0.0), (0.0, 0.0, 0.0)) ((0.0, 0.0), (0.0, 0.0, 0.0)) ((0.0, 0.0), (0.0, 0.0, 0.0))]


7. Generate 10 coordinates (x, y) to calculate the distance between 22

>>> Z = Np.random.random ((10,2)) >>> x, y = np.atleast_2d (z[:,0]), np.atleast_2d (z[:,1]) >>> D = Np.sqrt ((x-x. T) **2 + (y-y. T) **2) >>> print D%d is 10*10, the diagonal value is 0


You can also directly use the Cdist function in scipy.
# Much faster with Scipyimport Scipyz = Np.random.random ((10,2)) d = scipy.spatial.distance.cdist (z,z) Print D




8. Generating two-dimensional Gaussian matrices
>>> X, Y = Np.meshgrid (Np.linspace ( -1,1,10), Np.linspace (-1,1,10)) >>> D = np.sqrt (x*x+y*y) >> > Sigma, mu = 1.0, 0.0 variance = 1, mean =0>>> G = Np.exp (-((D-MU) **2/(2.0 * sigma**2)) >>> print g[[0. 36787944 0.44822088 0.51979489 0.57375342 0.60279818 0.602798180.57375342 0.51979489 0.44822088 0.36787944][0.44822088 0.54610814 0.63331324 0.69905581 0.73444367 0.734443670.69905581 0.63331324 0.54610814 0.44822088][0.51979489 0.63331324 0.73444367 0.81068432 0.85172308 0.851723080.81068432 0.73444367 0.63331324 0.51979489][0.57375342 0.69905581 0.81068432 0.89483932 0.9401382 0.94013820.89483932 0.81068432 0.69905581 0.57375342][0.60279818 0.73444367 0.85172308 0.9401382 0.98773022 0.987730220.9401382 0.85172308 0.73444367 0.60279818][0.60279818 0.73444367 0.85172308 0.9401382 0.98773022 0.987730220.9401382 0.85172308 0.73444367 0.60279818][0.57375342 0.69905581 0.81068432 0.89483932 0.9401382 0.94013820.89483932 0.81068432 0.69905581 0.57375342][0.51979489 0.63331324 0.73444367 0.81068432 0.85172308 0.851723080.81068432 0.73444367 0.63331324 0.51979489][ 0.44822088 0.54610814 0.63331324 0.69905581 0.73444367 0.734443670.69905581 0.63331324 0.54610814 0.44822088][ 0.36787944 0.44822088 0.51979489 0.57375342 0.60279818 0.602798180.57375342 0.51979489 0.44822088 0.36787944]]


9, any function, determine whether the element in the matrix satisfies the given condition, is the word return true, otherwise returns false. Example: Determine if there is an entire column number of 0 in the two-dimensional matrix?
>>> Z = Np.random.randint (0,3, (2,10)) >>> print z[[1 0 1 2 2 0 1 0 1 0][0 0 1 2 2 0 1 0 0 1]]>>> Print Z.any (axis=0)% by column, full 0 is false[true false True True false true false true true]>>> print (~z.any (axis =0)). any () True


10. Find the number closest to the given value in the array
>>> Z=array ([[0,1,2,3],[4,5,6,7]]) >>> print z[[0 1 2 3][4 5 6 7]]>>> z=5.1>>> Np.abs ( z-z). Argmin () 5>>> print z.flat[np.abs (z-z). Argmin ()]5


Additional information:
Flat is a one-dimensional iterator that uses Z as a one-dimensional vector to traverse, z[5] will go beyond the boundaries of the original matrix, z.flat[5] is the correct usage.
In addition, you have to mention flatten, which flatten the matrix into one-dimensional vectors:
>>> Z.flatten () array ([0, 1, 2, 3, 4, 5, 6, 7])



"NumPy Foundation" 100 NumPy Practice--apprentice Chapter

Related 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.