test14.py

#-*- coding:utf-8
import sys
sys.path.append("svdRec.py") import svdRec
from numpy import *
from numpy import linalg as la # U, Sigma, VT = linalg.svd([[1, 1], [7, 7]])
# print(U)
# print(Sigma)
# print(VT) # Data = svdRec.loadExData()
# U, Sigma, VT = linalg.svd(Data)
# print(Sigma)
#
# Sig3 = mat([[Sigma[0], 0, 0], [0, Sigma[1], 0], [0, 0, Sigma[2]]])
# res = U[:, :3]*Sig3*VT[:3, :]
# print("res:")
# print(res)
#
# myMat = mat(svdRec.loadExData())
# ecl = svdRec.ecludSim(myMat[:, 0], myMat[:, 4])
# print("ecl:")
# print(ecl)
# cos = svdRec.cosSim(myMat[:, 0], myMat[:, 4])
# print("cos:")
# print(cos)
# pears = svdRec.pearsSim(myMat[:, 0], myMat[:, 4])
# print("pears:")
# print(pears)
# myMat = mat(svdRec.loadExData())
# myMat[0, 1] = myMat[0, 0] = myMat[1, 0] = myMat[2, 0] = 4
# myMat[3, 3] = 2
# print("myMat:")
# print(myMat)
#
# tuiJian = svdRec.recommend(myMat, 2)
# print("tuiJian:")
# print(tuiJian)
#
# tuiJian1 = svdRec.recommend(myMat, 2, simMeas = svdRec.ecludSim)
# print("tuiJian1:")
# print(tuiJian1)
#
# tuiJian2 = svdRec.recommend(myMat, 2, simMeas = svdRec.pearsSim)
# print("tuiJian2:")
# print(tuiJian2) # myMat = mat(svdRec.loadExData2())
# U, Sigma, VT = la.svd(mat(svdRec.loadExData2()))
# print(Sigma)
#
# Sig2 = Sigma**2
# total = sum(Sig2)
# total9 = total*0.9
# print("total9:")
# print(total9)
#
# total3 = sum(Sig2[:3])
# print("total3:")
# print(total3) # svdRes = svdRec.recommend(myMat, 1, estMethod = svdRec.svdEst)
# print("svdRes:")
# print(svdRes) originalMat = svdRec.imgCompress(2)
print(originalMat) print("over!!!")
svdRec.py
'''
Created on Mar 8, 2011 @author: Peter
'''
from numpy import *
from numpy import linalg as la def loadExData():
return[[0, 0, 0, 2, 2],
[0, 0, 0, 3, 3],
[0, 0, 0, 1, 1],
[1, 1, 1, 0, 0],
[2, 2, 2, 0, 0],
[5, 5, 5, 0, 0],
[1, 1, 1, 0, 0]] def loadExData2():
return[[0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 5],
[0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 3],
[0, 0, 0, 0, 4, 0, 0, 1, 0, 4, 0],
[3, 3, 4, 0, 0, 0, 0, 2, 2, 0, 0],
[5, 4, 5, 0, 0, 0, 0, 5, 5, 0, 0],
[0, 0, 0, 0, 5, 0, 1, 0, 0, 5, 0],
[4, 3, 4, 0, 0, 0, 0, 5, 5, 0, 1],
[0, 0, 0, 4, 0, 4, 0, 0, 0, 0, 4],
[0, 0, 0, 2, 0, 2, 5, 0, 0, 1, 2],
[0, 0, 0, 0, 5, 0, 0, 0, 0, 4, 0],
[1, 0, 0, 0, 0, 0, 0, 1, 2, 0, 0]] def ecludSim(inA,inB):
return 1.0/(1.0 + la.norm(inA - inB)) def pearsSim(inA,inB):
if len(inA) < 3 : return 1.0
return 0.5+0.5*corrcoef(inA, inB, rowvar = 0)[0][1] def cosSim(inA,inB):
num = float(inA.T*inB)
denom = la.norm(inA)*la.norm(inB)
return 0.5+0.5*(num/denom) def standEst(dataMat, user, simMeas, item):
n = shape(dataMat)[1]
simTotal = 0.0; ratSimTotal = 0.0
for j in range(n):
userRating = dataMat[user,j]
if userRating == 0: continue
# test0 = dataMat[:,item].A>0
# test1 = dataMat[:,j].A>0
# test2 = logical_and(dataMat[:,item].A>0, dataMat[:,j].A>0)
overLap = nonzero(logical_and(dataMat[:,item].A>0, dataMat[:,j].A>0))[0]
if len(overLap) == 0: similarity = 0
else: similarity = simMeas(dataMat[overLap,item], dataMat[overLap,j])
print('the %d and %d similarity is: %f' % (item, j, similarity))
simTotal += similarity
ratSimTotal += similarity * userRating
if simTotal == 0: return 0
else: return ratSimTotal/simTotal def svdEst(dataMat, user, simMeas, item):
n = shape(dataMat)[1]
simTotal = 0.0; ratSimTotal = 0.0
U,Sigma,VT = la.svd(dataMat)
Sig4 = mat(eye(4)*Sigma[:4]) #arrange Sig4 into a diagonal matrix
xformedItems = dataMat.T * U[:,:4] * Sig4.I #create transformed items
for j in range(n):
userRating = dataMat[user,j]
if userRating == 0 or j==item: continue
similarity = simMeas(xformedItems[item,:].T, xformedItems[j,:].T)
print('the %d and %d similarity is: %f' % (item, j, similarity))
simTotal += similarity
ratSimTotal += similarity * userRating
if simTotal == 0: return 0
else: return ratSimTotal/simTotal def recommend(dataMat, user, N=3, simMeas=cosSim, estMethod=standEst):
unratedTest = nonzero(dataMat[user,:].A==0)
unratedItems = nonzero(dataMat[user,:].A==0)[1]#find unrated items
if len(unratedItems) == 0: return 'you rated everything'
itemScores = []
for item in unratedItems:
estimatedScore = estMethod(dataMat, user, simMeas, item)
itemScores.append((item, estimatedScore))
# testSort = sorted(itemScores, key=lambda jj: jj[1], reverse=True)[:N]
return sorted(itemScores, key=lambda jj: jj[1], reverse=True)[:N] def printMat(inMat, thresh=0.8):
for i in range(32):
for k in range(32):
if float(inMat[i,k]) > thresh:
print(1),
else: print(0),
print('') def imgCompress(numSV=3, thresh=0.8):
myl = []
for line in open('0_5.txt').readlines():
newRow = []
for i in range(32):
newRow.append(int(line[i]))
myl.append(newRow)
myMat = mat(myl)
print("****original matrix******")
printMat(myMat, thresh)
U,Sigma,VT = la.svd(myMat)
SigRecon = mat(zeros((numSV, numSV)))
for k in range(numSV):#construct diagonal matrix from vector
SigRecon[k,k] = Sigma[k]
reconMat = U[:,:numSV]*SigRecon*VT[:numSV,:]
print("****reconstructed matrix using %d singular values******" % numSV)
printMat(reconMat, thresh)
												

