mirror of
https://github.com/apachecn/ailearning.git
synced 2026-05-07 14:13:14 +08:00
@@ -1,6 +1,8 @@
|
||||
|
||||
# 第 10 章 K-Means(K-均值)聚类算法
|
||||
|
||||

|
||||
|
||||
## K-Means 算法
|
||||
聚类是一种无监督的学习, 它将相似的对象归到一个簇中, 将不相似对象归到不同簇中.
|
||||
相似这一概念取决于所选择的相似度计算方法.
|
||||
@@ -195,4 +197,8 @@ def biKMeans(dataSet, k, distMeas=distEclud):
|
||||
|
||||
上述函数可以运行多次,聚类会收敛到全局最小值,而原始的 kMeans() 函数偶尔会陷入局部最小值。
|
||||
运行参考结果如下:
|
||||

|
||||

|
||||
|
||||
* **作者:[那伊抹微笑](http://www.apache.wiki/display/~xuxin)**
|
||||
* [GitHub地址](https://github.com/apachecn/MachineLearning): <https://github.com/apachecn/MachineLearning>
|
||||
* **版权声明:欢迎转载学习 => 请标注信息来源于 [ApacheCN](http://www.apachecn.org/)**
|
||||
@@ -142,34 +142,44 @@
|
||||
|
||||
`假如一个人在家决定外出吃饭,但是他并不知道该到哪儿去吃饭,该点什么菜。推荐系统可以帮他做到这两点。`
|
||||
|
||||
|
||||
#### 开发流程
|
||||
|
||||
> 收集 并 准备数据
|
||||
|
||||

|
||||
|
||||
```python
|
||||
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 loadExData3():
|
||||
# 利用SVD提高推荐效果,菜肴矩阵
|
||||
"""
|
||||
行:代表人
|
||||
列:代表菜肴名词
|
||||
值:代表人对菜肴的评分,0表示未评分
|
||||
"""
|
||||
return[[2, 0, 0, 4, 4, 0, 0, 0, 0, 0, 0],
|
||||
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5],
|
||||
[0, 0, 0, 0, 0, 0, 0, 1, 0, 4, 0],
|
||||
[3, 3, 4, 0, 3, 0, 0, 2, 2, 0, 0],
|
||||
[5, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||
[0, 0, 0, 0, 0, 0, 5, 0, 0, 5, 0],
|
||||
[4, 0, 4, 0, 0, 0, 0, 0, 0, 0, 5],
|
||||
[0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 4],
|
||||
[0, 0, 0, 0, 0, 0, 5, 0, 0, 5, 0],
|
||||
[0, 0, 0, 3, 0, 0, 0, 0, 4, 5, 0],
|
||||
[1, 1, 2, 1, 1, 2, 1, 0, 4, 5, 0]]
|
||||
```
|
||||
|
||||
> 分析数据: 暂时不需要
|
||||
> 分析数据: 这里不做过多的讨论(当然此处可以对比不同距离之间的差别)
|
||||
|
||||
> 训练算法: 通过调用 recommend() 函数进行推荐
|
||||
|
||||
* 基于物品相似度(参考地址:http://www.codeweblog.com/svd-%E7%AC%94%E8%AE%B0/)
|
||||
recommend() 会调用 基于物品相似度 或者是 基于SVD,得到推荐的物品评分。
|
||||
|
||||

|
||||
* 1.基于物品相似度
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
```python
|
||||
# 基于物品相似度的推荐引擎
|
||||
@@ -217,7 +227,7 @@ def standEst(dataMat, user, simMeas, item):
|
||||
return ratSimTotal/simTotal
|
||||
```
|
||||
|
||||
* 基于SVD(参考地址:http://www.codeweblog.com/svd-%E7%AC%94%E8%AE%B0/)
|
||||
* 2.基于SVD(参考地址:http://www.codeweblog.com/svd-%E7%AC%94%E8%AE%B0/)
|
||||
|
||||

|
||||
|
||||
@@ -295,7 +305,7 @@ def recommend(dataMat, user, N=3, simMeas=cosSim, estMethod=standEst):
|
||||
return sorted(itemScores, key=lambda jj: jj[1], reverse=True)[: N]
|
||||
```
|
||||
|
||||
> 测试 和 使用 该算法,可以自行编写
|
||||
> 测试 和 项目调用,可直接参考我们的代码
|
||||
|
||||
[完整代码地址](https://github.com/apachecn/MachineLearning/blob/master/src/python/14.SVD/svdRecommend.py): <https://github.com/apachecn/MachineLearning/blob/master/src/python/14.SVD/svdRecommend.py>
|
||||
|
||||
@@ -312,7 +322,7 @@ def recommend(dataMat, user, N=3, simMeas=cosSim, estMethod=standEst):
|
||||
问题
|
||||
* 1)在大规模的数据集上,SVD分解会降低程序的速度
|
||||
* 2)存在其他很多规模扩展性的挑战性问题,比如矩阵的表示方法和计算相似度得分消耗资源。
|
||||
* 3)如何在缺乏数据时给出好的推荐-称为冷启动【简单说:用户不会喜欢一个无效的物品,而用户不喜欢的物品又无效。】
|
||||
* 3)如何在缺乏数据时给出好的推荐-称为冷启动【简单说:用户不会喜欢一个无效的物品,而用户不喜欢的物品又无效】
|
||||
|
||||
建议
|
||||
* 1)在大型系统中,SVD分解(可以在程序调入时运行一次)每天运行一次或者其频率更低,并且还要离线运行。
|
||||
@@ -342,7 +352,7 @@ def imgLoadData(filename):
|
||||
|
||||
> 分析数据: 分析 Sigma 的长度个数
|
||||
|
||||
通常保留矩阵 80% ~ 90% 的能量,就可以得到重要的特征并取出噪声。
|
||||
通常保留矩阵 80% ~ 90% 的能量,就可以得到重要的特征并去除噪声。
|
||||
|
||||
```python
|
||||
def analyse_data(Sigma, loopNum=20):
|
||||
@@ -367,6 +377,9 @@ def analyse_data(Sigma, loopNum=20):
|
||||
|
||||
> 使用算法: 对比使用 SVD 前后的数据差异对比,对于存储大家可以试着写写
|
||||
|
||||
|
||||
例如:`32*32=1024 => 32*2+2*1+32*2=130`(2*1表示去掉了除对角线的0), 几乎获得了10倍的压缩比。
|
||||
|
||||
```python
|
||||
# 打印矩阵
|
||||
def printMat(inMat, thresh=0.8):
|
||||
|
||||
BIN
images/10.KMeans/K-Means_首页.jpg
Normal file
BIN
images/10.KMeans/K-Means_首页.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 153 KiB |
BIN
images/14.SVD/基于物品相似度.jpg
Normal file
BIN
images/14.SVD/基于物品相似度.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 26 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 10 KiB |
BIN
images/14.SVD/欧式距离的计算方式.jpg
Normal file
BIN
images/14.SVD/欧式距离的计算方式.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 64 KiB |
BIN
images/14.SVD/项目数据导入.jpg
Normal file
BIN
images/14.SVD/项目数据导入.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 64 KiB |
@@ -303,15 +303,15 @@ def imgCompress(numSV=3, thresh=0.8):
|
||||
if __name__ == "__main__":
|
||||
|
||||
# # 对矩阵进行SVD分解(用python实现SVD)
|
||||
Data = loadExData()
|
||||
print 'Data:', Data
|
||||
U, Sigma, VT = linalg.svd(Data)
|
||||
# Data = loadExData()
|
||||
# print 'Data:', Data
|
||||
# U, Sigma, VT = linalg.svd(Data)
|
||||
# # 打印Sigma的结果,因为前3个数值比其他的值大了很多,为9.72140007e+00,5.29397912e+00,6.84226362e-01
|
||||
# # 后两个值比较小,每台机器输出结果可能有不同可以将这两个值去掉
|
||||
print 'U:', U
|
||||
print 'Sigma', Sigma
|
||||
print 'VT:', VT
|
||||
print 'VT:', VT.T
|
||||
# print 'U:', U
|
||||
# print 'Sigma', Sigma
|
||||
# print 'VT:', VT
|
||||
# print 'VT:', VT.T
|
||||
|
||||
# # 重构一个3x3的矩阵Sig3
|
||||
# Sig3 = mat([[Sigma[0], 0, 0], [0, Sigma[1], 0], [0, 0, Sigma[2]]])
|
||||
@@ -335,15 +335,15 @@ if __name__ == "__main__":
|
||||
"""
|
||||
|
||||
# 计算相似度的方法
|
||||
# myMat = mat(loadExData2())
|
||||
myMat = mat(loadExData3())
|
||||
# print myMat
|
||||
# 计算相似度的第一种方式
|
||||
# print recommend(myMat, 1, estMethod=svdEst)
|
||||
print recommend(myMat, 1, estMethod=svdEst)
|
||||
# 计算相似度的第二种方式
|
||||
# print recommend(myMat, 1, estMethod=svdEst, simMeas=pearsSim)
|
||||
print recommend(myMat, 1, estMethod=svdEst, simMeas=pearsSim)
|
||||
|
||||
# 默认推荐(菜馆菜肴推荐示例)
|
||||
# print recommend(myMat, 2)
|
||||
print recommend(myMat, 2)
|
||||
|
||||
"""
|
||||
# 利用SVD提高推荐效果
|
||||
|
||||
Reference in New Issue
Block a user