简体   繁体   中英

quantization vector with numpy/pytorch

I have created quantized values as: {0.0, 0.5, 1.0} and would like to do quantization for a vector based on the set. For instance, given vector [0.1, 0.1, 0.9, 0.8, 0.6, 0.6] would be transferred into [0.0, 0.0, 1.0, 1.0, 0.5, 0.5] .

Please guide me fastest way using python/numpy/pytorch. Thank you very much!

use map() and lambda()

you can get the round off nearest to 0.5 with round(x * 2) / 2

a = [0.1, 0.1, 0.9, 0.8, 0.6, 0.6]

list(map(lambda x: round(x * 2) / 2, a))

output:

[0.0, 0.0, 1.0, 1.0, 0.5, 0.5]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM