I'm trying to create an integer from a numpy array of 1s and 0s, and I need to do it fast as I'll be doing up to 400 million of these.
x = numpy.array([1, 0, 1, 0, 0])
y = integer representation of x
The numpy array could be created type string, ie
x=numpy.array(['1', '0', '1', '0', '0'])
I've haven't found anything yet that doesn't involve concatenations and type conversions before running
int(bits_as_string, 2)
which seems a lot of overhead.
I'm constructing an NK landscape for use with an evolutionary biology project. I have an N bit string with K of those bits interacting, and I'm generating 2^N performance values for every possible bit combination after jumping through a lot of other hoops to attach various weights to differing interactions. That's why I'm using binary digits. I'm currently using x as a list to which I'm appending the 1s and 0s. I'm trying to change this so that x is a numpy array and I simply change x[i] instead of appending to a list. I do believe the numpy array formulation will be faster than the string concatenation, but as I can't get it to work yet I haven't been able to benchmark ; )
Why don't you build y at the same time you are building x? That is, when the code does x.append(bit)
, it also does y = 2*y + bit
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.