|
@@ -37,29 +37,36 @@ def generate_report(city_uuid, data, filter_dict, recommend_data, delivery_count
|
|
|
valid_cust_feats = group_sums[group_sums > 0].index.tolist()
|
|
valid_cust_feats = group_sums[group_sums > 0].index.tolist()
|
|
|
cust_relation = data[data["cust_feat"].isin(valid_cust_feats)]
|
|
cust_relation = data[data["cust_feat"].isin(valid_cust_feats)]
|
|
|
cust_relation = cust_relation.reset_index(drop=True)
|
|
cust_relation = cust_relation.reset_index(drop=True)
|
|
|
- cust_relation.to_csv(os.path.join(save_dir, "feats_interaction.csv"), index=False, encoding='utf-8-sig')
|
|
|
|
|
|
|
+ cust_relation = cust_relation.rename(
|
|
|
|
|
+ columns = {
|
|
|
|
|
+ "product_feat": "卷烟特征",
|
|
|
|
|
+ "cust_feat": "商户特征",
|
|
|
|
|
+ "relation": "相关性"
|
|
|
|
|
+ }
|
|
|
|
|
+ )
|
|
|
|
|
+ cust_relation.to_csv(os.path.join(save_dir, "品规商户特征关系表.csv"), index=False, encoding='utf-8-sig')
|
|
|
|
|
|
|
|
|
|
|
|
|
# 2. 品规信息
|
|
# 2. 品规信息
|
|
|
cust_relation[:20].to_csv(os.path.join(save_dir, "cust_relation.csv"), index=False, encoding='utf-8-sig')
|
|
cust_relation[:20].to_csv(os.path.join(save_dir, "cust_relation.csv"), index=False, encoding='utf-8-sig')
|
|
|
- with open(os.path.join(save_dir, "product_info.csv"), "w", encoding='utf-8-sig') as f:
|
|
|
|
|
|
|
+ with open(os.path.join(save_dir, "卷烟信息表.csv"), "w", encoding='utf-8-sig') as f:
|
|
|
for key, value in filter_dict.items():
|
|
for key, value in filter_dict.items():
|
|
|
if key != 'product_code':
|
|
if key != 'product_code':
|
|
|
f.write(f"{ImportanceFeaturesMap.PRODUCT_FEATRUES_MAP[key]}, {value}\n")
|
|
f.write(f"{ImportanceFeaturesMap.PRODUCT_FEATRUES_MAP[key]}, {value}\n")
|
|
|
|
|
|
|
|
# 3. 生成推荐报告
|
|
# 3. 生成推荐报告
|
|
|
recommend_report = generate_recommend_report(city_uuid, recommend_data, delivery_count)
|
|
recommend_report = generate_recommend_report(city_uuid, recommend_data, delivery_count)
|
|
|
- recommend_report.to_csv(os.path.join(save_dir, "recommend_report.csv"), index=False, encoding="utf-8-sig")
|
|
|
|
|
|
|
+ recommend_report.to_csv(os.path.join(save_dir, "商户售卖推荐表.csv"), index=False, encoding="utf-8-sig")
|
|
|
|
|
|
|
|
def generate_recommend_report(city_uuid, recommend_data, delivery_count):
|
|
def generate_recommend_report(city_uuid, recommend_data, delivery_count):
|
|
|
recommend_data = pd.DataFrame(recommend_data)
|
|
recommend_data = pd.DataFrame(recommend_data)
|
|
|
|
|
|
|
|
- recpmmend_list = recommend_data["cust_code"].to_list()
|
|
|
|
|
- recommend_cust_info = dao.get_cust_by_ids(city_uuid, recpmmend_list)
|
|
|
|
|
|
|
+ recommend_list = recommend_data["cust_code"].to_list()
|
|
|
|
|
+ recommend_cust_info = dao.get_cust_by_ids(city_uuid, recommend_list)
|
|
|
|
|
|
|
|
cust_ids = recommend_cust_info.set_index("BB_RETAIL_CUSTOMER_CODE")
|
|
cust_ids = recommend_cust_info.set_index("BB_RETAIL_CUSTOMER_CODE")
|
|
|
recommend_data = recommend_data.join(cust_ids, on="cust_code", how="inner")
|
|
recommend_data = recommend_data.join(cust_ids, on="cust_code", how="inner")
|
|
|
- recommend_data = recommend_data[["cust_code", "BB_RETAIL_CUSTOMER_NAME", "recommend_score"]]
|
|
|
|
|
|
|
+ recommend_data = recommend_data[["cust_code", "BB_RETAIL_CUSTOMER_NAME", "sale_qty", "recommend_score"]]
|
|
|
# 1. 计算每个商户的理论应得数量(带小数)
|
|
# 1. 计算每个商户的理论应得数量(带小数)
|
|
|
recommend_data["delivery_float"] = (
|
|
recommend_data["delivery_float"] = (
|
|
|
recommend_data["recommend_score"] / recommend_data["recommend_score"].sum() * delivery_count
|
|
recommend_data["recommend_score"] / recommend_data["recommend_score"].sum() * delivery_count
|
|
@@ -80,7 +87,17 @@ def generate_recommend_report(city_uuid, recommend_data, delivery_count):
|
|
|
recommend_data = recommend_data.reset_index()
|
|
recommend_data = recommend_data.reset_index()
|
|
|
# 5. 按recommend_score从大到小重新排序
|
|
# 5. 按recommend_score从大到小重新排序
|
|
|
recommend_data = recommend_data.sort_values("index")
|
|
recommend_data = recommend_data.sort_values("index")
|
|
|
- recommend_data = recommend_data.rename(columns={"index": "推荐序号", "BB_RETAIL_CUSTOMER_NAME": "商户名称", "recommend_score": "匹配评分", "delivery_count": "建议投放量(条)"})
|
|
|
|
|
|
|
+ recommend_data["sale_qty"] = recommend_data["sale_qty"].round(0).astype(int) # 将月均销量四舍五入取整
|
|
|
|
|
+ recommend_data = recommend_data.rename(
|
|
|
|
|
+ columns={
|
|
|
|
|
+ "index": "推荐序号",
|
|
|
|
|
+ "cust_code": "商户编号",
|
|
|
|
|
+ "BB_RETAIL_CUSTOMER_NAME": "商户名称",
|
|
|
|
|
+ "sale_qty": "历史月均销量",
|
|
|
|
|
+ "recommend_score": "推荐系数",
|
|
|
|
|
+ "delivery_count": "建议投放量(条)"
|
|
|
|
|
+ }
|
|
|
|
|
+ )
|
|
|
recommend_data["推荐序号"] = recommend_data["推荐序号"] + 1
|
|
recommend_data["推荐序号"] = recommend_data["推荐序号"] + 1
|
|
|
|
|
|
|
|
return recommend_data
|
|
return recommend_data
|
|
@@ -93,19 +110,29 @@ def get_cust_list_from_history_order(city_uuid, product_code):
|
|
|
# 确保cust_code是字符串类型
|
|
# 确保cust_code是字符串类型
|
|
|
order_data["cust_code"] = order_data["cust_code"].astype(str)
|
|
order_data["cust_code"] = order_data["cust_code"].astype(str)
|
|
|
|
|
|
|
|
- order_data = order_data.groupby(["cust_code", "cust_name", "product_code", "product_name"])[["sale_qty", "sale_amt"]].sum().reset_index()
|
|
|
|
|
|
|
+ order_data = order_data.groupby(["cust_code", "cust_name", "product_code", "product_name"])[["sale_qty", "sale_amt"]].mean().reset_index()
|
|
|
|
|
+ order_data["sale_qty"] = order_data["sale_qty"].round(0).astype(int)
|
|
|
order_data = order_data.sort_values("sale_qty", ascending=False)
|
|
order_data = order_data.sort_values("sale_qty", ascending=False)
|
|
|
|
|
|
|
|
# 读取推荐数据
|
|
# 读取推荐数据
|
|
|
- recommend_data = pd.read_csv('./data/recommend_report.csv')
|
|
|
|
|
|
|
+ recommend_data = pd.read_csv('./data/商户售卖推荐表.csv')
|
|
|
# recommend_data = recommend_data.drop(columns=["sale_qty"])
|
|
# recommend_data = recommend_data.drop(columns=["sale_qty"])
|
|
|
# 确保recommend_data中的cust_code也是字符串类型
|
|
# 确保recommend_data中的cust_code也是字符串类型
|
|
|
- recommend_data["cust_code"] = recommend_data["cust_code"].astype(str)
|
|
|
|
|
- cust_ids = recommend_data.set_index("cust_code")
|
|
|
|
|
|
|
+ recommend_data["商户编号"] = recommend_data["商户编号"].astype(str)
|
|
|
|
|
+ cust_ids = recommend_data.set_index("商户编号")
|
|
|
|
|
|
|
|
# 执行合并操作
|
|
# 执行合并操作
|
|
|
merge_data = order_data.join(cust_ids, on="cust_code", how="left")
|
|
merge_data = order_data.join(cust_ids, on="cust_code", how="left")
|
|
|
- merge_data = merge_data[["cust_code", "cust_name", "product_code", "product_name", "sale_qty", "推荐序号"]]
|
|
|
|
|
|
|
+ merge_data = merge_data[["cust_code", "cust_name", "product_code", "product_name", "sale_qty", "推荐序号", "推荐系数", "历史月均销量"]]
|
|
|
|
|
+ merge_data = merge_data.rename(
|
|
|
|
|
+ columns={
|
|
|
|
|
+ "cust_code": "商户编号",
|
|
|
|
|
+ "cust_name": "商户名称",
|
|
|
|
|
+ "product_code": "卷烟编码",
|
|
|
|
|
+ "product_name": "卷烟名称",
|
|
|
|
|
+ "sale_qty": "月均销量"
|
|
|
|
|
+ }
|
|
|
|
|
+ )
|
|
|
return merge_data
|
|
return merge_data
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if __name__ == "__main__":
|