Python NumPy Library Installation Use notes

Source: Internet
Author: User
Tags arrays split unpack python list in python

This article mainly introduces the Python NumPy library installation Use notes, this article explains the installation and basic use of numpy, and every code has done a detailed explanation, the need for friends can refer to the

1. NumPy Installation

Install using the PIP Package management tool

The code is as follows:

$ sudo pip install NumPy

Install Ipython using the PIP Package management tool (interactive shell tool)

The code is as follows:

$ sudo pip Instlal Ipython

$ Ipython--pylab #pylab模式下, will automatically import scipy, NumPy, matplotlib module

2. NumPy Foundation

2.1. NumPy Array Object

The explanation and output after each line of code can be seen in detail

The code is as follows:

In [1]: A = Arange (5) # Create data

In [2]: A.dtype

OUT[2]: Dtype (' Int64 ') # Create the data type of an array

In [3]: A.shape # array dimension, output to tuple

OUT[3]: (5,)

In [6]: M = Array ([[1, 2], [3, 4]]) # array converts list to NumPy array object

In [7]: M # Creating multidimensional arrays

OUT[7]:

Array ([[1, 2],

[3, 4]])

In [ten]: M.shape # dimensions are 2 * 2

OUT[10]: (2, 2)

in [[]: M[0, 0] # Accessing elements of a specific position in a multidimensional array, subscript starting from 0

OUT[14]: 1

in [[]: M[0, 1]

OUT[15]: 2

2.2. Indexing and slicing of arrays

The code is as follows:

in [[]: A[2:4] # Slice operations like slices with Python list

OUT[16]: Array ([2, 3])

in [[]: A[2:5: 2] # Slice step size is 2

OUT[18]: Array ([2, 4])

in [[]: a[::-1] # Flip Array

OUT[19]: Array ([4, 3, 2, 1, 0])

In [m]: b = arange. Reshape (2, 3, 4) # Modify the dimensions of an array

in [[]: B.shape

OUT[21]: (2, 3, 4)

in [[]: B # Print Array

OUT[22]:

Array ([[[0, 1, 2, 3],

[4, 5, 6, 7],

[8, 9, 10, 11]],

[[12, 13, 14, 15],

[16, 17, 18, 19],

[20, 21, 22, 23]]]

in [[]: B[1, 2, 3] # Select specific elements

OUT[23]: 23

in [[]: b[:, 0, 0] # Ignoring a subscript can be replaced with a colon

OUT[24]: Array ([0, 12])

in [[]: B[1, 2, 3]

OUT[23]: 23

in [[]: b[:, 0, 0] # Ignore multiple subscript you can use ellipses instead

OUT[24]: Array ([0, 12])

in [[]: B.ravel () # array flattening operation

OUT[26]:

Array ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,

17, 18, 19, 20, 21, 22, 23])

In [$]: B.flatten () # Same as the Revel function, this function will request the allocation of memory to save the results

OUT[27]:

Array ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,

17, 18, 19, 20, 21, 22, 23])

In [m]: B.shape = (6, 4) # You can set the dimension directly on the Shape property assignment tuple

In [to]: b

OUT[31]:

Array ([[0, 1, 2, 3],

[4, 5, 6, 7],

[8, 9, 10, 11],

[12, 13, 14, 15],

[16, 17, 18, 19],

[20, 21, 22, 23]]

In [m]: B.shape = (6, 4) # Matrix Transpose

In [to]: b

OUT[31]:

Array ([[0, 1, 2, 3],

[4, 5, 6, 7],

[8, 9, 10, 11],

[12, 13, 14, 15],

[16, 17, 18, 19],

[20, 21, 22, 23]]

2.3. Combined array

The code is as follows:

In [1]: a = Arange (9). Reshape (3, 3) # Generate array objects and change dimensions

In [2]: A

OUT[2]:

Array ([[0, 1, 2],

[3, 4, 5],

[6, 7, 8]])

In [3]: b = A * 2 # to all elements of a array object by 2

In [4]: b

OUT[4]:

Array ([[0, 2, 4],

[6, 8, 10],

[12, 14, 16]])

#######################

In [5]: Hstack ((A, B)) # Horizontal combination array A and Group B

OUT[5]:

Array ([[0, 1, 2, 0, 2, 4],

[3, 4, 5, 6, 8, 10],

[6, 7, 8, 12, 14, 16]]

In [6]: Vstack ((A, B)) # Vertically combined arrays A and B

OUT[6]:

Array ([[0, 1, 2],

[3, 4, 5],

[6, 7, 8],

[0, 2, 4],

[6, 8, 10],

[12, 14, 16]])

In [7]: Dstack ((A, B)) # depth combined array, cascading array along Z axis

OUT[7]:

Array ([[[0, 0],

[1, 2],

[2, 4]],

[[3, 6],

[4, 8],

[5, 10]],

[[6, 12],

[7, 14],

[8, 16]]]

2.4. Split array

Copy code code as follows:

In [8]: A

OUT[8]:

Array ([[0, 1, 2],

[3, 4, 5],

[6, 7, 8]])

In [9]: Hsplit (A, 3) # divides the array horizontally into three identical sized child arrays

OUT[9]:

[Array ([0],

[3],

[6]]),

Array ([[1],

[4],

[7]]),

Array ([[2],

[5],

[8]])]

In [ten]: Vsplit (A, 3) # divides the array vertically into three sub arrays

OUT[10]: [Array ([[0, 1, 2]]), Array ([[3, 4, 5]]), Array ([[6, 7, 8]])]

2.5. The properties of the array

Copy code code as follows:

in [[]: A.ndim # Number of mantissa or axes of array

OUT[12]: 2

in [[]: A.size # Number of elements in an array

OUT[13]: 9

in [[]: A.itemsize # The number of bytes in memory in an array (Int64)

OUT[14]: 8

in [[]: A.nbytes # The total number of bytes in the array, size * itemsize

OUT[15]: 72

in [[]: A.T # and the transpose function, to find the transpose of an array

OUT[18]:

Array ([[0, 3, 6],

[1, 4, 7],

[2, 5, 8]])

2.6. Conversion of arrays

Copy code code as follows:

in [[]: A.tolist () # Converts a numpy array into a list in Python

OUT[19]: [[0, 1, 2], [3, 4, 5], [6, 7, 8]]

3. Common functions

Copy code code as follows:

in [[]: c = Eye (2) # build 2-D unit Matrix

In [to]: C

OUT[23]:

Array ([[1., 0.],

[0., 1.]]

in [[]: Savetxt ("Eye.txt", c) # Save matrix to file

In [5]: c, v = loadtxt ("Test.csv", delimiter= ",", usecols= (0, 1), unpack=true) # delimiter is, usecols for tuples represents the field data to get (0th and first paragraph in each row), Unpack is true to split the data stored in different columns, in C, V, respectively

in [[]: C

OUT[12]: Array ([1., 4., 7.])

in [[]: Mean (c) # Calculating mean mean of matrix C

OUT[13]: 4.0

in [[]: Np.max (c) # to find the maximum value in an array

OUT[14]: 7.0

in [[]: Np.min (c) # to find the minimum value in an array

OUT[15]: 1.0

in [[]: NP.PTP (c) # Returns the difference between the maximum and minimum values of an array

OUT[16]: 6.0

in [[]: Numpy.median (c) # Find the median of the array (average of two numbers in the middle)

OUT[18]: 4.0

in [[]: Numpy.var (c) # Calculates the variance of an array

OUT[19]: 6.0

In [m]: Numpy.diff (c) # Returns an array of the difference between adjacent array elements

OUT[20]: Array ([3., 3.])

in [[]: Numpy.std (c) # calculates the standard deviation of an array

OUT[21]: 2.4494897427831779

in [[]: Numpy.where (C > 3) # Returns an array of the subscript of an array element that satisfies a condition

OUT[22]: (Array ([1, 2])

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.