电商用户行为预测数据初探

电商用户行为预测

数据分析

导入相关包

%matplotlib inline
import pandas as pd
import numpy as np

import matplotlib.pyplot as plt
import seaborn as sns
plt.rc('font', family='SimHei', size=13)

import os,gc,re,warnings,sys
warnings.filterwarnings("ignore")

自定义采样函数

def get_all_click_sample(data_path, sample_nums=10000):
    """
        训练集中采样一部分数据调试
        data_path: 原数据的存储路径
        sample_nums: 采样数目(这里由于机器的内存限制,可以采样用户做)
    """
    all_click = pd.read_csv(data_path + 'train.csv')
    all_user_ids = all_click.user_id.unique()

    sample_user_ids = np.random.choice(all_user_ids, size=sample_nums, replace=False) 
    all_click = all_click[all_click['user_id'].isin(sample_user_ids)]
    
    all_click = all_click.drop_duplicates((['user_id', 'product_id', 'event_time']))
    return all_click

读取数据

采取10000个用户的样本集

data_path = 'F:/data/'
trn_click = get_all_click_sample(data_path, sample_nums=10000)
tst_click = pd.read_csv(data_path+'test.csv')

数据预处理

统计用户的交互行为次数,并添加新的一列count

#计算用户与商品交互的次数,并添加新的一列count
trn_click['click_cnts'] = trn_click.groupby(['user_id'])['event_time'].transform('count')
tst_click['click_cnts'] = tst_click.groupby(['user_id'])['event_time'].transform('count')

数据浏览

用户的交互行为_训练集

trn_click.csv文件数据中每个字段的含义

event_time:用户行为发生的时间

event_type:用户的行为类型[浏览,加入购物车,从购物车删除, 购买]

product_id:用户查看或购买的商品的唯一标识

catagory_id:商品类型ID

category_code:商品类型的实际意义

brand:商品品牌

price:商品价格

user_id: 用户的唯一标识

user_session:用户会话ID

click_cnts:同一用户的交互次数

用户点击日志信息

trn_click.info()

在这里插入图片描述

trn_click.describe()

在这里插入图片描述

#训练集中的用户数量为10000
trn_click.user_id.nunique()
trn_click.groupby('user_id')['product_id'].count().min()  # 训练集里面每个用户至少点击了一个商品

直方图查看基本属性分布

plt.figure()
plt.figure(figsize=(15, 20))
i = 1
for col in ['event_time', 'event_type', 'product_id', 'category_id', 'category_code', 'brand', 
            'price', 'user_id', 'user_session', 'click_cnts']:
    plot_envs = plt.subplot(4,3, i)
    i += 1
    v = trn_click[col].value_counts().reset_index()[:10]
    fig = sns.barplot(x=v['index'], y=v[col])
    for item in fig.get_xticklabels():
        item.set_rotation(90)
    plt.title(col)
plt.tight_layout()
plt.show()

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
从直方图可以基本看出,交互行为发生的时间大多在10月2日。
用户的行为类型大多数为浏览,加入购物车,真正的购买行为较少。
在各大商品中,家电、真空吸尘器、环保用品的关注度最高,其次为文具、服装、手套等,家具,桌柜等产品不是很受购买者青睐。
从商品品牌来看,runail 品牌影响力更大一些。
客户点击率来看,大多数用户交互行为为4次,最高的为29次,至少1次。

数据分析

#用户重复点击
user_click_count = trn_click.groupby(['user_id', 'product_id'])['event_time'].agg({'count'}).reset_index()
user_click_count[:10]

在这里插入图片描述

user_click_count['count'].unique()
#用户交互次数
user_click_count.loc[:,'count'].value_counts() 

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值