2

this is not a duplicate question as far as I can tell. I will list related-but-different questions below.

Here's the code I'm trying to run, to convert an array of ints to binary as a single vectorized operation:

import numpy as np

a = np.array([1,2,3,4],dtype=int)
b = bin(a)

----> 4 b = bin(a)

TypeError: only integer scalar arrays can be converted to a scalar index

...Um... I am literally operating on an "integer scalar array". (And changing dtype=int to something like dtype=np.int64 has no effect.)

I realize could do do this with a loop (Python 2 question), or as a list comprehension (I understand that bin produces strings), so...this post is more a question of: Why can't I do it as a single vectorized operation, and why that error message? It seems so inappropriate in this case. Any thoughts?

Related but non-dup questions: here, here, here, and here, and here.

The docs for bin read "Convert an integer number..." suggesting that arrays are just not allowed, and it's a Python function not a numpy function. (Ok, so then list comprehension or loop it is -- these just seem to be slow and I'm hoping for something fast). But then why the error message about integer scalar arrays?

Thanks.

sh37211
  • 1,411
  • 1
  • 17
  • 39
  • 1
    `bin` attempts to call `__index__` on the object if it is not an `int` as described in the docs you linked. An integer scalar array is something like `np.array(1)`. `np.array(1).__index__()` returns `1` but `np.array([1]).__index__()` gives the same `TypeError`. – iz_ May 01 '21 at 17:30
  • 1
    To do the actual conversion, check this question: https://stackoverflow.com/q/57836960 – iz_ May 01 '21 at 17:34
  • 1
    `np.vectorize` and `map` and all those are still essentially loops and aren't going to show a magic "speedup". Also `np.vectorize` can slow your code down, especially if there's not an underlying numpy operation happening, so I'd stay away from that. – Chrispresso May 01 '21 at 17:44
  • @Mark Yes, I even wrote in the OP, "I understand that bin produces strings". It's ok that you don't grasp the utility. ;-) Not that it's any of your business, but I'm looking at binary representations of [Champerowne's Number](https://en.wikipedia.org/wiki/Champernowne_constant). – sh37211 May 01 '21 at 18:05
  • 1
    Sorry @sh37211 for not catching that line the strings. I assumed since your code tried to pass a numpy array to `bin()` that you were just getting started and might not understand the basics. ;-) Just trying to help. Good luck. – Mark May 01 '21 at 18:13

1 Answers1

1

You need to map the function to every element of the numpy array -

import numpy as np
a = np.array([1,2,3,4],dtype=int)
b = np.array(list(map(bin,a)))

Additionally, you can check-out this link - Most efficient way to map function over numpy array

Nk03
  • 14,699
  • 2
  • 8
  • 22
  • Hey, thanks! And that link you shared helped me do a comparison of my own. Not sure if comments will show images but: [perfplot comparison](https://imgur.com/DNHTufA) (also included whether to wrap the map in np.array or not). So it looks like **I was wrong** and list-comprehension is plenty fast! The first test I ran before creating this post had some latency that I hadn't accounted for. – sh37211 May 01 '21 at 17:52