0

I want to convert binary numpy array to decimal. Is there a similar function to this numpy.binary_repr that works the other way around?

x = array([ 1., 1., 0., 1., 0., 0., 0., 1., 0.])

I know I can do this:

int("110100010",2) = 418

But here I need to extract the elements of the array and put them into string? Is there an easier way? if not, then how can I extract the elements of the array and create a string out of them?

owise
  • 1,055
  • 16
  • 28

1 Answers1

0

You can use reduce

>>> from functools import reduce
>>> reduce(lambda a,b: 2*a+b, x)
418.0

Alternatively, as you said, you can construct a string and then convert it to int (base 2)

>>> int(''.join(map(lambda x: str(int(x)), x)), 2)
418
Sunitha
  • 11,777
  • 2
  • 20
  • 23