forked from XpressAI/xai-xgboost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxgboost_components.py
215 lines (179 loc) · 8.25 KB
/
xgboost_components.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
from xai_components.base import InArg, OutArg, InCompArg, Component, xai_component
@xai_component
class XGBoostBinaryClassifier(Component):
"""
Trains an XGBoost classifier for binary classification tasks.
#### Reference:
- [XGBoost Binary Classification](https://xgboost.readthedocs.io/en/stable/tutorials/model.html)
##### inPorts:
- X_train: The training data.
- y_train: The target variable for the training data.
- n_estimators: Number of gradient boosted trees. Default: 100
- max_depth: Maximum tree depth for base learners. Default: 3
- learning_rate: Boosting learning rate. Default: 0.1
- objective: Specify 'logistic' or 'hinge', or full specification 'binary:logistic' or 'binary:hinge'. Default: 'logistic'
##### outPorts:
- model: The trained XGBoost binary classification model.
"""
X_train: InCompArg[any]
y_train: InCompArg[any]
n_estimators: InArg[int]
max_depth: InArg[int]
learning_rate: InArg[float]
objective: InArg[str]
model: OutArg[any]
def __init__(self):
super().__init__()
self.n_estimators.value = 100
self.max_depth.value = 3
self.learning_rate.value = 0.1
self.objective.value = 'logistic'
def execute(self, ctx) -> None:
from xgboost import XGBClassifier
objective_full = self.objective.value if ':' in self.objective.value else 'binary:' + self.objective.value
self.model.value = XGBClassifier(n_estimators=self.n_estimators.value,
max_depth=self.max_depth.value,
learning_rate=self.learning_rate.value,
objective=objective_full)
self.model.value.fit(self.X_train.value, self.y_train.value)
@xai_component
class XGBoostMultiClassClassifier(Component):
"""
Trains an XGBoost classifier for multi-class classification tasks.
#### Reference:
- [XGBoost Multi-Class Classification](https://xgboost.readthedocs.io/en/stable/tutorials/model.html)
##### inPorts:
- X_train: The training data.
- y_train: The target variable for the training data.
- n_estimators: Number of gradient boosted trees. Default: 100
- max_depth: Maximum tree depth for base learners. Default: 3
- learning_rate: Boosting learning rate. Default: 0.1
- num_class: Number of classes in the target variable. Default: 3 (Assuming a common scenario)
- objective: Specify 'softmax' or 'softprob', or full specification 'multi:softmax' or 'multi:softprob'. Default: 'softmax'
##### outPorts:
- model: The trained XGBoost multi-class classification model.
"""
X_train: InCompArg[any]
y_train: InCompArg[any]
n_estimators: InArg[int]
max_depth: InArg[int]
learning_rate: InArg[float]
num_class: InCompArg[int]
objective: InArg[str]
model: OutArg[any]
def __init__(self):
super().__init__()
self.n_estimators.value = 100
self.max_depth.value = 3
self.learning_rate.value = 0.1
self.objective.value = 'softmax'
def execute(self, ctx) -> None:
from xgboost import XGBClassifier
objective_full = self.objective.value if ':' in self.objective.value else 'multi:' + self.objective.value
self.model.value = XGBClassifier(n_estimators=self.n_estimators.value,
max_depth=self.max_depth.value,
learning_rate=self.learning_rate.value,
objective=objective_full,
num_class=self.num_class.value)
self.model.value.fit(self.X_train.value, self.y_train.value)
@xai_component
class XGBoostRegressor(Component):
"""
Trains an XGBoost regressor for regression tasks.
#### Reference:
- [XGBoost Regression](https://xgboost.readthedocs.io/en/stable/python/sklearn_estimator.html)
##### inPorts:
- X_train: The training data.
- y_train: The target variable for the training data.
- n_estimators: Number of gradient boosted trees. Default: 100
- max_depth: Maximum tree depth for base learners. Default: 3
- learning_rate: Boosting learning rate. Default: 0.1
- objective: Specify regression type 'squarederror', 'logistic', 'squaredlogerror', 'pseudohubererror', or full specification like 'reg:logistic'. Default: 'squarederror'
##### outPorts:
- model: The trained XGBoost regression model.
"""
X_train: InCompArg[any]
y_train: InCompArg[any]
n_estimators: InArg[int]
max_depth: InArg[int]
learning_rate: InArg[float]
objective: InArg[str]
model: OutArg[any]
def __init__(self):
super().__init__()
self.n_estimators.value = 100
self.max_depth.value = 3
self.learning_rate.value = 0.1
self.objective.value = 'squarederror'
def execute(self, ctx) -> None:
from xgboost import XGBRegressor
objective_full = self.objective.value if ':' in self.objective.value else 'reg:' + self.objective.value
self.model.value = XGBRegressor(n_estimators=self.n_estimators.value,
max_depth=self.max_depth.value,
learning_rate=self.learning_rate.value,
objective=objective_full)
self.model.value.fit(self.X_train.value, self.y_train.value)
@xai_component
class XGBoostRanker(Component):
"""
Trains an XGBoost model for ranking tasks.
#### Reference:
- [XGBoost Learning to Rank](https://xgboost.readthedocs.io/en/latest/tutorials/ranking.html)
##### inPorts:
- X_train: The training data.
- y_train: The target variable for the training data.
- n_estimators: Number of gradient boosted trees. Default: 100
- max_depth: Maximum tree depth for base learners. Default: 3
- learning_rate: Boosting learning rate. Default: 0.1
- objective: Specify ranking type 'pairwise', 'ndcg', 'map', or full specification like 'rank:pairwise'. Default: 'rank:pairwise'
##### outPorts:
- model: The trained XGBoost ranking model.
"""
X_train: InCompArg[any]
y_train: InCompArg[any]
n_estimators: InArg[int]
max_depth: InArg[int]
learning_rate: InArg[float]
objective: InArg[str]
model: OutArg[any]
def __init__(self):
super().__init__()
self.n_estimators.value = 100
self.max_depth.value = 3
self.learning_rate.value = 0.1
self.objective.value = 'rank:pairwise'
def execute(self, ctx) -> None:
from xgboost import XGBRanker
objective_full = self.objective.value if ':' in self.objective.value else 'rank:' + self.objective.value
self.model.value = XGBRanker(n_estimators=self.n_estimators.value,
max_depth=self.max_depth.value,
learning_rate=self.learning_rate.value,
objective=objective_full)
self.model.value.fit(self.X_train.value, self.y_train.value)
@xai_component
class XGBoostBinaryPredict(Component):
"""
Makes predictions using a trained XGBoost classifier and optionally evaluates the accuracy of those predictions.
#### Reference:
- [XGBoost Prediction](https://xgboost.readthedocs.io/en/latest/python/python_intro.html#prediction)
##### inPorts:
- bst: The trained XGBoost model.
- X_test: The testing data.
- y_test: The target variable for the testing data. If provided, the accuracy of the predictions is evaluated.
##### outPorts:
- preds: The model's predictions.
- accuracy: The accuracy of the model's predictions, if y_test was provided.
"""
bst: InCompArg[any]
X_test: InCompArg[any]
y_test: InArg[any]
preds: OutArg[any]
accuracy: OutArg[float]
def execute(self, ctx) -> None:
from sklearn.metrics import accuracy_score
self.preds.value = self.bst.value.predict(self.X_test.value)
if self.y_test.value is not None:
self.accuracy.value = accuracy_score(self.y_test.value, self.preds.value)
print(f"Accuracy: {self.accuracy.value * 100:.2f}%")
else:
self.accuracy.value = None