w4_聚类分析_airbnb_参考代码

Airbnb数据字典

在这里插入图片描述

#调包
import pandas as pd
import seaborn as sns#更方便直接视图,查看结果
import matplotlib.pyplot as plt#调参更加灵活
%matplotlib inline#用于jupter视图语句
#数据导入
airbnb=pd.read_csv('w3_airbnb.csv')
#查看数据类型
#变量类别:用户个人信息、用户与airbnb的关系、app使用语言、用户去的国家、用户下单渠道
#这里有2个日期变量,之后会进行操作
airbnb.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 6752 entries, 0 to 6751
Data columns (total 14 columns):
age                     6752 non-null int64
date_account_created    6752 non-null object
date_first_booking      6752 non-null object
gender                  6752 non-null object
Language_EN             6752 non-null int64
Language_ZH             6752 non-null int64
Country_US              6752 non-null int64
Country_EUR             6752 non-null int64
android                 6752 non-null int64
moweb                   6752 non-null int64
web                     6752 non-null int64
ios                     6752 non-null int64
Married                 6752 non-null int64
Children                6752 non-null int64
dtypes: int64(11), object(3)
memory usage: 738.6+ KB
#用户数据具体情况
airbnb.head()
agedate_account_createddate_first_bookinggenderLanguage_ENLanguage_ZHCountry_USCountry_EURandroidmowebwebiosMarriedChildren
0331/7/20101/8/2010F1000101011
1301/10/20101/11/2010M1010101012
2301/19/20101/21/2010F1010101011
3302/3/20102/4/2010F1010101011
4322/7/20102/7/2010F1010101012
#单变量分析
#查看数字型变量核心指标
airbnb.describe()
ageLanguage_ENLanguage_ZHCountry_USCountry_EURandroidmowebwebiosMarriedChildren
count6752.0000006752.0000006752.0000006752.0000006752.0000006752.0000006752.0000006752.0000006752.0000006752.0000006752.000000
mean47.7913210.9721560.0069610.7132700.1627670.6584720.3406400.9007700.0644250.7969491.535841
std146.1777460.1645370.0831470.4522680.3691800.4742570.4739590.2989930.2455270.4023000.841394
min2.0000000.0000000.0000000.0000000.0000000.0000000.0000000.0000000.0000000.0000000.000000
25%28.0000001.0000000.0000000.0000000.0000000.0000000.0000001.0000000.0000001.0000001.000000
50%33.0000001.0000000.0000001.0000000.0000001.0000000.0000001.0000000.0000001.0000001.000000
75%42.0000001.0000000.0000001.0000000.0000001.0000001.0000001.0000000.0000001.0000002.000000
max2014.0000001.0000001.0000001.0000001.0000001.0000001.0000001.0000001.0000001.0000003.000000
#发现年龄最小是2最大是2014,属于数据异常,进行数据清洗,这里保留用户年龄在18-80岁之间的群体
airbnb=airbnb[airbnb['age']<=80]
airbnb=airbnb[airbnb['age']>=18]
airbnb.age.describe()
count    6607.000000
mean       35.982443
std        10.896507
min        18.000000
25%        28.000000
50%        33.000000
75%        41.000000
max        80.000000
Name: age, dtype: float64
#类别型变量(日期)的调整
#计算用户注册到2019年的时间
#第一步将注册日期转变为日期时间格式
airbnb['date_account_created']=pd.to_datetime(airbnb['date_account_created'])
airbnb.info()
#发现data_account_created变量格式从object转变为datetime64
<class 'pandas.core.frame.DataFrame'>
Int64Index: 6607 entries, 0 to 6751
Data columns (total 14 columns):
age                     6607 non-null int64
date_account_created    6607 non-null datetime64[ns]
date_first_booking      6607 non-null object
gender                  6607 non-null object
Language_EN             6607 non-null int64
Language_ZH             6607 non-null int64
Country_US              6607 non-null int64
Country_EUR             6607 non-null int64
android                 6607 non-null int64
moweb                   6607 non-null int64
web                     6607 non-null int64
ios                     6607 non-null int64
Married                 6607 non-null int64
Children                6607 non-null int64
dtypes: datetime64[ns](1), int64(11), object(2)
memory usage: 774.3+ KB
#第二步,将年份从中提取出来,将2019-注册日期的年份,并生成一个新的变量year_since_account_created
airbnb['year_since_account_created']=airbnb['date_account_created'].apply(lambda x:2019-x.year)
airbnb.year_since_account_created.describe()
#发现注册时间最短的是5年,最长的是9年
count    6607.000000
mean        6.034812
std         0.961253
min         5.000000
25%         5.000000
50%         6.000000
75%         7.000000
max         9.000000
Name: year_since_account_created, dtype: float64
#计算用户第一次预定到2019年的时间
#第一步将用户第一次预定时间转变为日期时间格式
airbnb['date_first_booking']=pd.to_datetime(airbnb['date_first_booking'])
#第二步,将年份从中提取出来,将2019-第一次注册的年份,并生成一个新的变量year_since_first_booking  
airbnb['year_since_first_booking']=airbnb['date_first_booking'].apply(lambda x:2019-x.year)
airbnb.year_since_first_booking.describe()
#发现距离第一次预定时间最短的是4年,最长的是9年
count    6607.000000
mean        5.910095
std         0.990769
min         4.000000
25%         5.000000
50%         6.000000
75%         6.000000
max         9.000000
Name: year_since_first_booking, dtype: float64
#将类别型型转化成哑变量(gender)
airbnb=pd.get_dummies(airbnb)
airbnb.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 6607 entries, 0 to 6751
Data columns (total 18 columns):
age                           6607 non-null int64
date_account_created          6607 non-null datetime64[ns]
date_first_booking            6607 non-null datetime64[ns]
Language_EN                   6607 non-null int64
Language_ZH                   6607 non-null int64
Country_US                    6607 non-null int64
Country_EUR                   6607 non-null int64
android                       6607 non-null int64
moweb                         6607 non-null int64
web                           6607 non-null int64
ios                           6607 non-null int64
Married                       6607 non-null int64
Children                      6607 non-null int64
year_since_account_created    6607 non-null int64
year_since_first_booking      6607 non-null int64
gender_F                      6607 non-null uint8
gender_M                      6607 non-null uint8
gender_U                      6607 non-null uint8
dtypes: datetime64[ns](2), int64(13), uint8(3)
memory usage: 845.2 KB
#删除两个日期变量,可以根据数据格式来进行drop
airbnb.drop(airbnb.select_dtypes(['datetime64']),inplace=True,axis=1)
#数据准备完成
#选择五个变量,作为分群的维度
#!这里需要注意,变量变为了airbnb_5,后面的操作中airbnb变为airbnb_5,需要提醒用户
airbnb_5=airbnb[['age','web','moweb','ios','android']]
#数据标准化,使用sklearn中预处理的scale
from sklearn.preprocessing import scale
x=pd.DataFrame(scale(airbnb_5))
#模型建立
#使用cluster建模
from sklearn import cluster
#先尝试分为3类
model=cluster.KMeans(n_clusters=3,random_state=10)
model.fit(x)
KMeans(algorithm='auto', copy_x=True, init='k-means++', max_iter=300,
    n_clusters=3, n_init=10, n_jobs=1, precompute_distances='auto',
    random_state=10, tol=0.0001, verbose=0)
