A general function (that is, ufunc) is a function that performs an element-level operation on data in Ndarray. It can be seen as a vectorization wrapper for simple functions that accept one or more scalar values and produce one or more scalar values.
SQRT and exp are a unary (unary) Ufunc,add or maxinum accept 2 arrays, so also called two (binary) Ufunc, and return an array of results
import NumPy as Nparr = Np.arange (10< Span style= "COLOR: #000000" >) np.sqrt (arr) out[ 110]: Array ([0. , 1. , 1.41421356, 1.73205081, 2. , 2.23606798, 2.44948974, 2.64575131, 2.82842712, 3 111]: Array ([ 1.00000000e+00, 2.71828183e+00, 7.38905610e+00 2.00855369e+01, 5.45981500e+01, 1.48413159e+02 4.03428793e+02, 1.09663316e+03, 2.98095799e+03 8.10308393e+03])
x = NP.RANDOM.RANDN (8= NP.RANDOM.RANDN (8) x,yout[): (Array ([-1.68554158,-0.62988644,- 0.65300182, -0.9357815, -0.58973656, -1.13627121, -0.25952295, -0.71442670.45716238, 0.49681059, 0.61541084, -2.41726508, -0.40145024, -0.74636291, -0.31083867, 0.58094538 # element-level max out[1130.45716238, 0.49681059, 0.61541084,-0.9357815,- 0.40145024, -0.74636291, -0.25952295, 0.58094538])
But some ufunc can return multiple arrays, but they are not common. For example, MODF, which is a vectorized version of the Python built-in function Divmod, is used for the fractional and integral parts of a floating-point group.
arr = NP.RANDOM.RANDN (7) * 5arrout[]: Array ([-8.13879901, 0.5628696, 0.50146831,- 0.94937254, -4.13344095, 6.55263049, 9.20516911]) np.modf (arr) out[117]: (Array ([ -0.13879901, 0.5628696, 0.50146831, -0.94937254, -0.13344095, 0.55263049, 0.20516911]), Array ([ -8., 0., 0., -0., -4., 6., 9.]))
One dollar Ufunc
Note, log1p, is log (1+x), base is 1+x;
Ceil is the smallest integer greater than or equal to the value;
Floor is the largest integer less than or equal to the value
Rint rounds the values of each element to the nearest integer, preserving the Dtype
MODF returns the fractional and integral parts of an array as two independent arrays
Logical_not calculates the true value of not x for each element, which is equivalent to-arr
Dual Ufunc
Commonly used should be add plus, subtract minus, multiply, divide, power, maximum (Fmax), Minimum (fmin), MoD
1. Using Arrays for data processing
The NumPy array can represent many kinds of data-processing tasks as concise array expressions (otherwise you need to write loops). Use array expressions instead of loops, by being called vectorization. In general, vectorized array operations are one or two orders of magnitude (or more) than the cost of pure python, especially for various numerical calculations.
# 1000 points with equal intervals = Np.meshgrid (points, points) ysout[]: Array ([[-5. , -5. , -5., ..... , -5. , -5. ,-5. ], [-4.99, -4.99, -4.99, ..., -4.99, -4.99, -4.99], [-4.98,-4.98, -4.98, ..., -4.98, -4.98, -4.98], ..., 4.97, 4.97, 4.97, ..., 4.97, 4.97, 4.97], 4.98, 4.98, 4.98, ..., 4.98, 4.98, 4.98], 4.99, 4.99, 4.99, ..., 4.99, 4.99, 4.99]])
To evaluate the function, write the expression as if the two arrays were two floating-point numbers:
ImportMatplotlib.pyplot as Pltz= NP.SQRT (XS * * 2 + ys * * 2) zout[123]: Array ([[7.07106781, 7.06400028, 7.05693985, ..., 7.04988652, 7.05693985, 7.06400028], [ 7.06400028, 7.05692568, 7.04985815, ..., 7.04279774, 7.04985815, 7.05692568], [ 7.05693985, 7.04985815, 7.04278354, ..., 7.03571603, 7.04278354, 7.04985815], ..., [ 7.04988652, 7.04279774, 7.03571603, ..., 7.0286414 , 7.03571603, 7.04279774], [ 7.05693985, 7.04985815, 7.04278354, ..., 7.03571603, 7.04278354, 7.04985815], [ 7.06400028, 7.05692568, 7.04985815, ..., 7.04279774, 7.04985815, 7.05692568]])
Plt.imshow (z, cmap =plt.cm.gray);p Lt.colorbar () plt.title ("image plot of $\sqrt{x^2 + y^2}$ for a grid of values") out[124]: <matplotlib.text.text at 0xa4987b8>?
2. Describe the conditional logic as an array operation
Application of Np.where function
"Learning" General function: fast element progression group function "Numpy"