title: Create new numpy one-hot encoded matrix based on complex logical expression?
body: <p>I stumbled upon <a href="https://stackoverflow.com/questions/19766757/replacing-numpy-elements-if-condition-is-met">this post</a> asking how to create a new <code>numpy</code> matrix based upon a simple logical expression</p>
  
  <p>For instance</p>
  
  <pre><code>&gt;&gt;&gt; import numpy as np
  &gt;&gt;&gt; a = np.random.randint(0, 5, size=(5, 4))
  &gt;&gt;&gt; a
  array([[4, 2, 1, 1],
         [3, 0, 1, 2],
         [2, 0, 1, 1],
         [4, 0, 2, 3],
         [0, 0, 0, 2]])
  &gt;&gt;&gt; b = a &lt; 3
  &gt;&gt;&gt; c = b.astype(int)
  &gt;&gt;&gt; c
  array([[0, 1, 1, 1],
         [0, 1, 1, 1],
         [1, 1, 1, 1],
         [0, 1, 1, 0],
         [1, 1, 1, 1]])
  </code></pre>
  
  <p>But I would like to use a more complex logical function, for example I would like to create a categorical, one-hot encoded variable, like so</p>
  
  <pre><code>X_Slow = (Y &lt; 500).astype(int)
  X_Medium = (Y &gt;= 500 and Y &lt; 1000).astype(int)
  X_Fast = (Y &gt;= 1000).astype(int)
  </code></pre>
  
  <p>But obviously the above manipulation is incorrect syntactically. I have tried</p>
  
  <pre><code>X_Medium = (Y[np.logical_and(Y &gt;= 500,Y &lt; 1000)]).astype(int)
  </code></pre>
  
  <p>Though this only returns an array with the number of elements that match the given criteria, I would like both "1s and 0s" in the column. </p>
  
  <p><strong>How can I do this in <code>numpy</code>?</strong></p>