import time
import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D

rng = np.random.RandomState(1)
X = np.linspace(0, 50, 100000)
X = X.reshape(50000, 2)
y = np.sin(X[:,0])+X[:,1]*np.random.rand(1)

from sklearn.pipeline import make_pipeline
poly_model = make_pipeline(PolynomialFeatures(20), LinearRegression(n_jobs=2))

tm = time.time()
poly_model.fit(X, y)
tm_train = time.time() - tm

tm = time.time()
y_pred = poly_model.predict(X)
tm_predict = time.time() - tm

print("Training Time: %4fs" %tm_train)
print("Prediction Time: %4fms" %(tm_predict*1000))

fig = plt.figure()
ax = fig.gca(projection='3d')

ax.plot(X[:,0].ravel(), X[:,1].ravel(), y.ravel(), 'b.')
ax.plot(X[:,0].ravel(), X[:,1].ravel(), y_pred.ravel(), 'r')
plt.show()