Python NumPy Library Installation Use notes

Source: Internet
Author: User

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 : A = Arange (5) # Create data

In : A.dtype

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

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

OUT: (5,)

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

In : M # Creating multidimensional arrays

OUT:

Array ([[1, 2],

[3, 4]])

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

OUT: (2, 2)

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

OUT: 1

in [[]: M[0, 1]

OUT: 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: Array ([2, 3])

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

OUT: Array ([2, 4])

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

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

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

in [[]: B.shape

OUT: (2, 3, 4)

in [[]: B # Print Array

OUT:

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

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

OUT: Array ([0, 12])

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

OUT: 23

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

OUT: Array ([0, 12])

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

OUT:

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:

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:

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:

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 : a = Arange (9). Reshape (3, 3) # Generate array objects and change dimensions

In : A

OUT:

Array ([[0, 1, 2],

[3, 4, 5],

[6, 7, 8]])

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

In : b

OUT:

Array ([[0, 2, 4],

[6, 8, 10],

[12, 14, 16]])

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

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

OUT:

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

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

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

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

OUT:

Array ([[0, 1, 2],

[3, 4, 5],

[6, 7, 8],

[0, 2, 4],

[6, 8, 10],

[12, 14, 16]])

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

OUT:

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 : A

OUT:

Array ([[0, 1, 2],

[3, 4, 5],

[6, 7, 8]])

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

OUT:

[Array (,

,

]),

Array ([,

,

]),

Array ([,

,

])]

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

OUT: [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: 2

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

OUT: 9

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

OUT: 8

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

OUT: 72

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

OUT:

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: [[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:

Array ([[1., 0.],

[0., 1.]]

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

In : 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: Array ([1., 4., 7.])

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

OUT: 4.0

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

OUT: 7.0

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

OUT: 1.0

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

OUT: 6.0

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

OUT: 4.0

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

OUT: 6.0

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

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

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

OUT: 2.4494897427831779

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

OUT: (Array ([1, 2])

Related Keywords:
Related Article E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth \$300-1200 USD