python - Convert Bitstring (String of 1 and 0s) to numpy array -
i have pandas dataframe containing 1 columns contains string of bits eg.'100100101'. want convert string numpy array.
how can that?
edit:
using
features = df.bit.apply(lambda x: np.array(list(map(int,list(x))))) #... model.fit(features, lables) leads error on model.fit:
valueerror: setting array element sequence. the solution works case came due marked answer:
for bitstring in input_table['bitstring'].values: bits = np.array(map(int, list(bitstring))) featurelist.append(bits) features = np.array(featurelist) #.... model.fit(features, lables)
for string s = "100100101", can convert numpy array @ least 2 different ways.
the first using numpy's fromstring method. bit awkward, because have specify datatype , subtract out "base" value of elements.
import numpy np s = "100100101" = np.fromstring(s,'u1') - ord('0') print # [1 0 0 1 0 0 1 0 1] where 'u1' datatype , ord('0') used subtract "base" value each element.
the second way converting each string element integer (since strings iterable), passing list np.array:
import numpy np s = "100100101" b = np.array(map(int, s)) print b # [1 0 0 1 0 0 1 0 1] then
# see numpy array: print type(a) # <type 'numpy.ndarray'> print a[0] # 1 print a[1] # 0 # ... note second approach scales worse first length of input string s increases. small strings, it's close, consider timeit results strings of 90 characters (i used s * 10):
fromstring: 49.283392424 s map/array: 2.154540959 s (this using default timeit.repeat arguments, minimum of 3 runs, each run computing time run 1m string->array conversions)
Comments
Post a Comment