Created
March 15, 2021 09:56
-
-
Save PranjalDureja0002/13c92af45855538bb9acbbbfffb6d2aa to your computer and use it in GitHub Desktop.
model
This file contains hidden or 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
# A parameter grid for XGBoost | |
parameters = { | |
'n_estimators': [100,500,1000], | |
'learning_rate': [0.1, 0.01, 0.05] | |
} | |
xgb = XGBClassifier(objective='binary:logistic', | |
silent=True, nthread=4) | |
xg_grid = GridSearchCV(xgb, param_grid=parameters, n_jobs=-1, verbose=1,scoring='f1_macro',cv=3,return_train_score=True) | |
xg_grid.fit(X_train,y_train) | |
from sklearn.metrics import f1_score | |
best_est=xg_grid.best_params_['n_estimators'] | |
best_rate=xg_grid.best_params_['learning_rate'] | |
xgb_model = XGBClassifier(max_depth=10,n_estimators=best_est,learning_rate=best_rate,objective='binary:logistic',silent=True, nthread=4,njobs=-1).fit(X_train, y_train) | |
xgb_prediction = xgb_model.predict(X_test) | |
print('training score:', f1_score(y_train,xgb_model.predict(X_train), average='macro')) | |
print('testing score:', f1_score(y_test,xgb_prediction, average='macro')) | |
parameters = { | |
'n_estimators': [100,500,1000], | |
'learning_rate': [0.1, 0.01, 0.05] | |
} | |
xgb = XGBClassifier(objective='binary:logistic', | |
silent=True, nthread=4) | |
xg_grid = GridSearchCV(xgb, param_grid=parameters, n_jobs=-1, verbose=1,scoring='roc_auc',cv=3,return_train_score=True) | |
xg_grid.fit(X_train,y_train) | |
def pred_func(model,data): | |
list1=[] | |
list1=model.predict_proba(data)[:,1] | |
return list1 | |
best_est=xg_grid.best_params_['n_estimators'] | |
best_rate=xg_grid.best_params_['learning_rate'] | |
xg1= XGBClassifier(learning_rate=best_rate,n_estimators=best_est,objective='binary:logistic', | |
silent=True, nthread=4) | |
xg1.fit(X_train, y_train) | |
y_train_pred = pred_func(xg1,X_train) | |
y_test_pred = pred_func(xg1,X_test) | |
train_fpr, train_tpr, tr_thresholds = roc_curve(y_train, y_train_pred) | |
test_fpr, test_tpr, te_thresholds = roc_curve(y_test, y_test_pred) | |
plt.plot(train_fpr, train_tpr, label="Train AUC ="+str(auc(train_fpr, train_tpr))) | |
plt.plot(test_fpr, test_tpr, label="Test AUC ="+str(auc(test_fpr, test_tpr))) | |
plt.legend() | |
plt.xlabel("FPR") | |
plt.ylabel("TPR") | |
plt.title("AUC") | |
plt.grid() | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment