用户特征分析和行为预测

摘要

本文主要分析Airbnb用户的特征属性,并对新用户的民宿预订结果进行预测。包括了数据探索特征工程建模预测的整个过程。

其中:

1.探索数据部分主要基于pandas库,利用常见函数和matplotlib绘图进行数据分析和探索

2.特征工程部分主要通过从时间中提取年、月、日、季节、weekday,对年龄进行分段,并通过one_hot对数据进行编码来提取特征

3.构建模型部分主要基于sklearn包建立逻辑回归模型Logistic Regression


1.背景

Airbnb的新用户可以在190多个国家的34,000多个城市预订住宿地点。 通过准确预测新用户预订首次旅行体验的位置,Airbnb可以分享更多个性化内容,减少首次预订的平均时间,并更好地预测需求 

 

2.数据描述

总共包含3个csv文件,涉及27万用户多达1千万条数据

1. train_users_2.csv -(训练数据) 
2. test_users.csv -(测试数据)
- id:(用户id) 
- date_account_created(帐号注册时间)
- timestamp_first_active(首次活跃时间)
- date_first_booking(首次订房时间)
- gender(性别) 
- age(年龄) 
- signup_method(注册方式) 
- signup_flow(注册页面)
- language(语言)
- affiliate_channel(付费市场渠道)
- affiliate_provider(付费市场渠道名称)
- first_affiliate_tracked(注册前第一个接触的市场渠道)
- signup_app(注册app) 
- first_device_type(设备类型) 
- first_browser(浏览器类型) 
- country_destination(订房国家-需要预测的量)
3. sessions.csv -(网页浏览数据) 
- user_id(用户id)
- action(用户行为) 
- action_type(用户行为类型) 
- action_detail(用户行为具体) 
- device_type(设备类型) 
- secs_elapsed(停留时长)

3.数据探索

3.1读取数据

导入包

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import datetime 
import sklearn
%matplotlib inline

读取数据

train=pd.read_csv('train_users_2.csv')
test=pd.read_csv('test_users.csv')

查看数据特征

print('Train dataset columns:\n',train.columns)
print('Test dataset columns:\n',test.columns)

分析:

1.train比test多一列country_destination

2.country_destination是要预测的目标变量

3.可以合并train和test

查看数据信息

print('Train dataset info:\n',train.info())
print('Test dataset info:\n',test.info())

分析:

1.train数据集有213451条数据,16个特征

2.train数据集有62096条数据,16个特征

3.date_first_booking train数据中缺失较多,在test数据集中为0,可以考虑删掉此特征

合并数据集

data=pd.concat([train,test],ignore_index=True)

3.2特征分析

1.date_account_created

data.date_account_created.head()

计算每天注册人数

print(data.date_account_created.value_counts().head())
print(data.date_account_created.value_counts().tail())

查看date_account_created统计信息

data.date_account_created.describe()

观察用户增长情况

data_created=data.date_account_created.value_counts()
data_created_date=pd.to_datetime(data_created.index)
data_created_day=data_created_date-data_created_date.min()

plt.scatter(data_created_day.days,data_created.values)
plt.title('Accounts created')
plt.xlabel('Days')
plt.ylabel('Accounts created')

分析:

1.x轴表示距离首个用户注册时间的天数

2.y轴表示当天注册的用户数量

3.网站进入第三个年头,用户爆发性增长

 

2.timestamp_first_active

data.timestamp_first_active.head()

将时间戳timestamp_first_active转换成日期格式

data.timestamp_first_active=data.timestamp_first_active.astype(str).apply(lambda x: datetime.datetime(int(x[:4]),
                                                                                                     int(x[4:6]),
                                                                                                     int(x[6:8]),
                                                                                                     int(x[8:10]),
                                                                                                     int(x[10:12]),
                                                                                                     int(x[12:])))
data.timestamp_first_active.describe()

分析:

1.没有重复数据

2.数据时间从2009至2014跨越5年

 

3.date_first_booking 

由于test数据集中date_first_booking 完全缺失,故可以删除这一特征

 

4.age

data.age.value_counts().head()

分析:用户年龄主要集中在30左右

画柱形图统计

#将年龄分成4段:missing values,too small age,reasonable age,too large age
age=[data[data.age.isnull()].age.shape[0],
     data.query('age <15').age.shape[0],
    data.query('age>=15&age<=90').age.shape[0],
    data.query('age >90').age.shape[0]]
columns=['Null','age<15','age','age>90']

fig,ax=plt.subplots(1,1)
sns.barplot(columns,age,ax=ax)
ax.set_title('Age')

分析:异常年龄较少,有一点的缺失值

 

5.其他特征

其他特征的labels较少,可以直接硬编码转换

封装柱形图函数观察其他几个特征

def feature_barplot(feature,data_set=data,figsize=(10,5),rot=90):
    feature_data=data[feature].value_counts()
    fig,ax=plt.subplots(1,1,figsize=figsize)
    sns.barplot(feature_data.index,feature_data.values,ax=ax)
    ax.set_title(feature)                   
    ax.set_xticklabels(ax.xaxis.get_majorticklabels(),rotation=rot)
    ax.set_ylabel('Counts')
    figname=feature+'.png'  
    fig.savefig(figname,dpi=75)

5.1 gender

feature_barplot('gender')

分析:未知性别还是非常多的

 

5.2 signup_method 

feature_barplot('signup_method')

5.3 signup_flow

feature_barplot('signup_flow')

5.4 language

feature_barplot('language')

5.5 affiliate_channel

feature_barplot('affiliate_channel')

5.6 affiliate_provider

feature_barplot('affiliate_provider')

5.7 first_affiliate_tracked

feature_barplot('first_affiliate_tracked')

 5.8 signup_app

feature_barplot('signup_app')

5.9 first_device_type

feature_barplot('first_device_type')

 5.10 first_browser

feature_barplot('first_browser')

 

 

 

 


版权声明:本文为weixin_42389386原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。