fork download
  1. import time
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. from sklearn.linear_model import LinearRegression
  5. from sklearn.preprocessing import PolynomialFeatures
  6. import matplotlib as mpl
  7. from mpl_toolkits.mplot3d import Axes3D
  8.  
  9. rng = np.random.RandomState(1)
  10. X = np.linspace(0, 50, 100000)
  11. X = X.reshape(50000, 2)
  12. y = np.sin(X[:,0])+X[:,1]*np.random.rand(1)
  13.  
  14. from sklearn.pipeline import make_pipeline
  15. poly_model = make_pipeline(PolynomialFeatures(20), LinearRegression(n_jobs=2))
  16.  
  17. tm = time.time()
  18. poly_model.fit(X, y)
  19. tm_train = time.time() - tm
  20.  
  21. tm = time.time()
  22. y_pred = poly_model.predict(X)
  23. tm_predict = time.time() - tm
  24.  
  25. print("Training Time: %4fs" %tm_train)
  26. print("Prediction Time: %4fms" %(tm_predict*1000))
  27.  
  28. fig = plt.figure()
  29. ax = fig.gca(projection='3d')
  30.  
  31. ax.plot(X[:,0].ravel(), X[:,1].ravel(), y.ravel(), 'b.')
  32. ax.plot(X[:,0].ravel(), X[:,1].ravel(), y_pred.ravel(), 'r')
  33. plt.show()
Runtime error #stdin #stdout #stderr 0.87s 49572KB
stdin
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 4, in <module>
ImportError: No module named 'sklearn'