Introduction to Python for statistics,analysis Note 3

Source: Internet
Author: User

First, generating arrays and matrices

1, Linspace (start,end,number), generates number between start and end number

>>> x = Linspace (0)>>>01  23456789.])

2, Logspace (start,end,number) produces numbers, between 10**start,10**end, the equivalent of an exponential function, the x-axis is divided into number of numbers, to calculate the exponent.

Same as 10**linspace (start,end,number) effect

3, Arange (l,u, s)

4,Meshgrid ()

>>> x = Arange (5)>>> y = arange (3)>>> x, y =Meshgrid (x, y)>>>Xarray ([[0,1,2,3,4],[0,1,2,3,4],[0,1,2,3,4]])>>>Yarray ([[0,0,0,0,0],[1,1,1,1,1],[2,2,2,2,2]])

5, Ix_ (a,b) Irregular selection of elements, where a, a can be a list or a tuple

>>> x = Reshape (Arange (25.0),(5,5))>>>Xarray ([[0.,1.,2.,3.,4.],[ 5.,6.,7.,8.,9.],[ Ten., One., A., -., -.],[  the., -., -., -., +.],[  -., +., A., at., -.]])>>> X[ix_ ([2,3],[0,1,2])] # Rows2&3, cols0,1and2Array ([[Ten., One., A.],[  the., -., -.]])>>> x[2:4,:3] # Same, Standard Slicearray ([[Ten., One., A.],[  the., -., -.]])>>> X[ix_ ([0,3],[0,1,4])] # No Slice equiv

Second, approximate

1,around, round

x=NP.  Random. Randn (3)   
X
Np. Around (x) 
Np. Around (x,2) #近似精度为2位小数 
[0.23073931  1.08865135-0.95564268][0.  1.-1.] [0.23  1.09-0.96]

2, floor (x), Ceil (x),

III. Statistical characteristics

1/sum, calculation and

A=np.reshape (Np.arange (Ten),(2,5));p rint A,'\ n'print Np.sum (a),'\ n'Print Np.sum (A,0),'\ n'Print Np.sum (A,1)[[0 1 2 3 4] [5 6 7 8 9]]  $ [ 5  7  9  One  -] [Ten  *]

2/prod the same characteristics as SUM, he is the product of the calculation

A=np.reshape (Np.arange (1,5),(2,2));p rint A,'\ n'? Print Np.prod (a),'\ n'Print Np.prod (A,0),'\ n'Print Np.prod (A,1),'\ n'[[1 2] [3 4]]  - [3 8] [ 2  A]

3,exp,log--equivalent to ln (),log10,sqrt,Square,absolute, ABS, Sign is the manipulation of elements

A=NP.RANDOM.RANDN (2,3);p rint A,'\ n'print Np.abs (a) print np.sign (a)
[[-0.35632202-0.56913468-0.5054189 ] [-0.13182024 1.62914028 1.57704769]] [[ 0.35632202 0.56913468 0.5054189 ] [ 0.13182024 1.62914028 1.57704769]][[-1. -1. -1.] [-1.1.1.]]

4, for the operation of complex numbers, the following operations are also the operation of the elements

    • Real (a) or a.real, the real part of the plural
    • Imag (A) or a.imag, imaginary part of a complex number
    • Conj (A) , conjugate, conjugate plural

5,Unique (A) is the operation of all elements, equivalent to the Python set (), the effect of the redo

6, in1d (A, B)

>>> x = arange (10.0)>>> y = arange (5.0,15.0) >>> in1d (x, y) array ([False, False, False, False, False, True, True, True, true, True], Dtype =
   
    bool)
   

7,union1d (A, B), returns the unique set of elements in 2 arrays. Equivalent set merge

8,intersect1d (A, B) is equivalent to the collection of the pickup

9,setdiff1d (b), in set A, not in set B

10,setxor1d (A, B), equivalent to take a set XOR, only in a set of elements

11. Sort

A=NP.RANDOM.RANDN (2,3);p rint aprint Np.sort (A,1) Print Np.sort (A,0) Print Np.sort (A,none) [[2.33262004-2.17579511  1.02508041] [-0.11651321  1.02673882  1.25183328]][[-2.17579511  1.02508041  2.33262004] [-0.11651321  1.02673882  1.25183328]][[-0.11651321-2.17579511  1.02508041] [ 2.33262004  1.02673882  1.25183328]][-2.17579511-0.11651321  1.02508041  1.02673882  1.25183328  2.33262004]

Note: The difference between a.sort () and sort (a), one will change the data structure, one will not.

>>> x = Randn (3)>>>Xarray ([2.70362768, -0.80380223, -0.10376901])>>>sort (x) array ([-0.80380223, -0.10376901,2.70362768])>>>Xarray ([2.70362768, -0.80380223, -0.10376901])>>> X.sort () # in-Place , changes x>>>Xarray ([-0.80380223, -0.10376901,2.70362768])

12,Max, amax, argmax , min, Amin , Argmin

Max is the method of the array, Amax is the function, Argtmax returns

A=NP.RANDOM.RANDN (3,5);p rint aprint Np.amax (A,1) Print Np.amax (A,0) Print Np.amax (A,none) [[-1.20363617e-01   6.09840964e-01-2.42821192e-01-1.87136859e+00-9.24036132e-01] [ -2.12137767e-04-4.49847000e-01   6.05104140e-02   5.00253683e-01    1.63359279e+00] [ -3.41458128e-01-9.52592527e-01   8.66845911e-01-1.26919405e+00    1.67080515e+00]][ 0.60984096  1.63359279  1.67080515][ -2.12137767e-04   6.09840964e-01   8.66845911e-01   5.00253683e-01   1.67080515e+00]1.67080515388

13,minimum (a , b), maximum (A, B) compares two arrays, returns the smallest or largest number in the corresponding position of two arrays

Introduction to Python for statistics,analysis Note 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.