numpy function
1.shape function
Its function is to read the length of the matrix, such as shape[0, to read the first dimension of the matrix. Its input parameter can make an integer representation dimension, or it can be a matrix 2.tile function
His function is to repeat an array. For example Tile (a,n), the function is to repeat the array a n times, forming a new array of 3. Array Objects
An array is the most important object in NumPy, and we usually use the array () method to create the object, and his argument is a list object 4.reshape method
It is the shape that is used for the array of the table, which is the method in the Array object 5.dtype array element type
The type of an array element can be obtained by the Dtype property in 6.arange
The Arange function is used to create equal-margin groups, with very high frequencies, arange very similar to the range function, and Python people must often use the range function, for example, in the For loop, almost all of the range, 7.linespace create arithmetic progression
Used to create an array, and an array of arithmetic progression, it 蕞 commonly used three parameters, of course, not more than three parameters, the first parameter represents the starting point, the second parameter represents the termination point, the third parameter represents the number of series 8.logspace create geometric progression
Create a geometric progression 9,zeros create a 0 matrix
Create an array of array 10.ones and empty that are all 0
You can use ones to create an array of arbitrary dimensions and elements, with elements of 1. The empty function uses the same method, except that it creates an array of all elements that are empty, so it is the fastest way to create an array. 11.fromfunction Creating an array in a functional form
Create an array of 12 from a function . Array slices
Slice the array
* NumPy introduces All
From numpy Import *
* Create a one-dimensional array that contains 10 elements
A = Arange (
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) '
* You can use a subscript to get the value in the array, or you can change his <br/>
' python
A[5]
5
a[5]=54
a
array ([0, 1, 2, 3, 4, Si, 6, 7, 8, 9])
<div class= "Se-preview-section-delimiter" ></div>
Use a colon to get several consecutive elements, such as getting 4th to 6th
A[3:6]
Array ([3, 4, si])
<div class= "Se-preview-section-delimiter" ></div>
The rule to index here is [), take the upper bound, do not take the lower bound
* Omit the number before the colon, indicating starting from 0
A[:6]
Array ([0, 1, 2, 3, 4, si])
<div class= "Se-preview-section-delimiter" > </div>
Use negative numbers to indicate the number of nth numbers from the back forward (nth number not taken)
A[:-1]
Array ([0, 1, 2, 3, 4, Si, 6, 7, 8])
<div class= " Se-preview-section-delimiter "></div>
You can also set the step length to read an array element
A[1:9:2]
Array ([1, 3, 7])
<div class= "Se-preview-section-delimiter" ></div>
The step is-1, the other argument is not set, that is, the array is in a flashback sequence
A[::-1]
Array ([9, 8, 7, 6, Si, 4, 3, 2, 1, 0])
<div class= " Se-preview-section-delimiter "></div>
13. Slice array Shared memory
Using a slice method to get part of an array without creating a new array, and the slice is just a view of the original array, changing the value in the slice also changes the element values of the original array
To ensure that the values of the original array do not change, we'd better use the copy method to get the array 14. List subscript access method for arrays
In addition to using slices to access the elements of an array, you can also use an integer list as the subscript access method
Array index counts starting at 0
Continue to operate with an array of section 12
* If you want to read and remove the array elements marked as 2,4,6,8, we can write the list [2,4,6,8] as subscript
a[[1,3,5,7]]
Array ([1, 3, 7])
<div class= "Se-preview-section-delimiter" ></div>
This method gets an array that does not share memory with the original array, so changing an element of an array does not change the value of the corresponding element in the other array 15. Reading an array as subscript
We can also use arrays as subscript when we use the list to read the array most subscript
<div class= "Se-preview-section-delimiter" ></div>
#引入Numpy from
numpy import *
<div class = "Se-preview-section-delimiter" ></div>
#创建二维数组, as subscript array
e=arange. Reshape (2,5)
< Div class= "Se-preview-section-delimiter" ></div>
#创建数组啊, as an array to read
A=arange (100,50,-3)
<div class= "Se-preview-section-delimiter" ></div>
#使用e作为下标, indexed by array
f=a[e]
F
< Div class= "Se-preview-section-delimiter" ></div>
Output is
Array ([[[+], the <div, the "," )
, [a] class= "Se-preview-section-delimiter" ></div>
16. Multidimensional Array Slice access
Multidimensional array and one-dimensional array of access methods similar, this article describes the use of slices to access a two-dimensional array, multidimensional array of the same reason
<div class= "Se-preview-section-delimiter" ></div>
#引入Numpy from
numpy import *
<div class = "Se-preview-section-delimiter" ></div>
#创建一维数组
a=arange (
) print (a)
<div class= " Se-preview-section-delimiter "></div>
#将一维数组重新组织成一个二维数组
b=a.reshape (7,7)
print (b)
<div class= "Se-preview-section-delimiter" ></div>
#使用切片来读取第一行中的第二和第三个数,
<div class= " Se-preview-section-delimiter "></div>
#其中逗号前的数字表示行的范围, after the comma indicates the column's range
C=b[0,2:5]
print (c)
<div class= "Se-preview-section-delimiter" ></div>
#可以返回二维数组
D=b[3:5,1:3]
print ( d)
<div class= "Se-preview-section-delimiter" ></div>
The output is:
[0 1 2 3 4
5 6 7 8-A-the 9 25 26 27 28 The [
0 1 2 3 4 5 6 ] [
7
8 9 [an] [
35 36 3] [a] [a] [a] (a) [a] [a] 7 [m]]
[2 3 4]
[[a
] [<div]]
the se-preview-sectio of the class= " N-delimiter "></div>
17.diag function
Diag () returns a diagonal element of a matrix, or creates a diagonal array
* Create diagonal matrix
<div class= "Se-preview-section-delimiter" ></div>
#创建对角矩阵
import numpy
a = Numpy.diag ( 1,2,3,4))
print (a)
<div class= "Se-preview-section-delimiter" ></div>
The output is:
[[1 0 0 0]
[0 2 0 0]
[0 0 3 0]
[0 0 0 4]]
<div class= "Se-preview-section-delimiter" ></div>
Extract Matrix diagonal elements
<div class= "Se-preview-section-delimiter" ></div>
#提取矩阵对角线元素
import numpy
a = Numpy.arange ( 1,10). Reshape (3,3)
print (a)
B = Diag (a)
print (b)
<div class= "Se-preview-section-delimiter" > </div>
The output is:
[[1 2 3]
[4 5 6]
[7 8 9]]
[1 5 9]
<div class= "Se-preview-section-delimiter" ></div>
Diag can also accept two parameters, the second parameter is the diagonal element that moves x position diagonally, the positive number moves to the right, and the negative number moves like the left
<div class= "Se-preview-section-delimiter" ></div>
#提取矩阵对角线右移一位元素
import numpy
a = Numpy.arange (1,10). Reshape (3,3)
B = diag (a,1)
print (b)
<div class= "Se-preview-section-delimiter "></div>
The output is:
[2 6]
<div class= "Se-preview-section-delimiter" ></div>
<div class= "Se-preview-section-delimiter" ></div>
#提取矩阵对角线右移一位元素
import numpy
a = Numpy.arange (1,10). Reshape (3,3)
B = diag (a,-1)
print (b)
<div class= "Se-preview-section-delimiter" ></div>
The output is:
[4 8]
<div class= "Se-preview-section-delimiter" ></div>
18.all () and any () comparison matrix
If we want to know if all the corresponding elements in matrix A and matrix B are equal, we need to use the all method, and if we want to know whether the corresponding element in matrix A and matrix B has an equality, we need to use any method.
<div class= "Se-preview-section-delimiter" ></div>
#提取矩阵对角线右移一位元素
import numpy
<div class= "Se-preview-section-delimiter" ></div>
#创建两个数组
a = Array ([1,2,3,4,5])
b = Array ([ 1,2,3,4,5])
c = Array ([1,2,3,4,6])
print (a==b). All ()) print ((
a==c). All ())
print ((a==c). Any ( )
<div class= "Se-preview-section-delimiter" ></div>
The output is:
true False True