forked from phuongdo/catboost-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrain.py
130 lines (111 loc) · 3.64 KB
/
train.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
from catboost import CatBoostClassifier
# a classification model with numerical, categorical and embedding features
def fit_full_feature_classification_model():
# Initialize data
cat_features = [3]
embedding_features = [0, 1]
train_data = [
[[0.1, 0.12, 0.33], [1.0, 0.7], 2, "male"],
[[0.0, 0.8, 0.2], [1.1, 0.2], 1, "female"],
[[0.2, 0.31, 0.1], [0.3, 0.11], 2, "female"],
[[0.01, 0.2, 0.9], [0.62, 0.12], 1, "male"],
]
train_labels = [1, 0, 0, 1]
eval_data = [
[[0.2, 0.1, 0.3], [1.2, 0.3], 1, "female"],
[[0.33, 0.22, 0.4], [0.98, 0.5], 2, "female"],
[[0.78, 0.29, 0.67], [0.76, 0.34], 2, "male"],
]
model = CatBoostClassifier(iterations=10, learning_rate=0.2, depth=3, verbose=0)
model.fit(train_data, train_labels, cat_features=cat_features, embedding_features=embedding_features)
model.save_model('full_features.bin')
# Get predictions
print('full_features')
preds = model.predict_proba(eval_data)
print(preds) # [0.46018641 0.47496323 0.65977057]
# [[0.56015706 0.43984294]
# [0.55445946 0.44554054]
# [0.43797584 0.56202416]]
fit_full_feature_classification_model()
def fit_numerical_classification_model():
train_data = [
[1, 2, 3],
[2, 3, 4],
[3, 4, 5],
[4, 5, 6],
]
train_labels = [1, 0, 0, 1]
eval_data = [
[2, 4, 6],
[3, 5, 7],
[4, 6, 8],
]
model = CatBoostClassifier(iterations=10, learning_rate=0.2, depth=3, verbose=0)
model.fit(train_data, train_labels, cat_features=[], embedding_features=[])
print('numerical_only')
model.save_model('numerical_only.bin')
# Get predictions
preds = model.predict_proba(eval_data)
print(preds)
fit_numerical_classification_model()
def fit_category_classification_model():
train_data = [
["a", "a", "b"],
["b", "b", "b"],
["b", "b", "a"],
["a", "a", "a"],
]
train_labels = [1, 0, 0, 1]
eval_data = [
["a", "a", "a"],
["a", "a", "b"],
["b", "b", "a"],
["b", "b", "b"],
]
model = CatBoostClassifier(iterations=10, learning_rate=0.2, depth=3, verbose=0)
model.fit(train_data, train_labels, cat_features=[0, 1, 2], embedding_features=[])
print('category_only')
model.save_model('category_only.bin')
# Get predictions
preds = model.predict_proba(eval_data)
print(preds)
fit_category_classification_model()
def fit_embedding_classification_model():
# Initialize data
train_data = [
[[0.1, 0.12, 0.33], [1.0, 0.7]],
[[0.0, 0.8, 0.2], [1.1, 0.2]],
[[0.2, 0.31, 0.1], [0.3, 0.11]],
[[0.01, 0.2, 0.9], [0.62, 0.12]],
]
train_labels = [1, 0, 0, 1]
eval_data = [
[[0.2, 0.1, 0.3], [1.2, 0.3]],
[[0.33, 0.22, 0.4], [0.98, 0.5]],
[[0.78, 0.29, 0.67], [0.76, 0.34]],
]
model = CatBoostClassifier(iterations=10, learning_rate=0.2, depth=3, verbose=0)
model.fit(train_data, train_labels, cat_features=[], embedding_features=[0, 1])
model.save_model('embedding_only.bin')
# Get predictions
print('embedding_only')
preds = model.predict_proba(eval_data)
print(preds)
fit_embedding_classification_model()
# full_features
# [[0.56015706 0.43984294]
# [0.55445946 0.44554054]
# [0.43797584 0.56202416]]
# numerical_only
# [[0.5149358 0.4850642 ]
# [0.44320543 0.55679457]
# [0.42863304 0.57136696]]
# category_only
# [[0.38610862 0.61389138]
# [0.38610862 0.61389138]
# [0.61389138 0.38610862]
# [0.61389138 0.38610862]]
# embedding_only
# [[0.53241853 0.46758147]
# [0.53961316 0.46038684]
# [0.53961316 0.46038684]]