|
| 1 | +# XGBoost Classifier Example |
| 2 | +import numpy as np |
| 3 | +from matplotlib import pyplot as plt |
| 4 | +from sklearn.datasets import load_iris |
| 5 | +from sklearn.metrics import plot_confusion_matrix |
| 6 | +from sklearn.model_selection import train_test_split |
| 7 | +from xgboost import XGBClassifier |
| 8 | + |
| 9 | + |
| 10 | +def data_handling(data: dict) -> tuple: |
| 11 | + # Split dataset into features and target |
| 12 | + # data is features |
| 13 | + """ |
| 14 | + >>> data_handling(({'data':'[5.1, 3.5, 1.4, 0.2]','target':([0])})) |
| 15 | + ('[5.1, 3.5, 1.4, 0.2]', [0]) |
| 16 | + >>> data_handling( |
| 17 | + ... {'data': '[4.9, 3.0, 1.4, 0.2], [4.7, 3.2, 1.3, 0.2]', 'target': ([0, 0])} |
| 18 | + ... ) |
| 19 | + ('[4.9, 3.0, 1.4, 0.2], [4.7, 3.2, 1.3, 0.2]', [0, 0]) |
| 20 | + """ |
| 21 | + return (data["data"], data["target"]) |
| 22 | + |
| 23 | + |
| 24 | +def xgboost(features: np.ndarray, target: np.ndarray) -> XGBClassifier: |
| 25 | + """ |
| 26 | + >>> xgboost(np.array([[5.1, 3.6, 1.4, 0.2]]), np.array([0])) |
| 27 | + XGBClassifier(base_score=0.5, booster='gbtree', callbacks=None, |
| 28 | + colsample_bylevel=1, colsample_bynode=1, colsample_bytree=1, |
| 29 | + early_stopping_rounds=None, enable_categorical=False, |
| 30 | + eval_metric=None, gamma=0, gpu_id=-1, grow_policy='depthwise', |
| 31 | + importance_type=None, interaction_constraints='', |
| 32 | + learning_rate=0.300000012, max_bin=256, max_cat_to_onehot=4, |
| 33 | + max_delta_step=0, max_depth=6, max_leaves=0, min_child_weight=1, |
| 34 | + missing=nan, monotone_constraints='()', n_estimators=100, |
| 35 | + n_jobs=0, num_parallel_tree=1, predictor='auto', random_state=0, |
| 36 | + reg_alpha=0, reg_lambda=1, ...) |
| 37 | + """ |
| 38 | + classifier = XGBClassifier() |
| 39 | + classifier.fit(features, target) |
| 40 | + return classifier |
| 41 | + |
| 42 | + |
| 43 | +def main() -> None: |
| 44 | + |
| 45 | + """ |
| 46 | + >>> main() |
| 47 | +
|
| 48 | + Url for the algorithm: |
| 49 | + https://xgboost.readthedocs.io/en/stable/ |
| 50 | + Iris type dataset is used to demonstrate algorithm. |
| 51 | + """ |
| 52 | + |
| 53 | + # Load Iris dataset |
| 54 | + iris = load_iris() |
| 55 | + features, targets = data_handling(iris) |
| 56 | + x_train, x_test, y_train, y_test = train_test_split( |
| 57 | + features, targets, test_size=0.25 |
| 58 | + ) |
| 59 | + |
| 60 | + names = iris["target_names"] |
| 61 | + |
| 62 | + # Create an XGBoost Classifier from the training data |
| 63 | + xgboost_classifier = xgboost(x_train, y_train) |
| 64 | + |
| 65 | + # Display the confusion matrix of the classifier with both training and test sets |
| 66 | + plot_confusion_matrix( |
| 67 | + xgboost_classifier, |
| 68 | + x_test, |
| 69 | + y_test, |
| 70 | + display_labels=names, |
| 71 | + cmap="Blues", |
| 72 | + normalize="true", |
| 73 | + ) |
| 74 | + plt.title("Normalized Confusion Matrix - IRIS Dataset") |
| 75 | + plt.show() |
| 76 | + |
| 77 | + |
| 78 | +if __name__ == "__main__": |
| 79 | + import doctest |
| 80 | + |
| 81 | + doctest.testmod(verbose=True) |
| 82 | + main() |
0 commit comments