[NumPy Basis] - Numpy array and vector calculation

Welcome to this blog
Hello, I'm: I love learning A little white who just started 鳶
鳶 Working direction: computer vision
I love to learn, but there are mistakes in sharing. Welcome to point out
This article introduces: learning about Numpy

Why should we learn NumPy??

Note: The reason why NumPy is particularly important for numerical calculation is that it can effectively process the data of array.

1 Some functions of NumPy are as follows:

  • Darray, a fast and space saving multidimensional array with non-quantity arithmetic operation and complex broadcast energy.
  • A standard mathematical function for fast operation of the whole set of data (a loop is required).
  • A device for reading and writing disk data and a device for operating memory mapping.
  • Linear algebra, random number factorization and Fourier transform functions.
  • Using A C API integrating code written in C, C++, Fortran, etc.

2 Advantages of choosing NumPy:

  • NumPy stores data in three consecutive memory blocks, which is unique to other Python built-in objects. The algorithm library written in C language of NumPy can operate the memory without type check or other early operations. The NumPy array makes the memory of Python less.
  • NumPy can perform complex calculations on the entire array, without Python's for loop

1 Numpy array multidimensional array object

1.1 Creating darrows

import numpy as np
#   Randomly generate 2 * 3 sequences
data=np.random.randn(2,3)
print(data)
print(data*10)
'''
[[ 0.06546319 -0.11897689 -1.82124962]
 [ 1.19808965 -0.30471992 -0.1017561 ]]
[[  0.65463189  -1.18976893 -18.21249625]
 [ 11.9808965   -3.04719919  -1.01756102]]
'''
# View the dimensions of an array
print(data.shape)
# (2, 3)
# View the type of array
print(data.dtype)
# float64

1.2 array

import numpy as np

data=[[1,2,3,4],[5,6,7,8]]
arr=np.array(data)
# print(arr)
# print(arr.ndim)
# print(arr.shape)
'''
[[1 2 3 4]
 [5 6 7 8]]
2
(2, 4)
'''
#   Generate arrays that are all 0
a=np.zeros(10)  #The same ones are 1
# print(a)
# [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
a=np.zeros((2,2))
# print(a)
'''
[[0. 0.]
 [0. 0.]]
'''
#   Generate an array of 0~9
a=np.arange(10) #Left opening and right closing
print(a)
# [0 1 2 3 4 5 6 7 8 9]

1.3 Data Type

1) dtype View data type

a1=np.array([1,2,3],dtype=np.float64)
print(a1.dtype)
a2=np.array([1,2,3],dtype=np.int32)
print(a2.dtype)
# float64
# int32

2) astyoe data type conversion

a2=a2.astype(np.float64)
print(a2.dtype)
print(a2)
'''
float64
[1. 2. 3.]
'''

1.4 Calculation of arrays

a=np.array([[1,2,3],[4,5,6],[1,2,3]])
print(a)
b=a*a
print(b)
'''
[[1 2 3]
 [4 5 6]
 [1 2 3]]
[[ 1  4  9]
 [16 25 36]
 [ 1  4  9]]
'''
print(b*0.5)
'''
[[ 0.5  2.   4.5]
 [ 8.  12.5 18. ]
 [ 0.5  2.   4.5]]
'''

1.5 Basic index and slice

a=np.arange(10)
print(a)
print(a[5])
'''
[0 1 2 3 4 5 6 7 8 9]
5
'''
print(a[5:8])
# [5 6 7]

#   Copy 5-7 to 99
a[5:8]=99
print(a)
# [ 0  1  2  3  4 99 99 99  8  9]
a1=a[5:8]
print(a1)
# [99 99 99]
a1[1]=123
print(a1)
print(a)
'''
It can be seen from the output results that if the copied slice changes, the original array will also change
 such Numpy Processing very large arrays will not cause memory problems
[ 99 123  99]
[  0   1   2   3   4  99 123  99   8   9]
'''

