title: plotting curve decision boundary in python using matplotlib
body: <p>I am new to machine learning with python. I've managed to draw the straight decision boundary for logistic regression using matplotlib. However, I am facing a bit of difficulty in plotting a curve line to understand the case of overfitting using some sample dataset.</p>
  
  <p>I am trying to build a logistic regression model using regularization and use regularization to control overfitting my data set.</p>
  
  <p>I am aware of the sklearn library, however I prefer writing code separately</p>
  
  <p>The test data sample I am working on is given below:</p>
  
  <pre><code>x=np.matrix('2,300;4,600;7,300;5,500;5,400;6,400;3,400;4,500;1,200;3,400;7,700;3,550;2.5,650')
  y=np.matrix('0;1;1;1;0;1;0;0;0;0;1;1;0') 
  </code></pre>
  
  <p>The decision boundary I am expecting is given in the graph below:</p>
  
  <p><img src="https://i.stack.imgur.com/qw9cA.png" alt="enter image description here"><br>
  Any help would be appreciated.</p>
  
  <p>I could plot a straight decision boundary using the code below:</p>
  
  <pre><code># plot of x 2D
  plt.figure()
  pos=np.where(y==1)
  neg=np.where(y==0)
  
  plt.plot(X[pos[0],0], X[pos[0],1], 'ro')
  plt.plot(X[neg[0],0], X[neg[0],1], 'bo')
  plt.xlim([min(X[:,0]),max(X[:,0])])
  plt.ylim([min(X[:,1]),max(X[:,1])])
  plt.show()
  
  # plot of the decision boundary
  plt.figure()
  pos=np.where(y==1)
  neg=np.where(y==0)
  
  plt.plot(x[pos[0],1], x[pos[0],2], 'ro')
  plt.plot(x[neg[0],1], x[neg[0],2], 'bo')
  plt.xlim([x[:, 1].min()-2 , x[:, 1].max()+2])
  plt.ylim([x[:, 2].min()-2 , x[:, 2].max()+2])
  
  
  plot_x = [min(x[:,1])-2,  max(x[:,1])+2]   # Takes a lerger decision line
  
  plot_y = (-1/theta_NM[2])*(theta_NM[1]*plot_x +theta_NM[0])
  plt.plot(plot_x, plot_y)
  </code></pre>
  
  <p>And my decision boundary looks like this:</p>
  
  <p><img src="https://i.stack.imgur.com/89eUa.png" alt="enter image description here"></p>
  
  <p>In an ideal scenario the above decision boundary is good but I would like to plot a curve decision boundary that will fit my training data very well but will overfit my test data. something similar to shown in the 1st plot</p>