-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_xgb.py
executable file
·118 lines (85 loc) · 4.38 KB
/
export_xgb.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
#! /Users/asiu/opt/anaconda3/bin/python3
code_content = """
import sys
import numpy as np
import os
from xgboost import XGBClassifier
from sklearn.preprocessing import LabelEncoder
def evaluate_model(model, X_test, y_test):
y_pred = model.predict(X_test)
# Calculate Confusion Matrix
confusion_mat = confusion_matrix(y_test, y_pred)
# Calculate Accuracy
accuracy = accuracy_score(y_test, y_pred)
# Calculate Macro precision rate
precision_macro = precision_score(y_test, y_pred, average='macro')
# Calculate Macro Recall
recall_macro = recall_score(y_test, y_pred, average='macro')
# Calculate Macro F1
f1_macro = f1_score(y_test, y_pred, average='macro')
metrics = {
"Accuracy": accuracy,
"Precision (Macro)": precision_macro,
"Recall (Macro)": recall_macro,
"F1 Score (Macro)": f1_macro,
}
return metrics, confusion_mat
def plot_confusion_matrix(cm, classes, title, normalize=False, cmap=plt.cm.Blues, custom_text=None):
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
# print("Normalized confusion matrix")
# print(cm)
plt.figure(figsize=(12, 6))
plt.imshow(cm, interpolation='nearest', cmap=cmap) # imshow displays data on a 2D raster
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=90)
plt.yticks(tick_marks, classes)
fmt = '.2f' if normalize else 'd'
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, format(cm[i, j], fmt),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
if custom_text:
plt.text(0.3 * plt.gca().get_xlim()[0], 0.05 * plt.gca().get_ylim()[1], custom_text, fontsize=10, ha='right',
va='bottom', color='red')
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.show()
def run_xgb(path_to_training, path_to_testing, n_estimators, max_depth, learning_rate):
#path_to_training: the path to your training data
#path_to_testing: the path to your testing data
#n_estimators, max_depth, learning_rate: the parameter needed for an XGB model, learning_rate should be float, and the rest need to be int
#designate your training data and corresponding label of your training dataset as x_train and y_train, respectively
#designate your testing data and corresponding label of your testing dataset as x_test and y_test, respectively
# Because the XGBoost model needs to deal with Integer type labels rather than original groups of labels, so use
# LabelEncoder to transfer the original labels to an integer and use them to do the training and testing works.
x_train =
y_train =
label_encoder = LabelEncoder()
y_encoded = label_encoder.fit_transform(y_train)
x_test =
y_test =
y_test_encoded = label_encoder.fit_transform(y_test)
#The label of testing data is also the axis for confusion matrix in the result.
axis = y_test
#You can choose your own random_state, this is for your convenience to reproduce your result.
xgb = XGBClassifier(random_state=42, objective='multi:softmax', num_class=7, learning_rate=learning_rate, max_depth=max_depth,
n_estimators=n_estimators)
# Train this model using x_train and y_train
xgb.fit(x_train, y_encoded)
#Use the provided function to evaluate your model
#xgb_metrics contains: Accuracy, Precision(Macro), Recall(Macro), F1 Score(Macro), you can refer to their value by xgb_metrics["name"]
#In confusion_mat, you can see how your testing data is recognized by the classifier.
xgb_metrics, confusion_mat = evaluate_model(xgb, x_test, y_test_encoded)
print("===============================================================================================================")
text = "The accuracy of xgb model with feature combination is: ", xgb_metrics['Accuracy']
plot_confusion_matrix(confusion_mat, axis,
"Confusion Matrix for xgb model with features RMS & Skewness & Kurtosis & Mean Spectral Energy",
normalize=True, custom_text=text)
print("The accuracy of xgb model with feature combination is: ", xgb_metrics['Accuracy'])
"""
print(code_content)