DataWhale (numpy): task4 mathematical functions and logic functions

DataWhale (numpy): task4 mathematical functions and logic functions


One, vectorization and broadcasting

The two concepts of vectorization and broadcasting are the basis of numpy's internal implementation. With vectorization, there is no need to use explicit loops when writing code. These loops cannot actually be omitted, but are implemented internally and replaced by other structures in the code. The application of vectorization makes the code more concise and more readable. It can also be said that the code using the vectorization method looks more "Pythonic".

The Broadcasting mechanism describes how numpy handles arrays of different shapes during arithmetic operations, allowing smaller arrays to "broadcast" on larger arrays so that they have compatible shapes. Not all dimensions must be compatible with each other to meet the requirements of the broadcast mechanism, but they must meet certain conditions.

If the dimensions of the two arrays are compatible, that is, each dimension of the two arrays is the same length, or one of the arrays is one-dimensional, then the broadcast mechanism is applicable. If these two conditions are not met, numpy will throw an exception saying that the two arrays are incompatible.

In summary, there are three rules for broadcasting:

  • If the dimensions dim of the two arrays are not the same, then the shape of the small-dimensional array will be filled with 1 on the left.
  • If the shape dimension does not match, but one dimension is 1, then the dimension with dimension 1 can be expanded to match another array;
  • If the shape dimensions do not match, but none of the dimensions are 1, the matching will cause an error;
二维数组加一维数组

import numpy as np

x = np.arange(4)
y = np.ones((3, 4))
print(x.shape)  # (4,)
print(y.shape)  # (3, 4)

print((x + y).shape)  # (3, 4)
print(x + y)
# [[1. 2. 3. 4.]
#  [1. 2. 3. 4.]
#  [1. 2. 3. 4.]]
两个数组均需要广播

import numpy as np

x = np.arange(4).reshape(4, 1)
y = np.ones(5)

print(x.shape)  # (4, 1)
print(y.shape)  # (5,)

print((x + y).shape)  # (4, 5)
print(x + y)
# [[1. 1. 1. 1. 1.]
#  [2. 2. 2. 2. 2.]
#  [3. 3. 3. 3. 3.]
#  [4. 4. 4. 4. 4.]]

x = np.array([0.0, 10.0, 20.0, 30.0])
y = np.array([1.0, 2.0, 3.0])
z = x[:, np.newaxis] + y
print(z)
# [[ 1.  2.  3.]
#  [11. 12. 13.]
#  [21. 22. 23.]
#  [31. 32. 33.]]
不匹配报错的例子

import numpy as np

x = np.arange(4)
y = np.ones(5)

print(x.shape)  # (4,)
print(y.shape)  # (5,)

print(x + y)
# ValueError: operands could not be broadcast together with shapes (4,) (5,) 

2. Mathematical functions

2.1 Arithmetic operations

numpy.add

numpy.subtract

numpy.multiply

numpy.divide

numpy.floor_divide

numpy.power

  • numpy.add(x1, x2, *args, **kwargs) Add arguments element-wise.
  • numpy.subtract(x1, x2, *args, **kwargs) Subtract arguments element-wise.
  • numpy.multiply(x1, x2, *args, **kwargs) Multiply arguments element-wise.
  • numpy.divide(x1, x2, *args, **kwargs) Returns a true division of the inputs, element-wise.
  • numpy.floor_divide(x1, x2, *args, **kwargs) Return the largest integer smaller or equal to the division of the inputs.
  • numpy.power(x1, x2, *args, **kwargs) First array elements raised to powers from second array, element-wise.

The above functions are overloaded in numpy, and the operators are  element-level . In other words, they are only used between elements with the same position, and the result of the operation is formed into a new array.

注意 numpy 的广播规则。

import numpy as np

x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
y = x + 1
print(y)
print(np.add(x, 1))
# [2 3 4 5 6 7 8 9]

y = x - 1
print(y)
print(np.subtract(x, 1))
# [0 1 2 3 4 5 6 7]

y = x * 2
print(y)
print(np.multiply(x, 2))
# [ 2  4  6  8 10 12 14 16]

y = x / 2
print(y)
print(np.divide(x, 2))
# [0.5 1.  1.5 2.  2.5 3.  3.5 4. ]

y = x // 2
print(y)
print(np.floor_divide(x, 2))
# [0 1 1 2 2 3 3 4]

y = x ** 2
print(y)
print(np.power(x, 2))
# [ 1  4  9 16 25 36 49 64]
注意 numpy 的广播规则。

import numpy as np

x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])
y = x + 1
print(y)
print(np.add(x, 1))
# [[12 13 14 15 16]
#  [17 18 19 20 21]
#  [22 23 24 25 26]
#  [27 28 29 30 31]
#  [32 33 34 35 36]]

y = x - 1
print(y)
print(np.subtract(x, 1))
# [[10 11 12 13 14]
#  [15 16 17 18 19]
#  [20 21 22 23 24]
#  [25 26 27 28 29]
#  [30 31 32 33 34]]

y = x * 2
print(y)
print(np.multiply(x, 2))
# [[22 24 26 28 30]
#  [32 34 36 38 40]
#  [42 44 46 48 50]
#  [52 54 56 58 60]
#  [62 64 66 68 70]]

y = x / 2
print(y)
print(np.divide(x, 2))
# [[ 5.5  6.   6.5  7.   7.5]
#  [ 8.   8.5  9.   9.5 10. ]
#  [10.5 11.  11.5 12.  12.5]
#  [13.  13.5 14.  14.5 15. ]
#  [15.5 16.  16.5 17.  17.5]]

