-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
/
Copy pathcatboost_classifier.py
72 lines (58 loc) · 1.96 KB
/
catboost_classifier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Catboost Classifier Example
import numpy as np
from catboost import CatBoostClassifier
from matplotlib import pyplot as plt
from sklearn.datasets import load_iris
from sklearn.metrics import ConfusionMatrixDisplay
from sklearn.model_selection import train_test_split
def data_handling(data: dict) -> tuple:
# 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"])
def catboost(features: np.ndarray, target: np.ndarray) -> CatBoostClassifier:
"""
>>> catboost(np.array([[5.1, 3.6, 1.4, 0.2]]), np.array([0]))
<catboost.core.CatBoostClassifier object at 0x...>
"""
classifier = CatBoostClassifier(verbose=0)
classifier.fit(features, target)
return classifier
def main() -> None:
"""
>>> main()
Url for the algorithm:
https://catboost.ai/
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 a CatBoost Classifier from the training data
catboost_classifier = catboost(x_train, y_train)
# Display the confusion matrix of the classifier with both training and test sets
ConfusionMatrixDisplay.from_estimator(
catboost_classifier,
x_test,
y_test,
display_labels=names,
cmap="Blues",
normalize="true",
)
plt.title("Normalized Confusion Matrix - IRIS Dataset (CatBoost)")
plt.show()
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True)
main()