python - converting from numpy array of one type to another by re-interpreting raw bytes -
is there way "reinterpret_cast" numpy arrays? here's example:
>>> import numpy np >>> x=np.array([105,79,196,53,151,176,59,202,249,0,207,6], dtype=np.uint8) >>> np.fromstring(x.tostring(),'<h') array([ 20329, 13764, -20329, -13765, 249, 1743], dtype=int16) i can call tostring() , fromstring() convert array raw bytes , array. i'm wondering if there's way me skip intermediate step. (not it's big deal, understand.)
yes. when view array different dtype, reinterpreting underlying data (zeros , ones) according different dtype.
in [85]: x.view('<i2') out[85]: array([ 20329, 13764, -20329, -13765, 249, 1743], dtype=int16)
Comments
Post a Comment