Skip to content

Instantly share code, notes, and snippets.

@samyumobi
Last active October 5, 2021 16:26
Show Gist options
  • Select an option

  • Save samyumobi/d1e1456d58919cf07fd67891891cd31c to your computer and use it in GitHub Desktop.

Select an option

Save samyumobi/d1e1456d58919cf07fd67891891cd31c to your computer and use it in GitHub Desktop.
### Hyper parameter tuning Logistic Regression model
from sklearn.model_selection import GridSearchCV
from sklearn import linear_model
from sklearn.pipeline import make_pipeline
lr = linear_model.LogisticRegression(solver ='lbfgs', penalty = 'l2')
# Use gridsearch CV to search for the bes parameter
grid = GridSearchCV(lr, {'C':[0.0001,0.001,0.01,0.1,1,10]})
grid.fit(X_train, Y_train)
# Print out the best parameter
print("Optimal Regularization strength is :", grid.best_params_)
#Initializing logistic regression object
lr2 = linear_model.LogisticRegression( C = 1, penalty = 'l2')
lr2.fit(X_train, Y_train)
print("Accuracy score of the logistic regression model ",lr2.score(X_test, Y_test)*100,"%")
### Plot the inverse regularization strength
train_errors , test_errors = [], []
C_list = [0.0001,0.001,0.01,0.1,1,10]
for i in C_list:
lr3 = linear_model.LogisticRegression( C = i, penalty = 'l2')
lr3.fit(X_train, Y_train)
## Evaluate train and test error rates
train_errors.append(lr3.score(X_train, Y_train))
test_errors.append(lr3.score(X_test, Y_test))
plt.semilogx(C_list, train_errors,C_list, test_errors )
plt.legend(("train","test"))
plt.xlabel("Inverse regularization strength")
plt.ylabel('Accuracy score')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment