DA46. 某店铺用户消费特征评分
描述
现有某店铺会员消费情况sales.csv。包含以下字段:
请你分别对每个用户的每个消费特征进行评分。
请你对每个用户销售情况的每个特征进行评分,分值为1-4分。对于recency特征,值越小越好。对于frequency和monetary值越大越好。请分别将对应的数据进行四等分并评分,如对于recency:
对于frequency和monetary则方法刚好相反。
要求给所有数据进行评分,并输出前5行。以上数据的输出结果如下:
Python 3 解法, 执行用时: 783ms, 内存消耗: 524288KB, 提交时间: 2022-07-16
import pandas as pd sales = pd.read_csv('sales.csv') sales['monetary'] = sales['monetary'].astype('float') sales['R_Quartile'] = pd.qcut(sales['recency'],[0,0.25,0.5,0.75,1], labels=['4','3','2','1']).astype('int') sales['F_Quartile'] = pd.qcut(sales['frequency'],[0,0.25,0.5,0.75,1], labels=['1','2','3','4']).astype('int') sales['M_Quartile'] = pd.qcut(sales['monetary'],[0,0.25,0.5,0.75,1], labels=['1','2','3','4']).astype('int') print(sales.head())
Python 3 解法, 执行用时: 803ms, 内存消耗: 524288KB, 提交时间: 2022-07-15
import pandas as pd sales = pd.read_csv('sales.csv') sales['R_Quartile'] = pd.qcut(sales['recency'], [0, 0.25, 0.5, 0.75, 1],["4", "3", "2", "1"]).astype("int") sales['F_Quartile'] = pd.qcut(sales['frequency'], [0, 0.25, 0.5, 0.75, 1],["1", "2", "3", "4"]).astype("int") sales['M_Quartile'] = pd.qcut(sales['monetary'], [0, 0.25, 0.5, 0.75, 1],["1", "2", "3", "4"]).astype("int") print(sales.head())
Python 3 解法, 执行用时: 803ms, 内存消耗: 524288KB, 提交时间: 2022-06-30
import pandas as pd sales = pd.read_csv('sales.csv') def deal(x,name): if name == 'recency': if x <= dict_list[0][name]: return 4 elif x > dict_list[0][name] and x <= dict_list[1][name]: return 3 elif x > dict_list[1][name] and x <= dict_list[2][name]: return 2 else: return 1 else: if x <= dict_list[0][name]: return 1 elif x > dict_list[0][name] and x <= dict_list[1][name]: return 2 elif x > dict_list[1][name] and x <= dict_list[2][name]: return 3 else: return 4 sales_quantile = sales.quantile([.25,.50,.75]) dict_list = sales_quantile.to_dict('records') sales['R_Quartile'] = sales['recency'].apply(lambda x : deal(x,'recency')).astype('int') sales['F_Quartile'] = sales['frequency'].apply(lambda x : deal(x,'frequency')).astype('int') sales['M_Quartile'] = sales['monetary'].apply(lambda x : deal(x,'monetary')).astype('int') print(sales.head(5))
Python 3 解法, 执行用时: 806ms, 内存消耗: 524288KB, 提交时间: 2022-07-09
import pandas as pd sales = pd.read_csv('sales.csv') def deal(x,name): if name == 'recency': if x <= dict_list[0][name]: return 4 elif x > dict_list[0][name] and x <= dict_list[1][name]: return 3 elif x > dict_list[1][name] and x <= dict_list[2][name]: return 2 else: return 1 else: if x <= dict_list[0][name]: return 1 elif x > dict_list[0][name] and x <= dict_list[1][name]: return 2 elif x > dict_list[1][name] and x <= dict_list[2][name]: return 3 else: return 4 sales_quantile = sales.quantile([.25,.50,.75]) dict_list = sales_quantile.to_dict('records')#此处的'records'是参数,转化后是list形式的字典 sales['R_Quartile'] = sales['recency'].apply(lambda x : deal(x,'recency')).astype('int') sales['F_Quartile'] = sales['frequency'].apply(lambda x : deal(x,'frequency')).astype('int') sales['M_Quartile'] = sales['monetary'].apply(lambda x : deal(x,'monetary')).astype('int') print(sales.head(5))
Python 3 解法, 执行用时: 811ms, 内存消耗: 524288KB, 提交时间: 2022-07-15
import pandas as pd sales = pd.read_csv('sales.csv') sales['monetary']=sales['monetary'].astype('float') a=sales['recency'].quantile([0.25,0.5,0.75]).values b=sales['frequency'].quantile([0.25,0.5,0.75]).values c=sales['monetary'].quantile([0.25,0.5,0.75]).values def col_recency(x): x=float(x) if x<=a[0]: return 4 elif a[0]<x<=a[1]:return 3 elif a[1]<x<=a[2]:return 2 else: return 1 def col_frequency(x): x=float(x) if x<=b[0]: return 1 elif b[0]<x<=b[1]:return 2 elif b[1]<x<=b[2]:return 3 else: return 4 def col_monetary(x): x=float(x) if x<=c[0]: return 1 elif c[0]<x<=c[1]:return 2 elif c[1]<x<=c[2]:return 3 else: return 4 sales['R_Quartile']=sales['recency'].apply(col_recency) sales['F_Quartile']=sales['frequency'].apply(col_frequency) sales['M_Quartile']=sales['monetary'].apply(col_monetary) print(sales.head(5))