NumPy types with underscore: `int_`, `float_`, etc

后端 未结 2 1478
眼角桃花
眼角桃花 2021-02-19 04:30

What is the significance of the underscore suffixing in int_, float_, etc.?

2条回答
  •  离开以前
    2021-02-19 05:08

    If you're unsure if your variable is scalar, list, or array, using the ones with "_" will ensure your code will work regardless (if that's the behavior you intended). See the example code below.

    import numpy as np
    scalar = 3
    L1 = [3]
    L2 = [1, 2, 3]
    
    np.float(scalar)  # okay
    np.float(L1)  # breaks (TypeError)
    np.float(L2)  # breaks (TypeError)
    
    np.float_(scalar)  # okay
    np.float_(L1)  # okay
    np.float_(L2)  # okay
    

提交回复
热议问题