a guest Mar 20th, 2019 50 Never
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):
- 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
- def string_to_array(string, shape=(-1,300)):
- # '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
RAW Paste Data