title: Python polynomial regression plotting wrong?
body: <blockquote>
    <p>Blockquote</p>
  </blockquote>
  
  <p>New to python and trying to complete a third order polynomial regression on some data. when I use polynomial regression I don't get the fit I am expecting. I am trying to understand why the polynomial regression in python is worse then in excel. When I fit the same data in excel I get a coefficient of determination of ≈.95 and the plot looks like a third order polynomial. However, using sickitlearn it is ≈.78 and the fit almost looks linear. Is this happening because I do not have enough data? Also does having x as datetime64[ns]type on my x-axis effect the regression? The code runs. However,I am not sure if this is a coding problem or some other problem.</p>
  
  <p>I am using anaconda (python 3.7) and running the code in spyder</p>
  
  <pre><code>import operator
  import numpy as np
  import matplotlib.pyplot as plt
  import pandas as pd
  from sklearn.linear_model import LinearRegression
  from sklearn.preprocessing import PolynomialFeatures
  #import data
  data = pd.read_excel(r'D:\Anaconda\Anaconda\XData\data.xlsx', skiprows = 0)
  
  x=np.c_[data['Date']]
  y=np.c_[data['level']]
  #regression
  polynomial_features= PolynomialFeatures(degree=3)
  x_poly = polynomial_features.fit_transform(x)
  
  model = LinearRegression()
  model.fit(x_poly, y)
  y_poly_pred = model.predict(x_poly)
  #check regression stats
  rmse = np.sqrt(mean_squared_error(y,y_poly_pred))
  r2 = r2_score(y,y_poly_pred)
  print(rmse)
  print(r2)
  
  #plot
  plt.scatter(x, y, s=10)
  
  # sort the values of x b[![enter image description here][1]][1]efore line plot
  sort_axis = operator.itemgetter(0)
  sorted_zip = sorted(zip(x,y_poly_pred), key=sort_axis)
  x, y_poly_pred = zip(*sorted_zip)
  plt.plot(x, y_poly_pred, color='m')
  plt.show()
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/rQlzr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rQlzr.png" alt="Python plot"></a></p>
  
  <p><a href="https://i.stack.imgur.com/AqlSz.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AqlSz.jpg" alt="enter image description here"></a></p>