赞
踩
使用sklearn.cluester的KMeans类对航空公司客户数据进行聚类分析,
把乘客分到不同的类别中
数据集:air_data.csv
数据集大小:62052条不重复数据
原数据有40个属性,为了大家训练模型方便,本实验使用预处理后的标准化数据;
该数据有5个属性。
数据说明:
ZL:入会至当前时长,反应可能的活跃时间
ZR:最近消费时间间隔,反应最近一段时间活跃程度
ZF:消费频次,反应客户忠诚度
ZM:消费里程总额,反应客户对乘机的依赖程度
ZC:舱位等级对应折扣系数,一般舱位等级越高,折扣系数越大
import pandas as pd
data = pd.read_csv("air_data.csv",header = 0)
print(data.shape)
print("1.载入数据,输出前五条")
print(data.head())
from sklearn.cluster import KMeans
k = 5
kmodel = KMeans(max_iter=300,n_clusters=5,random_state=None,tol=0.0001)
kmodel.fit(data)
r1 = pd.Series(kmodel.labels_).value_counts()
print('统计各个类别的数目')
print(r1)
r2 = pd.DataFrame(kmodel.cluster_centers_)#找出聚类中心
print('聚类中心:')
print(r2)
r = pd.concat([r2, r1], axis = 1)#聚类中心对应的类别下的数目
r.columns = list(data.columns) + [u'聚类个数']
print('查看聚类个数及中心点统计')
print(r)
from matplotlib import pyplot as plt
plt.xlabel("ZL-ZR-ZF-ZM-ZC")
plt.ylabel("Custer-center-value")
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.title("title")
clu = kmodel.cluster_centers_
colors = ['red','green','yellow','blue','black']
x = [1,2,3,4,5]
for i in range(len(clu)):
plt.plot(x,clu[i],label = u'cluster'+str(i),color=colors[i],marker='o')
plt.legend()
plt.show()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。