Python--numpy (continuously updated)

Table of contents

1. ndarray array

(1) numpy.array()

N-dimensional array object (matrix), all elements must be of the same type
numpy object is divided into array array and matrix mat, which need to be distinguished from list list

  1. The first dimension of a two-dimensional array is a column, and the second dimension is a row; (3, 2) means that the third row is sorted on one column, and the second row is sorted second, that is, the third row and the second column
  2. [0,1,2] can represent a one-dimensional matrix, [[0,1,2]] represents a two-dimensional matrix 1*3

(2) numpy.asarray()

np.array and np.asarray have the same function, they both convert the input into matrix format. When the input is a list, changing the value of the list will not affect the value converted into
a matrix. When the input is an array, np.array will copy the input and np.asarray will cut the input, so as the input changes np The output of .array remains unchanged, while the output of np.asarray is changing, and when we use np.asarray to change its type (the input is float64, changed to float32), so when the input changes, the output of np.asarray Nor will it change.
 

2. The method of numpy.array()

(1) Basic attributes

1. .it's me

Indicates the number of dimensions
 

2. .dtype

(datatype object) property representing the datatype

import numpy

data=[[1,2],[3,4],[5,6]]
x=numpy.array(data)

print(x)
#[[1 2],[3 4],[5 6]]
print(x.ndim)
#2
print(x.dtype)
#int32

 

(2) numpy.shape and numpy.reshape

1.numpy.shape(array) / .shape

It is used to read the length of the matrix, indicating the size of each dimension.
 
The former return value:

  • When the parameter array is a number, return an empty tuple;
  • When it is a one-dimensional matrix, returns a tuple of numbers;
  • For a two-dimensional matrix, return a tuple of rows and columns

The return value of the latter: look at the previous array array, the other is the same as above, without "()"

It can be considered that one-dimensional is the outermost square bracket, one-dimensional number is the number of elements in the outermost square bracket, two-dimensional is the second layer, and so on

import numpy as np

print(np.shape(7))
#()

a=numpy.array([7,8])
print(a.shape)
#(2,)

print(np.shape([[7,8],[1,2],[100,102]]))
#(3, 2)

 

2.numpy.reshape(array, (newshape)) / .reshape(newshape)

Used to give an array a new shape without changing the data.
 
Parameters:
(1) array: the array that needs to be reshaped
(2) newshape: the new shape should be compatible with the original shape. If an integer, the result will be a 1D array of that length. A shape dimension can be -1 or other negative numbers. In this case, the value is inferred by the system based on the length and remaining dimensions of the array.

import numpy as np

a=np.array([[2,3,4],[5,6,7]])
a=np.reshape(a,(3,2))
print(a)
#[[2 3],[4 5],[6 7]]

b=np.array([[2,3,4],[5,6,7]])
b=b.reshape(2,-2)
print(b)
#[[2 3 4],[5 6 7]]

c=b.reshape(-1,2)
print(c)
#[[2 3],[4 5],[6 7]]

illustrate:

The newly generated array of reshape and the original array share a memory, no matter which one is changed, it will affect each other

import numpy as np

a=np.array([[1,2],
            [3,4]])
b=a.reshape(1,4)
b*=2

print(a)
#[[2 4]
# [6 8]]
print(b)
#[[2 4 6 8]]

An array can be created using arange and reshape at the same time

import numpy as np
print(np.arange(1,6).reshape(5,1))
#[[1]
# [2]
# [3]
# [4]
# [5]]

 

(3) Data and sorting

1.numpy.sum(array,axis,dtype) / .sum(axis,dtype)

Summation
 
parameters:
(1) array
(2) axis: dimension, the default is -1
(3) dtype

 

2.numpy.max(array,axis,dtype) / .max(axis,dtype)

find the maximum
 

3.numpy.min(array,axis,dtype) / .min(axis,dtype)

Find the minimum
 

4.numpy.abs(array,axis,dtype) / .abs(axis,dtype)

Find the absolute value


Distinguish between np.min(), np.max(), np.abs() and min(), max(), abs()
 

5.numpy.average(array,axis,dtype) / .average(axis,dtype)

average
 

6.numpy.sort(array,axis,dtype) / .sort(axis,dtype)

to sort

import numpy as np

vector1=np.array([26,10,15,20])

print(vector1.sum())
#71
print(vector1.max())
#26
print(vector1.min())
#10
print(np.average(vector1))
#17.75
print(np.sort(vector1))
#[10 15 20 26]

vector2=np.array([[35,5,15],
                     [10,25,45],
                     [20,40,30]])

print(vector2)
#[[35  5 15],[10 25 45],[20 40 30]]
print(vector2.max(axis=0))#第一维列取最大值
#[35 40 45]
print(vector2.max(axis=1))#第二维行取最大值
#[35 45 40]
print(vector2.sum(axis=0))#第一维列求和
#[65 70 90]
print(vector2.sum(axis=1))#第二维行求和
#[55 80 90]

 

(4) ravel and flatten

1.numpy.ravel() / .ravel()

Convert a multidimensional array to a one-dimensional
 
return value: view (view), which will affect the original matrix
 

2. .flatten()

Convert a multidimensional array to a one-dimensional
 
return value: copy (copy), modifications made to the copy will not affect the original matrix


illustrate:

  1. It can only be applied to numpy objects, that is, array or mat, ordinary list lists are not acceptable
  2. There is no function numpy.flatten()
import numpy as np

a = np.array([[1, 2],[3, 4]])
b=a.flatten()
b*=2
print(a)
#[[1 2],[3 4]]
print(b)
#[2 4 6 8]

c1=a.ravel()
c2=np.ravel(a)
c1*=2
print(a)
#[[2 4],[6 8]]
print(c1)
#[2 4 6 8]
print(c2)
#[2 4 6 8]

#降维默认行序有限,传入参数‘F’表示列序优先
d=a.ravel('F')
print(d)
#[2 6 4 8]

 

(5) Others

1.numpy.transpose() / .transpose() / .T

Matrix transpose

import numpy as np

x1=np.array([[90,10,15],[20,25,30]])
x2=np.array([[5,10,15],[20,25,30],[90,10,15],[90,10,15]])
x1_x2_1=np.dot(x1,x2.transpose())
x1_x2_2=np.dot(x1,np.transpose(x2))

print(x1_x2_1)
#[[ 775 2500 8425 8425],[ 800 1925 2500 2500]]
print(x1_x2_2)
#[[ 775 2500 8425 8425],[ 800 1925 2500 2500]]

 

2.numpy.squeeze(array,axis) / .squeeze(array,axis)

Delete single-dimensional entries from the shape of the array, that is, remove the dimension of 1 in the shape Return value: array parameters
 
with possible reduction in dimension : (1) array: input array (2) axis: specify the dimension to be deleted, but specify The dimension of must be single-dimensional, otherwise an error will be reported; the value can be None or int or tuple of ints, optional. If axis is empty, delete all single-dimensional entries
 


  1. This function does not modify the original array
  2. In machine learning and deep learning, usually the result of the algorithm is an array that can represent a vector (that is, contains two or more pairs of square brackets [[]]), if you directly use this array for drawing, the display interface may be empty. You can use the squeeze() function to convert an array representing a vector into an array of rank 1
import numpy as np

a=np.array([[1,2,3]])
b=np.squeeze(a)
c=a.squeeze()

print(b)
#[1 2 3]
print(c)
#[1 2 3]

 

3. General functions

(1) Generate an array

1. Arithmetic array

1.1 numpy.arange(start,end,step)

Parameters:
(1) start: the starting point of the numerical range
(2) end: the end point of the numerical range, excluding this value
(3) step: the step size of the numerical range

import numpy as np

print(np.arange(6))
#[0 1 2 3 4 5]
print(np.arange(0,6,2))
#[0 2 4]
print(np.arange(0,6,0.9))
#[0.  0.9 1.8 2.7 3.6 4.5 5.4]

 

1.2 numpy.linspace(start,stop,num,endpoint,dtype)

Parameters:
(1) start: the starting point of the value range
(2) stop: the end point of the value range
(3) num (optional): the number of elements in the result, the default is 50
(4) endpoint (optional): Determine whether the termination value is included in the result array. If endpoint=True, the termination value is included in the result. Otherwise, the termination value is not included. The default is True (
5) dtype (optional): determines the data type of the output array


illustrate:

numpy.linspace(start=0,stop=100,num=5) numpy.linspace(0,100,5)
named parameters positional parameters

The difference between the two:

  1. The former interval is left-closed and right-opened, the latter interval is left-closed and right-closed when endpoint=True, and left-closed and right-opened when endpoint=False
  2. When the specified parameters of steps and num are integers, arange will return the numpy.int32 data type, while linspace will return the numpy.float data type
import numpy as np

a=np.arange(0,10,step=1)
b=np.arange(0,10,step=1.0)
c=np.linspace(0,10,num=10,endpoint=False)
d=np.linspace(0,10,num=11,endpoint=True)

print(a)
#[0 1 2 3 4 5 6 7 8 9]
print(b)
#[0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
print(c)
#[0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
print(d)
#[ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9. 10.]

 

2. Constant array

2.1 numpy.ones(shape,dtype)

Generate all 1 matrix
 
parameters:
(1) shape: can be a number or a tuple, indicating the shape of the array
(2) dtype

import numpy as np

a=np.ones(4)
b=np.ones((2,3,4),dtype=np.int32)

print(a)
#[1. 1. 1. 1.]
print(b)
#[[[1 1 1 1]
#  [1 1 1 1]
#  [1 1 1 1]]
# [[1 1 1 1]
#  [1 1 1 1]
#  [1 1 1 1]]]

 

2.2 numpy.zeros(shape,dtype)

Generate all 0 matrix
 
parameters:
(1) shape: can be a number or a tuple, indicating the shape of the array
(2) dtype

import numpy as np

a=np.zeros(5)
b=np.zeros((3,4))

print(a)
#[0. 0. 0. 0. 0.]
print(b)
#[[0. 0. 0. 0.]
# [0. 0. 0. 0.]
# [0. 0. 0. 0.]]

 

2.3 numpy.full(shape, fill_value, dtype)

Generate a matrix parameter with all the same elements
 
:
(1) shape: it can be a number or a tuple, indicating the shape of the array
(2) fill_value: the filled value

import numpy as np

a=np.full(2,3)
b=np.full((2,3),4)

print(a)
#[3 3]
print(b)
#[[4 4 4],[4 4 4]]

 

3. Random array numpy.random

numpy.random.seed (int)
random number seed


The return value of the following functions:

  • When the function has no parameters inside the parentheses, it returns a floating-point number or an integer
  • When there is one parameter in the function brackets, it returns a one-dimensional array with rank 1, which cannot represent vectors and matrices
  • When there are two or more parameters in the function brackets, an array of the corresponding dimension is returned, which can represent a vector or a matrix
     

3.1 numpy.random.rand()

Generate a random decimal parameter that obeys a uniform distribution in the [0,1) interval
 
: that is, the shape of the array, and outputs 1 value by default

import numpy as np

np.random.seed(2)

a=np.random.rand()
b=np.random.rand(2)
c=np.random.rand(2,3)

print(a)
#0.43599490214200376
print(b)
#[0.02592623 0.54966248]
print(c)
#[[0.43532239 0.4203678  0.33033482]
# [0.20464863 0.61927097 0.29965467]]

 

3.2 numpy.random.random()

Generate random decimal
 
parameters in the [0,1) interval: that is, the shape of the array, more than two parameters need to be in the form of tuples, and output 1 value by default

import numpy as np

np.random.seed(1)

a=np.random.random()
b=np.random.random(2)
c=np.random.random((2,3))

print(a)
#0.417022004702574
print(b)
#[7.20324493e-01 1.14374817e-04]
print(c)
#[[0.30233257 0.14675589 0.09233859]
# [0.18626021 0.34556073 0.39676747]]

numpy.random.rand() and numpy.random.random() seem to produce the same random number when using the same random number seed, which is not the case for other functions

 

3.3 numpy.random.randn()

Generate a decimal parameter that obeys (0, 1) normal distribution
 
: that is, the shape of the array, and outputs 1 value by default

import numpy as np

np.random.seed(1)

a=np.random.randn()
b=np.random.randn(2)
c=np.random.randn(2,3)
d=4*np.random.randn(3,3,3) + 3 #生成服从均值为3,标准差为4的正态分布的三维数组

print(a)
#1.6243453636632417
print(b)
#[-0.61175641 -0.52817175]
print(c)
#[[-1.07296862  0.86540763 -2.3015387 ]
# [ 1.74481176 -0.7612069   0.3190391 ]]

print(d) #三维数组
#[[[ 2.0025185   8.84843175 -5.24056284]
#  [ 1.71033118  1.46378258  7.53507777]
#  [-1.39956507  2.31028717 -0.51143367]]
# [[ 3.16885499  5.33126085 -1.40247671]
#  [ 7.57889484  6.60636288  5.00997736]
#  [ 6.6034238   0.26508856  2.5084391 ]]
# [[-0.74307774  1.92844768  5.12142187]
#  [ 0.23335699  1.41298589  0.2513092 ]
#  [-0.38082257  0.31501548  2.9493416 ]]]

 

3.4 numpy.random.randint(*args) / numpy.random.randint(low,high,size,dtype)

To generate an integer in a certain interval, there must be at least one parameter
 
parameter:
(1) * args: three numbers, used to correspond to low, high, size respectively
(2) low (optional): int or float, the minimum value generated Value (included), the default is 0
(3) high: int or float, the maximum value of the generated value (not included)
(4) size (optional): int or tuple of ints, the size of the random number, integer means generated The number, the tuple indicates the shape of the array, the default is 1, and the empty tuple is regarded as 1
(5) dtype (optional): the result type of the output, the default is int

import numpy as np

np.random.seed(1)

a=np.random.randint(10) #0到10的一个随机整数
b=np.random.randint(1,10) #1到10的一个随机整数
c=np.random.randint(1,10,10) #1到10的十个随机整数,构成一维数组
d=np.random.randint(10,99,(5,5)) #10到99的二十五个随机整数,构成5*5的二维数组

print(a)
#5
print(b)
#9
print(c)
#[6 1 1 2 8 7 3 5 6 3]
print(d)
#[[94 21 38 39 24]
# [60 78 97 97 96]
# [23 19 17 73 71]
# [32 67 11 10 70]
# [91 18 98 23 57]]

Special usage:

import numpy as np

np.random.seed(1)

# 可单独指定每个元素的最小值和最大值
# 如果不指定 size 默认根据第一个和第二个参数的长度来决定生成结果的长度
a=np.random.randint([3, 5, 7], 10) #生成3个分别不小于3,5,7的值,都小于10的值
print(a)
#[8 8 7]

b=np.random.randint(1, [3, 5, 10]) #生成3个都不小于1,分别小于3,5,10的值
print(b)
#[1 4 6]

c=np.random.randint([1, 2, 3,], [4, 5, 10]) #生成3个分别不小于1,2,3,分别小于4,5,10的值
print(c)
#[1 2 4]

#生成2*4的数组,其中每一行不小于[1, 3, 5, 7],第一行都小于10,第二行都小于20
#注意第二个参数里面的每个元素都要用[],因为它控制的是一整行
d=np.random.randint([1, 3, 5, 7], [[10], [20]])
print(d)
#[[ 8  8  9  9]
# [19  8  7 11]]

#生成4*2的数组,其中,第一行都不小于1,第二行都不小于3,第三行都不小于5,第四行都不小于7,每一行都小于[10,20]
e=np.random.randint([[1],[3],[5],[7]],[10,20])
print(e)
#[[ 3  5]
# [ 4  3]
# [ 6 14]
# [ 8 13]]

When generating arrays of more than two dimensions, numpy.random.rand() and numpy.random.randn() require only one parenthesis (), while numpy.random.random() and numpy.random.randint() require two parentheses ()

 

3.5 numpy.random.uniform(low, high, size)

Generate floating-point number parameters in a certain interval
 
:
(1) low: float type, the minimum value of the generated value (included), the default is 0
(2) high: float type, the minimum value of the generated value (not included), the default is 1
(3) size: int or integer tuple type, indicating the shape of the output array, and outputs 1 value by default

import numpy as np

np.random.seed(1)

a=np.random.uniform()
b=np.random.uniform(2,4,3)
c=np.random.uniform(2,3,(2,4))

print(a)
#0.417022004702574
print(b)
#[3.44064899 2.00022875 2.60466515]
print(c)
#[[2.14675589 2.09233859 2.18626021 2.34556073]
# [2.39676747 2.53881673 2.41919451 2.6852195 ]]

 

3.6 numpy.random.normal(loc, scale, size)

Generate random numbers with normal distribution probability density

Parameters:
(1) loc: float, the mean value of this probability distribution, the default is 0.0
(2) scale: float, the standard deviation of this probability distribution, the default is 1.0
(3) size: int or integer tuple, indicating the output The shape of the array, output 1 value by default

import numpy as np

np.random.seed(1)

a=np.random.normal()
b=np.random.normal(2,4,3)
c=np.random.normal(2,3,(2,4))

print(a)
#1.6243453636632417
print(b)
#[-0.44702565 -0.11268701 -2.29187449]
print(c)
#[[ 4.59622289 -4.90461609  7.23443529 -0.2836207 ]
# [ 2.95711729  1.25188887  6.38632381 -4.18042213]]

Special usage:

import numpy as np

np.random.seed(1)

loc = [0,2,4,6]
scale = [1,3,5,7]

a=np.random.normal(loc, scale)
b=np.random.normal(loc, scale, size=4)
c=np.random.normal(loc, scale, size=(3,4))
d=np.random.normal(loc, scale, size=(2,3,4))

#输出数组形状最后一维与列表等长,四个表示同时处理四组数据
print(a)
#[ 1.62434536  0.16473076  1.35914124 -1.51078036]
print(b)
#[ 0.86540763 -4.90461609 12.72405882  0.67155169]
print(c)
#[[ 0.3190391   1.25188887 11.31053969 -8.42098497]
# [-0.3224172   0.84783694  9.66884721 -1.69923887]
# [-0.17242821 -0.63357525  4.21106873 10.0797065 ]]
print(d)
#[[[-1.10061918  5.43417113  8.5079536   9.51746037]
#  [ 0.90085595 -0.05118358  3.38554887 -0.55038604]
#  [-0.26788808  3.5910664   0.54169624  3.22272531]]
# [[-0.6871727  -0.53561692  0.64376935  5.91134781]
#  [-1.11731035  2.70324709 12.29901089 11.19430912]
#  [-0.19183555 -0.66288689  0.26420853 17.84718221]]]

 

3.7 numpy.random.choice(array, size, replace, p)

Randomly returns an element of the specified shape, with at least one argument

Parameters:
(1) array: array array or list list or tuple tuple, must be one-dimensional, take the source of the number
(2) size: int or integer tuple, indicating the shape of the output array, and output 1 value by default
( 3) replace: Boolean value, indicating whether the same number can be taken, the default is True
(4) p: corresponds to the array array, requires equal length but does not need to be consistent, indicates the probability of taking each element in the array array, and It is not an absolute probability 0<p<1, but a relative weight. By default, the probability of selecting each element is the same.

import numpy as np

np.random.seed(1)

m=[1,3,5,7,9]
n=(2,4,6,8,10)

a=np.random.choice(m)
b=np.random.choice(m,2)
c=np.random.choice(m,(2,3))
d=np.random.choice(n,(2,3),m)

print(a)
#7
print(b)
#[9 1]
print(c)
#[[3 7 1]
# [1 3 9]]
print(d)
#[[10  4  6]
# [10  6 10]]

x=np.random.choice(m,(2,2),replace=True)
y=np.random.choice(m,(2,2),replace=False)

print(x)
#[[7 9]
# [5 9]]
print(y)
#[[7 9]
# [3 1]]

 

3.8 numpy.random.shuffle(x)

Rearrange the given array on the first dimension, perform on the original array, change its own sequence, and return no value.

import numpy as np

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

np.random.shuffle(a)
print(a)
#[[ 9 10 11]
# [ 6  7  8]
# [ 3  4  5]
# [ 0  1  2]]

np.random.shuffle(a)
print(a)
#[[ 9 10 11]
# [ 6  7  8]
# [ 3  4  5]
# [ 0  1  2]]

 

3.9 numpy.random.permutation(x)

Rearrange the given array on the first dimension instead of the original array, and return a new array without changing its own array

  • numpy.random.permutation(int)==numpy.random.permutation(numpy.arange(int))
import numpy as np

n=np.random.permutation(10)
print(n)
#[7 9 8 6 2 1 3 5 0 4]

a = np.arange(9).reshape((3, 3))
print(a)
#[[0 1 2]
# [3 4 5]
# [6 7 8]]
b = np.random.permutation(a)
print(a)
#[[0 1 2]
# [3 4 5]
# [6 7 8]]
print(b)
#[[0 1 2]
# [6 7 8]
# [3 4 5]]

 

(2) Dimensional operation

numpy.newaxis

insert new dimension

The effect of y=x[:,np.newaxis] is equivalent to adding a square bracket to the element in the innermost bracket, and the effect of y=x[np.newaxis,:] is equivalent to adding a square bracket to the outermost layer.

import numpy as np

x1=np.arange(3)
print(np.shape(x1))
#(3,)

x2=x1[:,np.newaxis]
print(x2)
#[[0],[1],[2]]
print(np.shape(x2))
#(3, 1)

x3=x1[np.newaxis,:]
print(x3)
#[[0 1 2]]
print(np.shape(x3))
#(1, 3)


vector1=np.array([5,10,15,20])
print(vector1[0:3])
#[ 5 10 15]

vector2=np.array([[5,10,15],[20,25,30],[35,40,45]])

print(vector2[:,1])
#[10 25 40]
print(vector2[:,0:2])
#[[ 5 10],[20 25],[35 40]]
print(vector2[1:3,0:2])
#[[20 25],[35 40]]

 

(3) Element operation

1. General operation of vector and vector, operation of vector and scalar

In the former, operations between array keys of the same shape and size are applied to elements, and corresponding elements are added, subtracted, multiplied, and divided. In the
latter, scalar operations are applied to each element


Multiply corresponding elements Divide corresponding elements
array1*array2 array1/array2
numpy.multiply(array1,array2) numpy.divide(array1,array2)

import numpy as np

a=np.array([1,2])
b=np.array([3,4])
c1=b+a
c2=b-a
c3=b*a
c4=b/a
d=a*2+b*3

print(c1)
#[4 6]
print(c2)
#[2 2]
print(c3)
#[3 8]
print(c4)
#[3. 2.]
print(d)
#[11 16]


vector1=numpy.array([[2,6,3],[2,1,2]])
vector2=numpy.array([[1,2,3],[4,5,8]])

m1=vector1*vector2
m2=numpy.multiply(vector1,vector2)
print(m1)
#[[ 2 12  9],[ 8  5 16]]
print(m2)
#[[ 2 12  9],[ 8  5 16]]

n1=vector1/vector2
n2=numpy.divide(vector1,vector2)
print(n1)
#[[2.   3.   1.  ],[0.5  0.2  0.25]]
print(n2)
#[[2.   3.   1.  ],[0.5  0.2  0.25]]

 

2. Broadcast Broadcast

  1. If the shapes are different, numpy will automatically be compatible.
  2. The so-called compatibility means that at least one of the rows and columns of the two arrays is the same, and one of them is a one-dimensional array or a two-dimensional array with rank 1
    Alt
import numpy as np

a=np.array([[1,2],[2,6]])
b1=np.array([1,2])
b2=np.array([[1,2]])
b3=np.array([[[1,2]]])
            
print(a+b1)
#[[2 4],[3 8]]
print(a+b2)
#[[2 4],[3 8]]
print(a+b3)
#[[[2 4],[3 8]]]
print(a*b1)
#[[ 1  4],[ 2 12]]

 

3.numpy.all()

Determine whether the values ​​of the elements in the entire array meet the conditions
 
Return value: Return True if all conditions are met, otherwise return False.
 

4.numpy.absolute() / numpy.abs()

Find the absolute value of each element in the array

import numpy as np

a=np.array([4,2,3])
b=np.array([1,6,9])
c=b-a
print(c)
#[-3  4  6]

d=np.absolute(c)
print(d)
#[3 4 6]

if numpy.all((d)>1):
    print('yes')
    #yes

 

5.numpy.floor(array,dtype)

Round down each element in the array

import numpy as np

a=np.array([-1.5,-0.5,0.5,1.5])
b=np.floor(a)
print(b)
#[-2. -1.  0.  1.]

 

(4) Matrix operation

1.numpy.dot(array1,array2)

matrix inner product

import numpy as np

vector1=np.array([1,2,3]) #1-D array
vector2=np.array([4,5,6])
m=np.dot(vector1,vector2) #计算两个一维矩阵的内积
print(m)
#32

vector3=np.array([[1,2,3],[4,5,6]]) #2-D array
vector4=np.array([[1,2],[3,4],[5,6]])
n=np.dot(vector3,vector4) #计算两个二维矩阵的内积
print(n)
#[[22 28],[49 64]]

 

2.numpy.outer(array1,array2)

matrix outer product

import numpy as np

vector1=np.array([1,2,3]) #1-D array
vector2=np.array([4,5,6])
m=np.outer(vector1,vector2) #计算两个一维矩阵的内积
print(m)
#[[ 4  5  6]
# [ 8 10 12]
# [12 15 18]]

vector3=np.array([[1,2,3],[4,5,6]]) #2-D array
vector4=np.array([[1,2],[3,4],[5,6]])
n=np.outer(vector3,vector4) #计算两个二维矩阵的内积
print(n)
#[[ 1  2  3  4  5  6]
# [ 2  4  6  8 10 12]
# [ 3  6  9 12 15 18]
# [ 4  8 12 16 20 24]
# [ 5 10 15 20 25 30]
# [ 6 12 18 24 30 36]]

 

3.numpy.rot90(array,num)

Realize the rotation of any multiple of the 90° angle of the matrix, the positive number is counterclockwise, and the negative number is clockwise

import numpy as np

a=np.array([[1,2],
            [3,4]])
b1=np.rot90(a,-1)
b2=np.rot90(a,2)

print(b1)
#[[3 1]
# [4 2]]
print(b2)
#[[4 3]
# [2 1]]

 

4.numpy.fliplr(array)

Matrix Horizontal Mirror Flip

import numpy as np

a=np.array([[1,2],
            [3,4]])
b=np.fliplr(a)

print(b)
#[[2 1]
# [4 3]]

 

5.numpy.meshgrid(array1,array2)

Generate a coordinate matrix according to the coordinate array of plane points
 
Return value: a two-element list

The elements of the list are an array composed of point x coordinates and an array composed of y coordinates. The number of elements in the array means the number of representative points.

import numpy as np

x=np.linspace(1,3,3)
y=np.linspace(4,5,2)
Z=np.meshgrid(x,y)

print(Z)
#[array([[1., 2., 3.],
#       [1., 2., 3.]]),
# array([[4., 4., 4.],
#       [5., 5., 5.]])]

X,Y=np.meshgrid(x,y)

print(X)
#[[1. 2. 3.],
# [1. 2. 3.]]

print(Y)
#[[4. 4. 4.],
# [5. 5. 5.]]

 

(5) Row and column processing

1.numpy.hsplit(array,indices_or_sections)

Horizontal cutting array
 
parameters:
(1) array: array to be processed
(2) indices_or_sections: index or section. If it is a list, it will be cut with the elements of the list as the index; if it is a positive integer, it will be divided equally, but this positive integer must be divisible

 

2.numpy.vsplit(array,indices_or_sections)

Cut the array vertically, same as above

import numpy as np

np.random.seed(10)

a1=np.floor(10*np.random.random((2,6))) #random会产生0-1的随机数
a2=np.floor(10*np.random.random((6,2))) #a1,a2的元素都是0-9的整型浮点数

print(a1)
#[[7. 0. 6. 7. 4. 2.],
# [1. 7. 1. 0. 6. 9.]]
print(a2)
#[[0. 5.],
# [8. 6.],
# [7. 2.],
# [9. 7.],
# [5. 1.],
# [3. 6.]]

