The Numpy module can efficiently process data, provide array support, and many modules rely on it, such as: pandas, scipy, matplotlib
Install Numpy
First go to the website: https://www.lfd.uci.edu/~gohlke/pythonlibs/ to find numpy+mkl
My Python version is 3.6.1 and the system is 64 bit
So the corresponding downloaded package is:
After downloading the package, go to the directory where the package is located (for example: D:\installation package\installation package~Python\numpy-1.13.3+mkl-cp36-cp36m-win_amd64.whl)
Install using the following command
pip install numpy-1.13.3+mkl-cp36-cp36m-win_amd64.whl
The first installation error is as follows:
The reason for the above error is: The environment variable is not configured
solution:
Add the above path to the environment variable
After the addition is complete, re-execute
pip install numpy-1.13.3+mkl-cp36-cp36m-win_amd64.whl
After the installation is successful, then we can use Numpy
Numpy Tutorial
(1) Numpy creates a one-dimensional array
Syntax: numpy.array([element 1, element 2, ..., element n])
import numpy x = numpy.array(["1","2","5","11"]) print(x)
Running result: ['1' '2' '5' '11']
(2) Numpy creates a two-dimensional array
Syntax: numpy.array([[element1,element2,...,elementn],[element1,element2,...,elementn],...,[element1,element2,.. .,element n]])
import numpy y = numpy.array([[11,4,2],[2,6,1],[32,6,42 ]]) print (y)
operation result:
[[11 4 2]
[ 2 6 1]
[32 6 42]]
(3) Use sort to sort
import numpy # numpy.array([element 1, element 2,..., element n]) x = numpy.array([ " m " , " 2 " , " 5 " , " 11 " ]) #sort x x .sort() print (x) # numpy.array([[element1,element2,...,elementn],[element1,element2,...,elementn],...,[element 1, element 2,..., element n]]) y = numpy.array([[11,4,2],[2,6,1],[32,6,42 ]]) #sort y y .sort() print (y)
Result after sorting:
['11' '2' '5' 'm']
[[ 2 4 11]
[ 1 2 6]
[ 6 32 42]]
Note: The following operations are based on the sorted array
(4) Get the value in the array
For example: get the value 6 of the array y
#Get the value of 6 of the array y y1 = y[1][2 ] print (y1)
(5) Get the maximum and minimum values
#Get the maximum and minimum values in y y2 = y.max() print (y2) #The running result is: 1 y3 = y.min() print (y3) #The running result is: 42
(6) Slicing
Get the value in the array according to the defined subscript value
Syntax: array[start subscript: end subscript+1]
#Slice x1 = x[1:3] #From the element with subscript 1 to the element with subscript 2 print (x1) #Running result : ['2' '5'] x2 = x[:2 ] # From the beginning to the element with index 1 print (x2) #Running result : ['11' '2'] x3 = x[1:] #From the element with index 1 to the end print ( x3) #Run result : ['2' '5' 'm']