PCA实现一个简单的酒店推荐系统(附Python源码)
微信公众号
关键字全网搜索最新排名
【机器学习算法】:排名第一
【机器学习】:排名第二
【Python】:排名第三
【算法】:排名第四
PCA前言
众所周知,PCA 的主要目的是降维,同时也可以起到分类的作用。当数据维度很大的时候,如果相信大部分变量之间存在线性关系,那么我们就希望降低维数,用较少的变量来抓住大部分的信息。(一般来讲做PCA 之前要做normalization 使得变量中心为0,而且方差为1.)比较广泛应用于图像识别,文档处理,推荐系统等。
PCA应用举例
推荐系统
如果一个旅游网站里面有10000000 个注册用户,以及100 个注册酒店. 网站有用户通过本网站点击酒店页面的记录信息.A = [Aij ]10000000100,Aij 表示第i 个用户点击j 酒店的次数.
问题:
如何评价酒店之间的相似度?
给定一个酒店,请找出与它最相似的其他几个酒店?
如果要给酒店分类,有什么办法?
生成100000x100的用户-酒店行为数据
import pandas as pd
import numpy as np
#prepare data set, suppose there are 5 types of hotels
generatorNum=5
hotelNum=100
customerNum=100000
generators = np.random.randint(5, size=(customerNum, generatorNum))
hotelComp=np.random.random(size=(generatorNum, hotelNum)) - 0.5
hotelRating = pd.DataFrame(generators.dot(hotelComp),index=['c%.6d'%i for i in range(100000)]
,columns = ['hotel_%.3d'%j for j in range(100)]).astype(int)
def normalize(s):
if s.std()>1e-6:
return (s-s.mean())*s.std()**(-1)
else:
return (s-s.mean())
hotelRating数据如下所示。
hotelRating_norm = hotelRating.apply(normalize)
hotelRating_norm_corr = hotelRating_norm.cov()
hotelRating_norm_corr如下所示。
u,s,v = np.linalg.svd(hotelRating_norm_corr)
In [9]:
import matplotlib.pyplot as plt
plt.plot(s,'o')
plt.title("singular value spectrum")
plt.show()
In [10]:
u_short = u[:,:5]
v_short = v[:5,:]
s_short = s[:5]
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())
In [11]:
#get the top components
top_components = hotelRating_norm.dot(u_short).dot(np.diag(np.power(s_short,-0.5)))
In [12]:
#classfication of each hotel
hotel_ind = 30
rating = hotelRating_norm.loc[:,'hotel_%.3d'%hotel_ind]
print "classification of the %dth hotel"%hotel_ind
top_components.T.dot(rating)/customerNum
classification of the 30th hotel
0 0.136138
1 0.179586
2 -0.071347
3 -0.384475
4 0.647324
投稿、商业合作
请发邮件到:[email protected]
【机器学习算法与Python学习】联合雷锋网,CCF-GAIR 2017 正在火热预售限量 200 张的六折门票,抢购这批门票的参会者可以享受与全价票同等的福利;此外还有 50 个 VIP 席位虚位以待,除了普通参会门票所享受的权益外,还为 VIP 嘉宾提供了贵宾区座位、VIP 午餐、「最佳 AI 雇主」及「新智造创新榜单」的两场晚宴入场券。可点击「阅读原文」了解更多信息。
最新评论
推荐文章
作者最新文章
你可能感兴趣的文章
Copyright Disclaimer: The copyright of contents (including texts, images, videos and audios) posted above belong to the User who shared or the third-party website which the User shared from. If you found your copyright have been infringed, please send a DMCA takedown notice to [email protected]. For more detail of the source, please click on the button "Read Original Post" below. For other communications, please send to [email protected].
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。