Python Data Analysis Foundation--numpy Tutorial

Source: Internet
Author: User

Refer to link https://docs.scipy.org/doc/numpy-dev/user/quickstart.html

Basis

NumPy is primarily used to process multidimensional arrays, where elements are usually numbers, and the index value is the natural number

In NumPy, the dimension is called axes, and the total number ofaxes is rank (rank)

(for the concept of matrix rank, you can refer to https://www.zhihu.com/question/21605094 and

https://www.applysquare.com/topic-cn/78QfWkiPt/)

NumPy Array class is called Ndarray, alias Array

(Numpy.array differs from Array.array, which only handles one-dimensional arrays)

Ndarray Property

1. Ndim

Returns the rank of an array

2.shape

Returns the size of each dimension of an array

3.size

The total number of elements in the array, equal to the shape result

4.dtype

Array element type

5.itemsize

The size of the element type represented by the byte, equal to Ndarray.dtype.itemsize

Int32, 4 (32/8) Int64, 8 (64/8)

6.data

A buffer containing the actual elements of the array, usually without using

Example

1 ImportNumPy as NP2A=np.arange (reshape) (3,5)3 Print(a)4 5 Out :6Array ([[[0, 1, 2, 3, 4],7[5, 6, 7, 8, 9],8[10, 11, 12, 13, 14]])9 Ten Print(Type (a)) One Print(A.shape) A Print(A.ndim) - Print(A.dtype.name) - Print(a.item.size) the  - Out : - Numpy.ndarray -(3,5) +2 - 'Int32' +4

Create an array

1 #convert a list to an array2>>>ImportNumPy as NP3>>> a = Np.array ([2,3,4])4>>>a5Array ([2, 3, 4])6>>>A.dtype7Dtype'Int32')8>>> B = Np.array ([1.2, 3.5, 5.1])9>>>B.dtypeTenDtype'float64') One  A #creating two-dimensional arrays array ->>> B = Np.array ([(1.5,2,3), (4,5,6)]) ->>>b theArray ([[1.5, 2., 3. ], -[4., 5., 6. ]]) -  - #creating a one-dimensional array +>>> Np.arange (10, 30, 5 ) -Array ([10, 15, 20, 25]) +>>> np.arange (0, 2, 0.3 )                AArray ([0., 0.3, 0.6, 0.9, 1.2, 1.5, 1.8]) at  - #creating an array using Linespace ->>> fromNumPyImportPi ->>> np.linspace (0, 2, 9 )          -Array ([0., 0.25, 0.5, 0.75, 1., 1.25, 1.5, 1.75, 2. ]) ->>> x = np.linspace (0, 2*PI, 100 )         in>>> f = np.sin (x)
1>>> B = Np.arange (a). Reshape (3,4)2>>>b3Array ([[[0, 1, 2, 3],4[4, 5, 6, 7],5[8, 9, 10, 11]])6>>>7>>> B.sum (axis=0)#Sum by Column8Array ([12, 15, 18, 21])9>>> B.sum (Axis=1)#sum by RowsTenArray ([6, 22, 38]) One>>> B.min (Axis=1)#minimum value per line AArray ([0, 4, 8]) ->>> B.cumsum (Axis=1)#cumulative sum by column -Array ([[[0, 1, 3, 6], the[4, 9, 15, 22], -[8, 17, 27, 38]])
1 #Common Functions2>>> B = Np.arange (3)3>>>B4Array ([0, 1, 2])5>>>np.exp (B)6Array ([1. , 2.71828183, 7.3890561 ])7>>>np.sqrt (B)8Array ([0.  , 1. , 1.41421356])9>>> C = Np.array ([2.,-1., 4.])Ten>>>Np.add (B, C) OneArray ([2., 0., 6.])
1 #indexes, slices, iterations2 #one-dimensional arrays3>>a=np.arange (10) **34>>Print(a)5Array ([0, 1, 8, 216, 343, 729], dtype=int32)6>>a[2]788>>a[2:5]9Array ([8, +, +], dtype=int32)Ten>>a[:6:2]=-100 One>>a AArray ([ -10, 1, -10, -10, 216, 343, 729], dtype=int32) ->>a[::-1] -Array ([729, 343, 216, -10, -10, 1, -10], dtype=int32) the  - #Multidimensional Arrays ->>>deff (x, y): -...return10*x+y + ... ->>> B = Np.fromfunction (f, (5,4), dtype=int) +>>>b AArray ([[[0, 1, 2, 3], at[10, 11, 12, 13], -[20, 21, 22, 23], -[30, 31, 32, 33], -[40, 41, 42, 43]]) ->>> b[2,3] -23 in>>> B[0:5, 1]#second column -Array ([1, 11, 21, 31, 41]) to>>> b[:, 1]#second column +Array ([1, 11, 21, 31, 41]) ->>> B[1:3,:]#second row, third row theArray ([[[10, 11, 12, 13], *[20, 21, 22, 23]]) $>>B[-1]#equivalent to B[-1,:], last linePanax NotoginsengArray ([40, 41, 42, 43])

Python Data Analysis Foundation--numpy tutorial

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.