title: Comparing the GLMNET output of R with Python using LogisticRegression()
body: <p>I am using Logistic Regression with the <code>L1</code> norm (LASSO).</p>
  
  <p>I have opted to used the <code>glmnet</code> package in <code>R</code> and the <code>LogisticRegression()</code>from the <code>sklearn.linear_model</code> in <code>python</code>. From my understanding this should give the same results however they are not.</p>
  
  <p>Note that I did not scale my data.</p>
  
  <p>For <code>python</code> I have used the below link as a reference:</p>
  
  <p><a href="https://chrisalbon.com/machine_learning/logistic_regression/logistic_regression_with_l1_regularization/" rel="nofollow noreferrer">https://chrisalbon.com/machine_learning/logistic_regression/logistic_regression_with_l1_regularization/</a></p>
  
  <p>and for <code>R</code> I have used the below link:</p>
  
  <p><a href="http://www.sthda.com/english/articles/36-classification-methods-essentials/149-penalized-logistic-regression-essentials-in-r-ridge-lasso-and-elastic-net/?fbclid=IwAR0ZTjoGqRgH5vNum9CloeGVaHdwlqDHwDdoGKJXwncOgIT98qUXGcnV70k" rel="nofollow noreferrer">http://www.sthda.com/english/articles/36-classification-methods-essentials/149-penalized-logistic-regression-essentials-in-r-ridge-lasso-and-elastic-net/?fbclid=IwAR0ZTjoGqRgH5vNum9CloeGVaHdwlqDHwDdoGKJXwncOgIT98qUXGcnV70k</a></p>
  
  <p>Here is the code used in <code>R</code></p>
  
  <pre><code>###################################
  #### LASSO LOGISTIC REGRESSION ####
  ##################################
  x &lt;- model.matrix(Y~., Train.Data.SubPop)[,-1]
  y &lt;- Train.Data.SubPop$Y
  lambda_seq = c(0.0001, 0.01, 0.05, 0.0025)
  
  cv_output &lt;- cv.glmnet(x,y,alpha=1, family = "binomial", lambda = lambda_seq)
  
  cv_output$lambda.min
  
  lasso_best &lt;- glmnet(x,y, alpha = 1, family = "binomial", lambda = cv_output$lambda.min)
  </code></pre>
  
  <p>Below is my Python code:</p>
  
  <pre><code>C = [0.001, 0.01, 0.05, 0.0025]
  
  for c in C:
      clf = LogisticRegression(penalty='l1', C=c, solver='liblinear')
      clf.fit(X_train, y_train)
      print('C:', c)
      print('Coefficient of each feature:', clf.coef_)
      print('Training accuracy:', clf.score(X_train_std, y_train))
      print('Test accuracy:', clf.score(X_test_std, y_test))
      print('')
  </code></pre>
  
  <p>When I exported the optimal value from the <code>cv.glment()</code> function in <code>R</code> it gave me that the optimal lambda is <code>0.0001</code> however, if I look at the analysis from <code>python</code> the best accuracy/precision and recall came from <code>0.05</code>.</p>
  
  <p>I have tried to fit the model with the 0.05 in <code>R</code> and only 1 non-zero coefficient gave me but in <code>phython</code> I had 7.</p>
  
  <p>can someone help me understand why this discrepancies and difference pleasE?</p>
  
  <p>Also, if someone can guide me how to replicate <code>python</code> code in <code>R</code> it would be very helpful!</p>