b=np.array([[1,2,3],[4,5,6]])
print(b)
print(b[1][2])
'''
[[1 2 3]
 [4 5 6]]
6
'''

1.6 Boolean Index

# Note that the string array should be consistent with the randomly generated array
names=np.array(['Bob','Job','kangkang','Bob'])
data=np.random.randn(4,4)   #Generate a 4 * 4 random number
print(names)
print(data)
'''
['Bob' 'Job' 'kangkang' 'Bob']
[[-1.23685512 -0.9903232  -0.40647589 -0.09657184]
 [-0.58888265 -0.9094857   1.26346656 -0.13548616]
 [-0.78342903 -2.02202405 -0.09077977 -0.86846325]
 [ 2.2965722   0.18479656  0.0219635   1.16912506]]
'''
print(names=='Bob')
# [ True False False  True]
print(data[names=='Bob'])
'''
   according to bool Value of, taken as True Row of
[[-1.23685512 -0.9903232  -0.40647589 -0.09657184]
 [ 2.2965722   0.18479656  0.0219635   1.16912506]]
'''
print(~(names=='Bob'))
# [False  True  True False]
print(data[~(names=='Bob')])
'''
    according to bool The value of, taken as the original False The row of, that is, the row after the reversal True that 's ok
[[-0.58888265 -0.9094857   1.26346656 -0.13548616]
 [-0.78342903 -2.02202405 -0.09077977 -0.86846325]]
'''
print(data[names=='Bob',1])
# [-0.9903232   0.18479656]

1.7 Magic Index

arr=np.empty((8,4))
#   empty is to create an 8 * 4 memory value, that is, what the memory has to create what value
for i in range(8):
    arr[i]=i
# print(arr)
'''
[[0. 0. 0. 0.]
 [1. 1. 1. 1.]
 [2. 2. 2. 2.]
 [3. 3. 3. 3.]
 [4. 4. 4. 4.]
 [5. 5. 5. 5.]
 [6. 6. 6. 6.]
 [7. 7. 7. 7.]]
'''

#   Get the data of the required rows in the order you want
# print(arr[[3,5,2,0]])
'''
[[3. 3. 3. 3.]
 [5. 5. 5. 5.]
 [2. 2. 2. 2.]
 [0. 0. 0. 0.]]
'''
# print(arr[[-1,-2]])
'''
    Take it upside down
[[7. 7. 7. 7.]
 [6. 6. 6. 6.]]
'''
arr=np.arange(32).reshape((8,4))
# print(arr)
'''
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]
 [16 17 18 19]
 [20 21 22 23]
 [24 25 26 27]
 [28 29 30 31]]
'''
print(arr[[1,5,7,2],[0,3,1,2]])
#   The final selected elements are: (1,0), (5,3), (7,1), (2,2). No matter how many dimensions of the array, the fancy index is always one-dimensional
# [ 4 23 29 10]

1.8 Transposition and axis of array

Note: Transposition is a special form of remolding and beating. It returns the view of source data (no copy operation will be performed)

1) . T is suitable for transposing one or two dimensional arrays

arr=np.arange(15).reshape((3,5))
# print(arr)
# print(arr.T)    #Converted to 5 * 3 array
'''
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]]
[[ 0  5 10]
 [ 1  6 11]
 [ 2  7 12]
 [ 3  8 13]
 [ 4  9 14]]
'''
#   Use this operation to calculate the inner product of a matrix
a=np.dot(arr,arr.T)
print(a)
'''
[[ 30  80 130]
 [ 80 255 430]
 [130 430 730]]
'''

2) . transpose is suitable for high-dimensional arrays. You need to use an array numbered by axes to transpose

arr=np.arange(24).reshape((2,3,4))
# print(arr)
'''
[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]
'''
#   1. The original axis: 2 * 3 * 4 corresponds to (0, 1, 2)
# print(arr.transpose((0,1,2)))
'''
[[[ 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. Now carry out shaft rotation: 3 * 2 * 4 corresponding to (1, 0, 2)
# print(arr.transpose((1,0,2)))
'''
[[[ 0  1  2  3]
  [12 13 14 15]]

 [[ 4  5  6  7]
  [16 17 18 19]]

 [[ 8  9 10 11]
  [20 21 22 23]]]
'''
# 3. Now carry out shaft rotation: 4 * 3 * 2 corresponding to (2, 1, 0)
print(arr.transpose((2,1,0)))
'''
[[[ 0 12]
  [ 4 16]
  [ 8 20]]

 [[ 1 13]
  [ 5 17]
  [ 9 21]]

 [[ 2 14]
  [ 6 18]
  [10 22]]

 [[ 3 15]
  [ 7 19]
  [11 23]]]
'''

2 General function

Description: It is a function that operates on the elements of an array

arr=np.arange(10)
# print(arr)
#   Root the array in arr
b=np.sqrt(arr)
# print(b)
'''
[0 1 2 3 4 5 6 7 8 9]
[0.         1.         1.41421356 1.73205081 2.         2.23606798
 2.44948974 2.64575131 2.82842712 3.        ]
'''
x=np.random.rand(8)
y=np.random.rand(8)

#This function can be used to return one or a group of random sample values subject to "0~1" uniform distribution.
# The random sample value range is [0,1), excluding 1.
# print(x)
'''
[0.30064764 0.73481023 0.10527066 0.50979887 0.95441699 0.13730305
 0.80034829 0.12164392]
'''
# print(y)
'''
[0.11402696 0.27831363 0.87334048 0.85939591 0.55736659 0.45990634
 0.72938544 0.65383401]
'''
#   Calculate the element with the highest element level in x and y
# print(np.maximum(x,y))
'''
[0.30064764 0.73481023 0.87334048 0.85939591 0.95441699 0.45990634
 0.80034829 0.65383401]
'''
arr=np.random.rand(5)*5 #Randomly generated number multiplied by 5 times
#If the root of a randomly generated negative number is calculated, nan is returned
print(arr)
# [3.61367997 1.46649634 1.52355202 0.90171673 1.23587831]
remainder,whole_part=np.modf(arr)
#   Keep decimal part
print(remainder)
# [0.61367997 0.46649634 0.52355202 0.90171673 0.23587831]
#Keep integer part
print(whole_part)
# [3. 1. 1. 0. 1.]
a=whole_part.astype(np.int32)
print(a)
# [1 1 3 0 3]
print(np.sign(a))
# [1 1 1 1 0]

1) Unary array operation:

2) Operation of binary array:

3 Data processing with arrays

points=np.arange(-5,5,0.1)
#From - 5 to 5, accuracy 0.1, 100 pieces in total
xs,ys=np.meshgrid(points,points) # Returns a coordinate matrix composed of xs, ys
# print(xs)
# print(ys)
'''
[[-5.  -4.9 -4.8 ...  4.7  4.8  4.9]
 [-5.  -4.9 -4.8 ...  4.7  4.8  4.9]
 [-5.  -4.9 -4.8 ...  4.7  4.8  4.9]
 ...
 [-5.  -4.9 -4.8 ...  4.7  4.8  4.9]
 [-5.  -4.9 -4.8 ...  4.7  4.8  4.9]
 [-5.  -4.9 -4.8 ...  4.7  4.8  4.9]]
'''
z=np.sqrt(xs**2+ys**2)
print(z)
import matplotlib.pyplot as plt
plt.imshow(z,cmap=plt.cm.gray)
plt.colorbar()
plt.title("Image plot of $\sqrt{x^2 + y^2}$ for a grid of values")
plt.show()

3.1 Mathematical and statistical methods

1) Generate an array and perform clustering statistics

arr=np.arange(6).reshape((2,3))
print(arr)
#   1. Find the average number of arrays
print(arr.mean())   #Or print(np.mean(arr))
# 2.5
#   2. Summation
print(arr.sum())
# 15
#   3. Use axis to calculate the statistical value on this axis
print(arr.mean(axis=0)) #axis=0 means average by column, 1 means by row
# [1.5 2.5 3.5]
print(arr.sum(axis=0))  #axis=0 means sum by column, 1 means sum by row
# [3 5 7]

2) Do not aggregate statistics, and generate new arrays from intermediate results

arr=np.arange(8)
print(arr)
print(arr.cumsum())
'''
[0 1 2 3 4 5 6 7]
[ 0  1  3  6 10 15 21 28]
'''

3) In a multi-dimensional array, the cumulative function (cumsum) returns an array of the same size, but will be partially clustered according to the marker axis

arr=np.array([[0,1,2],[3,4,5],[6,7,8]])
print(arr)
print(arr.cumsum(axis=0))   #Similarly, 0 means column operation
'''
[[0 1 2]
 [3 4 5]
 [6 7 8]]