y = x // 2
print(y)
print(np.floor_divide(x, 2))
# [[ 5  6  6  7  7]
#  [ 8  8  9  9 10]
#  [10 11 11 12 12]
#  [13 13 14 14 15]
#  [15 16 16 17 17]]

y = x ** 2
print(y)
print(np.power(x, 2))
# [[ 121  144  169  196  225]
#  [ 256  289  324  361  400]
#  [ 441  484  529  576  625]
#  [ 676  729  784  841  900]
#  [ 961 1024 1089 1156 1225]]
注意 numpy 的广播规则。

import numpy as np

x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])

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

z = x + y
print(z)
print(np.add(x, y))
# [[12 14 16 18 20]
#  [17 19 21 23 25]
#  [22 24 26 28 30]
#  [27 29 31 33 35]
#  [32 34 36 38 40]]

z = x - y
print(z)
print(np.subtract(x, y))
# [[10 10 10 10 10]
#  [15 15 15 15 15]
#  [20 20 20 20 20]
#  [25 25 25 25 25]
#  [30 30 30 30 30]]

z = x * y
print(z)
print(np.multiply(x, y))
# [[ 11  24  39  56  75]
#  [ 16  34  54  76 100]
#  [ 21  44  69  96 125]
#  [ 26  54  84 116 150]
#  [ 31  64  99 136 175]]

z = x / y
print(z)
print(np.divide(x, y))
# [[11.          6.          4.33333333  3.5         3.        ]
#  [16.          8.5         6.          4.75        4.        ]
#  [21.         11.          7.66666667  6.          5.        ]
#  [26.         13.5         9.33333333  7.25        6.        ]
#  [31.         16.         11.          8.5         7.        ]]

z = x // y
print(z)
print(np.floor_divide(x, y))
# [[11  6  4  3  3]
#  [16  8  6  4  4]
#  [21 11  7  6  5]
#  [26 13  9  7  6]
#  [31 16 11  8  7]]

z = x ** np.full([1, 5], 2)
print(z)
print(np.power(x, np.full([5, 5], 2)))
# [[ 121  144  169  196  225]
#  [ 256  289  324  361  400]
#  [ 441  484  529  576  625]
#  [ 676  729  784  841  900]
#  [ 961 1024 1089 1156 1225]]
import numpy as np

x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])

y = np.arange(1, 26).reshape([5, 5])
print(y)
# [[ 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]]

z = x + y
print(z)
print(np.add(x, y))
# [[12 14 16 18 20]
#  [22 24 26 28 30]
#  [32 34 36 38 40]
#  [42 44 46 48 50]
#  [52 54 56 58 60]]

z = x - y
print(z)
print(np.subtract(x, y))
# [[10 10 10 10 10]
#  [10 10 10 10 10]
#  [10 10 10 10 10]
#  [10 10 10 10 10]
#  [10 10 10 10 10]]

z = x * y
print(z)
print(np.multiply(x, y))
# [[ 11  24  39  56  75]
#  [ 96 119 144 171 200]
#  [231 264 299 336 375]
#  [416 459 504 551 600]
#  [651 704 759 816 875]]

z = x / y
print(z)
print(np.divide(x, y))
# [[11.          6.          4.33333333  3.5         3.        ]
#  [ 2.66666667  2.42857143  2.25        2.11111111  2.        ]
#  [ 1.90909091  1.83333333  1.76923077  1.71428571  1.66666667]
#  [ 1.625       1.58823529  1.55555556  1.52631579  1.5       ]
#  [ 1.47619048  1.45454545  1.43478261  1.41666667  1.4       ]]

z = x // y
print(z)
print(np.floor_divide(x, y))
# [[11  6  4  3  3]
#  [ 2  2  2  2  2]
#  [ 1  1  1  1  1]
#  [ 1  1  1  1  1]
#  [ 1  1  1  1  1]]

z = x ** np.full([5, 5], 2)
print(z)
print(np.power(x, np.full([5, 5], 2)))
# [[ 121  144  169  196  225]
#  [ 256  289  324  361  400]
#  [ 441  484  529  576  625]
#  [ 676  729  784  841  900]
#  [ 961 1024 1089 1156 1225]]

numpy.sqrt

numpy.square

  • numpy.sqrt(x, *args, **kwargs) Return the non-negative square-root of an array, element-wise.
  • numpy.square(x, *args, **kwargs) Return the element-wise square of the input.
import numpy as np

x = np.arange(1, 5)
print(x)  # [1 2 3 4]

y = np.sqrt(x)
print(y)
# [1.         1.41421356 1.73205081 2.        ]
print(np.power(x, 0.5))
# [1.         1.41421356 1.73205081 2.        ]

y = np.square(x)
print(y)
# [ 1  4  9 16]
print(np.power(x, 2))
# [ 1  4  9 16]

Three, trigonometric functions

numpy.sin

numpy.cos

numpy.tan

numpy.arcsin

numpy.arccos

numpy.arctan

  • numpy.sin(x, *args, **kwargs) Trigonometric sine, element-wise.
  • numpy.cos(x, *args, **kwargs) Cosine element-wise.
  • numpy.tan(x, *args, **kwargs) Compute tangent element-wise.
  • numpy.arcsin(x, *args, **kwargs) Inverse sine, element-wise.
  • numpy.arccos(x, *args, **kwargs) Trigonometric inverse cosine, element-wise.
  • numpy.arctan(x, *args, **kwargs) Trigonometric inverse tangent, element-wise.

