Sunday, 15 March 2015

python - Convert Bitstring (String of 1 and 0s) to numpy array -


I have a panda dataframe containing 1 column, in which there is a string of bits like. '100100101' . I want to convert this string into a NMP array.

How can I do this?

Edit:

Using

  features = df.bit.apply (Lambda X: NP.Ere (list (map (int , List (X)))) # Models ... Feeds (FEATURES, LABLES)  

Goes to an error on the model. Format :

  Value error: Setting an array element with a sequence  

The solution that worked for my answer marked me Was caused by:

Bitstrings for bitstrings ['bitstrings']. Value: bits = Np.array (map (int, list (bitstring)) featureList.append (bits) attributes = np.array (featureList) # .... Models.Fix (Features, Lobbles) < / Pre>

For a string s = "100100101" , you assign it a numpy array Convert it to at least two different ways.

Using the first name method. It's a bit weird, because you have to specify the datatype and decrease the "base" value of the elements. Import as a = np.fromstring (S, 'u1') - ord ('0') Print <[0 0 0 0 1]

Where 'u1' is

The second method is to repeat the strings (in integers) by changing each string element to an integer Can be done), then import that list into np.array

  np s = "100100101" b = np.array (map (int, s )) Print B # [1 0 0 0 0 0]  

Then

  to see a numpy array: Print Type ( A) # & lt; Type 'numpy.ndarray' & gt; Print a [0] # 1 [Print] # 0 # Print ...  

Note that the second approach scale importantly is the length of the input string s For smaller wires, it is close, but consider the results of the strings of 90 characters (I just used s * 10 ):

  Fromstring: 49.2833 92424 s map / array: 2.154540 9 59 s  

(This is the default timeit.repeat using arguments, at least 3 Run C runs 1m string-> calculates time to run the array conversion)


No comments:

Post a Comment