#提取标签,查看分类结果
airbnb_5['cluster']=model.labels_
C:\Anaconda3\lib\site-packages\ipykernel_launcher.py:1: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  """Entry point for launching an IPython kernel.
airbnb_5.head(20)
agewebmowebiosandroidcluster
03310011
13010011
23010011
33010011
43210011
54611000
63010011
74610011
93310011
104510011
113211000
124610011
132910011
142910011
163311000
173710011
182810011
194110011
213011000
223510011
#绘制散点图,查看分群结果
#横坐标为age(年龄),纵坐标为ios(是否使用ios客户端),类别会为分群类别
sns.scatterplot(x='age',y='ios',hue='cluster',data=airbnb_5)
<matplotlib.axes._subplots.AxesSubplot at 0x15e7a28c940>

在这里插入图片描述

#模型评估与优化
#使用groupby函数,评估各个变量维度的分群效果
airbnb_5.groupby(['cluster'])['age'].describe()
countmeanstdmin25%50%75%max
cluster
02108.034.9112909.86627318.028.032.039.078.0
14072.036.87131611.51915318.029.034.043.080.0
2427.032.7939118.26382218.027.031.036.070.0
airbnb_5.groupby(['cluster'])['ios'].describe()
countmeanstdmin25%50%75%max
cluster
02108.00.00.00.00.00.00.00.0
14072.00.00.00.00.00.00.00.0
2427.01.00.01.01.01.01.01.0
#使用silhouette score,评估模型效果
from sklearn import metrics#调用sklearn的metrics库
x_cluster=model.fit_predict(x)#个体与群的距离
score=metrics.silhouette_score(x,x_cluster)#评分越高,个体与群越近;评分越低,个体与群越远
print(score)
0.6359835014766492
centers=pd.DataFrame(model.cluster_centers_)
centers.to_csv('center_3.csv')
#将群体分为5组
model=cluster.KMeans(n_clusters=5,random_state=10)
model.fit(x)
KMeans(algorithm='auto', copy_x=True, init='k-means++', max_iter=300,
    n_clusters=5, n_init=10, n_jobs=1, precompute_distances='auto',
    random_state=10, tol=0.0001, verbose=0)
centers=pd.DataFrame(model.cluster_centers_)
centers.to_csv('center_5.csv')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值