Universal function (universal function) is usually called ufunc, which operates on each element in the array one by one. This shows that the general function processes each element of the input array separately, and the result generated forms a new output array. The output array has the same size as the input array.

Many mathematical operations such as trigonometric functions conform to the definition of general functions, for example, sqrt()functions for calculating square roots, functions for taking logarithms, and log()functions for finding sine values sin().

import numpy as np

x = np.linspace(start=0, stop=np.pi / 2, num=10)
print(x)
# [0.         0.17453293 0.34906585 0.52359878 0.6981317  0.87266463
#  1.04719755 1.22173048 1.3962634  1.57079633]

y = np.sin(x)
print(y)
# [0.         0.17364818 0.34202014 0.5        0.64278761 0.76604444
#  0.8660254  0.93969262 0.98480775 1.        ]

z = np.arcsin(y)
print(z)
# [0.         0.17453293 0.34906585 0.52359878 0.6981317  0.87266463
#  1.04719755 1.22173048 1.3962634  1.57079633]

y = np.cos(x)
print(y)
# [1.00000000e+00 9.84807753e-01 9.39692621e-01 8.66025404e-01
#  7.66044443e-01 6.42787610e-01 5.00000000e-01 3.42020143e-01
#  1.73648178e-01 6.12323400e-17]

z = np.arccos(y)
print(z)
# [0.         0.17453293 0.34906585 0.52359878 0.6981317  0.87266463
#  1.04719755 1.22173048 1.3962634  1.57079633]

y = np.tan(x)
print(y)
# [0.00000000e+00 1.76326981e-01 3.63970234e-01 5.77350269e-01
#  8.39099631e-01 1.19175359e+00 1.73205081e+00 2.74747742e+00
#  5.67128182e+00 1.63312394e+16]

z = np.arctan(y)
print(z)
# [0.         0.17453293 0.34906585 0.52359878 0.6981317  0.87266463
#  1.04719755 1.22173048 1.3962634  1.57079633]

Four, exponent and logarithm

numpy.exp

numpy.log

numpy.exp2

numpy.log2

numpy.log10

  • numpy.exp(x, *args, **kwargs) Calculate the exponential of all elements in the input array.
  • numpy.log(x, *args, **kwargs) Natural logarithm, element-wise.
  • numpy.exp2(x, *args, **kwargs) Calculate 2**p for all p in the input array.
  • numpy.log2(x, *args, **kwargs) Base-2 logarithm of x.
  • numpy.log10(x, *args, **kwargs) Return the base 10 logarithm of the input array, element-wise.
The natural logarithm log is the inverse of the exponential function, so that log(exp(x)) = x. The natural logarithm is logarithm in base e.

import numpy as np

x = np.arange(1, 5)
print(x)
# [1 2 3 4]
y = np.exp(x)
print(y)
# [ 2.71828183  7.3890561  20.08553692 54.59815003]
z = np.log(y)
print(z)
# [1. 2. 3. 4.]

Five, addition function, multiplication function

numpy.sum

  • numpy.sum(a[, axis=None, dtype=None, out=None, …]) Sum of array elements over a given axis.

Through the difference  axis, numpy will operate in different directions: if it is not set, then all elements are operated; if axis=0it is operated along the vertical axis;, axis=1it is operated along the horizontal axis. But this is just a simple two-digit array, what if it is multidimensional? It can be summarized in one sentence: Suppose axis=i, then numpy ioperates along the direction of the first subscript change.

numpy.cumsum

  • numpy.cumsum(a, axis=None, dtype=None, out=None) Return the cumulative sum of the elements along a given axis.

Aggregate functions  are functions that operate on a set of values ​​(such as an array) and return a single value as the result. Therefore, the function that sums all the elements of the array is the aggregate function. ndarrayThe class implements multiple such functions.

Returns the sum of the array elements on the given axis.

import numpy as np

x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])
y = np.sum(x)
print(y)  # 575

y = np.sum(x, axis=0)
print(y)  # [105 110 115 120 125]

y = np.sum(x, axis=1)
print(y)  # [ 65  90 115 140 165]

 Returns the cumulative sum of the array elements on the given axis.

import numpy as np

x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])
y = np.cumsum(x)
print(y)
# [ 11  23  36  50  65  81  98 116 135 155 176 198 221 245 270 296 323 351
#  380 410 441 473 506 540 575]

y = np.cumsum(x, axis=0)
print(y)
# [[ 11  12  13  14  15]
#  [ 27  29  31  33  35]
#  [ 48  51  54  57  60]
#  [ 74  78  82  86  90]
#  [105 110 115 120 125]]

y = np.cumsum(x, axis=1)
print(y)
# [[ 11  23  36  50  65]
#  [ 16  33  51  70  90]
#  [ 21  43  66  90 115]
#  [ 26  53  81 110 140]
#  [ 31  63  96 130 165]]

numpy.prod product

  • numpy.prod(a[, axis=None, dtype=None, out=None, …]) Return the product of array elements over a given axis.

numpy.cumprod multiply

  • numpy.cumprod(a, axis=None, dtype=None, out=None) Return the cumulative product of elements along a given axis.

 Returns the product of the array elements on the given axis.

import numpy as np

x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])
y = np.prod(x)
print(y)  # 788529152

y = np.prod(x, axis=0)
print(y)
# [2978976 3877632 4972968 6294624 7875000]

