title: In a regression surface Z, how to find maximum X value where Z is less than a predefined value?
body: <p>I have created a meshgrid of X, Y with dimensions I x I and corresponding surface Z from a regression model. How can I pull out the coordinates of maximum of X, where Z is less than a given threshold value?</p>
  
  <h1>Input data</h1>
  
  <pre><code>x = np.array( )
  y = np.array( )
  z = np.array( )
  
  data = np.column_stack((x, y, z))
  
  # regular grid covering the domain of the data
  X, Y = np.meshgrid(x, y)
  
  XX = X.flatten()
  YY = Y.flatten()
  
  # best-fit quadratic curve
  A = np.c_[np.ones(data.shape[0]), data[:, :2], np.prod(data[:, :2], axis=1), data[:, :2]**2]
  C, _, _, _ = scipy.linalg.lstsq(A, data[:, 2])
  
  # evaluate it on a grid
  Z = np.dot(np.c_[np.ones(XX.shape), XX, YY, XX * YY, XX**2, YY**2],C).reshape(X.shape)
  
  # Desired output
  
  max(X, where Z &lt; a) = [x1, y1, z1]
  </code></pre>
  
  <p>List with coordinates of maximized X value where Z is less than a.</p>