-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcatboostmodel.py
155 lines (132 loc) · 4.63 KB
/
catboostmodel.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
# ---
# jupyter:
# jupytext:
# formats: ipynb,py:percent
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.11.4
# kernelspec:
# display_name: Python 3 (ipykernel)
# language: python
# name: python3
# ---
# %%
from sklearn.model_selection import train_test_split
import numpy as np
import pandas as pd
import datetime as dt
from catboost import CatBoostRegressor, Pool
print("Imports done")
# %%
# DATA CLEANING
# dataset = pd.read_csv('/raid/cs152/eBay/eBay_ML_Challenge_Dataset_2021_train.csv')
dataset = pd.read_csv('/raid/cs152/eBay/missing_data_dropped.csv') # , nrows=10000)
b2c_c2c = np.array(dataset["b2c_c2c"])
seller_id = np.array(dataset["seller_id"])
declared_handling_days = np.array(dataset["declared_handling_days"])
shipment_method_id = np.array(dataset["shipment_method_id"])
shipping_fee = np.array(dataset["shipping_fee"])
carrier_min_estimate = np.array(dataset["carrier_min_estimate"])
carrier_max_estimate = np.array(dataset["carrier_max_estimate"])
item_zip = dataset["item_zip"]
buyer_zip = dataset["buyer_zip"]
category_id = np.array(dataset["category_id"])
item_price = np.array(dataset["item_price"])
quantity = np.array(dataset["quantity"])
weight = np.array(dataset["weight"])
package_size = np.array(dataset["package_size"])
record_number = np.array(dataset["record_number"])
zip_distance = np.array(dataset["zip_distance"])
item_zip_pop_density = np.array(dataset["item_zip_pop_density"])
item_zip_median_income = np.array(dataset["item_zip_median_income"])
buyer_zip_pop_density = np.array(dataset["buyer_zip_pop_density"])
buyer_zip_median_income = np.array(dataset["buyer_zip_median_income"])
handling_days = np.array(dataset["handling_days"])
delivery_days = np.array(dataset["delivery_days"])
features = np.column_stack((b2c_c2c,
seller_id,
declared_handling_days,
shipment_method_id,
shipping_fee,
carrier_min_estimate,
carrier_max_estimate,
item_zip,
buyer_zip,
category_id,
item_price,
quantity,
weight,
package_size,
record_number,
zip_distance,
item_zip_pop_density,
item_zip_median_income,
buyer_zip_pop_density,
buyer_zip_median_income,
handling_days))
labels = np.array(delivery_days)
print("data import done")
# %%
X = features
y = labels
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.75, random_state=42)
# %% [markdown]
# CAT_FEATURES are the catagorical features. early_stopping_rounds tells the model to stop if it doesn't
# improve for 10 rounds (stopping overfitting).
# %%
CAT_FEATURES = [0, 1, 3, 7, 8, 9, 12, 13, 15]
# b2c_c2c, seller_id, shipment_method_id, item_zip, buyer_zip, category_id, quantity,
# weight, package_size, zip_distance
model = CatBoostRegressor(iterations=600,
cat_features = CAT_FEATURES,
learning_rate = 0.375,
depth = 8,
l2_leaf_reg = 5,
# plot=True,
early_stopping_rounds = 20
)
print("model initialized")
# %% [markdown]
# Below is hyperparameter tuning (commented out when running model)
# %%
# grid = {'learning_rate': [0.36, 0.37, 0.375, 0.38, 0.385, 0.39, 0.4],
# 'depth': [6, 7, 8, 9, 10, 11, 12],
# 'l2_leaf_reg': [0, 1, 2, 3, 4, 5, 6]}
# randomized_search_result = model.randomized_search(grid,
# X=X_train,
# y=y_train,
# plot=True)
# print(model.get_params())
# %%
# train model
model.fit(X_train, y_train,
eval_set = (X_test, y_test),
plot = False,
)
# %%
print("Feature importances:")
print(model.get_feature_importance(
Pool(X_train, y_train, cat_features=CAT_FEATURES)))
# %%
def evaluate_loss(preds, actual):
early_loss, late_loss = 0,0
for i in range(len(preds)):
if preds[i] < actual[i]:
#early shipment
early_loss += actual[i] - preds[i]
elif preds[i] > actual[i]:
#late shipment
late_loss += preds[i] - actual[i]
loss = (1/len(preds)) * (0.4 * (early_loss) + 0.6 * (late_loss))
return loss
# %% [markdown]
# Below is evaluation of the model using the loss function from eBay
#
# %%
train_score = model.score(X_train, y_train)
print("train score: " + str(train_score))
pred = model.predict(X_test)
loss = evaluate_loss(pred, y_test)
print("loss: " + str(loss))