-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxgboost-in-python-with-rmspe-v2.py
178 lines (142 loc) · 5.97 KB
/
xgboost-in-python-with-rmspe-v2.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
#!/usr/bin/python
'''
Based on https://www.kaggle.com/justdoit/rossmann-store-sales/xgboost-in-python-with-rmspe/code
Public Score : 0.11389
Private Validation Score : 0.096959
'''
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
import xgboost as xgb
import operator
import matplotlib
# matplotlib.use("Agg") #Needed to save figures
import matplotlib.pyplot as plt
def create_feature_map(features):
outfile = open('xgb.fmap', 'w')
for i, feat in enumerate(features):
outfile.write('{0}\t{1}\tq\n'.format(i, feat))
outfile.close()
def rmspe(y, yhat):
return np.sqrt(np.mean((yhat/y-1) ** 2))
def rmspe_xg(yhat, y):
y = np.expm1(y.get_label())
yhat = np.expm1(yhat)
return "rmspe", rmspe(y,yhat)
# Gather some features
def build_features(features, data) -> None :
# remove NaNs
data.fillna(0, inplace=True)
data.loc[data.Open.isnull(), 'Open'] = 1
# Use some properties directly
features.extend(['Store', 'CompetitionDistance', 'Promo', 'Promo2', 'SchoolHoliday'])
# Label encode some features
features.extend(['StoreType', 'Assortment', 'StateHoliday'])
mappings = {'0':0, 'a':1, 'b':2, 'c':3, 'd':4}
data.StoreType.replace(mappings, inplace=True)
data.Assortment.replace(mappings, inplace=True)
data.StateHoliday.replace(mappings, inplace=True)
features.extend(['DayOfWeek', 'Month', 'Day', 'Year', 'WeekOfYear'])
data['Year'] = data.Date.dt.year
data['Month'] = data.Date.dt.month
data['Day'] = data.Date.dt.day
data['DayOfWeek'] = data.Date.dt.dayofweek
data['WeekOfYear'] = data.Date.dt.weekofyear
# CompetionOpen en PromoOpen from https://www.kaggle.com/ananya77041/rossmann-store-sales/randomforestpython/code
# Calculate time competition open time in months
features.append('CompetitionOpen')
data['CompetitionOpen'] = 12 * (data.Year - data.CompetitionOpenSinceYear) + \
(data.Month - data.CompetitionOpenSinceMonth)
# Promo open time in months
features.append('PromoOpen')
data['PromoOpen'] = 12 * (data.Year - data.Promo2SinceYear) + \
(data.WeekOfYear - data.Promo2SinceWeek) / 4.0
data['PromoOpen'] = data.PromoOpen.apply(lambda x: x if x > 0 else 0)
data.loc[data.Promo2SinceYear == 0, 'PromoOpen'] = 0
# Indicate that sales on that day are in promo interval
features.append('IsPromoMonth')
month2str = {1:'Jan', 2:'Feb', 3:'Mar', 4:'Apr', 5:'May', 6:'Jun', \
7:'Jul', 8:'Aug', 9:'Sept', 10:'Oct', 11:'Nov', 12:'Dec'}
data['monthStr'] = data.Month.map(month2str)
data.loc[data.PromoInterval == 0, 'PromoInterval'] = ''
data['IsPromoMonth'] = 0
for interval in data.PromoInterval.unique():
if interval != '':
for month in interval.split(','):
data.loc[(data.monthStr == month) & (data.PromoInterval == interval), 'IsPromoMonth'] = 1
## Start of main script
print("Load the training, test and store data using pandas")
types = {'CompetitionOpenSinceYear': np.dtype(int),
'CompetitionOpenSinceMonth': np.dtype(int),
'StateHoliday': np.dtype(str),
'Promo2SinceWeek': np.dtype(int),
'SchoolHoliday': np.dtype(float),
'PromoInterval': np.dtype(str)}
train = pd.read_csv("rawdata/train.csv", parse_dates=[2], dtype=types)
test = pd.read_csv("rawdata/test.csv", parse_dates=[3], dtype=types)
store = pd.read_csv("rawdata/store.csv")
for column in store.columns[store.dtypes == 'object']:
print(column)
train.isna().sum()
test.isna().sum()
print("Assume store open, if not provided")
train.fillna(1, inplace=True)
test.fillna(1, inplace=True)
print("Consider only open stores for training. Closed stores wont count into the score.")
train = train[train["Open"] != 0]
# 因为 rmspe 的计算中会涉及到 yhat/y。因此 y=0 时计算会出错
print("Use only Sales bigger then zero. Simplifies calculation of rmspe")
train = train[train["Sales"] > 0]
print("Join with store")
train = pd.merge(train, store, on='Store')
test = pd.merge(test, store, on='Store')
features = []
print("augment features")
build_features(features, train)
build_features([], test)
print(features)
print('training data processed')
params = {
"objective": "reg:linear",
"booster" : "gbtree",
"eta": 0.3,
"max_depth": 10,
"subsample": 0.9,
"colsample_bytree": 0.7,
"silent": 1,
"seed": 1301
}
num_boost_round = 300
print("Train a XGBoost model")
X_train, X_valid = train_test_split(train, test_size=0.012, random_state=10)
# log(1+x) # 为什么要按这个去训练?
y_train = np.log1p(X_train.Sales)
y_valid = np.log1p(X_valid.Sales)
dtrain = xgb.DMatrix(X_train[features], y_train)
dvalid = xgb.DMatrix(X_valid[features], y_valid)
watchlist = [(dtrain, 'train'), (dvalid, 'eval')]
gbm = xgb.train(params, dtrain, num_boost_round, evals=watchlist, \
early_stopping_rounds=100, feval=rmspe_xg, verbose_eval=True)
print("Validating")
yhat = gbm.predict(xgb.DMatrix(X_valid[features]))
error = rmspe(X_valid.Sales.values, np.expm1(yhat))
print('RMSPE: {:.6f}'.format(error))
print("Make predictions on the test set")
dtest = xgb.DMatrix(test[features])
test_probs = gbm.predict(dtest)
# Make Submission
result = pd.DataFrame({"Id": test["Id"], 'Sales': np.expm1(test_probs)})
result.to_csv("xgboost_10_submission.csv", index=False)
# XGB feature importances
# Based on https://www.kaggle.com/mmueller/liberty-mutual-group-property-inspection-prediction/xgb-feature-importance-python/code
create_feature_map(features)
importance = gbm.get_fscore(fmap='xgb.fmap')
importance = sorted(importance.items(), key=operator.itemgetter(1))
df = pd.DataFrame(importance, columns=['feature', 'fscore'])
df['fscore'] = df['fscore'] / df['fscore'].sum()
featp = df.plot(kind='barh', x='feature', y='fscore', legend=False, figsize=(6, 10))
plt.title('XGBoost Feature Importance')
plt.xlabel('relative importance')
plt.show()
fig_featp = featp.get_figure()
fig_featp.savefig('feature_importance_xgb.png', bbox_inches='tight', pad_inches=1)