[[ 0  1  2]
 [ 3  5  7]
 [ 9 12 15]]
'''

4) The following lists the statistical methods of arrays

3.2 Sorting

Note: sort() method is used to sort in place

arr=np.random.randn(6)*5
# print(arr)
arr.sort()
# print(arr)
'''
[ -2.98478748  -2.67058322  -5.51832824 -12.23731593  -3.81424203
   1.33803487]
[-12.23731593  -5.51832824  -3.81424203  -2.98478748  -2.67058322
   1.33803487]
'''
#   Multidimensional arrays can be sorted on any axis. Similarly, 0 represents columns and 1 represents rows
a=np.random.randn(3,3)
print(a)
a.sort(1)
print(a)
'''
[[ 0.69739385 -0.29640634 -0.54231462]
 [-1.60388036  0.65765065  0.11135261]
 [ 0.06904195  2.4380053   0.42962269]]
[[-0.54231462 -0.29640634  0.69739385]
 [-1.60388036  0.11135261  0.65765065]
 [ 0.06904195  0.42962269  2.4380053 ]]
'''

3.3 Uniqueness and set logic

Note: Use np Unique () is used to find the unique value in the array and return the sorted result. That is to remove the duplicate values in the array.

names=np.array(['Bob','Job','WIll','Bob'])
print(np.unique(names))
# ['Bob' 'Job' 'WIll']
arr=np.array([1,2,7,4,2,1,3,4,6])
print(np.unique(arr))
# [1 2 3 4 6 7]

4 File input and output for array

Note: Numpy can read and write text data or binary data on the disk. Through np.save and np Load is the two main functions for reading and writing disk array data. By default, it is saved in an uncompressed raw binary format in a file with an. npy extension. However, users generally choose pandas or other tools to load text or table data.

5 Linear Algebra

Linear algebra includes matrix multiplication, matrix decomposition, determinant and other square matrices. Numpy provides a dot function for matrix multiplication

Some Commonly Used Linear Algebraic Functions

x=np.array([[1,2,3],[4,5,6]])
y=np.array([[1,2],[2,3],[3,4]])
# print(x)
'''
[[1 2 3]
 [4 5 6]]
'''
# print(y)
'''
[[1 2]
 [2 3]
 [3 4]]
'''

#   Do the inner product of xy
z=x.dot(y)  #Level z=np.dot(x,y)
# print(z)
'''
[[14 20]
 [32 47]]
'''
#   One dimensional array is obtained by dot product of a two-dimensional array and a one-dimensional array matrix
c=np.ones(3)
print(np.dot(x,c))
# [ 6. 15.]

6 Pseudo random number generation

Here are some functions in numpy.random

Use normal to get a 4x4 sample array of standard normal distribution, which has the advantage that we can generate a large number of sample values at one time.

sample=np.random.normal(size=(4,4))
print(sample)
'''
[[ 2.5075111  -0.2278162   2.27533051  0.40876153]
 [-0.64022587  1.15964118 -1.59200453  1.08199528]
 [-0.56770742 -0.43835872 -0.06797861 -0.19994357]
 [ 0.9548936  -1.4356151  -1.46462771  1.29446398]]
'''

Posted by valshooter on Mon, 26 Sep 2022 23:04:36 +0300