title: Errors Trying to Fancy Ternary Statements with Arrays, Python
body: <p>The basic problem is one of very highly customized kernel density estimators that do not fit the standard so I can't use existing code.</p>
  
  <p>Here is code that <em>works</em> for a Gaussian:</p>
  
  <pre><code>for counter1 in range (0,int(samples)):
      edf[counter1] = np.sum( np.reciprocal(len(values)*sigmas[:]*2)*np.exp(-0.5*((edf_diam[counter1]-values[:])/sigmas[:])*((edf_diam[counter1]-values[:])/sigmas[:])) )
  </code></pre>
  
  <p>What this is basically doing is, at every diameter point (counter1), add up every Gaussian piece from every values, and each values has its own bandwidth (sigmas).  It might not be the most efficient, but I've gotten it to work, so that's something.</p>
  
  <p>Where I'm running into issues is where I'm attempting to add in other kernel shapes.  The most simple being a "top hat" or uniform.  A Gaussian is unbound, but all the other kernels I want to program can only exist in a certain diameter range.  While I could iterate through each feature (values with its sigmas), that is much less efficient than if I can use fancy indexing.</p>
  
  <p>I have tried to do this with a ternary operator:</p>
  
  <pre><code>for counter1 in range (0,int(samples)):
      edf[counter1] = 0 if np.logical_or((edf_diam[counter1] &lt; (values[:]-sigmas[:])),((values[:]+sigmas[:]) &lt; edf_diam[counter1])) else np.sum( 1/(len(values)*sigmas[:]) )
  </code></pre>
  
  <p>What this code is supposed to do is be 0 if the logical_or() is true, and be a constant if the logical_or() is false.  Seems simple enough ... but I'm getting the usual error n00bs get with NumPy's logical statements of "ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()".</p>
  
  <p>The problem is that I don't think I can use any or all in this context.  I want it to go through every value in values with its corresponding sigmas, evaluate the statement for each one, and add it into my uniform kernel density estimator.</p>
  
  <p>A brute-force method may be:</p>
  
  <pre><code>for counter1 in range (0,len(values)):
      for counter2 in range (0,len(edf)):
          edf[counter2] += 0 if (edf_diam[counter2] &lt; values[counter1]-sigmas[counter1] || edf_diam[counter2] &gt; values[counter1]+sigmas[counter1]) else 1/(len(values)*sigmas[counter1]
  </code></pre>
  
  <p>However, that is much slower (it was with the Gaussian, anyway), and since I will also need to bootstrap this and run it a few thousand times on a few thousand features, I need the fastest and most efficient way.</p>
  
  <p>Help?</p>