Python数据分析与挖掘实战 14章
2018-05-03 21:01
399 查看
因学习中发现《Python数据分析与挖掘实战》中的代码,有些不能实现,自己学习的时候走了很多弯路,特此分享可直接实现的代码,希望能让有需要的朋友少走弯路。
#14-1 离差标准化 import pandas as pd inputfile='../14.2/business_circle.xls' outfile='../14.2/standardized.xls' data=pd.read_excel(inputfile,index_col=u'基站编号') data=(data-data.min())/(data.max()-data.min()) data=data.reset_index() data.to_excel(outfile,index=False)#14-2谱系聚类图 import pandas as pd inputfile='../14.2/standardized.xls' data=pd.read_excel(inputfile,index_col=u'基站编号') import matplotlib.pyplot as plt from scipy.cluster.hierarchy import linkage,dendrogram #scipy的层次聚类函数 Z=linkage(data,method='ward',metric='euclidean') P=dendrogram(Z,0) plt.show()
#14-3 层次聚类算法 import pandas as pd inputfile='../14.2/standardized.xls' data=pd.read_excel(inputfile,index_col=u'基站编号') k=3 from sklearn.cluster import AgglomerativeClustering #sklearn层次聚类函数 model=AgglomerativeClustering(n_clusters=k,linkage='ward') model.fit(data) r=pd.concat([data,pd.Series(model.labels_,index=data.index)],axis=1) r.columns=list(data.columns)+[u'聚类类别'] import matplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['SimHei'] plt.rcParams['axes.unicode_minus']=False style=['ro-','go-','bo-'] xlabels=[u'工作日人均停留时间',u'凌晨人均停留时间',u'周末人均停留时间',u'日均人流量'] pic_output='../14.2/type_' for i in range(k): plt.figure() tmp=r[r[u'聚类类别']==i].iloc[:,:4] for j in range(len(tmp)): plt.plot(range(1,5),tmp.iloc[j],style[i]) plt.xticks(range(1,5),xlabels,rotation=20) plt.subplots_adjust(bottom=0.15) plt.savefig(u'%s%s.png'%(pic_output,i))
阅读更多
相关文章推荐
- 用Python 爬取淘宝商品数据挖掘分析实战
- python数据分析与挖掘实战 第六章 拓展思考
- Python数据分析与挖掘实战学习笔记(二)
- python 数据分析与挖掘实战
- python数据分析与挖掘学习笔记(7)-交通路标自动识别实战与神经网络算法
- Python数据分析与挖掘实战—挖掘建模
- 利用Python爬虫爬取淘宝商品做数据挖掘分析实战篇,超详细教程
- Python数据分析与挖掘实战学习笔记(一)
- Python数据分析与挖掘实战代码纠错 代码3-1
- Python数据分析与挖掘实战(开发流程及常用库安装)
- python数据分析与挖掘实战-第六章拓展偷漏税用户识别
- python数据分析与挖掘实战—聚类算法对比
- python数据挖掘与分析实战—1、2、3
- python数据分析与挖掘实战 第七章 拓展思考
- 『Python数据分析与挖掘实战』第五章:挖掘建模
- python数据分析和挖掘实战
- python数据分析与挖掘学习笔记(6)-电商网站数据分析及商品自动推荐实战与关联规则算法
- python数据分析与挖掘项目实战记录
- Python数据分析与挖掘实战 15章