Convert string to numpy array

后端 未结 3 1202
死守一世寂寞
死守一世寂寞 2021-01-01 13:35

I have a string like mystr = \"100110\" (the real size is much bigger) I want to convert it to numpy array like mynumpy = [1, 0, 0, 1, 1, 0], mynumpy.shap

3条回答
  •  [愿得一人]
    2021-01-01 14:13

    Adding to above answers, numpy now gives a deprecation warning when you use fromstring
    DeprecationWarning: The binary mode of fromstring is deprecated, as it behaves surprisingly on unicode inputs. Use frombuffer instead.
    A better option is to use the fromiter. It performs twice as fast. This is what I got in jupyter notebook -

    import numpy as np
    mystr = "100110"
    
    np.fromiter(mystr, dtype=int)
    >> array([1, 0, 0, 1, 1, 0])
    
    # Time comparison
    %timeit np.array(list(mystr), dtype=int)
    >> 3.5 µs ± 627 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
    
    %timeit np.fromstring(mystr, np.int8) - 48
    >> 3.52 µs ± 508 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
    
    %timeit np.fromiter(mystr, dtype=int)
    1.75 µs ± 133 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
    

提交回复
热议问题