最新文章

  1. [Algorithm] 局部敏感哈希算法(Locality Sensitive Hashing)
  2. HTML元素事件说明
  3. CSS3与页面布局学习总结
  4. Expression Blend制作自定义按钮(转)
  5. Linux下tmpfs介绍及使用
  6. Face++云相册应用IOS源码
  7. 【转】 申请对齐某种结构体大小的buffer
  8. Linux Mono Asp.net 部署方案
  9. 简单的web三层架构系统【第四版】
  10. poj 3311 状压DP
  11. IntelliJ IDEA于Make Project时报:子字符串不是票面金额的结束、非法的表达式显示启动
  12. 微信红包店小程序开发过程中遇到的问题 php获取附近周边商家 显示最近商家
  13. SkiaSharp图像处理
  14. ActiveMQ在Windows下的安装与启动(懒人专属)
  15. 《 Oracle查询优化改写 技巧与案例 》电子工业出版社
  16. python 前端 html
  17. python-线程的暂停, 恢复, 退出
  18. Kafka C++客户端库librdkafka笔记
  19. 解决Eclipse里项目名有红叉,但是底下的每一个文件都没有红叉
  20. java基础11天

热门文章

  1. 洛谷——P1258 小车问题
  2. EasyUI Datagrid 单元格编辑
  3. Number of Connected Components in an Undirected Graph -- LeetCode
  4. Unity3d之MonoBehavior的各个函数的执行顺序,回调,顺序,次数等
  5. Windows 8.1中WinRT的变化(一)——新增控件
  6. linux之网络配置相关
  7. JsonDataObjects序列和还原
  8. kubernetes1.5.2集群部署过程--安全模式
  9. 前端打包利器:webpack工具
  10. Java 中 ConcurrentHashMap 原理分析