Advertisement
SepandMeenu

Untitled

Feb 13th, 2020
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.56 KB | None | 0 0
  1. """ Problem with Numpy dtypes in a Python dict """
  2.  
  3. import numpy as np
  4.  
  5. # dict for conversion from unsigned to signed
  6. typesDict = {np.uint8: np.int8,
  7.              np.uint16: np.int16,
  8.              np.uint32: np.int32,
  9.              np.uint64: np.int64}
  10.  
  11. # array of unsigned type
  12. aU = np.array([1, 2, 3], dtype=np.uint16)
  13. dtypeU = aU.dtype
  14. # make sure array is of unsigned type
  15. assert dtypeU == np.uint16
  16.  
  17. # get the corresponding signed type
  18. dtypeS = typesDict.get(dtypeU, None)
  19.  
  20. print("dtype: {} => {}".format(dtypeU, dtypeS))  # expected: uint16 => int16
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement