Python NumPy library Installation notes, pythonnumpy

Source: Internet
Author: User

Python NumPy library Installation notes, pythonnumpy

1. Install NumPy
Use pip package management tool for Installation
Copy codeThe Code is as follows:
$ Sudo pip install numpy

Use pip package management tool to install ipython (Interactive shell tool)
Copy codeThe Code is as follows:
$ Sudo pip instlal ipython
$ Ipython -- pylab # In pylab mode, the SciPy, NumPy, and Matplotlib modules are automatically imported.

2. NumPy Basics

2.1. NumPy array object

For details, refer to the explanation and output of each line of code.
Copy codeThe Code is as follows:
In [1]: a = arange (5) # create data
In [2]: a. dtype
Out [2]: dtype ('int64') # create an array data type
In [3]: a. shape # dimension of the array. The output is tuple.
Out [3]: (5 ,)
In [6]: m = array ([[1, 2], [3, 4]) # array converts a list to a NumPy array object
In [7]: m # create a multi-dimensional array
Out [7]:
Array ([[1, 2],
[3, 4])
In [10]: m. shape # The dimension is 2*2.
Out [10]: (2, 2)
In [14]: m [0, 0] # access the element at a specific position In the multi-dimensional array. The subscript starts from 0.
Out [14]: 1
In [15]: m [0, 1]
Out [15]: 2

2.2. Index and slice of the array

Copy codeThe Code is as follows:
In [16]: a [2: 4] # The slicing operation is similar to the slicing operation of list In Python.
Out [16]: array ([2, 3])
In [18]: a [2: 5: 2] # The Slice step size is 2.
Out [18]: array ([2, 4])
In [19]: a [:-1] # flip an array
Out [19]: array ([4, 3, 2, 1, 0])
In [20]: B = arange (24). reshape (2, 3, 4) # modify the array dimension
In [21]: B. shape
Out [21]: (2, 3, 4)
In [22]: B # print the 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 [23]: B [1, 2, 3] # select a specific element
Out [23]: 23
In [24]: B [:, 0, 0] # ignore a subscript and replace it with a colon.
Out [24]: array ([0, 12])
In [23]: B [1, 2, 3]
Out [23]: 23
In [24]: B [:, 0, 0] # ignore multiple subscripts and use ellipsis instead.
Out [24]: array ([0, 12])
In [26]: B. ravel () # flattened Array Operations
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 [27]: B. flatten () # It has the same function as revel. This function will request memory allocation 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 [30]: B. shape = (6, 4) # You can directly assign values to the shape attribute to set the dimension.
In [31]: 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 [30]: B. shape = (6, 4) # transpose of the matrix
In [31]: 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. Combination Array

Copy codeThe Code is as follows:
In [1]: a = arange (9). reshape (3, 3) # generate an array object and change the dimension
In [2]:
Out [2]:
Array ([[0, 1, 2],
[3, 4, 5],
[6, 7, 8])
In [3]: B = a * 2 # multiply all elements of an array object by 2
In [4]: B
Out [4]:
Array ([[0, 2, 4],
[6, 8, 10],
[12, 14, 16])
#######################
In [5]: hstack (a, B) # horizontally composite array 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) # vertically combines array 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) # deeply combines arrays and cascade arrays along the 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 the Array

Copy codeThe Code is as follows:
In [8]:
Out [8]:
Array ([[0, 1, 2],
[3, 4, 5],
[6, 7, 8])
In [9]: heatmap (a, 3) # splits the array horizontally into three child arrays of the same size.
Out [9]:
[Array ([0],
[3],
[6]),
Array ([[1],
[4],
[7]),
Array ([[2],
[5],
[8])]
In [10]: 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. array attributes

Copy codeThe Code is as follows:
In [12]: a. ndim # returns the ending number of the array or the number of axes of the array.
Out [12]: 2
In [13]: a. size # number of elements In the array
Out [13]: 9
In [14]: a. itemsize # number of nodes occupied by elements In the array In memory (int64)
Out [14]: 8
In [15]: a. nbytes # Total number of bytes occupied by the array, size * itemsize
Out [15]: 72
In [18]: a. T # returns an array transpose, just like the transpose function.
Out [18]:
Array ([[0, 3, 6],
[1, 4, 7],
[2, 5, 8])

2.6. array Conversion
Copy codeThe Code is as follows:
In [19]: a. tolist () # convert the NumPy array into a list In python
Out [19]: [0, 1, 2], [3, 4, 5], [6, 7, 8]

3. Common functions

Copy codeThe Code is as follows:
In [22]: c = eye (2) # construct a 2-dimensional unit matrix
In [23]: c
Out [23]:
Array ([[1., 0.],
[0., 1.])
In [25]: savetxt ("eye.txt", c) # Save the Matrix to the file.
In [5]: c, v = loadtxt ("test.csv", delimiter = ",", usecols = (0, 1), unpack = True) # The delimiter is, usecols indicates the field data to be obtained (the zeroth segment and the first segment of each row). If unpack is set to True, data in different columns is split and stored in c, v, respectively.
In [12]: c
Out [12]: array ([1., 4., 7.])
In [13]: mean (c) # calculate the mean of matrix c
Out [13]: 4.0
In [14]: np. max (c) # calculate the maximum value In the array
Out [14]: 7.0
In [15]: np. min (c) # Calculate the minimum value In the array
Out [15]: 1.0
In [16]: np. ptp (c) # returns the difference between the maximum and minimum values of the array.
Out [16]: 6.0
In [18]: numpy. median (c) # Find the median In the array (the average of the two numbers In the middle)
Out [18]: 4.0
In [19]: numpy. var (c) # Calculate the variance of the array
Out [19]: 6.0
In [20]: numpy. diff (c) # returns an array composed of the Difference values of adjacent array elements.
Out [20]: array ([3 .., 3.])
In [21]: numpy. std (c) # Calculate the standard deviation of the array
Out [21]: 2.4494897427831779
In [22]: numpy. where (c> 3) # returns an array consisting of subscript of the array element that meets the conditions.
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.