数据简单分析importpandasaspddatafile='../data/air_data.csv'#航空原始数据,第一个行为属性标签csv'#数据探索结果表#读取原始数据并指定UTF-8编码(需要使用文本编辑器将数据转换为UTF-8编码)data=pd.read_csv(datafile,encoding='utf-8')#包括对数据的基本描述,percentiles参数是一个分位数表,指定要计算多少(例如1/4分位数,中位数等)explore=data.describe(percentiles=[],include='all').T#T是一个转置,比较方便查阅explore['null']=len(data)-explore['count']#describe()函数自动计算非空值个数,需要手动计算空值个数explore=explore[['null','max','min']]explore.columns=[u'空值个数',u'最大值',u'minimumvalue']#headerrename'''这里只选择了部分探索结果。describe()函数自动计算的字段有count(非空值个数)、unique(唯一值个数)、top(出现频率最高的)、freq(出现频率最高的)、mean(平均值),std(方差),min(最小值),50%(中值),max(最大值)'''简单可视化importpandasaspdimportmatplotlib.pyplotaspltdatafile='../data/air_data.csv'#航空原始数据,第一行Attributelabel#读取原始数据,指定UTF-8编码(需要用文本编辑器将数据转为UTF-8编码)data=pd.read_csv(datafile,encoding='utf-8')#客户信息类别#从日期时间中提取会员年份importdatetimeffp=data['FFP_DATE'].apply(lambdax:datetime.strptime(x,'%Y/%m/%d'))ffp_year=ffp.map(lambdax:x.year)#绘制每年注册会员数量的直方图fig=plt.figure(figsize=(8,5))#设置画布大小plt.rcParams['font.sans-serif']='SimHei'#设置中文显示plt.rcParams['axes.unicode_minus']=Falseplt.hist(ffp_year,bins='auto',color='#0504aa')plt.xlabel('year')plt.ylabel('numberofmembers')plt.title('每年注册的会员人数')plt.show()plt.close#提取不同性别的会员人数male=pd.value_counts(data['GENDER'])['male']female=pd.value_counts(data['GENDER']female=pd.value_counts(data['GENDER']'])['female']#绘制成员性别比例饼图fig=plt.figure(figsize=(7,4))#设置画布大小plt.pie([male,female],labels=['male','Female'],colors=['lightskyblue','lightcoral'],autopct='%1.1f%%')plt.title('会员性别比例')plt.show()plt.closedata清理importnumpyasnpiimportpandasaspddatafile='../data/air_data.csv'#航空原始数据路径cleanedfile='../tmp/data_cleaned.csv'#数据清洗后保存的文件路径#读取数据airline_data=pd.read_csv(datafile,encoding='utf-8')print('原始数据的shape为:',airline_data.shape)#去掉fare为空的记录airline_notnull=airline_data.loc[airline_data['SUM_YR_1'].notnull()&airline_data['SUM_YR_2'].notnull(),:]print('删除缺失记录后数据的形状为:',airline_notnull.shape)#只保留票价非零的记录,或者平均折扣率不为0且飞行总公里数大于0index1=airline_notnull['SUM_YR_1']!=0index2=airline_notnull['SUM_YR_2']!=0index3=(airline_notnull['SEG_KM_SUM']>0)&(airline_notnull['avg_discount']!=0)index4=airline_notnull['AGE']>100#移除年龄大于100的记录airline=airline_notnull[(index1|index2)&index3&~index4]print('数据清洗后的数据形状为:',airline.shape)airline.to_csv(cleanedfile)#保存清洗后的数据原始数据的形状为:(62988,44)删除缺失记录后的数据形状为:(62299,44)数据清洗后的数据形状为:(62043,44)属性选择、构建和数据标准化#属性选择、构建和数据标准化importpandasaspdimportnumpyasnp#清洗后读取数据cleanedfile='../tmp/data_cleaned.csv'#数据清洗后保存的文件路径airline=pd.read_csv(cleanedfile,encoding='utf-8')#选择需要的属性airline_selection=airline[['FFP_DATE','LOAD_TIME','LAST_TO_END','FLIGHT_COUNT','SEG_KM_SUM','avg_discount'预筛选属性]](5Behaviors:\n',airline_selection.head())#代码7-8#构造属性LL=pd.to_datetime(airline_selection['LOAD_TIME'])-\pd.to_datetime(airline_selection['FFP_DATE'])L=L.astype('str').str.split().str[0]L=L.astype('int')/30#合并属性airline_features=pd.concat([L,airline_selection.iloc[:,2:]],axis=1)airline_features.columns=['L','R','F','M','C']print('构造的LRFMC属性的前5个行为:\n',airline_features.head())#Datastandardizationfromsklearn.preprocessingimportStandardScalerdata=StandardScaler().fit_transform(airline_features)np.savez('../tmp/airline_scale.npz',data)print('标准化后LRFMC的五个属性分别是:\n',data[:5,:])过滤前5行属性:FFP_DATELOAD_TIMELAST_TO_ENDFLIGHT_COUNTSEG_KM_SUMavg_discount02006/11/22014/3/3112105807170.96163912007/2/192014/3/3129161.25231422007/2/22014/3/31111352837121.25467632008/8/8/8/8/222014/3197232813361.09087042009/4/4/4/102014/102014/315709570957095709570957095709570957095728MC8C090.20000012105807170.961639186.56666771402936781.252314287.166667111352837121.254676368.2333339723281036061.23281036067.53333351523099280.970658标准化后LRFMC五个属性为:[[1.43579256-0.9449390214.0340240126.761156991.29554188][1.30723219-0.911885649.0732159513.126864362.86817777][1.32846234-0.889850068.7188725212.653481442.88095186][0.65853304-0.416085040.7815796212.540621931.99471546][0.3860794-0.922903439.9236401913.898735971.34433641]]importpandasaspdimportnumpyasnpfromsklearn.clusterimportKMeans?#导入kmeansalgorithm#读取标准化数据airline_scale=np.load('../tmp/airline_scale.npz')['arr_0']k=5#确定聚类中心的个数#构建模型,随机种子设置为123kmeans_model=KMeans(n_clusters=k,n_jobs=4,random_state=123)fit_kmeans=kmeans_model.fit(airline_scale)#模型训练#查看聚类结果kmeans_cc=kmeans_model.cluster_centers_#聚类中心print('各种聚类中心为:\n',kmeans_cc)kmeans_labels=kmeans_model.labels_#样本类别标签print('每个样本的类别标签为:\n',kmeans_labels)r1=pd.Series(kmeans_model.labels_).value_counts()#不同类别的统计iesofsamplesThenumberofsamplesprint('Thefinalnumberofeachcategoryis:\n',r1)#输出聚类的结果cluster_center=pd.DataFrame(kmeans_model.cluster_centers_,\columns=['ZL','ZR','ZF','ZM','ZC'])#放聚类框function(){//外汇返利www.fx61.comcluster_center.index=pd.DataFrame(kmeans_model.labels_).\drop_duplicates().iloc[:,0]#使用样本类别作为数据框索引print(cluster_center)#代码7-10importmatplotlib.pyplotasplt#客户组雷达图labels=['ZL','ZR','ZF','ZM','ZC']legen=['客户组'+str(i+1)foriincluster_center.index]#客户组名,作为雷达图的图例lstype=['-','--',(0,(3,5,1,5,1,5)),':','-.']kinds=list(cluster_center.iloc[:,0])#由于雷达图必须保证数据是闭合的,所以增加L列,转换为np.ndarraycluster_center=pd.concat([cluster_center,cluster_center[['ZL']]],axis=1)centers=np.array(cluster_center.iloc[:,0:])#把圆的周长分开,让它闭合n=len(labels)angle=np.linspace(0,2*np.pi,n,endpoint=False)angle=np.concatenate((angle,[angle[0]]))#drawingfig=plt.figure(figsize=(8,6))ax=fig.add_subplot(111,polar=True)#绘制图形的形式极坐标plt.rcParams['font.sans-serif']=['SimHei']#用于正常显示中文标签plt.rcParams['axes.unicode_minus']=False#用于正常显示负号#画一条线对于我在范围内(len(种类)):ax.plot(角度,中心[i],linestyle=lstype[i],linewidth=2,label=kinds[i])#Addattributelabelax.set_thetagrids(angle*180/np.pi,labels)plt.title('customercharacteristicanalysisradarmap')plt.legend(legen)plt.show()plt.close各类聚类中心为:[[1.1606821-0.37729768-0.08690742-0.09484273-0.15591932][-0.313655571.68628985-0.57402225-0.53682279-0.17332449][0.05219076-0.00264741-0.22674532-0.231168462.19158505][-0.70022016-0.4148591-0.16116192-0.1609779-0.2550709][0.48337963-0.799373472.483198412.424721880.30863168]]各样本的类别标签为:[444…311]最终每个类别的数目为:3246610157391121254533624182dtype:int64ZLZRZFZMZC041.160682-0.377298-0.086907-0.094843-0.1559192-0.3136561.686290-0.574022-0.536823-0.17332400.052191-0.002647-0.226745-0.2311682.1915853-0.700220-0.414859-0.161162-0.160978-0.25507110.483380-0.7993732.4831982.4247220.308632
