title: Numpy apply function to every item in array
body: <p>So let's say I have a 2d array. How can I apply a function to every single item in the array and replace that item with the return? Also, the function's return will be a tuple, so the array will become 3d.</p>
  <p>Here is the code in mind.</p>
  <pre class="lang-py prettyprint-override"><code>def filter_func(item):
      if 0 &lt;= item &lt; 1:
          return (1, 0, 1)
      elif 1 &lt;= item &lt; 2:
          return (2, 1, 1)
      elif 2 &lt;= item &lt; 3:
          return (5, 1, 4)
      else:
          return (4, 4, 4)
  
  myarray = np.array([[2.5, 1.3], [0.4, -1.0]])
  
  # Apply the function to an array
  
  print(myarray)
  
  # Should be array([[[5, 1, 4],
  #                   [2, 1, 1]],
  #                  [[1, 0, 1],
  #                   [4, 4, 4]]])
  </code></pre>
  <p>Any ideas how I could do it? One way is to do <code>np.array(list(map(filter_func, myarray.reshape((12,))))).reshape((2, 2, 3))</code> but that's quite slow, especially when I need to do it on an array of shape (1024, 1024).</p>
  <p>I've also seen people use np.vectorize, but it somehow ends up as <code>(array([[5, 2], [1, 4]]), array([[1, 1], [0, 4]]), array([[4, 1], [1, 4]])).</code> Then it has shape of (3, 2, 2).</p>