title: How would I display a 3rd order polynomial boundary line for linear regression in Python?
body: <p>Assume I have an <strong>m x 2</strong> dataset <strong>X</strong> and run a linear regression on it to find a weight set <strong>W</strong>.  Also assume I transform my data by third order polynomial operator <strong>P((x1,x2)) =(1,x1,x2, x1^2, x1*x2, x2^2,x1^3, x1^2 * x2, x1*x2^2, x2^3)</strong>, and run a linear regression on the transformed data and find a weight set <strong>w</strong>.</p>
  
  <p>My goal is to reproduce plots like these. <a href="https://i.stack.imgur.com/AcMIv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AcMIv.png" alt="enter image description here"></a></p>
  
  <p>I know how to plot the line on the left, but I am unsure how to display the 3rd order polynomial.</p>
  
  <p>My idea is to do:</p>
  
  <pre><code>plot_poly(X,labels, weights, initial, final, num):
      plt.scatter(X[:, 0][labels=='Blue'], X[:, 1][labels=='Blue'], color='blue', marker = '.')
      plt.scatter(X[:, 0][labels=='Red'], X[:, 1][labels=='Red']], color='red', marker = '.')
      w = weights
      x = np.linspace(initial, final, num)
      y = w[0]*1 + w[1]*(x) + w[2]*(x) + w[3]*(x**2) + w[4]*(x**2) + \
          w[5]*(x**2) + w[6]*(x**3) + w[7]*(x**3) + w[8]*(x**3) + \
          w[9]*(x**3)
      plt.plot(x,y)
  </code></pre>
  
  <p>But when I try this out it seems to fail, specifically the vertical axis becomes so huge that it shrinks the data and the polynomial doesn't come close to the data (Picture below).  Is there a better way to go about plotting this?</p>
  
  <p><a href="https://i.stack.imgur.com/mzde4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mzde4.png" alt="enter image description here"></a></p>