<jupyter_start><jupyter_text>XGBoost exampleIn this notebook we will train a simple gradient boosting model (using XGBoost) on telecom data.<jupyter_code>import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, f1_score
import xgboost as xgb
df = pd.read_csv('telecom_churn.csv')
df.head()
# Let's just use numbers for States and make International plan,Voice mail plan and Churn binary.

state_enc = LabelEncoder()
df['State'] = state_enc.fit_transform(df['State'])
df['International plan'] = (df['International plan'] == 'Yes').astype('int')
df['Voice mail plan'] = (df['Voice mail plan'] == 'Yes').astype('int')
df['Churn'] = (df['Churn']).astype('int')
df.head()
# Split to train/test in the following proportion 7:3. Initialize DMatrix objects.

X_train, X_test, y_train, y_test = train_test_split(df.drop('Churn', axis=1), df['Churn'],
                                                    test_size=0.3, stratify=df['Churn'], random_state=17)
dtrain = xgb.DMatrix(X_train, y_train)
dtest = xgb.DMatrix(X_test, y_test)
# Initialize parameters of the model

params = {
    'objective':'binary:logistic',    # binary classification
    'max_depth': 3,                   # limit max depth of trees
    'silent': 1,                      # muted output
    'eta': 1                          # learning rate
}

num_rounds = 50
# Pass the dict of params, data itself and number of rounds

xgb_model = xgb.train(params, dtrain, num_rounds)
# Check accuracy on test data using watchlist

watchlist  = [(dtest,'test'), (dtrain,'train')]
xgb_model = xgb.train(params, dtrain, num_rounds, watchlist)
# Make predictions
preds_prob = xgb_model.predict(dtest)
predicted_labels = preds_prob > 0.5
print("Accuracy and F1 on the test set are: {} and {}".format(
    round(accuracy_score(y_test, predicted_labels), 3),
    round(f1_score(y_test, predicted_labels), 3)))
# Plot feature imporatance
ax = xgb.plot_importance(xgb_model)
fig = ax.figure
fig.set_size_inches(12, 8)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11814744801512288, 'fraction_numerical': 0.010396975425330813, 'mean_word_length': 4.214285714285714, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '590076', 'license': 'MIT', 'n_tokens_mistral': 707, 'n_tokens_neox': 675, 'n_words': 193, 'path': '03_gradient_boosting/xgboost_example.ipynb', 'repo_name': 'r1sotto/ml-practice', 'size': '2116'}