Python Learning Note (ii)--numpy

Source: Internet
Author: User

Python can use the list as an array, but because the list element can be any object, saving a list requires saving all pointers and elements. Consumes memory very much.

This study blog: use Python to make scientific calculations to organize notes, to wait for backup.

The first is the NumPy library import

Importnumpy as NP

Create an array

Array

Use array to create multidimensional arrays

A = Np.array ([[1, 2, 3, 4],[4, 5, 6, 7], [7, 8, 9,10]])

Shape

Use shape to get the number of array dimensions

A.shape (3,4)

Reshape

Use reshape to change the size of an array

B=a.reshape ((2,6))

Note, however, that the two arrays share memory, modify one, and the other changes.

Special functions for creating arrays

Arange: Specify start value, end value (not included), step size

Np.arange (0,10,1.5) array ([0., 1.5,  3.,  4.5, 6.,  7.5,  9.])

Linspace: Specifies the start value, the final value (including), the arithmetic progression of the number of elements

Np.linspace (0,10,7) array ([0., 1.66666667, 3.33333333, 5., 6.66666667, 8.33333333,10.)

Logspace: Specifies the start value, the final value (including), the geometric series of the number of elements

Np.logspace (0,1,7) array ([1., 1.46779927, 2.15443469, 3.16227766, 4.64158883, 6.81292069, 10.])

Access element

-Integer sequence

A=np.arange (10,1,-1) B=a[4,4,4,-2]barray ([6,6,6,3])

Note: B and a do not share memory, so change either one, and the other will not change.


-Boolean array

Collects the elements in the array that correspond to the subscript true.

A=np.arange (5,0,-1) A[np.array ([True, True, True,false, False])]array ([5,4,3])

Attention:

1. Two array does not share memory.

2. Can only correspond to a Boolean array, not a Boolean list a[[True, True, True,false,false]]

Otherwise the output should be

Array ([4,4,4,5,5])


Array of structures

Create a Dtype object with a dictionary of two keywords : names,formats.

Import NumPy as Nppeople=np.dtype ({                 ' names ': [' name ', ' gender ']                 ' formats ': [' S32 ', ' S32 ']}) A=np.array ([' Liujingjing ', ' Female '), (' Chengyin ', ' Male ')],dtype=people)

Ufunc operations

X=np.linspace (0,np.pi,10) t=np.sin (x,x)

Here, the second parameter of sin is used to save the result, which indicates that X has been overwritten , just like the effect of T.

Broadcasting

Broadcasts are arrays of different sizes, allowing them to operate on a par.

A=np.arange (10,70,10). Reshape ( -1,1) b=np.arange (1,6) C=a+bcarray ([One, one,, A, a, a],       [21,22, +, +],       [ 31,32, (       62,       63, 64, 65]), [51,52, 41,42, si, si], [61], [+]]


Matrix Product

DOT, inner, outer

Where DOT does the inner product, inner the last dimension to do the inner product, outer the column vector and the line vector to do the matrix product.

Welcome to participate in the discussion and concern this blog and the Micro Blog as well as the Personal homepage Follow-up content continue to update Oh ~

reproduced please respect the author's labor, complete reservations The above text as well article links , thank you for your support!

Python Learning Note (ii)--numpy

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.