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)
												

机器学习14—SVD学习笔记的更多相关文章

  1. 《机器学习实战》学习笔记第十四章 —— 利用SVD简化数据

    相关博客: 吴恩达机器学习笔记(八) —— 降维与主成分分析法(PCA) <机器学习实战>学习笔记第十三章 —— 利用PCA来简化数据 奇异值分解(SVD)原理与在降维中的应用 机器学习( ...

  2. 《机器学习实战》学习笔记第九章 —— 决策树之CART算法

    相关博文: <机器学习实战>学习笔记第三章 —— 决策树 主要内容: 一.CART算法简介 二.分类树 三.回归树 四.构建回归树 五.回归树的剪枝 六.模型树 七.树回归与标准回归的比较 ...

  3. (转载)林轩田机器学习基石课程学习笔记1 — The Learning Problem

    (转载)林轩田机器学习基石课程学习笔记1 - The Learning Problem When Can Machine Learn? Why Can Machine Learn? How Can M ...

  4. Coursera台大机器学习基础课程学习笔记1 -- 机器学习定义及PLA算法

    最近在跟台大的这个课程,觉得不错,想把学习笔记发出来跟大家分享下,有错误希望大家指正. 一机器学习是什么? 感觉和 Tom M. Mitchell的定义几乎一致, A computer program ...

  5. 《SAS编程和数据挖掘商业案例》第14部分学习笔记

    继续<SAS编程与数据挖掘商业案例>学习笔记系列,本次重点:经常使用全程语句 所谓全程语句.是指能够用在不论什么地方的sas语句,既能够用在data数据步语句里面,也能够用在proc过程步 ...

  6. MNIST机器学习入门【学习笔记】

    平台信息:PC:ubuntu18.04.i5.anaconda2.cuda9.0.cudnn7.0.5.tensorflow1.10.GTX1060 作者:庄泽彬(欢迎转载,请注明作者) 说明:本文是 ...

  7. 《机器学习实战》学习笔记——第14章 利用SVD简化数据

    一. SVD 1. 基本概念: (1)定义:提取信息的方法:奇异值分解Singular Value Decomposition(SVD) (2)优点:简化数据, 去除噪声,提高算法的结果 (3)缺点: ...

  8. [转]Python3《机器学习实战》学习笔记(一):k-近邻算法(史诗级干货长文)

    转自http://blog.csdn.net/c406495762/article/details/75172850 版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[-] 一 简 ...

  9. 林轩田机器学习基石课程学习笔记5 — Training versus Testing

    上节课,我们主要介绍了机器学习的可行性.首先,由NFL定理可知,机器学习貌似是不可行的.但是,随后引入了统计学知识,如果样本数据足够大,且hypothesis个数有限,那么机器学习一般就是可行的.本节 ...

随机推荐

  1. Codeforces 570D - Tree Requests(树上启发式合并)

    570D - Tree Requests 题意 给出一棵树,每个节点上有字母,查询 u k,问以 u 为根节点的子树下,深度为 k 的所有子节点上的字母经过任意排列是否能构成回文串. 分析 一个数组 ...

  2. 线段树【 bzoj3132 】【p4145 】上帝造题的七分钟2 / 花神游历各国

    题目大意 给定一个区间 支持开方和查询区间值操作 (多组数据 分析 如果一个区间的最大值小于1,那就没有开方的必要了(具体不会证明,听大佬讲的 一个数经过多次开方就会变成1(可以用计算器试一下 因此我 ...

  3. elasticsearch 插件使用

    5.3.0新版本好像插件和开源的项目没有以前的多,官网就那么几个 常用的先安装Kibana: 提供炫丽的可视化图形展示并且作为elasticsearch的搜索的小清新客户端 1.下载安装包  wget ...

  4. 将本地jar包安装进入maven仓库

    实际项目中pom.xml依赖写法: <dependency> <groupId>org.springframework</groupId> <artifact ...

  5. VMware Converter 迁移物理机到 esxi 虚拟机

    http://dngood.blog.51cto.com/446195/685082/ VMware Converter  有啥用? VMware Converter 是一款能将物理电脑系统.VMwa ...

  6. TQ2440平台上LCD驱动的移植

    参考: http://liu1227787871.blog.163.com/blog/static/205363197201242393031250/ http://blog.csdn.net/cum ...

  7. Qt自己定义事件实现及子线程向主线程传送事件消息

    近期在又一次学习Qt的时候,由于要涉及到子线程与主线程传递消息,所以便琢磨了一下.顺便把有用的记录下来,方便自己以后查询及各位同仁的參考! 特此声明,本篇博文主要讲述有用的,也就是直接说明怎么实现,就 ...

  8. C++之类成员所占内存大小问题总结

    1.空类所占字节数为1,可见代码如下 #include <iostream> using namespace std; class Parent { }; class Child:publ ...

  9. oracle数据库中函数和存储过程中的区别

    一.函数必须有返回值,过程没有返回值: 二.函数可以单独执行,过程必须通过execute执行: 三.函数可以嵌入SQL中执行,过程不能. 可以将比较复杂的查询写成函数,然后在过程中调用.

  10. 编程算法 - 食物链 并查集 代码(C)

    食物链 并查集 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 有N仅仅动物, 分别编号为1,2,...,N. 全部动物都属于A,B,C中的一种 ...