y = np.prod(x, axis=1)
print(y)
# [  360360  1860480  6375600 17100720 38955840]

Returns the cumulative multiplication of the array elements on the given axis.

import numpy as np

x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])
y = np.cumprod(x)
print(y)
# [         11         132        1716       24024      360360     5765760
#     98017920  1764322560  -837609728   427674624   391232512    17180672
#    395155456   893796352   870072320  1147043840   905412608  -418250752
#    755630080  1194065920 -1638662144  -897581056   444596224 -2063597568
#    788529152]

y = np.cumprod(x, axis=0)
print(y)
# [[     11      12      13      14      15]
#  [    176     204     234     266     300]
#  [   3696    4488    5382    6384    7500]
#  [  96096  121176  150696  185136  225000]
#  [2978976 3877632 4972968 6294624 7875000]]

y = np.cumprod(x, axis=1)
print(y)
# [[      11      132     1716    24024   360360]
#  [      16      272     4896    93024  1860480]
#  [      21      462    10626   255024  6375600]
#  [      26      702    19656   570024 17100720]
#  [      31      992    32736  1113024 38955840]]

numpy.diff difference

  • numpy.diff(a, n=1, axis=-1, prepend=np._NoValue, append=np._NoValue) Calculate the n-th discrete difference along the given axis.
    • a: input matrix
    • n: Optional, representing how many times the difference needs to be executed
    • axis: the default is the last

The first difference is given by out[i] = a[i+1] - a[i] along the given axis, higher differences are calculated by using diffrecursively.

Calculate the discrete difference of the Nth dimension along the specified axis.

import numpy as np

A = np.arange(2, 14).reshape((3, 4))
A[1, 1] = 8
print(A)
# [[ 2  3  4  5]
#  [ 6  8  8  9]
#  [10 11 12 13]]
print(np.diff(A))
# [[1 1 1]
#  [2 0 1]
#  [1 1 1]]
print(np.diff(A, axis=0))
# [[4 5 4 4]
#  [4 3 4 4]]

Six, rounding

numpy.around rounding

  • numpy.around(a, decimals=0, out=None) Evenly round to the given number of decimals.

Round the array to the given number of decimal places.

import numpy as np

x = np.random.rand(3, 3) * 10
print(x)
# [[6.59144457 3.78566113 8.15321227]
#  [1.68241475 3.78753332 7.68886328]
#  [2.84255822 9.58106727 7.86678037]]

y = np.around(x)
print(y)
# [[ 7.  4.  8.]
#  [ 2.  4.  8.]
#  [ 3. 10.  8.]]

y = np.around(x, decimals=2)
print(y)
# [[6.59 3.79 8.15]
#  [1.68 3.79 7.69]
#  [2.84 9.58 7.87]]

numpy.ceil upper limit

numpy.floor lower limit

  • numpy.ceil(x, *args, **kwargs) Return the ceiling of the input, element-wise.
  • numpy.floor(x, *args, **kwargs) Return the floor of the input, element-wise.
import numpy as np

x = np.random.rand(3, 3) * 10
print(x)
# [[0.67847795 1.33073923 4.53920122]
#  [7.55724676 5.88854047 2.65502046]
#  [8.67640444 8.80110812 5.97528726]]

y = np.ceil(x)
print(y)
# [[1. 2. 5.]
#  [8. 6. 3.]
#  [9. 9. 6.]]

y = np.floor(x)
print(y)
# [[0. 1. 4.]
#  [7. 5. 2.]
#  [8. 8. 5.]]

Miscellaneous

numpy.clip crop

  • numpy.clip(a, a_min, a_max, out=None, **kwargs): Clip (limit) the values in an array.

Given an interval, values outside the interval are clipped to the interval edges. For example, if an interval of [0, 1] is specified, values smaller than 0 become 0, and values larger than 1 become 1.

Clip (limit) the values ​​in the array.

import numpy as np

x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])
y = np.clip(x, a_min=20, a_max=30)
print(y)
# [[20 20 20 20 20]
#  [20 20 20 20 20]
#  [21 22 23 24 25]
#  [26 27 28 29 30]
#  [30 30 30 30 30]]

numpy.absolute absolute value

numpy.abs

  • numpy.absolute(x, *args, **kwargs) Calculate the absolute value element-wise.
  • numpy.abs(x, *args, **kwargs) is a shorthand for this function.
import numpy as np

x = np.arange(-5, 5)
print(x)
# [-5 -4 -3 -2 -1  0  1  2  3  4]

y = np.abs(x)
print(y)
# [5 4 3 2 1 0 1 2 3 4]

y = np.absolute(x)
print(y)
# [5 4 3 2 1 0 1 2 3 4]

numpy.sign returns the element-wise indication of the number sign

  • numpy.sign(x, *args, **kwargs) Returns an element-wise indication of the sign of a number.
x = np.arange(-5, 5)
print(x)
#[-5 -4 -3 -2 -1  0  1  2  3  4]
print(np.sign(x))
#[-1 -1 -1 -1 -1  0  1  1  1  1]

Seven, logic function

7.1 Truth test

numpy.all

numpy.any

  • numpy.all(a, axis=None, out=None, keepdims=np._NoValue) Test whether all array elements along a given axis evaluate to True.
  • numpy.any(a, axis=None, out=None, keepdims=np._NoValue) Test whether any array element along a given axis evaluates to True.
import numpy as np

a = np.array([0, 4, 5])
b = np.copy(a)
print(np.all(a == b))  # True
print(np.any(a == b))  # True

b[0] = 1
print(np.all(a == b))  # False
print(np.any(a == b))  # True

print(np.all([1.0, np.nan]))  # True
print(np.any([1.0, np.nan]))  # True

a = np.eye(3)
print(np.all(a, axis=0))  # [False False False]
print(np.any(a, axis=0))  # [ True  True  True]

7.2 Array contents

numpy.isnan

  • numpy.isnan(x, *args, **kwargs) Test element-wise for NaN and return result as a boolean array.
a=np.array([1,2,np.nan])
print(np.isnan(a))
#[False False  True]

7.3 Logical operations

numpy.logical_not

numpy.logical_and

numpy.logical_or

numpy.logical_xor

  • numpy.logical_not(x, *args, **kwargs)Compute the truth value of NOT x element-wise.
  • numpy.logical_and(x1, x2, *args, **kwargs) Compute the truth value of x1 AND x2 element-wise.
  • numpy.logical_or(x1, x2, *args, **kwargs)Compute the truth value of x1 OR x2 element-wise.
  • numpy.logical_xor(x1, x2, *args, **kwargs)Compute the truth value of x1 XOR x2, element-wise.
计算非x元素的真值。

import numpy as np

print(np.logical_not(3))  
# False
print(np.logical_not([True, False, 0, 1]))
# [False  True  True False]

x = np.arange(5)
print(np.logical_not(x < 3))
# [False False False  True  True]
计算x1 AND x2元素的真值。

print(np.logical_and(True, False))  
# False
print(np.logical_and([True, False], [True, False]))
# [ True False]
print(np.logical_and(x > 1, x < 4))
# [False False  True  True False]
逐元素计算x1 OR x2的真值。


print(np.logical_or(True, False))
# True
print(np.logical_or([True, False], [False, False]))
# [ True False]
print(np.logical_or(x < 1, x > 3))
# [ True False False False  True]
计算x1 XOR x2的真值,按元素计算。

print(np.logical_xor(True, False))
# True
print(np.logical_xor([True, True, False, False], [True, False, True, False]))
# [False  True  True False]
print(np.logical_xor(x < 1, x > 3))
# [ True False False False  True]
print(np.logical_xor(0, np.eye(2)))
# [[ True False]
#  [False  True]]

 7.4 Control

numpy.greater

numpy.greater_equal

numpy.equal

numpy.not_equal

numpy.less

numpy.less_equal

  • numpy.greater(x1, x2, *args, **kwargs) Return the truth value of (x1 > x2) element-wise.
  • numpy.greater_equal(x1, x2, *args, **kwargs) Return the truth value of (x1 >= x2) element-wise.
  • numpy.equal(x1, x2, *args, **kwargs) Return (x1 == x2) element-wise.
  • numpy.not_equal(x1, x2, *args, **kwargs) Return (x1 != x2) element-wise.
  • numpy.less(x1, x2, *args, **kwargs) Return the truth value of (x1 < x2) element-wise.
  • numpy.less_equal(x1, x2, *args, **kwargs) Return the truth value of (x1 =< x2) element-wise.

numpy has carried out operator overloading on the above comparison function.

import numpy as np

x = np.array([1, 2, 3, 4, 5, 6, 7, 8])

y = x > 2
print(y)
print(np.greater(x, 2))
# [False False  True  True  True  True  True  True]

y = x >= 2
print(y)
print(np.greater_equal(x, 2))
# [False  True  True  True  True  True  True  True]

y = x == 2
print(y)
print(np.equal(x, 2))
# [False  True False False False False False False]

y = x != 2
print(y)
print(np.not_equal(x, 2))
# [ True False  True  True  True  True  True  True]

y = x < 2
print(y)
print(np.less(x, 2))
# [ True False False False False False False False]

y = x <= 2
print(y)
print(np.less_equal(x, 2))
# [ True  True False False False False False False]
import numpy as np

x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])
y = x > 20
print(y)
print(np.greater(x, 20))
# [[False False False False False]
#  [False False False False False]
#  [ True  True  True  True  True]
#  [ True  True  True  True  True]
#  [ True  True  True  True  True]]

y = x >= 20
print(y)
print(np.greater_equal(x, 20))
# [[False False False False False]
#  [False False False False  True]
#  [ True  True  True  True  True]
#  [ True  True  True  True  True]
#  [ True  True  True  True  True]]

y = x == 20
print(y)
print(np.equal(x, 20))
# [[False False False False False]
#  [False False False False  True]
#  [False False False False False]
#  [False False False False False]
#  [False False False False False]]

y = x != 20
print(y)
print(np.not_equal(x, 20))
# [[ True  True  True  True  True]
#  [ True  True  True  True False]
#  [ True  True  True  True  True]
#  [ True  True  True  True  True]
#  [ True  True  True  True  True]]


y = x < 20
print(y)
print(np.less(x, 20))
# [[ True  True  True  True  True]
#  [ True  True  True  True False]
#  [False False False False False]
#  [False False False False False]
#  [False False False False False]]

y = x <= 20
print(y)
print(np.less_equal(x, 20))
# [[ True  True  True  True  True]
#  [ True  True  True  True  True]
#  [False False False False False]
#  [False False False False False]
#  [False False False False False]]
import numpy as np

np.random.seed(20200611)
x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])

y = np.random.randint(10, 40, [5, 5])
print(y)
# [[32 28 31 33 37]
#  [23 37 37 30 29]
#  [32 24 10 33 15]
#  [27 17 10 36 16]
#  [25 32 23 39 34]]

z = x > y
print(z)
print(np.greater(x, y))
# [[False False False False False]
#  [False False False False False]
#  [False False  True False  True]
#  [False  True  True False  True]
#  [ True False  True False  True]]

z = x >= y
print(z)
print(np.greater_equal(x, y))
# [[False False False False False]
#  [False False False False False]
#  [False False  True False  True]
#  [False  True  True False  True]
#  [ True  True  True False  True]]

z = x == y
print(z)
print(np.equal(x, y))
# [[False False False False False]
#  [False False False False False]
#  [False False False False False]
#  [False False False False False]
#  [False  True False False False]]

z = x != y
print(z)
print(np.not_equal(x, y))
# [[ True  True  True  True  True]
#  [ True  True  True  True  True]
#  [ True  True  True  True  True]
#  [ True  True  True  True  True]
#  [ True False  True  True  True]]

z = x < y
print(z)
print(np.less(x, y))
# [[ True  True  True  True  True]
#  [ True  True  True  True  True]
#  [ True  True False  True False]
#  [ True False False  True False]
#  [False False False  True False]]

z = x <= y
print(z)
print(np.less_equal(x, y))
# [[ True  True  True  True  True]
#  [ True  True  True  True  True]
#  [ True  True False  True False]
#  [ True False False  True False]
#  [False  True False  True False]]
注意 numpy 的广播规则。

import numpy as np

x = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])

np.random.seed(20200611)
y = np.random.randint(10, 50, 5)

print(y)
# [32 37 30 24 10]

z = x > y
print(z)
print(np.greater(x, y))
# [[False False False False  True]
#  [False False False False  True]
#  [False False False False  True]
#  [False False False  True  True]
#  [False False  True  True  True]]

z = x >= y
print(z)
print(np.greater_equal(x, y))
# [[False False False False  True]
#  [False False False False  True]
#  [False False False  True  True]
#  [False False False  True  True]
#  [False False  True  True  True]]

z = x == y
print(z)
print(np.equal(x, y))
# [[False False False False False]
#  [False False False False False]
#  [False False False  True False]
#  [False False False False False]
#  [False False False False False]]

z = x != y
print(z)
print(np.not_equal(x, y))
# [[ True  True  True  True  True]
#  [ True  True  True  True  True]
#  [ True  True  True False  True]
#  [ True  True  True  True  True]
#  [ True  True  True  True  True]]

z = x < y
print(z)
print(np.less(x, y))
# [[ True  True  True  True False]
#  [ True  True  True  True False]
#  [ True  True  True False False]
#  [ True  True  True False False]
#  [ True  True False False False]]

z = x <= y
print(z)
print(np.less_equal(x, y))
# [[ True  True  True  True False]
#  [ True  True  True  True False]
#  [ True  True  True  True False]
#  [ True  True  True False False]
#  [ True  True False False False]]

numpy.isclose

numpy.allclose

  • numpy.isclose(a, b, rtol=1.e-5, atol=1.e-8, equal_nan=False) Returns a boolean array where two arrays are element-wise equal within a tolerance.
  • numpy.allclose(a, b, rtol=1.e-5, atol=1.e-8, equal_nan=False) Returns True if two arrays are element-wise equal within a tolerance.

numpy.allclose() Is equivalent to  numpy.all(isclose(a, b, rtol=rtol, atol=atol, equal_nan=equal_nan)).

The tolerance values are positive, typically very small numbers. The relative difference (rtol * abs(b)) and the absolute difference atol are added together to compare against the absolute difference between a and b.

The calculation basis for judging whether it is True:

np.absolute(a - b) <= (atol + rtol * absolute(b))

- atol:float,绝对公差。
- rtol:float,相对公差。

NaNs are treated as equal if they are in the same place and if equal_nan=True. Infs are treated as equal if they are in the same place and of the same sign in both arrays.

比较两个数组是否可以认为相等。

import numpy as np

x = np.isclose([1e10, 1e-7], [1.00001e10, 1e-8])
print(x)  # [ True False]

x = np.allclose([1e10, 1e-7], [1.00001e10, 1e-8])
print(x)  # False

x = np.isclose([1e10, 1e-8], [1.00001e10, 1e-9])
print(x)  # [ True  True]

x = np.allclose([1e10, 1e-8], [1.00001e10, 1e-9])
print(x)  # True

x = np.isclose([1e10, 1e-8], [1.0001e10, 1e-9])
print(x)  # [False  True]

x = np.allclose([1e10, 1e-8], [1.0001e10, 1e-9])
print(x)  # False

x = np.isclose([1.0, np.nan], [1.0, np.nan])
print(x)  # [ True False]

x = np.allclose([1.0, np.nan], [1.0, np.nan])
print(x)  # False

x = np.isclose([1.0, np.nan], [1.0, np.nan], equal_nan=True)
print(x)  # [ True  True]

x = np.allclose([1.0, np.nan], [1.0, np.nan], equal_nan=True)
print(x)  # True

Eight, practice

Mathematical function

8.1 Replace the values ​​greater than 30 in the array a with 30, and the values ​​less than 10 with 10.

  • a = np.random.uniform(1, 50, 20)

[Knowledge points: mathematical functions, search]

  • How to replace all values ​​greater than a given value with a given cutoff value?
import numpy as np

np.set_printoptions(precision=2)
np.random.seed(100)
a = np.random.uniform(1, 50, 20)
print(a)
# [27.63 14.64 21.8  42.39  1.23  6.96 33.87 41.47  7.7  29.18 44.67 11.25
#  10.08  6.31 11.77 48.95 40.77  9.43 41.   14.43]

# 方法1
b = np.clip(a, a_min=10, a_max=30)
print(b)
# [27.63 14.64 21.8  30.   10.   10.   30.   30.   10.   29.18 30.   11.25
#  10.08 10.   11.77 30.   30.   10.   30.   14.43]

# 方法2
b = np.where(a < 10, 10, a)
b = np.where(b > 30, 30, b)
print(b)
# [27.63 14.64 21.8  30.   10.   10.   30.   30.   10.   29.18 30.   11.25
#  10.08 10.   11.77 30.   30.   10.   30.   14.43]

 8.2 Find all peaks in a one-dimensional digital array a. The peak is the point surrounded by smaller values ​​on both sides.

  • a = np.array([1, 3, 7, 1, 2, 6, 0, 1])

[Knowledge points: mathematical functions, search]

  • How to find all local maxima (or peaks) in a one-dimensional array?
import numpy as np

a = np.array([1, 3, 7, 1, 2, 6, 0, 1])
b1 = np.diff(a)
b2 = np.sign(b1)
b3 = np.diff(b2)

print(b1)  # [ 2  4 -6  1  4 -6  1]
print(b2)  # [ 1  1 -1  1  1 -1  1]
print(b3)  # [ 0 -2  2  0 -2  2]
index = np.where(np.equal(b3, -2))[0] + 1
print(index) # [2 5]

8.3 For a given one-dimensional array, calculate the moving average with a window size of 3.

  • z = np.random.randint(10, size=10)

[Knowledge Points: Mathematical Functions]

  • How to calculate the moving average of a numpy array?
import numpy as np

np.random.seed(100)
z = np.random.randint(10, size=10)
print(z)
# [8 8 3 7 7 0 4 2 5 2]

def MovingAverage(arr, n=3):
    a = np.cumsum(arr)
    a[n:] = a[n:] - a[:-n]
    return a[n - 1:] / n


r = MovingAverage(z, 3)
print(np.around(r, 2))
# [6.33 6.   5.67 4.67 3.67 2.   3.67 3.  ]

8.4 Normalize a 5x5 random matrix

[Knowledge Points: Mathematical Functions]

  • (Hint: (x-min) / (max-min))
Z = np.random.random((5,5))
Zmax, Zmin = Z.max(), Z.min()
Z = (Z - Zmin)/(Zmax - Zmin)
print(Z)

[[0.65852707 0.23160203 0.9700425  0.43393441 0.36950354]
 [0.58585632 0.01877358 0.         0.27782335 0.89654906]
 [0.71307774 0.32030221 0.55187887 0.01411576 0.38903753]
 [0.05702349 0.40824762 0.23277188 0.74335934 0.72125366]
 [0.59816442 0.98727663 1.         0.86466287 0.42760849]]

8.6 Use five different methods to extract the integer part of a random array

[Knowledge Points: Mathematical Functions]

  • (提示: %, np.floor, np.ceil, astype, np.trunc)
Z = np.random.uniform(0,10,10)

print (Z - Z%1)
print (np.floor(Z))
print (np.ceil(Z)-1)
print (Z.astype(int))
print (np.trunc(Z))

#[8. 5. 5. 1. 2. 7. 0. 1. 5. 9.]
#[8. 5. 5. 1. 2. 7. 0. 1. 5. 9.]
#[8. 5. 5. 1. 2. 7. 0. 1. 5. 9.]
#[8 5 5 1 2 7 0 1 5 9]
#[8. 5. 5. 1. 2. 7. 0. 1. 5. 9.]

8.7 Consider a one-dimensional array Z, construct a two-dimensional array, its first row (Z [0], Z [1], Z [2]), each subsequent row is shifted by 1 (the last row should be (Z [ -3], Z [-2], Z [-1])

[Knowledge Points: Mathematical Functions]

  • (Hint np.lib.stride_tricks)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]-------->

np.arange(10).itemsize

from numpy.lib import stride_tricks
def rolling(a, window):
    shape = (a.size - window + 1, window)
    strides = (a.itemsize, a.itemsize)
    return stride_tricks.as_strided(a, shape=shape, strides=strides)
Z = rolling(np.arange(10), 3)
print (Z)

#
[[0 1 2]
 [1 2 3]
 [2 3 4]
 [3 4 5]
 [4 5 6]
 [5 6 7]
 [6 7 8]
 [7 8 9]]

 8.8  Consider two sets of points P0 and P1 to describe a set of lines (two-dimensional) and a point p. How to calculate the distance from point p to each line i (P0[i], P1[i])?

[Knowledge Points: Mathematical Functions]

  • Prompt to set P(x0,y0), the linear equation is: Ax+By+C=0, the distance from P to the straight line is: d=|Ax0+By0+C|/√(A²+B²)
import numpy as np
def distance(P0,P1,p):
    A=-1/(P1[:,0]-P0[:,0])
    B=1/(P1[:,1]-P0[:,1])
    C=P0[:,0]/(P1[:,0]-P0[:,0])-P0[:,1]/(P1[:,1]-P0[:,1])
    return np.abs(A*p[:,0]+B*p[:,1]+C)/np.sqrt(A**2+B**2)

P0 = np.random.uniform(-10,10,(10,2))
P1 = np.random.uniform(-10,10,(10,2))
p  = np.random.uniform(-10,10,( 1,2))

