Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save audhiaprilliant/972e7112f66954cfce8af6164509de8a to your computer and use it in GitHub Desktop.

Select an option

Save audhiaprilliant/972e7112f66954cfce8af6164509de8a to your computer and use it in GitHub Desktop.
End to end machine learning model deployment using flask
# Machine learning model development
# XGBoost model
xgb_model = xgb.XGBClassifier(
objective = 'binary:logistic',
use_label_encoder = False
)
# Define parameters range
params = {
'eta': np.arange(0.1, 0.26, 0.05),
'min_child_weight': np.arange(1, 5, 0.5).tolist(),
'gamma': [5],
'subsample': np.arange(0.5, 1.0, 0.11).tolist(),
'colsample_bytree': np.arange(0.5, 1.0, 0.11).tolist()
}
# Make a scorer from a performance metric or loss function
scorers = {
'f1_score': make_scorer(f1_score),
'precision_score': make_scorer(precision_score),
'recall_score': make_scorer(recall_score),
'accuracy_score': make_scorer(accuracy_score)
}
# k-fold cross validation
skf = KFold(n_splits = 10, shuffle = True)
# Set up the grid search CV
grid = GridSearchCV(
estimator = xgb_model,
param_grid = params,
scoring = scorers,
n_jobs = -1,
cv = skf.split(X_train, np.array(y_train)),
refit = 'accuracy_score'
)
# Fit the model
grid.fit(X = X_train, y = y_train)
# Best parameters
grid.best_params_
# Create a prediction of training
predicted = grid.predict(X_val)
# Model evaluation - training data
accuracy_baseline = accuracy_score(predicted, np.array(y_val))
recall_baseline = recall_score(predicted, np.array(y_val))
precision_baseline = precision_score(predicted, np.array(y_val))
f1_baseline = f1_score(predicted, np.array(y_val))
print('Accuracy for baseline :{}'.format(round(accuracy_baseline, 5)))
print('Recall for baseline :{}'.format(round(recall_baseline, 5)))
print('Precision for baseline :{}'.format(round(precision_baseline, 5)))
print('F1 Score for baseline :{}'.format(round(f1_baseline, 5)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment