Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. import time
  2.  
  3. import copt as cp
  4. from sklearn.datasets import make_regression
  5. from sklearn.linear_model import Ridge
  6.  
  7. X, y, c = make_regression(
  8. random_state=0, n_samples=600, n_features=5, coef=True)
  9.  
  10. print(X.shape, y.shape)
  11.  
  12. f = cp.SquaredLoss(X, y, alpha=1. / X.shape[0])
  13.  
  14. copt_start = time.time()
  15. copt_ridge = cp.minimize_SAGA(f, trace=False, tol=1e-6)
  16. copt_stop = time.time()
  17.  
  18. # note sklearn Ridge also performs some checks etc besides fitting the coefs
  19. sklearn_ridge = Ridge(alpha=1., solver='saga', fit_intercept=False, tol=1e-6)
  20. sklearn_start = time.time()
  21. sklearn_ridge.fit(X, y)
  22. sklearn_stop = time.time()
  23.  
  24. print('coef:', c)
  25. print('copt coef:', copt_ridge.x)
  26. print('sklearn coef:', sklearn_ridge.coef_)
  27. print('copt time:', copt_stop - copt_start)
  28. print('sklearn time:', sklearn_stop - sklearn_start)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement