user_137

scikit-learn

Oct 9th, 2014
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.83 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import random, time, math
  4.  
  5. aa = np.array(range(10))
  6. X = aa.reshape(len(aa), 1)
  7. y = np.array(range(10))
  8. print('\n', X, '\n\n', y)
  9.  
  10. # regression
  11. from sklearn import linear_model
  12. regr = linear_model.LinearRegression()
  13. regr.fit(X, y)
  14. print('\n', regr.coef_, regr.intercept_)
  15.  
  16. regr.fit(X, y*2)
  17. print('\n', regr.coef_, regr.intercept_)
  18.  
  19. regr.fit(X, y+10)
  20. print('\n', regr.coef_, regr.intercept_)
  21.  
  22.  
  23. np.dot(X, regr.coef_)
  24. X = np.array([10, 20, 30, 40]).reshape(4, 1)
  25. np.dot(X, regr.coef_)
  26. regr.predict(90)
  27.  
  28. # what happens with squaring
  29. regr.fit(X, y**2)
  30. print('\n', regr.coef_)
  31. y2 = np.dot(X, regr.coef_)
  32. # y2 is array([  0.,   9.,  18.,  27.,  36.,  45.,  54.,  63.,  72.,  81.])
  33. # So lets plot the original and the fit (green)
  34. plt.plot(aa, y**2), plt.plot(aa, y2),  plt.show()
Add Comment
Please, Sign In to add comment