基于模型融合的推荐系统实现(3):模型融合
2016-11-29 19:19
519 查看
基本思路很简单,最小二乘法就好了:
我们假设两个算法得到的结果权重分别是a,b利用最小二乘法和我们分出来的第二部分数据就可以获取a,b使得误差最小。其实最小二乘法就是求一个广义的逆即可。最后的RMSE比起单一的模型有所提高,变成了(0.86~~~~)
import numpy as np from code import read_file from SVD import construct_matrix,get_svd_predict,svd_predict,get_mean from PCA import get_train,get_pq,predict def merge_models(index,mean=0): train = get_train(path=r'smaller_test.txt')#x p,q,bu,bi = get_pq(index=index) svd_predictions = get_svd_predict(index) A = np.zeros((len(train),2)) Y = np.zeros((len(train))) for user_id,iid,r in read_file(r'smaller_test.txt'): pr_lfm = predict(user_id,iid,p,q,bu,bi) pr_svd = svd_predict(user_id,iid,svd_predictions) pr_lfm = pr_lfm if pr_lfm else 3 A[user_id-1] = np.array([pr_lfm,pr_svd+mean]) Y[user_id-1] = np.array([r]) alpha_mat = np.dot(np.dot(np.linalg.inv(np.dot(np.transpose(A), A)), np.transpose(A)), Y) return alpha_mat def REMS(index,mean=0): svd_predictions = get_svd_predict(index) p,q,bu,bi = get_pq(index=index) error = 0 cnt = 0 alpha1,alpha2 = merge_models(index,mean=mean) cutoff = {1:-0.8,2:-1,3:0,4:0.3,5:0} for uid,iid in read_file(r'test.txt',num = 2): pr_lfm = predict(uid,iid,p,q,bu,bi) pr_svd = svd_predict(uid,iid,svd_predictions) if pr_lfm == None or pr_svd==None:continue pr = alpha1*pr_lfm+alpha2*(pr_svd+mean) if pr<1:pr = 1.0 if pr>5:pr = 5.0 cnt += 1 yield pr def write_ans(w_path,data): with open(w_path,'w'): pass with open(w_path,'a') as file: for r in data: file.write('{0:.3f}\n'.format(r)) if __name__ == '__main__': #merge_models() train = get_train(path=r'smaller_test.txt') a,b,mean = get_mean(train) write_ans('scores-5.txt',REMS(5,mean)) write_ans('scores-20.txt',REMS(20,mean)) write_ans('scores-50.txt',REMS(50,mean))
相关文章推荐
- 基于模型融合的推荐系统实现(2):迭代式SVD分解
- 基于模型融合的推荐系统实现(1):基于SGD的PMF
- 在centos6系统上基于session绑定实现nat模型的lvs集群,并在RS上安装Discuz论坛 推荐
- 基于spark的多模型融合的推荐系统
- 基于Spark MLlib平台和基于模型的协同过滤算法的电影推荐系统(二)代码实现
- Scikit-learn实现基于模型的推荐系统(SVD)
- 基于RBAC模型的权限管理系统的设计和实现
- 基于RBAC模型的权限管理系统的设计和实现
- 基于协同过滤算法的推荐系统实现
- 基于Cobbler实现多版本系统批量部署 推荐
- 推荐系统之基于二部图的个性化推荐系统原理及C++实现
- 用Hadoop流实现mapreduce版推荐系统基于物品的协同过滤算法
- 推荐系统之基于二部图的个性化推荐系统原理及C++实现
- 基于Spark平台的电影推荐系统实现
- RedHat 5.4+ Postfix +Extmail实现基于虚拟用户的邮件系统(一) 推荐
- 推荐系统之基于二部图的个性化推荐系统原理及C++实现
- 基于RBAC扩展模型的实验室综合管理系统设计与实现
- 基于奇异值分解(SVD)的推荐系统算法实现
- 基于lvs的DR模型搭建高可用的web服务,并部署wordpress,(附有脚本实现健康状态检测的代码) 推荐
- 基于RBAC模型的权限管理系统的设计和实现