Created
September 30, 2020 20:55
-
-
Save Kamaldeep0077/1d8fdf88c715292dc6343840b80140b6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.model_selection import GridSearchCV, StratifiedKFold | |
lr = LogisticRegression(solver='newton-cg') | |
#Setting the range for class weights | |
weights = np.linspace(0.0,0.99,200) | |
#Creating a dictionary grid for grid search | |
param_grid = {'class_weight': [{0:x, 1:1.0-x} for x in weights]} | |
#Fitting grid search to the train data with 5 folds | |
gridsearch = GridSearchCV(estimator= lr, | |
param_grid= param_grid, | |
cv=StratifiedKFold(), | |
n_jobs=-1, | |
scoring='f1', | |
verbose=2).fit(x_train, y_train) | |
#Ploting the score for different values of weight | |
sns.set_style('whitegrid') | |
plt.figure(figsize=(12,8)) | |
weigh_data = pd.DataFrame({ 'score': gridsearch.cv_results_['mean_test_score'], 'weight': (1- weights)}) | |
sns.lineplot(weigh_data['weight'], weigh_data['score']) | |
plt.xlabel('Weight for class 1') | |
plt.ylabel('F1 score') | |
plt.xticks([round(i/10,1) for i in range(0,11,1)]) | |
plt.title('Scoring for different class weights', fontsize=24) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment