I have a long 2D matrix of Numpy array object whose dimension is n x 12. Here is the first 10 rows of this matrix:
b = ([[0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0],
[0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0],
[0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0]], dtype=uint8)
What I want to do with this array is to convert it to unsigned integer. As far as I know the fastest way to do it is by using np.packbits
function. However this function only packs 8 bits into integer while my array above has 12 bits in each row. What I expect when converting array above to unsigned integer are:
250, 248, 248, 250, 248, 248, 248, 248, 248, 248
Does any one know how get above result ? I also tried by np.packbits
above by extending the bits to 16 (`.view('u2'), the result is still not as I expected. Any feedback would be appreciated. Thanks.