b=np.hsplit(a1,3)
print(b)
#[array([[7., 0.],
#       [1., 7.]]), 
# array([[6., 7.],
#       [1., 0.]]), 
# array([[4., 2.],
#       [6., 9.]])]

b1,b2,b3=np.hsplit(a1,3)

print(b1)
#[[7. 0.]
# [1. 7.]]
print(b2)
#[[6. 7.]
# [1. 0.]]
print(b3)
#[[4. 2.]
# [6. 9.]]

c=np.vsplit(a2,[1,3,5])
print(c)
#[array([[0., 5.]]), 
# array([[8., 6.],
#       [7., 2.]]), 
# array([[9., 7.],
#       [5., 1.]]), 
# array([[3., 6.]])]

c1,c2,c3,c4=np.vsplit(a2,[1,3,5])

print(c1)
#[[0. 5.]]
print(c2)
#[[8. 6.]
# [7. 2.]]
print(c3)
#[[9. 7.]
# [5. 1.]]
print(c4)
#[[3. 6.]]

 

3.numpy.r_[array1,array2] / numpy.vstack([array1,array2])

Stitching in one dimension
For a one-dimensional matrix, it is equivalent to sequential splicing
For a two-dimensional matrix, it is equivalent to splicing vertically in the vertical direction, and the number of columns must be equal
 

4.numpy.c_[array1,array2] / numpy.hstack([array1,array2])

Splicing in two dimensions
For a one-dimensional matrix, it is equivalent to a two-dimensional matrix transformed into (n, 2)
For a two-dimensional matrix, it is equivalent to splicing horizontally along the left and right directions, and the number of rows must be equal

import numpy as np

a=np.arange(1,5).reshape(4,1)
b=np.ones((4,2))

c=np.hstack((a,b))
d=np.c_[a,b]

print(c)
#[[1. 1. 1.]
# [2. 1. 1.]
# [3. 1. 1.]
# [4. 1. 1.]]
print(d)
#[[1. 1. 1.]
# [2. 1. 1.]
# [3. 1. 1.]
# [4. 1. 1.]]

 

5.numpy.tile(array,shape)

Copy the array along the parameters in all directions
 
:
(1) array
(2) shape: tuple, the dimension must be consistent with array, and the elements are all integers

import numpy as np

a=np.array([[1,2],
            [3,4]])
b=np.tile(a,(2,3))

print(b)
#[[1 2 1 2 1 2]
# [3 4 3 4 3 4]
# [1 2 1 2 1 2]
# [3 4 3 4 3 4]]

 

6. numpy.stack([array1, … ], axis)

(6) View view and copy copy

  1. b=a: exactly the same as the source array, with the same id, any modification will affect the source array
  2. c=a[:], d=a.view(): the array slice is the view view of the original array, any modification on the view will affect the source array
  3. e=a.copy(): copy copy, modification will not affect the source array

Alt

import numpy as np

a=np.arange(4)
print(a)
#[0 1 2 3]

b=a
print(b)
#[0 1 2 3]
print(b is a)
#True
print(id(a))
#2522915940944
print(id(b))
#2522915940944
b*=2
print(id(a))
#2522915940944
print(id(b))
#2522915940944
print(a)
#[0 2 4 6]
print(b)
#[0 2 4 6]


c=a[:]
print(c)
#[0 2 4 6]
print(c is a)
#False
print(id(a))
#2522915940944
print(id(c))
#2522915942000
c*=2
print(a)
#[ 0  4  8 12]
print(c)
#[ 0  4  8 12]

d=a.view()
print(d)
#[ 0  4  8 12]
print(d is a)
#False
print(id(a))
#2522915940944
print(id(d))
#2522915941136
d*=2
print(a)
#[ 0  8 16 24]
print(d)
#[ 0  8 16 24]

e=a.copy()
print(e)
#[ 0  8 16 24]
print(e is a)
#False
print(id(a))
#2522915940944
print(id(e))
#2522915796048
e*=2
print(a)
#[ 0  8 16 24]
print(e)
#[ 0 16 32 48]

 

4. File reading

numpy.genfromtxt(fname, dtype, comments, delimiter, autostrip, skipd_header, skip_fonter, converters, missing_values, filling_values, usecols)

By default, it is read in float format. For data that cannot be converted to float type, it will be read as nan (not a number), so parameters should be added

Parameters:
(1) fname: str/list/IO Object, etc., indicating the data source; file path, string list, and StringIO object are all acceptable; if it ends with gz, it will be automatically decompressed before loading the data when reading, but it is relatively The common ones are txt text

IO --> input,output

(2) dtype: dtype, the data type of the final array
(3) comments: str, the comment identifier, the string after the line where the comment identifier is located will be automatically ignored when loading;
(4) delimiter: str/int/sequence

  • If delimiter is a character, it means the delimiter, and the elements of each line are separated by the delimiter;
  • If delimiter is a character list, it means that the delimiter can be multiple characters;
  • If delimiter is an integer tuple, it represents the maximum width of the element in each column
import numpy as np
from io import BytesIO

data1 = b"1, 2, 3\n4, 5, 6"
a=np.genfromtxt(BytesIO(data1), delimiter=",")
print(a)
#[[1. 2. 3.]
# [4. 5. 6.]]

data2 = b"  1  2  3\n  4  5 67\n890123  4"
b=np.genfromtxt(BytesIO(data2), delimiter=3) 
print(b)
#[[  1.   2.   3.]
# [  4.   5.  67.]
# [890. 123.   4.]]

data3 = B"123456789\n   4  7 9\n   4567 9"
c=np.genfromtxt(BytesIO(data3), delimiter=(4, 3, 2))
print(c)
#[[1234.  567.   89.]
# [   4.    7.    9.]
# [   4.  567.    9.]]

(5) autostrip: If it is True, the space identifier in the element will be automatically deleted
(6) skip_header: int, skip the number of string lines in the file header when data is loaded
(7) skip_footer: int, skip when data is loaded The number of string lines at the end of the file
(8) converters: variable, which can be in the form of a dictionary or a lambda function, which means converting a certain data format into another data format
(9) missing_values: variable, specify the values ​​in the array Missing values ​​are marked
(10) filling_values: variable, specifying the marked missing values ​​in the array to replace the filling values

举例:
missing_values={0:“N/A”, 1:" “, 2:”???"}, filling_values={0:0,1:0, 2:-999})

(11) usercols: sequence, specifying to read only the specified number of columns in the data, 0 means the first column, -1 is the last column


 

5. Cosine similarity

Alt
 
||A|| indicates the norm of A, expressed by numpy.linalg.norm(), and can use axis to achieve dimensionality reduction calculation

import numpy as np

v1=np.array([1,2,3])
v2=np.array([7,14,5])
result=(np.sum(v1*v2))/(np.linalg.norm(v1)*np.linalg.norm(v2))

print(result)
#0.8132500607904444

 
The following is to calculate the cosine similarity of two matrices:

import numpy

matrix1=numpy.array([[90,10,15],
                     [20,25,30]]) #二维数组2*3
matrix2=numpy.array([[5,10,15],
                     [20,25,30],
                     [90,10,15],
                     [90,10,15]]) #二维数组3*4

matrix1_matrix2=numpy.dot(matrix1,matrix2.T) #计算矩阵乘积,结果为二维数组2*4

matrix1_norm1=numpy.linalg.norm(matrix1,axis=1) #按行取范数,得一维数组1*2
matrix1_norm2=matrix1_norm1[:,numpy.newaxis] #增加维度,变成二维数组2*1

matrix2_norm1=numpy.linalg.norm(matrix2,axis=1) #按行取范数,得一维数组1*4
matrix2_norm2=matrix2_norm1[numpy.newaxis,:] #增加维度,变成二维数组1*4

#余弦相似度
cosine=numpy.divide(matrix1_matrix2,numpy.dot(matrix1_norm2,matrix2_norm2))

print(cosine)
#[[0.45131807 0.62078282 1.         1.        ]
# [0.97463185 1.         0.62078282 0.62078282]]

 

6. Others

(1) Relational operators can generate arrays whose elements are True or False

import numpy as np

a=np.array([1,2,3])
print(a>2)
#[False False  True]
b=np.array([3,4,5])
print(a<b)
#[ True  True  True]


x = np.linspace(-2,2,5)
y = np.linspace(0,4,5)

xx,yy = np.meshgrid(x,y)
z = np.square(xx) - yy>0

print(z)
#[[ True  True False  True  True]
# [ True False False False  True]
# [ True False False False  True]
# [ True False False False  True]
# [False False False False False]]


vector=numpy.array([[5,10,15],[20,25,30],[35,40,45]])
vector==25
#array([[False, False, False],
#       [False,  True, False],
#       [False, False, False]])

(2) Scalar and array

numpy.sin(), numpy.cos()

import numpy as np

a1=1
a2=np.sin(a1)
print(a2)
#0.8414709848078965

b1=[1,2]
b2=np.sin(b1)
print(b2)
#[0.84147098 0.90929743]

c1=[[1,2],
    [3,4]]
c2=np.cos(c1)
print(c2)
#[[ 0.54030231 -0.41614684]
# [-0.9899925  -0.65364362]]

The above is not finished and needs to be updated, it is only for personal study, the infringement contact is deleted, if there are any mistakes or deficiencies, please point out for improvement.

Guess you like

Origin blog.csdn.net/abc31431415926/article/details/127902027