鱼C论坛

 找回密码
 立即注册
查看: 1508|回复: 1

[技术交流] Python实现SVD

[复制链接]
发表于 2020-11-19 21:18:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
参考书籍:《机器学习实战》


  1. import numpy as np
  2. from numpy import linalg as la

  3. def loadExData():
  4.     return[[0, 0, 0, 2, 2],
  5.            [0, 0, 0, 3, 3],
  6.            [0, 0, 0, 1, 1],
  7.            [1, 1, 1, 0, 0],
  8.            [2, 2, 2, 0, 0],
  9.            [5, 5, 5, 0, 0],
  10.            [1, 1, 1, 0, 0]]
  11.    
  12. def loadExData2():
  13.     return[[0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 5],
  14.            [0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 3],
  15.            [0, 0, 0, 0, 4, 0, 0, 1, 0, 4, 0],
  16.            [3, 3, 4, 0, 0, 0, 0, 2, 2, 0, 0],
  17.            [5, 4, 5, 0, 0, 0, 0, 5, 5, 0, 0],
  18.            [0, 0, 0, 0, 5, 0, 1, 0, 0, 5, 0],
  19.            [4, 3, 4, 0, 0, 0, 0, 5, 5, 0, 1],
  20.            [0, 0, 0, 4, 0, 4, 0, 0, 0, 0, 4],
  21.            [0, 0, 0, 2, 0, 2, 5, 0, 0, 1, 2],
  22.            [0, 0, 0, 0, 5, 0, 0, 0, 0, 4, 0],
  23.            [1, 0, 0, 0, 0, 0, 0, 1, 2, 0, 0]]

  24. def eulidSim(inA, inB): #欧式距离
  25.     return 1 / (1 + la.norm(inA - inB))

  26. def pearsSim(inA, inB):
  27.     if len(inA) < 3:
  28.         return 1#??
  29.     return 1 / (1 + la.norm(inA - inB))

  30. def cosSim(inA, inB):
  31.     num = float(inA.T * inB)
  32.     denom = la.norm(inA) * la.norm(inB)
  33.     return 0.5 + 0.5*(num / denom)

  34. #基于物品相似度的推荐系统
  35. def standEst(dataMat, user, simMeas, item):
  36.     n = np.shape(dataMat)[1]
  37.     simTotal = 0
  38.     ratSimTotal = 0
  39.     for j in range(n):
  40.         userRating = dataMat[user, j]
  41.         if userRating == 0 or j==item:
  42.             continue
  43.         #https://blog.csdn.net/jningwei/article/details/78651535
  44.         overLap = np.nonzero(np.logical_and(dataMat[:, item] > 0,
  45.                                          dataMat[:, j] > 0))[0]#必须都有评分记录
  46.         if len(overLap) == 0:
  47.             similarity = 0
  48.         else:
  49.             similarity = simMeas(dataMat[overLap, item],
  50.                                  dataMat[overLap, j])
  51.             simTotal += similarity
  52.             ratSimTotal += similarity * userRating
  53.     if simTotal == 0:
  54.         return 0
  55.     return ratSimTotal / simTotal

  56. def svdEst(dataMat, user, simMeas, item):
  57.     n = np.shape(dataMat)[1]
  58.     simTotal = 0.0; ratSimTotal = 0.0
  59.     U,Sigma,VT = la.svd(dataMat)
  60.     Sig4 = np.mat(np.eye(4)*Sigma[:4])
  61.     xformedItems = dataMat.T * U[:,:4] * Sig4.I
  62.     for j in range(n):
  63.         userRating = dataMat[user,j]
  64.         if userRating == 0 or j==item:
  65.             continue
  66.         similarity = simMeas(xformedItems[item,:].T,\
  67.                              xformedItems[j,:].T)
  68.         simTotal += similarity
  69.         ratSimTotal += similarity * userRating
  70.     if simTotal == 0:
  71.         return 0
  72.     return ratSimTotal/simTotal
  73.    
  74.    
  75. def recommend(dataMat, user, N = 3, simMeas = cosSim, estMethod = standEst):
  76.     unratedItems = np.nonzero(dataMat[user,:] == 0)[1]
  77.     if len(unratedItems) == 0:
  78.         return 'you rated everything'
  79.     itemScores = []
  80.     for item in unratedItems:
  81.         estimatedScore = estMethod(dataMat, user, simMeas, item)
  82.         itemScores.append((item, estimatedScore))
  83.     return sorted(itemScores, key = lambda jj: jj[1], reverse = True)[:N]


  84. if __name__ == '__main__':
  85.        myMat = np.mat(loadExData())
  86.        myMat[0, 1] = myMat[0, 0] = myMat[1, 0] = myMat[2, 0] = 4
  87.        myMat[3, 3] = 2
  88.        res1 = recommend(myMat, 2)
  89.        res2 =recommend(myMat, 2, estMethod = svdEst)
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-11-19 21:19:12 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 19:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表