At 07:45 AM 2/12/2015, you wrote: >Robert Kern wrote: > > def tobeckerbits(x): > > return np.unpackbits(np.frombuffer(np.asarray(x), > > dtype=np.uint8)).astype(np.int32) > > > > def frombeckerbits(bits, dtype): > > return np.frombuffer(np.packbits(bits), dtype=dtype)[0] > > > > -- > > Robert Kern > >Nice! Also seems to work for arrays of values:
It's also fastest, with most pushed to numpy: >>> s='np.unpackbits(np.frombuffer(np.asarray(np.pi), dtype=np.uint8)).astype(np.int32)' >>> timr(s) 0.000252470827292 >>> s="np.array([b for b in bin(struct.unpack('!i',struct.pack('!f',1.0))[0])[2:]], 'int32')" >>> timr(s) 0.000513665240078 >>> s="np.unpackbits (np.array (memoryview(struct.pack ('d', np.pi)))).astype(np.int32)" >>> timr(s) 0.000466455247988 >>> s="np.array([b for b in np.binary_repr(314159)], 'int32')" >>> timr(s) 0.000423350472602 _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion