Issue
I'm using the a python glmnet wrapper to translate R code to Python that was originally written with the glmnet package in python. The original R package documentation can be found here: https://cran.r-project.org/web/packages/glmnet/glmnet.pdf. The Python wrapper can be found here: https://pypi.org/project/glmnet/. The R model was originally written with the parameter "lambda = 0" which I cannot seem to get working properly with the python version of the code.
The original R model was fit with the following parameters and the coefficients can be returned with the following code:
model <- glmnet(X, Y ,
family = "gaussian",
lambda = 0 ,
alpha = 1 ,
intercept = FALSE)
coefficients(model)
Based on my understanding of the documentation, the lambda parameter in R seems to be equivalent to the lambda_path parameter in the Python wrapped version. Based on this, my first attempt at translating this was following:
model = ElasticNet(alpha = 1,
lambda_path=0,
fit_intercept = False)
model.fit(X,Y)
coeff = model.coef_
This returned the following error: object of type 'int' has no len()
.
Subsequently, I changed lambda_path to a np array in my next attempt:
model = ElasticNet(alpha = 1,
lambda_path=np.array([0]),
fit_intercept = False)
model.fit(X,Y)
coeff = model.coef_
This gave me a new error: ValueError: cannot select an axis to squeeze out which has size not equal to one
.
According to the module documentation within help(ElasticNet) it seems that lambda_path needs to be an array in decreasing order, unlike lambda in R which can simply be an integer (or multiple in an utterable in decreasing order). This has brought me to my final solution here:
model = ElasticNet(alpha = 1,
lambda_path=np.array([.01,0]),
fit_intercept = False)
model.fit(X,Y)
coeff = model.coef_
This now at least runs error free, however, the coefficients returned from python are significantly different than the coefficients returned in R. I've validated that the data is the same and I believe this has something to do with how I must be misusing the lambda parameters in python in a way that is not equivalent to the way they were used in R. How can I properly translate the lambda = 0 parameter between the R package and the python wrapper?
Solution
As it turns out, the python wrapper solves for each value of lambda provided. One can access the coefficients of the second value of lambda path inside of the mode.coef_path_ method. The code below will overwrite the model's default coefficients with the specified coefficients from lambda path.
model.coef_ = model.coef_path_[:,1]
Similarly, one can do the same thing if the model is fit with an intercept by using .intercept_ and .intercept_path_.
The solution above has nearly identical coefficients to the R version of my code down to 5 decimal places.
Answered By - throwawaydisplayname
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.