Datasets:
Upload 9 files
Browse files- data/clients.csv.gz +3 -0
- data/products.csv.gz +3 -0
- data/purchases.csv.gz +3 -0
- data/uplift_sample_submission.csv.gz +3 -0
- data/uplift_test.csv.gz +3 -0
- data/uplift_train.csv.gz +3 -0
- description.txt +17 -0
- requirements.txt +3 -0
- uplift_solution.py +96 -0
data/clients.csv.gz
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:b8985170e03dc65fa532fb6b8ca6dd70ea5c30c35ea7c1019ee6ea2034d04099
|
3 |
+
size 7634884
|
data/products.csv.gz
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:6b72e025ff77974940ebdba63998a4349ae290a33efeb41f29989fdea6e96573
|
3 |
+
size 1054470
|
data/purchases.csv.gz
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:1342e14e1aa9d39dc242f1b66c903f0576fd184af9dc5a3bbe932607dc2d8aba
|
3 |
+
size 608830890
|
data/uplift_sample_submission.csv.gz
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:7dabb06e80f94e00137d694231ee4de1c03994176367d711cb8e7bbb3253a877
|
3 |
+
size 3179230
|
data/uplift_test.csv.gz
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8b98fe6ee94478253f40e7ed7ebbbefeb47c78ab37fd41111f3b35b8b69373e0
|
3 |
+
size 1074829
|
data/uplift_train.csv.gz
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:23aced68634c605acb93a7ab450aabac1a0ce0c8104e59ffed9941109ddc4ccd
|
3 |
+
size 1182429
|
description.txt
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
RetailHero Uplift Modeling
|
2 |
+
==========================
|
3 |
+
|
4 |
+
Задача на uplift-моделирование. Необходимо отранжировать клиентов по убыванию эффективности коммуникации.
|
5 |
+
|
6 |
+
Страница соревнования: https://retailhero.ai/c/uplift_modeling/
|
7 |
+
|
8 |
+
Доступные данные:
|
9 |
+
- data/clients.csv — информация о клиентах
|
10 |
+
- data/products.csv — информация о товарах
|
11 |
+
- data/purchases.csv — история покупок клиентов до смс кампании
|
12 |
+
- data/uplift_train.csv — обучающая выборка клиентов, информация о коммуникации и конверсии
|
13 |
+
- data/uplift_test.csv — тестовые клиенты, для которых необходимо оценить uplift
|
14 |
+
|
15 |
+
Пример решения: uplift_solution.py
|
16 |
+
|
17 |
+
Перед запуском, необходимо установить библиотеки из requirements.txt
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
pandas~=0.25.3
|
2 |
+
scikit-learn~=0.21.3
|
3 |
+
numpy~=1.17.4
|
uplift_solution.py
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas
|
2 |
+
import datetime
|
3 |
+
import numpy as np
|
4 |
+
from sklearn.base import clone
|
5 |
+
from sklearn.model_selection import train_test_split
|
6 |
+
from sklearn.ensemble import GradientBoostingClassifier
|
7 |
+
|
8 |
+
|
9 |
+
def uplift_fit_predict(model, X_train, treatment_train, target_train, X_test):
|
10 |
+
"""
|
11 |
+
Реализация простого способа построения uplift-модели.
|
12 |
+
|
13 |
+
Обучаем два бинарных классификатора, которые оценивают вероятность target для клиента:
|
14 |
+
1. с которым была произведена коммуникация (treatment=1)
|
15 |
+
2. с которым не было коммуникации (treatment=0)
|
16 |
+
|
17 |
+
В качестве оценки uplift для нового клиента берется разница оценок вероятностей:
|
18 |
+
Predicted Uplift = P(target|treatment=1) - P(target|treatment=0)
|
19 |
+
"""
|
20 |
+
X_treatment, y_treatment = X_train[treatment_train == 1, :], target_train[treatment_train == 1]
|
21 |
+
X_control, y_control = X_train[treatment_train == 0, :], target_train[treatment_train == 0]
|
22 |
+
model_treatment = clone(model).fit(X_treatment, y_treatment)
|
23 |
+
model_control = clone(model).fit(X_control, y_control)
|
24 |
+
predict_treatment = model_treatment.predict_proba(X_test)[:, 1]
|
25 |
+
predict_control = model_control.predict_proba(X_test)[:, 1]
|
26 |
+
predict_uplift = predict_treatment - predict_control
|
27 |
+
return predict_uplift
|
28 |
+
|
29 |
+
|
30 |
+
def uplift_score(prediction, treatment, target, rate=0.3):
|
31 |
+
"""
|
32 |
+
Подсчет Uplift Score
|
33 |
+
"""
|
34 |
+
order = np.argsort(-prediction)
|
35 |
+
treatment_n = int((treatment == 1).sum() * rate)
|
36 |
+
treatment_p = target[order][treatment[order] == 1][:treatment_n].mean()
|
37 |
+
control_n = int((treatment == 0).sum() * rate)
|
38 |
+
control_p = target[order][treatment[order] == 0][:control_n].mean()
|
39 |
+
score = treatment_p - control_p
|
40 |
+
return score
|
41 |
+
|
42 |
+
|
43 |
+
# Чтение данных
|
44 |
+
|
45 |
+
df_clients = pandas.read_csv('data/clients.csv', index_col='client_id')
|
46 |
+
df_train = pandas.read_csv('data/uplift_train.csv', index_col='client_id')
|
47 |
+
df_test = pandas.read_csv('data/uplift_test.csv', index_col='client_id')
|
48 |
+
|
49 |
+
# Извлечение признаков
|
50 |
+
|
51 |
+
df_clients['first_issue_unixtime'] = pandas.to_datetime(df_clients['first_issue_date']).astype(int)/10**9
|
52 |
+
df_clients['first_redeem_unixtime'] = pandas.to_datetime(df_clients['first_redeem_date']).astype(int)/10**9
|
53 |
+
df_features = pandas.DataFrame({
|
54 |
+
'gender_M': (df_clients['gender'] == 'M').astype(int),
|
55 |
+
'gender_F': (df_clients['gender'] == 'F').astype(int),
|
56 |
+
'gender_U': (df_clients['gender'] == 'U').astype(int),
|
57 |
+
'age': df_clients['age'],
|
58 |
+
'first_issue_time': df_clients['first_issue_unixtime'],
|
59 |
+
'first_redeem_time': df_clients['first_redeem_unixtime'],
|
60 |
+
'issue_redeem_delay': df_clients['first_redeem_unixtime'] - df_clients['first_issue_unixtime'],
|
61 |
+
}).fillna(0)
|
62 |
+
|
63 |
+
|
64 |
+
# Оценка качества на валидации
|
65 |
+
|
66 |
+
indices_train = df_train.index
|
67 |
+
indices_test = df_test.index
|
68 |
+
indices_learn, indices_valid = train_test_split(df_train.index, test_size=0.3, random_state=123)
|
69 |
+
|
70 |
+
valid_uplift = uplift_fit_predict(
|
71 |
+
model=GradientBoostingClassifier(),
|
72 |
+
X_train=df_features.loc[indices_learn, :].fillna(0).values,
|
73 |
+
treatment_train=df_train.loc[indices_learn, 'treatment_flg'].values,
|
74 |
+
target_train=df_train.loc[indices_learn, 'target'].values,
|
75 |
+
X_test=df_features.loc[indices_valid, :].fillna(0).values,
|
76 |
+
)
|
77 |
+
valid_score = uplift_score(
|
78 |
+
valid_uplift,
|
79 |
+
treatment=df_train.loc[indices_valid, 'treatment_flg'].values,
|
80 |
+
target=df_train.loc[indices_valid, 'target'].values,
|
81 |
+
)
|
82 |
+
print('Validation score:', valid_score)
|
83 |
+
|
84 |
+
|
85 |
+
# Подготовка предсказаний для тестовых клиентов
|
86 |
+
|
87 |
+
test_uplift = uplift_fit_predict(
|
88 |
+
model=GradientBoostingClassifier(),
|
89 |
+
X_train=df_features.loc[indices_train, :].fillna(0).values,
|
90 |
+
treatment_train=df_train.loc[indices_train, 'treatment_flg'].values,
|
91 |
+
target_train=df_train.loc[indices_train, 'target'].values,
|
92 |
+
X_test=df_features.loc[indices_test, :].fillna(0).values,
|
93 |
+
)
|
94 |
+
|
95 |
+
df_submission = pandas.DataFrame({'uplift': test_uplift}, index=df_test.index)
|
96 |
+
df_submission.to_csv('submission.csv')
|