Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. # from numpy array to redis
  2.  
  3. import redis
  4.  
  5. r = redis.StricteRedis(host='localhost', port=6379)
  6.  
  7.  
  8. def array_to_string(array):
  9. try:
  10. a = a.ravel().tostring()
  11. a = str(a) # This for read from redis, to avoid UnicodeDecoderError
  12. # UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
  13. return a
  14. except:
  15. pass
  16.  
  17. def string_to_array(string, shape=(-1,300)):
  18. try:
  19. # 'b\'\\xff%Z\\xce\\xd1J\\xe .............''
  20. s = eval(s) # turn back to b-string
  21. array = np.fromstring(s, dtype=np.float32).reshape(shape) # Note data type np.float32, if you got 150, probaly you are using float64
  22. return array
  23. except:
  24. pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement