title: Numpy: fuzzy 'greater_than' operator, working on list of values (requesting advices on existing code)
body: <p>I have implemented a numpy function that:</p>
  <ul>
  <li>takes as inputs:
  <ul>
  <li>a n (rows) x m (columns) array of floats.</li>
  <li>a <code>threshold</code> (float)</li>
  </ul>
  </li>
  <li>for each row:
  <ul>
  <li>if the max value of the row is larger than or equal to <code>threshold</code>,</li>
  <li>if this max value is not preceded in the same row by a min value lower than or equal to <code>-threshold</code>,</li>
  <li>then this row is flagged <code>True</code> (larger than),</li>
  <li>else this row is flagged <code>False</code> (not larger than)</li>
  </ul>
  </li>
  <li>returns then this n (rows) x 1 (column) array of booleans</li>
  </ul>
  <p>What I have implemented works (at least on provided example), but I am far from being an expert in numpy, and I wonder if there is no more efficient way of handling this (possibly avoid the miscellaneous <code>transpose</code> &amp; <code>tile</code> for instance?)
  I would gladly accept any advice on how making this function more efficient and/or readable.</p>
  <pre class="lang-py prettyprint-override"><code>import numpy as np
  import pandas as pd
  
  # Test data
  threshold=0.02       #2%
  df = pd.DataFrame({'variation_1': [0.01, 0.02, 0.005, -0.02, -0.01, -0.01],
                     'variation_2': [-0.01, 0.08, 0.08, 0.01, -0.02, 0.01],
                     'variation_3': [0.005, -0.03, -0.03, 0.002, 0.025, -0.03],
                    })
  
  data = df.values
  </code></pre>
  <p>Checking expected results:</p>
  <pre class="lang-py prettyprint-override"><code>In [75]: df
  Out[75]: 
     variation_1  variation_2  variation_3   # Expecting
  0        0.010        -0.01        0.005   # False (no value larger than threshold)
  1        0.020         0.08       -0.030   # True (1st value equal to threshold)
  2        0.005         0.08       -0.030   # True (2nd value larger than threshold)
  3       -0.020         0.01        0.002   # False (no value larger than threshold)
  4       -0.010        -0.02        0.025   # False (2nd value lower than -threshold)
  5       -0.010         0.01       -0.030   # False (no value larger than threshold)
  </code></pre>
  <p>Current function.</p>
  <pre class="lang-py prettyprint-override"><code>def greater_than(data: np.ndarray, threshold: float) -&gt; np.ndarray:
      # Step 1.
      # Filtering out from 'low_max' mask the rows which 'max' is not greater than or equal
      # to 'threshold'. 'low_max' is reshaped like input array for use in next step.
      data_max = np.amax(data, axis=1)
      low_max = np.transpose([data_max &gt;= threshold] * data.shape[1])
      
      # Step 2.
      # Filtering values preceding max of each row
      max_idx = np.argmax(data, axis=1)                   # Get idx of max.
      max_idx = np.transpose([max_idx] * data.shape[1])   # Reshape like input array.
      # Create an array of index.
      idx_array = np.tile(np.arange(data.shape[1]), (data.shape[0],1))
      # Keep indices lower than index of max for each row, and filter out rows with
      # a max too low vs 'threshold' (from step 1).
      mask_max = (idx_array &lt;= max_idx) &amp; (low_max)
      
      # Step 3.
      # On a masked array re-using mask from step 2 to filter out unqualifying values,
      # filter out rows with a 'min' preceding the 'max' and that are lower than or
      # equal to '-threshold'. 
      data = np.ma.array(data, mask=~mask_max)
      data_min = np.amin(data, axis=1)
      mask_min = data_min &gt; -threshold
      
      # Return 'mask_min', filling masked values with 'False'.
      return np.ma.filled(mask_min, False)
  </code></pre>
  <p>Results.</p>
  <pre class="lang-py prettyprint-override"><code>res = greater_than(data, threshold)
  In [78]:res
  Out[78]: array([False,  True,  True, False, False, False])
  </code></pre>
  <p>Thanks in advance for any advice!</p>