indexing
Example: take a submatrix
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]]) # Take the submatrix array([[ 6, 7 ], [10, 11]]) a[-2:,1:3] ### -2 : , row, penultimate row until the last 1:3 , column, from the first column, to the third column. # Take the number 7 a[1,-2]
Two-dimensional data, it is also possible to generate one-dimensional data.
When we are at a latitude and use an integer to get the element, a.shape, to get it, the latitude will be -1
When we do not use integers to get elements, the latitude may be +1, or the same.
arange: Generates an array of the specified range
a
array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
#a array, the second column number of each row + 10
Method 1:
a[np.arange(3),1]+= 10Method
2:
a[np.arange(3),[1,1,1]]+=10Method 3
:
a[[0,1, 2],[1,1,1]]+=10
array([[1,12,3,4],
[5,16,7,8],
[9,20,11,12]])
##
np.arange(3)
array([0,1,2]) #Generates an array containing [0,1,2]
##np.arange(3,7) arry
([3,4,5,6 ]) # produce an array from 3 to 7, excluding 7
result_index = a>10
a[result_index] #Get the elements of a>10 and save them to an array
#Simplified operation
a[a>10]
the data type of the element
import numpy as np a = np.array([1,2]) a.dtype #dtype(int64)
a = np.array([1.1,2.2]) a.dtype() #dtype('float64')
a = np.array([1,2.2])
#dtype('float64')
#a = np.array([1.1,2.6])
a= np.array([1.1,2.6],dtype=np.int64) #Specify the data type
result: remove the fractional part
array([ 1,2])
###
a = np.array([1.1,2.6])
b = np.array(a,type=np.int64)
b result:
array([1,2])
Array operations and common functions
Example: Addition, subtraction, multiplication and division of non-matrix operations
a = np.array([[1,2],
[2,3]]) b = np.array([[5,6], [ 7,8 ]])
addition operation: a+b np.add(a,b)
Result: Addition and subtraction of positions corresponding to a and b array([[ 6, 8], [ 9, 11]])
Subtraction operation: Subtraction of the corresponding position
ab
np.subtrack(a,b)
Multiplication operation:
a*b
np.multiply(a,b)
Division operation:
a/b
np.divide(a,b )
Matrix operation operation:
a =np.array([[1, 2], [2, 3]]) b = np.array([[1,2,3], [4,5,6]]) Operations between matrices: multiply the columns of a equal to the number of rows of b a.dot(b) np.dot(a,b) result: array([[ 9, 12, 15], [14, 19, 24]])
numpy common functions
sum function: summation
a= np.array([[1, 2],
[2, 3]])
np.sum(a)
# sum: sum the elements in the array a
np.sum (a, axis = 0)
# axis=0 for each column in the array, sum operation
# array([3,5])
np.sum (a, axis = 1)
# axis=1 for each row in the array, sum operation
# array([3,5])
mean function: mean
np.mean(a) #For array a , the mean of all sums #For
each column of array a, mean operation
np.mean(a,axis=0) #For
each row of data a, mean operation
np.mean(a,axis=1 )
uniform function: random value within a specified range
np.random.uniform(3,4) #Specify
to generate random numbers in the range of 3 and 4 (with decimals)
tile function: An element is repeated a specified number of times.
a = array([ [1,2], [2,3]]) np.tile(a,( 1,2 )) # 1 row and 2 columns, the basic unit is a, #row
unchanged, with a as the unit, repeat on the column array([[ 1, 2, 1, 2 ], [2, 3, 2, 3]])
np.tile(a,(2,1))
#2 row 1 column, a is the unit #column
unchanged, a is the unit, repeat on the row
array([[1, 2],
[2, 3],
[1, 2],
[2, 3]])
np.tile(a,(2,3))
#a is the unit, repeated 2 times on the row and 3 times on the column
array([[1, 2, 1, 2, 1, 2],
[2, 3, 2, 3, 2, 3],
[1, 2, 1, 2, 1, 2],
[2, 3, 2, 3, 2, 3]])
argsort function: used to sort the elements in an array.
a = np.array([[3,6,4,11], [ 5,10,1,3]])
a.argsort() #The
subscript of each line element, sorted from small to large
array([0,2,1,3],
[2,3,0,1])
#Each column of elements in the following table, sorted from small to large
array([0,0,1,1],
[1,1,0,0])
Matrix transpose operation
a = np.array([[3,6,4,11], [ 5,10,1,3 ]])
#2 transpose methods a.T
np.transpose(a) #The first column, transposed to the first row, and so on array([3,5 ], [6,10], [4,1], [11,3])
broadcast:
In the missing latitude, and the latitude of the array is 1
a = np.array([[1,2,3], [2,3,4], [12,31,22], [ 2,2,2]])
#Two-dimensional, 4*3 data
b = np.array([1,2,3]) #Add
each row of a to b
The first way:
for i in range(4):
a[i, :] += b
#result
array([[ 2, 4, 6],
[ 3, 5, 7],
[13, 33, 25],
[ 3, 4, 5]])
The second way:
a + np.tile(b,(4,1))
a+, with b as the unit, repeats 4 times for the row and 1 for the column.
#
array([[ 3, 6, 9],
[ 4, 7, 10],
[14, 35, 28],
[ 4, 6, 8]])
The third way:
a + b
Broadcasting will be performed at the missing latitude and the latitude with the array of 1, which is the broadcast feature.