-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_assortment_map.py
More file actions
357 lines (285 loc) · 13.7 KB
/
plot_assortment_map.py
File metadata and controls
357 lines (285 loc) · 13.7 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# -*- coding: utf-8 -*-
"""
Created on Wed Aug 2 23:38:19 2023
@author: wyl2020
"""
import os, sys
import matplotlib as mpl
# mpl.use('Agg')
import matplotlib.pyplot as plt
import seaborn as sns
import pylab as pl
import itertools, time, copy
import pandas as pd
import numpy as np
from statistics import median
pd.set_option("display.max_columns", 20)
pd.set_option("display.width", 150)
import BuildModels as bm
import ToolFunctions as tf
import CompareFunc as cm
opt_modelname = 'MC_Conv-mo-soc-aC'
#%% get result all
varying_type = sys.argv[1] # "varying_v0" or "varying_a0"
filename = "./output_assortment_map/SolutionDict_InfoDict_dataOptionDict_" + varying_type
InfoDict = tf.load(filename)[0]["InfoDict"]
SolutionDict = tf.load(filename)[0]["SolutionDict"]
dataOptionDict = tf.load(filename)[0]["dataOptionDict"]
probSettingSet = tf.load(filename)[0]["probSettingSet"]
time_stamp = pd.Timestamp.now()
time_stamp_str = str(time_stamp.date())+'-{:02}-{:02}-{:02}'.format(time_stamp.hour,time_stamp.minute,time_stamp.second)
#%% get probability
Solution_temp = pd.concat((SolutionDict[probSetting_info][1][opt_modelname]
for probSetting_info in probSettingSet),
axis=1, keys=probSettingSet)
data = dataOptionDict[probSettingSet[2]][1][0]
value_off_v = data.value_off_v
reven_off_r = data.r_off.reshape(data.numProd)
r_sort_ind = np.argsort(-reven_off_r)
r_sort_ind = r_sort_ind[:100]
#%% plot probability
for LuceOrNot in [0,1]:
# LuceOrNot = 1
index = [col for col in Solution_temp.columns if col[3]==LuceOrNot]
Solution = Solution_temp.loc[:, index].copy()
if varying_type == 'varying_a0':
fig = plt.figure(figsize=(9,12))
high = 5
width = 3
gs = fig.add_gridspec(nrows=high, ncols=width*3)
# y probability on offline
ax_yoff = fig.add_subplot(gs[:high, :width])
y_off_ind = [ind for ind in Solution.index if 'y_off_y' in ind]
y_off_solution = Solution.loc[y_off_ind]
dataplot = y_off_solution.mul(value_off_v, axis=0).iloc[r_sort_ind,:]
dataplot[dataplot < 1e-8] = 0
vmin = dataplot[dataplot > 1e-4].min().min()
vmax = dataplot.max().max()
norm = mpl.colors.LogNorm(vmin=vmin, vmax=vmax)
im = sns.heatmap(dataplot,
ax=ax_yoff,
cmap="YlGnBu", # crest, YlGnBu
norm=norm,
vmin=vmin,
vmax=vmax,
linewidths=0.005,
cbar=False,
clip_on=True,
xticklabels=False,
)
x_ticklabels = dataplot.columns.get_level_values(1)
y_ticklabels = np.arange(dataplot.shape[0]) + 1
x_ticks = [min(x_ticklabels), median(x_ticklabels), max(x_ticklabels)]
y_ticks = np.arange(stop=dataplot.shape[0], step=5, dtype=int)
# im = plt.imshow(dataplot, cmap='YlGnBu', norm=norm)
ax_yoff.set_xticks([0, len(x_ticklabels)//2, len(x_ticklabels)-1])
ax_yoff.set_xticklabels(x_ticks, fontsize=16, rotation=0, ha='left')
ax_yoff.set_yticks(y_ticks)
ax_yoff.set_yticklabels(y_ticklabels[y_ticks], fontsize=16)
ax_yoff.set_xlabel(r"$\alpha_0$", fontsize=20)
ax_yoff.set_ylabel("products index (revenue-ordered)", fontsize=22)
plt.title('offline', fontsize=22)
for _, spine in ax_yoff.spines.items():
spine.set_visible(True)
# plt.show()
# y probability on regularGroup
ax_yon_r = fig.add_subplot(gs[:high, width:2 * width])
value_on_v = data.value_on_v
j = data.J[0]
y_on_ind_j = [ind for ind in Solution.index if 'y_on_y' in ind and ',{}]'.format(j) in ind]
y_on_solution_j = Solution.loc[y_on_ind_j]
reven_off_r = data.r_off.reshape(data.numProd)
reven_on_j = data.r_on[:,j]
r_sort_ind = np.argsort(-reven_off_r)
r_sort_ind = r_sort_ind[:100]
# dataplot = y_on_solution_j.iloc[r_sort_ind,:]
dataplot = y_on_solution_j.mul(value_on_v[:,j], axis=0).iloc[r_sort_ind,:]
dataplot[dataplot < 1e-8] = 0
vmin = dataplot[dataplot > 1e-4].min().min()
vmax = dataplot.max().max()
norm = mpl.colors.LogNorm(vmin=vmin, vmax=vmax)
im = sns.heatmap(dataplot,
ax=ax_yon_r,
cmap="YlGnBu", # crest, YlGnBu
norm=norm,
vmin=vmin,
vmax=vmax,
linewidths=0.005,
cbar=False,
xticklabels=False,
)
ax_yon_r.set_xticks([0, len(x_ticklabels)//2, len(x_ticklabels)-1])
ax_yon_r.set_xticklabels(x_ticks, fontsize=16, rotation=0, ha="left")
ax_yon_r.set_yticks(y_ticks)
ax_yon_r.set_yticklabels(y_ticklabels[y_ticks], fontsize=16)
ax_yon_r.set_xlabel(r"$\alpha_0$", fontsize=20)
# ax_yon_r.set_ylabel("products index (revenue-ordered)", fontsize=10)
plt.title('regular', fontsize=22)
for _, spine in ax_yon_r.spines.items():
spine.set_visible(True)
# plt.show()
# y probability on VIPGroup
ax_yon_V = fig.add_subplot(gs[:high, 2 * width:3 * width])
j = data.J[-1]
y_on_ind_j = [ind for ind in Solution.index if 'y_on_y' in ind and ',{}]'.format(j) in ind]
y_on_solution_j = Solution.loc[y_on_ind_j]
reven_off_r = data.r_off.reshape(data.numProd)
reven_on_j = data.r_on[:,j]
r_sort_ind = np.argsort(-reven_off_r)
r_sort_ind = r_sort_ind[:100]
dataplot = y_on_solution_j.mul(value_on_v[:,j], axis=0).iloc[r_sort_ind,:]
dataplot[dataplot < 1e-8] = 0
vmin = dataplot[dataplot > 1e-4].min().min()
vmax = dataplot.max().max()
norm = mpl.colors.LogNorm(vmin=vmin, vmax=vmax)
im = sns.heatmap(dataplot,
ax=ax_yon_V,
cmap="YlGnBu", # crest, YlGnBu
norm=norm,
vmin=vmin,
vmax=vmax,
linewidths=0.005,
cbar=False,
xticklabels=False,
)
ax_yon_V.set_xticks([0, len(x_ticklabels)//2, len(x_ticklabels)-1])
ax_yon_V.set_xticklabels(x_ticks, fontsize=16, rotation=0, ha='left')
ax_yon_V.set_yticks(y_ticks)
ax_yon_V.set_yticklabels(y_ticklabels[y_ticks], fontsize=16)
ax_yon_V.set_xlabel(r"$\alpha_0$", fontsize=20)
# ax_yon_V.set_ylabel("products index (revenue-ordered)", fontsize=10)
# ax_yon_V.set_xlim(1,16)
plt.title('VIP', fontsize=22)
# fig.suptitle('Assortment map \nand purchase probability map for offline segment', x=0.5, y =0.99, fontsize=20)
fig.tight_layout()
for _, spine in ax_yon_V.spines.items():
spine.set_visible(True)
# plt.show()
if LuceOrNot == 0:
FileName = './Output/' + 'assortment_map_a0.png'
if LuceOrNot == 1:
FileName = './Output/' + 'assortment_map_a0_Luce.png'
fig.savefig(FileName, dpi=600, bbox_inches = 'tight')
plt.pause(2)
plt.close()
print("figure print to "+FileName)
##%% VARYING V0
if varying_type == 'varying_v0':
fig = plt.figure(figsize=(9,12))
high = 5
width = 3
gs = fig.add_gridspec(nrows=high, ncols=width*3)
# y probability on offline
ax_yoff = fig.add_subplot(gs[:high, :width])
y_off_ind = [ind for ind in Solution.index if 'y_off_y' in ind]
y_off_solution = Solution.loc[y_off_ind]
# dataplot = y_off_solution.iloc[r_sort_ind,:]
dataplot = y_off_solution.mul(value_off_v, axis=0).iloc[r_sort_ind,:]
dataplot[dataplot < 1e-8] = 0
vmin = dataplot[dataplot > 1e-4].min().min()
vmax = dataplot.max().max()
norm = mpl.colors.LogNorm(vmin=vmin, vmax=vmax)
im = sns.heatmap(dataplot,
ax=ax_yoff,
cmap="YlGnBu", # crest, YlGnBu
norm=norm,
vmin=vmin,
vmax=vmax,
linewidths=0.005,
cbar=False,
)
x_ticklabels = np.array([np.round(ind[1], 2) for ind in dataplot.columns.get_level_values(2)])
y_ticklabels = np.arange(dataplot.shape[0]) + 1
x_ticks = [min(x_ticklabels), median(x_ticklabels), max(x_ticklabels)]
y_ticks = np.arange(stop=dataplot.shape[0], step=5, dtype=int)
# im = plt.imshow(dataplot, cmap='YlGnBu', norm=norm)
ax_yoff.set_xticks([0, len(x_ticklabels)//2, len(x_ticklabels)-1])
ax_yoff.set_xticklabels(x_ticks, fontsize=16, rotation=0, ha='left')
ax_yoff.set_yticks(y_ticks)
ax_yoff.set_yticklabels(y_ticklabels[y_ticks], fontsize=16)
ax_yoff.set_xlabel(r'$u^{on}_0$', fontsize=20)
ax_yoff.set_ylabel("products index (revenue-ordered)", fontsize=22)
plt.title('offline', fontsize=22)
for _, spine in ax_yoff.spines.items():
spine.set_visible(True)
# plt.show()
# y probability on regularGroup
ax_yon_r = fig.add_subplot(gs[:high, width:2 * width])
value_on_v = data.value_on_v
j = data.J[0]
y_on_ind_j = [ind for ind in Solution.index if 'y_on_y' in ind and ',{}]'.format(j) in ind]
y_on_solution_j = Solution.loc[y_on_ind_j,:]
reven_off_r = data.r_off.reshape(data.numProd)
reven_on_j = data.r_on[:,j]
r_sort_ind = np.argsort(-reven_off_r)
r_sort_ind = r_sort_ind[:100]
# dataplot = y_on_solution_j.iloc[r_sort_ind,:]
dataplot = y_on_solution_j.mul(value_on_v[:,j], axis=0).iloc[r_sort_ind,:]
dataplot[dataplot < 1e-8] = 0
vmin = dataplot[dataplot > 1e-4].min().min()
vmax = dataplot.max().max()
norm = mpl.colors.LogNorm(vmin=vmin, vmax=vmax)
im = sns.heatmap(dataplot,
ax=ax_yon_r,
cmap="YlGnBu", # crest, YlGnBu
norm=norm,
vmin=vmin,
vmax=vmax,
linewidths=0.005,
cbar=False,
)
ax_yon_r.set_xticks([0, len(x_ticklabels)//2, len(x_ticklabels)-1])
ax_yon_r.set_xticklabels(x_ticks, fontsize=16, rotation=0, ha='left')
ax_yon_r.set_yticks(y_ticks)
ax_yon_r.set_yticklabels(y_ticklabels[y_ticks], fontsize=16)
ax_yon_r.set_xlabel(r'$u^{on}_0$', fontsize=20)
# ax_yon_r.set_ylabel("products index (revenue-ordered)", fontsize=10)
plt.title('regular', fontsize=22)
for _, spine in ax_yon_r.spines.items():
spine.set_visible(True)
# plt.show()
# y probability on VIPGroup
ax_yon_V = fig.add_subplot(gs[:high, 2 * width:3 * width])
j = data.J[-1]
y_on_ind_j = [ind for ind in Solution.index if 'y_on_y' in ind and ',{}]'.format(j) in ind]
y_on_solution_j = Solution.loc[y_on_ind_j,:]
reven_off_r = data.r_off.reshape(data.numProd)
reven_on_j = data.r_on[:,j]
r_sort_ind = np.argsort(-reven_off_r)
r_sort_ind = r_sort_ind[:100]
# dataplot = y_on_solution_j.iloc[r_sort_ind,:]
dataplot = y_on_solution_j.mul(value_on_v[:,j], axis=0).iloc[r_sort_ind,:]
dataplot[dataplot < 1e-8] = 0
vmin = dataplot[dataplot > 1e-4].min().min()
vmax = dataplot.max().max()
norm = mpl.colors.LogNorm(vmin=vmin, vmax=vmax)
im = sns.heatmap(dataplot,
ax=ax_yon_V,
cmap="YlGnBu", # crest, YlGnBu
norm=norm,
vmin=vmin,
vmax=vmax,
linewidths=0.005,
cbar=False,
)
ax_yon_V.set_xticks([0, len(x_ticklabels)//2, len(x_ticklabels)-1])
ax_yon_V.set_xticklabels(x_ticks, fontsize=16, rotation=0, ha='left')
ax_yon_V.set_yticks(y_ticks)
ax_yon_V.set_yticklabels(y_ticklabels[y_ticks], fontsize=16)
ax_yon_V.set_xlabel(r'$u^{on}_0$', fontsize=20)
# ax_yon_V.set_ylabel("products index (revenue-ordered)", fontsize=10)
# ax_yon_V.set_xlim(1,16)
plt.title('VIP', fontsize=22)
# fig.suptitle('Assortment map \nand purchase probability map for offline segment', x=0.5, y =0.99, fontsize=20)
fig.tight_layout()
for _, spine in ax_yon_V.spines.items():
spine.set_visible(True)
# plt.show()
if LuceOrNot == 0:
FileName = './Output/' + 'assortment_map_v0.png'
if LuceOrNot == 1:
FileName = './Output/' + 'assortment_map_v0_Luce.png'
fig.savefig(FileName, dpi=600, bbox_inches = 'tight')
plt.pause(2)
plt.close()
print("figure print to "+FileName)