Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # from numpy array to redis
- import redis
- r = redis.StricteRedis(host='localhost', port=6379)
- def array_to_string(array):
- try:
- a = a.ravel().tostring()
- a = str(a) # This for read from redis, to avoid UnicodeDecoderError
- # UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
- return a
- except:
- pass
- def string_to_array(string, shape=(-1,300)):
- try:
- # 'b\'\\xff%Z\\xce\\xd1J\\xe .............''
- s = eval(s) # turn back to b-string
- array = np.fromstring(s, dtype=np.float32).reshape(shape) # Note data type np.float32, if you got 150, probaly you are using float64
- return array
- except:
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement