Python Forum
How to quantize a 4 dimensional array?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to quantize a 4 dimensional array?
#1
Hello,

I am new to NumPy.

Lets say I have an array:
nparray = np.array(output2)
print(output2.shape)
Output:
(1, 26, 26, 128)
All the values are floating point numbers. Most of them are 0.xxxxxxx, few are between 1.xxxxxxx to 4.xxxxxxx and extreme rare ones are above 5.xxxxxxx. They can be negative or positive.


I want to quantize these values into 8bit integers from the range -127 to 128. So that the largest value in the array becomes a 128 and the smallest value becomes a -127. I know there might be resolution loss but I am okay with it.

How can I achieve this using NumPy (or some other technique)?
Reply
#2
numpy.interp() can be used to re-interpolate an array of values from one range to another.
Output:
import numpy as np floats = np.random.rand(10) print(floats) range_adjusted = np.interp(floats, (min(floats), max(floats)), (-128, 127)) ints = range_adjusted.astype("int8") print(ints)
Output:
[0.23561182 0.47971123 0.04360216 0.56386097 0.78886284 0.9104152 0.32166445 0.52518248 0.54179553 0.72678282] [ -71 0 -128 25 91 127 -46 13 18 72]
The array has to be a single dimension, but you can reshape down to one, do the value adjustments, then reshape back to 4 dimensions.
Gribouillis likes this post
Reply
#3
This function seems to work
>>> def func(a):
...     l, u = np.amin(a), np.amax(a)
...     return np.rint((a - l) * (255 / (u - l))) - 127
... 
>>> import numpy as np
>>> t = np.random.rand(5, 7)
>>> t
array([[0.48858387, 0.51885553, 0.50699018, 0.14738217, 0.105979  ,
        0.53540183, 0.86726475],
       [0.80267188, 0.57738387, 0.32951204, 0.71753945, 0.35037134,
        0.93836202, 0.18163814],
       [0.20919502, 0.27296382, 0.20278684, 0.82154427, 0.15883035,
        0.27340079, 0.82762161],
       [0.23281187, 0.54044188, 0.28653632, 0.79343013, 0.20178373,
        0.61876179, 0.47841846],
       [0.71916377, 0.81516669, 0.17768047, 0.11852501, 0.86190322,
        0.1823391 , 0.508792  ]])
>>> func(t)
array([[ -10.,   -1.,   -4., -114., -127.,    5.,  106.],
       [  86.,   17.,  -59.,   60.,  -52.,  128., -104.],
       [ -95.,  -76.,  -97.,   92., -111.,  -76.,   94.],
       [ -88.,    6.,  -72.,   84.,  -98.,   30.,  -13.],
       [  61.,   90., -105., -123.,  105., -104.,   -4.]])
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to create 2 dimensional variables in Python? plumberpy 5 1,878 Mar-31-2022, 03:15 AM
Last Post: plumberpy
  Slicing a 2 dimensional array Scott 2 1,675 Jan-12-2022, 07:18 AM
Last Post: paul18fr
  Problem using two-dimensional interpolation. Result looks bad player1682 4 2,530 Oct-12-2021, 09:27 AM
Last Post: player1682
  Index data must be 1-dimensional : Classifier with sklearn Salma 0 4,333 Apr-01-2021, 03:22 PM
Last Post: Salma
  2 Dimensional Arrays Prithak 4 2,606 Mar-21-2021, 09:35 PM
Last Post: deanhystad
  comparing 2 dimensional list glennford49 10 4,159 Mar-24-2020, 05:23 PM
Last Post: saikiran
  Python 2 dimensional array creations sharpe 1 1,979 Nov-24-2019, 10:33 AM
Last Post: ThomasL
  Python: Creating N Dimensional Grid jf451 0 2,331 Dec-06-2018, 10:53 PM
Last Post: jf451
  Get last tuple from two-dimensional array? Winfried 6 3,873 Aug-27-2018, 01:48 PM
Last Post: buran
  Sum only some values from a two-dimensional list bronfai 2 3,019 Feb-12-2018, 04:27 AM
Last Post: bronfai

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020