print (distance(P0, P1, p))

#[ 2.41120585  7.05924485  5.94906338  3.89096895  7.68364539 14.01851392
#  4.28148124  2.29582076  6.19155339 15.55087594]

8.9 Plot the sine function and cosine function, x = np.arange(0, 3 * np.pi, 0.1)?

import numpy as np
from matplotlib import pyplot as plt
x = np.arange(0, 3*np.pi, 0.1)
y1 = np.sin(x)
y2 = np.cos(x)
plt.plot(x, y1)
plt.plot(x, y2)
                                    [<matplotlib.lines.Line2D at 0x236a41aac88>]

 8.10  Subtract the average value of each row of the matrix?

X = np.random.rand(5, 10)
# 新
Y = X - X.mean(axis=1, keepdims=True)
# 旧
Y = X - X.mean(axis=1).reshape(-1, 1)
print(Y)

#[[-0.22611265  0.18681928  0.13113673 -0.42713648  0.13273158  0.01160406
   0.54266804 -0.15637426  0.16445073 -0.35978702]
 [-0.34359842  0.26798252  0.35328871  0.40386463 -0.45020099  0.40738698
   0.10061077 -0.32664784  0.1162996  -0.52898595]
 [ 0.08892378 -0.10264121  0.27575287 -0.00792712  0.01868823  0.18782665
   0.00795426 -0.12014542 -0.35177329  0.00334125]
 [-0.46156881 -0.40474198  0.15298977  0.17155644  0.17332436 -0.06791347
  -0.12123457  0.00349192  0.28866751  0.26542884]
 [-0.39891856 -0.01008639  0.50523343 -0.28914192  0.01382436 -0.15406104
  -0.22350635  0.45768516 -0.17832199  0.2772933 ]]

8.11 Perform probability statistical analysis?

  • arr1 = np.random.randint(1,10,10)
  • arr2 = np.random.randint(1,10,10))
f
arr1 = np.random.randint(1,10,10)
arr2 = np.random.randint(1,10,10)

print("arr1的平均数为:%s" %np.mean(arr1))
print("arr1的中位数为:%s" %np.median(arr1))
print("arr1的方差为:%s" %np.var(arr1))
print("arr1的标准差为:%s" %np.std(arr1))
print("arr1,arr的相关性矩阵为:%s" %np.cov(arr1,arr2))
print("arr1,arr的协方差矩阵为:%s" %np.corrcoef(arr1,arr2))

Logical function

8.12 Get the matching position of a and b elements.

  • a = np.array([1, 2, 3, 2, 3, 4, 3, 4, 5, 6])
  • b = np.array([7, 2, 10, 2, 7, 4, 9, 4, 9, 8])

[Knowledge Points: Logical Functions]

  • How to get the matching position of two array elements?
import numpy as np

a = np.array([1, 2, 3, 2, 3, 4, 3, 4, 5, 6])
b = np.array([7, 2, 10, 2, 7, 4, 9, 4, 9, 8])
mask = np.equal(a, b)

# 方法1
x = np.where(mask)
print(x)  # (array([1, 3, 5, 7], dtype=int64),)

# 方法2
x = np.nonzero(mask)
print(x)  # (array([1, 3, 5, 7], dtype=int64),)

#(array([1, 3, 5, 7], dtype=int64),)
#(array([1, 3, 5, 7], dtype=int64),)

8.13 Get all elements between 5 and 10.

  • a = np.array([2, 6, 1, 9, 10, 3, 27])

[Knowledge Points: Logical Functions]

  • How to extract all elements in a given range from a numpy array?
import numpy as np

a = np.array([2, 6, 1, 9, 10, 3, 27])
mask = np.logical_and(np.greater_equal(a, 5), np.less_equal(a, 10))

# 方法1
x = np.where(mask)
print(a[x])  # [ 6  9 10]

# 方法2
x = np.nonzero(mask)
print(a[x])  # [ 6  9 10]

# 方法3
x = a[np.logical_and(a >= 5, a <= 10)]
print(x)  # [ 6  9 10]

#[ 6  9 10]
#[ 6  9 10]
#[ 6  9 10]

8.14 For two random arrays A and B, check if they are equal

[Knowledge Points: Logical Functions]

  • (提示: np.allclose, np.array_equal)
A = np.random.randint(0,2,5)
B = np.random.randint(0,2,5)


# Assuming identical shape of the arrays and a tolerance for the comparison of values
equal = np.allclose(A,B)
print(equal)


#False

8.15 How to reverse a Boolean value, or change the sign of a floating-point number in-place?

[Knowledge Points: Logical Functions]

  • (提示: np.logical_not, np.negative)
Z = np.array([0,1])
print(Z)
np.logical_not(Z, out=Z)
# Z = np.random.uniform(-1.0,1.0,100)

# np.negative(Z, out=Z)

#[0 1]
#array([1, 0])

Z = np.array([0.2,1.15])
print(Z)
np.negative(Z, out=Z)

#[0.2  1.15]
#array([-0.2 , -1.15])

8.16 Find the number closest to the given value in the array

Z=np.array([[0,1,2,3],[4,5,6,7]])
print(Z)
z=5.1
np.abs(Z - z).argmin()
print(Z.flat[np.abs(Z - z).argmin()])

#
[[0 1 2 3]
 [4 5 6 7]]
5

 

Guess you like

Origin blog.csdn.net/adminkeys/article/details/109301528