|
ip_sum = np.apply_along_axis(np.sum, 1, intv_bin_ip) |
Current implementation:
ip_sum = np.apply_along_axis(np.sum, 1, intv_bin_ip)
con_sum = np.apply_along_axis(np.sum, 1, intv_bin_con)
Recommended replacement:
ip_sum = intv_bin_ip.sum(axis=1)
con_sum = intv_bin_con.sum(axis=1)
The use of np.apply_along_axis(np.sum, 1, ...) results in significant performance overhead. This function works by iterating over each row of the array in Python space and applying the np.sum function individually, which is inefficient, especially for large arrays.
By replacing it with the vectorized version .sum(axis=1), the operation is executed entirely in C, avoiding Python-level loops. This leads to much faster computation and lower memory overhead.
Both implementations are functionally identical, producing the same output, but the vectorized form is highly optimized and is the recommended practice for numerical array operations in NumPy.