Guest User

Untitled

a guest
May 20th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. #initialize libraries
  2. library(xlsx)
  3. library(glmnet)
  4. library(plotmo)
  5.  
  6. #Data Input
  7. x<-matrix(c(-1,-4,0,0,1,16),nrow=3,ncol=2,byrow=TRUE)
  8. y<-matrix(c(-2.2,0,3.8),nrow=3,ncol=1,byrow=TRUE)
  9.  
  10. #lasso fit and plots
  11. yfit=glmnet(x,y,family="gaussian",standardize.response=FALSE,intercept = FALSE,lambda=(exp(seq(log(0.0001), log(100), length.out=100))),upper.limits = 1000,lower.limits = -1000)
  12. ycvfit=cv.glmnet(x,y,family="gaussian",nfolds=3,standardize.response=FALSE,intercept = FALSE,grouped = FALSE,lambda=(exp(seq(log(0.0001), log(100), length.out=100))),upper.limits = 1000,lower.limits = -1000)
  13. plot.cv.glmnet(ycvfit)
  14. plot.glmnet(yfit,xvar="lambda")
  15.  
  16.  
  17. #writing results
  18. lassocoeff=as.matrix(coef(yfit,s=0.1))
  19. print(lassocoeff)
  20.  
  21. import numpy as np
  22. import matplotlib.pyplot as plt
  23. from sklearn.linear_model import Lasso, LassoCV
  24. from __future__ import division
  25. import time
  26.  
  27.  
  28. x=np.array([[-1,-4],[0,0],[1,16]])
  29. y=np.array([[-2.2],[0],[3.8]]).reshape(3,)
  30.  
  31. #set alphas to be tested, note this is in logspace
  32. alphas = np.logspace(-7,5,num=1000,base=np.e)
  33.  
  34. #plot coefficent size vs log(alpha) graph.
  35. lasso = Lasso(max_iter=10000, normalize=False, fit_intercept=False)
  36. coefs = []
  37. for a in alphas:
  38. lasso.set_params(alpha=a)
  39. lasso.fit((x), (y))
  40. coefs.append(lasso.coef_)
  41. ax = plt.gca()
  42. ax.plot(np.log(alphas), coefs)
  43. ax.set_xscale('linear')
  44. plt.axis('tight')
  45. plt.xlabel('log(alpha)')
  46. plt.ylabel('weights')
  47.  
  48.  
  49. print("Computing regularization path using the coordinate descent lasso...")
  50. t1 = time.time()
  51. model = LassoCV(alphas=None,cv=3, normalize=False, fit_intercept=False).fit((x), (y))
  52. t_lasso_cv = time.time() - t1
  53. # Display results
  54. m_log_alphas = np.log(model.alphas_)
  55. plt.figure()
  56. #To properly show, the ymin and ymax may need adjusted
  57. ymin, ymax = 0, 25
  58. plt.plot(m_log_alphas, model.mse_path_.mean(axis=-1), 'k',
  59. label='Average across the folds', linewidth=2)
  60. plt.axvline(np.log(model.alpha_), linestyle='--', color='k',
  61. label='alpha: CV estimate')
  62. plt.legend()
  63. plt.xlabel('log(alpha)')
  64. plt.ylabel('Mean square error')
  65. plt.title('Mean square error on each fold: coordinate descent '
  66. '(train time: %.2fs)' % t_lasso_cv)
  67. plt.axis('tight')
  68. plt.ylim(ymin, ymax)
  69. plt.xlim(-10,10)
  70.  
  71. #Calculate Lasso coefficents from proper alpha. Skipping for now, as we are just comparing 1 alpha value.
  72. #lassocv = LassoCV(eps=.000000001,max_n_alphas=1000, cv=N, max_iter=100000, normalize=False, fit_intercept=False)
  73. #lassocv.fit(x, y)
  74. #minimumalpha = lassocv.alpha_
  75. lasso=Lasso(alpha=.1,normalize=False,fit_intercept=False,max_iter=100000)
  76. lasso.fit(x, y)
  77. print(lasso.coef_)
Add Comment
Please, Sign In to add comment