Skip to content

Commit 1bd7304

Browse files
committed
Fixes: #{6551}
1 parent c73cb7e commit 1bd7304

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

machine_learning/xgboostclassifier.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# -*- coding: utf-8 -*-
2+
"""xgboostclassifier.ipynb
3+
4+
Automatically generated by Colaboratory.
5+
6+
Original file is located at
7+
https://colab.research.google.com/drive/1UlMmSrfKLuRW9LPCz6BvDDJMPgPhO-yu
8+
"""
9+
10+
# XGBoost Classifier Example
11+
from matplotlib import pyplot as plt
12+
from sklearn.datasets import load_iris
13+
from xgboost import XGBClassifier
14+
from sklearn.metrics import plot_confusion_matrix
15+
from sklearn.model_selection import train_test_split
16+
17+
18+
def main():
19+
20+
"""
21+
The Url for the algorithm
22+
https://xgboost.readthedocs.io/en/stable/
23+
Iris type dataset is used to demonstrate algorithm.
24+
"""
25+
26+
# Load Iris dataset
27+
iris = load_iris()
28+
29+
# Split dataset into train and test data
30+
x = iris["data"] # features
31+
y = iris["target"]
32+
x_train, x_test, y_train, y_test = train_test_split(
33+
x, y, test_size=0.3, random_state=1
34+
)
35+
36+
# XGBoost Classifier
37+
xgb = XGBClassifier()
38+
xgb.fit(x_train, y_train)
39+
40+
# Display Confusion Matrix of Classifier
41+
plot_confusion_matrix(
42+
xgb,
43+
x_test,
44+
y_test,
45+
display_labels=iris["target_names"],
46+
cmap="Blues",
47+
normalize="true",
48+
)
49+
plt.title("Normalized Confusion Matrix - IRIS Dataset")
50+
plt.show()
51+
52+
53+
if __name__ == "__main__":
54+
main()

0 commit comments

Comments
 (0)