Python NumPy Library installation using notes

Source: Internet
Author: User
1. NumPy Installation
Installing 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 Objects

Explanations and outputs can be seen after each line of code

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 the array
In [3]: A.shape # Array of dimensions, 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 # Dimension is 2 * 2
OUT[10]: (2, 2)
In []: m[0, 0] # access elements at a specific location 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 operation similar to the slice operation of list in Python
OUT[16]: Array ([2, 3])
In []: A[2:5: 2] # slicing step is 2
OUT[18]: Array ([2, 4])
in [+]: a[::-1] # Flip Array
OUT[19]: Array ([4, 3, 2, 1, 0])
In []: 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 [all]: B[1, 2, 3] # Select a specific element
OUT[23]: 23
in [+]: b[:, 0, 0] # Ignoring a subscript can be substituted with a colon
OUT[24]: Array ([0, 12])
In [all]: B[1, 2, 3]
OUT[23]: 23
in [+]: b[:, 0, 0] # Ignoring multiple subscripts you can use ellipses instead of
OUT[24]: Array ([0, 12])
in [+]: B.ravel () # Flattening operation of arrays
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 Revel function, this function will request to allocate memory to save the result
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 [+]: B.shape = (6, 4) # You can set a dimension directly on the Shape property assignment tuple
in [+]: 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 [+]: B.shape = (6, 4) # Transpose of the Matrix
in [+]: 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. Combining arrays

The code is as follows:


In [1]: a = Arange (9). Reshape (3, 3) # generate an Array object and change the dimension
In [2]: A
OUT[2]:
Array ([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
In [3]: b = A * 2 # for a array of objects all elements multiply by 2
In [4]: b
OUT[4]:
Array ([[0, 2, 4],
[6, 8, 10],
[12, 14, 16]])
#######################
In [5]: Hstack ((A, B)) # Horizontal combination of arrays A and array 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)) # Vertical combination of arrays A and array 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 combination array, stacked 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. Splitting an array

The code is as follows:


In [8]: A
OUT[8]:
Array ([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
In [9]: Hsplit (A, 3) # splits the array horizontally into three sub-arrays of the same size
OUT[9]:
[Array ([[0],
[3],
[6]]),
Array ([[1],
[4],
[7]]),
Array ([[2],
[5],
[8]])
In []: Vsplit (A, 3) # splits the array vertically into three sub-arrays
OUT[10]: [Array ([[0, 1, 2]]), Array ([[[[[3], 4, 5]]), Array ([[6, 7, 8]])]

2.5. Properties of an array

The code is as follows:


In []: A.ndim # gives array of mantissa or number of axes of arrays
OUT[12]: 2
In []: a.size # Number of elements in an array
OUT[13]: 9
In []: A.itemsize # The number of bytes in memory of an element 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 Transpose function, to find the transpose of the array
OUT[18]:
Array ([[0, 3, 6],
[1, 4, 7],
[2, 5, 8]])

2.6. Conversion of arrays

The code is as follows:


in [+]: a.tolist () # convert NumPy array to list in Python
OUT[19]: [[0, 1, 2], [3, 4, 5], [6, 7, 8]]

3. Common functions

The code is as follows:


in [+]: c = Eye (2) # build 2-dimensional unit matrix
In [all]: C
OUT[23]:
Array ([[1., 0.],
[0., 1.]])
in [+]: Savetxt ("Eye.txt", c) # Save the matrix to a file
In [5]: c, v = loadtxt ("Test.csv", delimiter= ",", usecols= (0, 1), unpack=true) # delimiter is, Usecols represents the field data for the tuple to get (0th and first paragraph of each row), Unpack is true to split the data stored in different columns, respectively, into C, V
In []: C
OUT[12]: Array ([1., 4., 7.])
in [+]: Mean (c) # Calculates the mean mean of matrix C
OUT[13]: 4.0
in [+]: Np.max (c) # to find the maximum value in the array
OUT[14]: 7.0
In [All]: Np.min (c) # Find the smallest value in the 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 the median two)
OUT[18]: 4.0
in [+]: Numpy.var (c) # Calculates the variance of an array
OUT[19]: 6.0
In []: Numpy.diff (c) # Returns an array of difference values of 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 subscript elements that meet the criteria
OUT[22]: (Array ([1, 2]),)

  • 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.