![]() |
VOOZH | about |
NumPy ufuncs (universal functions) are fast, vectorized functions that perform element-wise operations on NumPy arrays. They are highly optimized and support features like broadcasting and automatic type handling. NumPy includes many ufuncs for arithmetic, trigonometry, statistics, etc, and they execute operations very fast because they are implemented in optimized C code.
NumPy provides several trigonometric functions that operate element-wise on arrays. Angles should be in radians, so degree values must be converted using np.deg2rad(). These functions include standard, inverse, and hyperbolic trigonometric operations.
Common Trigonometric ufuncs in NumPy | |
|---|---|
| Function | Description |
| np.sin, np.cos, np.tan | Compute sine, cosine and tangent of angles |
| np.arcsin, np.arccos, np.arctan | Calculate inverse sine, cosine and tangent |
| np.sinh, np.cosh, np.tanh | Compute hyperbolic sine, cosine and tangent |
| np.deg2rad | Convert degrees to radians |
| np.rad2deg | Convert radians to degrees |
| np.hypot | Calculate hypotenuse of a right triangle |
Example: This example demonstrates sine, inverse sine, hyperbolic sine, and hypotenuse calculation using NumPy arrays.
Output
Sine values: [0. 0.5 0.70710678 0.8660254 1. ]
Inverse sine (degrees): [ 0. 30. 45. 60. 90.]
Hyperbolic sine: [0. 0.54785347 0.86867096 1.24936705 2.3012989 ]
Hypotenuse: 5.0
Explanation:
NumPy provides several statistical functions to calculate properties like mean, median, variance, and range. These functions operate element-wise and along specified axes, making analysis of arrays fast and efficient.
Common Statistical ufuncs in NumPy | |
|---|---|
| Function | Description |
| np.amin, np.amax | Minimum or maximum of an array or along a specific axis |
| np.ptp | Range (max − min) of array values |
| np.percentile(a, p) | p-th percentile of array values |
| np.mean | Compute mean of data |
| np.median | Compute median of data |
| np.std | Compute standard deviation |
| np.var | Compute variance |
| np.average | Compute average value |
Example: This example demonstrates common statistical calculations on an array of student weights.
Output
Min and Max: 45.0 73.25
Range: 28.25
70th Percentile: 55.317
Mean: 54.3225
Median: 51.6
Std Dev: 8.052773978574091
Variance: 64.84716875
Average: 54.3225
Explanation:
NumPy provides bitwise functions to perform operations on the binary representation of integers. These allow element-wise manipulation of arrays at the bit level.
Common Bitwise ufuncs in NumPy | |
|---|---|
| Function | Description |
| np.bitwise_and | Element-wise AND operation |
| np.bitwise_or | Element-wise OR operation |
| np.bitwise_xor | Element-wise XOR operation |
| np.invert | Bitwise NOT (invert) of elements |
| np.left_shift | Shift bits of elements to the left |
| np.right_shift | Shift bits of elements to the right |
Example: This example demonstrates basic bitwise operations on arrays of integers.
AND: [ 0 2 4 6 8 16 32] OR : [ 1 3 5 7 9 17 33] XOR: [1 1 1 1 1 1 1] Invert: [ -1 -3 -5 -7 -9 -17 -33] Left shift : [ 0 4 8 12 16 32 64] Right shift: [ 0 1 2 3 4 8 16]
Explanation: