python 实现数据降维推荐系统(附Python源码)
2017-11-07 19:39
706 查看
主成分分析原理:请点击PCA查看
![](https://oscdn.geek-share.com/Uploads/Images/Content/201711/ffaa0117f99127384d015d50ff68ed42)
结果
#!usr/bin/env python #_*_ coding:utf-8 _*_ import pandas as pd import numpy as np import matplotlib.pyplot as plt #如果一个旅游网站里面有100000个注册用户,以及100个注册酒店,网站有用户通过本网站点击酒店页面的 #记录数据信息A=Aij 100000*100 Aij表示第i个用户点击j酒店的次数 #Q1:如何评价酒店之间的相似度 #Q2:给定一个酒店,请找出与它最相似的其他几个酒店 #Q3:如何要给酒店分类,有什么办法? #prepare data set,suppose there are 5 types of hotels 纬度评分 generatorNum=5 #5 hotelNum=100 #100 customerNum=100000 #100000 #10000用户个对五个纬度的侧重点的评分 generators=np.random.randint(5,size=(customerNum,generatorNum)) print 4000 (generators) #酒店在各个纬度为评分 hotelcomp=np.random.random(size=(generatorNum,hotelNum))-0.5# 0.5出现负值 print(hotelcomp) #.dot矩阵运算,生成顾客对酒店评分 hotelRating=pd.DataFrame(generators.dot(hotelcomp),index=['c%.6d'%i for i in range(customerNum)],columns=['hotel_%.3d'%j for j in range(100)]).astype(int) #data z-score公式 def normalize(s): if s.std()>1e-6: #**乘方,就散标准分数z-score,用来算离数据中心的偏差的,https://www.zhihu.com/question/21600637 return(s-s.mean())*s.std()**(-1) else: return (s-s.mean()) #如何评价酒店之间的相似度? #data to z-score hotelRating_norm=hotelRating.apply(normalize) print('hotelRating_norm\n{}'.format(hotelRating_norm)) print(type(hotelRating_norm)) #计算协方差 hotelRating_norm_corr=hotelRating_norm.cov() print('hotelRating_norm_corr\n{}'.format(hotelRating_norm_corr)) #SVD,即奇异值分解 u,s,v=np.linalg.svd(hotelRating_norm_corr) #碎石图确定分类,测试时是5 print('u\n{}'.format(u)) print(s) plt.plot(s,'o') plt.title("singular value spectrum") plt.show()
#截取SVD纬度 u_short = u[:,:5] v_short = v[:5,:] s_short = s[:5] print('u,s,v,short{}'.format(u_short,v_short,s_short)) #numpy.diag()创建一个对角矩阵 hotelRating_norm_corr_rebuild=pd.DataFrame(u_short.dot(np.diag(s_short).dot(v_short)), index=hotelRating_norm_corr.index,columns=hotelRating_norm_corr.keys()) #get the top components ,np.power数组的元素分别求n次方 top_component=hotelRating_norm.dot(u_short).dot(np.diag(np.power(s_short,-0.5))) #classfication of each hotel hotel_ind = 3 rating = hotelRating_norm.loc[:,'hotel_%.3d'%hotel_ind] print ("classification of the %dth hotel"%hotel_ind,top_component.T.dot(rating)/customerNum)
结果
相关文章推荐
- python 实现斐波那契数列,动态删除历史数据demo,很久以前写的,分享给大家 推荐
- <数据挖掘><python><一个小的推荐系统示例>
- 【甘道夫】使用HIVE SQL实现推荐系统数据补全
- 数据挖掘项目之---通过对web日志的挖掘来实现内容推荐系统
- 关于数据挖掘推荐系统实现
- 使用storm实现实时GPS数据客流特征分析系统:源码分析
- python 实现汽车之家车型数据爬虫 推荐
- 【甘道夫】使用HIVE SQL实现推荐系统数据补全
- 【Python数据挖掘课程】八.关联规则挖掘及Apriori实现购物推荐
- 源码推荐(5.20):CDPSearchController封装,可实现model数据的搜索显示,DOPNavbarMe
- python脚本实现数据导出excel格式的简单方法(推荐)
- 通用权限管理系统组件 (GPM - General Permissions Manager) 中实现数据列的权限,附源码
- 在Python中实现你自己的推荐系统【转载】
- 基于用户协同过滤的推荐系统算法,python 实现
- 关于数据挖掘推荐系统实现
- 用于推荐系统的SVD算法python实现
- 使用Python MrJob的MapReduce实现电影推荐系统
- 数据挖掘之推荐分析--python实现
- 通用权限管理系统组件 (GPM - General Permissions Manager) 中实现数据列的权限,附源码
- 使用Python MrJob的MapReduce实现电影推荐系统