Get numpy ceil and floor value for nearest two decimals - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Get numpy ceil and floor value for nearest two decimals (/thread-40099.html) |
Get numpy ceil and floor value for nearest two decimals - klllmmm - Jun-02-2023 I'm trying to get ceiling value of the input for two decimals. import numpy as np a = np.array([-1.784, -1.5888, -0.24444, 0.25555, 1.5, 1.75, 2.0]) np.around(a,2)array([-1.78, -1.59, -0.24, 0.26, 1.5 , 1.75, 2. ]) np.ceil(a)array([-1., -1., -0., 1., 2., 2., 2.]) np.floor(a)array([-2., -2., -1., 0., 1., 1., 2.]) Is there a way I can use Numpy ceil and floor methods to get such values to nearest second decimal? RE: Get numpy ceil and floor value for nearest two decimals - paul18fr - Jun-02-2023 you can use np.around : numpy.around(a, decimals=2, out=None) (see np.around)
RE: Get numpy ceil and floor value for nearest two decimals - Gribouillis - Jun-02-2023 Why not this ? >>> import numpy as np >>> a = np.array([-1.784, -1.5888, -0.24444, 0.25555, 1.5, 1.75, 2.0]) >>> >>> np.floor(100 * a)/100 array([-1.79, -1.59, -0.25, 0.25, 1.5 , 1.75, 2. ]) >>> np.ceil(100 * a) / 100 array([-1.78, -1.58, -0.24, 0.26, 1.5 , 1.75, 2. ]) RE: Get numpy ceil and floor value for nearest two decimals - rajeshgk - Jun-06-2023 import numpy as np # Input array arr = np.array([-2.025, -2.123, -1.456, 0.789, 1.234, 1.567, 2.345]) # Round values to the nearest second decimal using ceil and floor methods ceil_values = np.ceil(arr * 100) / 100 floor_values = np.floor(arr * 100) / 100 print("Ceil values:", ceil_values) print("Floor values:", floor_values) In this example, we multiply the array by 100 to shift the decimal point two places to the right. Then, we apply the ceil and floor functions to round the values to the nearest whole number. Finally, we divide the values by 100 again to shift the decimal point back to the original position, resulting in values rounded to the nearest second decimal. RE: Get numpy ceil and floor value for nearest two decimals - paul18fr - Jun-07-2023 As I suggested: import numpy as np a = np.array([-1.784, -1.5888, -0.24444, 0.25555, 1.5, 1.75, 2.0]) a_rounded = np.around(a, decimals=2) print(f"{a_rounded}") Actually I'm just wondering if that's your need or if you're asking as well for "zeros" to complete decimals; for instance "1.5" and "2." must provide "1.50" and "2.00" respectively
|