-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
XGBoost Classifier #7106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
XGBoost Classifier #7106
Changes from all commits
1bd7304
8990641
5feb35b
a927e97
aaaf398
06a9025
0decdf8
d795ac4
e16b32b
f797b34
bd88865
3cbd77b
06b6981
0519814
bc690dd
3b899b1
8c111ca
08cf249
c282905
776162d
93f1c64
49a6223
147bfd4
4451683
489822d
844b4be
79603f7
5751029
9e77f64
0ccc63f
f44646b
ccf61cd
9c0bbff
34d7dc3
6d13459
80d86c1
5f9cae8
22d8913
3f36987
a357282
ae11a52
8a42329
d9cded2
682943f
0de2c82
d04fd28
2c7f99c
1f3affb
307122f
7d42c2e
32a5206
32091d3
98dc009
6496c2c
c78ede9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# XGBoost Classifier Example | ||
import numpy as np | ||
from matplotlib import pyplot as plt | ||
from sklearn.datasets import load_iris | ||
from sklearn.metrics import plot_confusion_matrix | ||
from sklearn.model_selection import train_test_split | ||
from xgboost import XGBClassifier | ||
|
||
|
||
def data_handling(data: dict) -> tuple: | ||
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# Split dataset into features and target | ||
# data is features | ||
""" | ||
>>> data_handling(({'data':'[5.1, 3.5, 1.4, 0.2]','target':([0])})) | ||
('[5.1, 3.5, 1.4, 0.2]', [0]) | ||
>>> data_handling( | ||
... {'data': '[4.9, 3.0, 1.4, 0.2], [4.7, 3.2, 1.3, 0.2]', 'target': ([0, 0])} | ||
... ) | ||
('[4.9, 3.0, 1.4, 0.2], [4.7, 3.2, 1.3, 0.2]', [0, 0]) | ||
""" | ||
return (data["data"], data["target"]) | ||
|
||
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def xgboost(features: np.ndarray, target: np.ndarray) -> XGBClassifier: | ||
""" | ||
>>> xgboost(np.array([[5.1, 3.6, 1.4, 0.2]]), np.array([0])) | ||
XGBClassifier(base_score=0.5, booster='gbtree', callbacks=None, | ||
colsample_bylevel=1, colsample_bynode=1, colsample_bytree=1, | ||
early_stopping_rounds=None, enable_categorical=False, | ||
eval_metric=None, gamma=0, gpu_id=-1, grow_policy='depthwise', | ||
importance_type=None, interaction_constraints='', | ||
learning_rate=0.300000012, max_bin=256, max_cat_to_onehot=4, | ||
max_delta_step=0, max_depth=6, max_leaves=0, min_child_weight=1, | ||
missing=nan, monotone_constraints='()', n_estimators=100, | ||
n_jobs=0, num_parallel_tree=1, predictor='auto', random_state=0, | ||
reg_alpha=0, reg_lambda=1, ...) | ||
""" | ||
classifier = XGBClassifier() | ||
classifier.fit(features, target) | ||
return classifier | ||
|
||
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def main() -> None: | ||
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file
Moddy2024 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
""" | ||
>>> main() | ||
|
||
Url for the algorithm: | ||
https://xgboost.readthedocs.io/en/stable/ | ||
Iris type dataset is used to demonstrate algorithm. | ||
""" | ||
|
||
# Load Iris dataset | ||
iris = load_iris() | ||
features, targets = data_handling(iris) | ||
x_train, x_test, y_train, y_test = train_test_split( | ||
features, targets, test_size=0.25 | ||
) | ||
|
||
names = iris["target_names"] | ||
|
||
# Create an XGBoost Classifier from the training data | ||
xgboost_classifier = xgboost(x_train, y_train) | ||
|
||
# Display the confusion matrix of the classifier with both training and test sets | ||
plot_confusion_matrix( | ||
xgboost_classifier, | ||
x_test, | ||
y_test, | ||
display_labels=names, | ||
cmap="Blues", | ||
normalize="true", | ||
) | ||
plt.title("Normalized Confusion Matrix - IRIS Dataset") | ||
plt.show() | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod(verbose=True) | ||
main() |
Uh oh!
There was an error while loading. Please reload this page.