Search code examples
pythonnumpypytorchquantization

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!


Solution

  • 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]