


Detailed explanation of collaborative filtering recommendation algorithm implemented in Python
Different data and different programmers write different collaborative filtering recommendation algorithms, but their core is the same. This article mainly introduces the complete code example of implementing collaborative filtering recommendation algorithm in Python, which has certain reference value. Friends who need it can For reference. Hope it helps everyone.
Test data
http://grouplens.org/datasets/movielens/
#!/usr/bin/python3 # -*- coding: utf-8 -*- from numpy import * import time from texttable import Texttable class CF: def __init__(self, movies, ratings, k=5, n=10): self.movies = movies self.ratings = ratings # 邻居个数 self.k = k # 推荐个数 self.n = n # 用户对电影的评分 # 数据格式{'UserID:用户ID':[(MovieID:电影ID,Rating:用户对电影的评星)]} self.userDict = {} # 对某电影评分的用户 # 数据格式:{'MovieID:电影ID',[UserID:用户ID]} # {'1',[1,2,3..],...} self.ItemUser = {} # 邻居的信息 self.neighbors = [] # 推荐列表 self.recommandList = [] self.cost = 0.0 # 基于用户的推荐 # 根据对电影的评分计算用户之间的相似度 def recommendByUser(self, userId): self.formatRate() # 推荐个数 等于 本身评分电影个数,用户计算准确率 self.n = len(self.userDict[userId]) self.getNearestNeighbor(userId) self.getrecommandList(userId) self.getPrecision(userId) # 获取推荐列表 def getrecommandList(self, userId): self.recommandList = [] # 建立推荐字典 recommandDict = {} for neighbor in self.neighbors: movies = self.userDict[neighbor[1]] for movie in movies: if(movie[0] in recommandDict): recommandDict[movie[0]] += neighbor[0] else: recommandDict[movie[0]] = neighbor[0] # 建立推荐列表 for key in recommandDict: self.recommandList.append([recommandDict[key], key]) self.recommandList.sort(reverse=True) self.recommandList = self.recommandList[:self.n] # 将ratings转换为userDict和ItemUser def formatRate(self): self.userDict = {} self.ItemUser = {} for i in self.ratings: # 评分最高为5 除以5 进行数据归一化 temp = (i[1], float(i[2]) / 5) # 计算userDict {'1':[(1,5),(2,5)...],'2':[...]...} if(i[0] in self.userDict): self.userDict[i[0]].append(temp) else: self.userDict[i[0]] = [temp] # 计算ItemUser {'1',[1,2,3..],...} if(i[1] in self.ItemUser): self.ItemUser[i[1]].append(i[0]) else: self.ItemUser[i[1]] = [i[0]] # 找到某用户的相邻用户 def getNearestNeighbor(self, userId): neighbors = [] self.neighbors = [] # 获取userId评分的电影都有那些用户也评过分 for i in self.userDict[userId]: for j in self.ItemUser[i[0]]: if(j != userId and j not in neighbors): neighbors.append(j) # 计算这些用户与userId的相似度并排序 for i in neighbors: dist = self.getCost(userId, i) self.neighbors.append([dist, i]) # 排序默认是升序,reverse=True表示降序 self.neighbors.sort(reverse=True) self.neighbors = self.neighbors[:self.k] # 格式化userDict数据 def formatuserDict(self, userId, l): user = {} for i in self.userDict[userId]: user[i[0]] = [i[1], 0] for j in self.userDict[l]: if(j[0] not in user): user[j[0]] = [0, j[1]] else: user[j[0]][1] = j[1] return user # 计算余弦距离 def getCost(self, userId, l): # 获取用户userId和l评分电影的并集 # {'电影ID':[userId的评分,l的评分]} 没有评分为0 user = self.formatuserDict(userId, l) x = 0.0 y = 0.0 z = 0.0 for k, v in user.items(): x += float(v[0]) * float(v[0]) y += float(v[1]) * float(v[1]) z += float(v[0]) * float(v[1]) if(z == 0.0): return 0 return z / sqrt(x * y) # 推荐的准确率 def getPrecision(self, userId): user = [i[0] for i in self.userDict[userId]] recommand = [i[1] for i in self.recommandList] count = 0.0 if(len(user) >= len(recommand)): for i in recommand: if(i in user): count += 1.0 self.cost = count / len(recommand) else: for i in user: if(i in recommand): count += 1.0 self.cost = count / len(user) # 显示推荐列表 def showTable(self): neighbors_id = [i[1] for i in self.neighbors] table = Texttable() table.set_deco(Texttable.HEADER) table.set_cols_dtype(["t", "t", "t", "t"]) table.set_cols_align(["l", "l", "l", "l"]) rows = [] rows.append([u"movie ID", u"Name", u"release", u"from userID"]) for item in self.recommandList: fromID = [] for i in self.movies: if i[0] == item[1]: movie = i break for i in self.ItemUser[item[1]]: if i in neighbors_id: fromID.append(i) movie.append(fromID) rows.append(movie) table.add_rows(rows) print(table.draw()) # 获取数据 def readFile(filename): files = open(filename, "r", encoding="utf-8") # 如果读取不成功试一下 # files = open(filename, "r", encoding="iso-8859-15") data = [] for line in files.readlines(): item = line.strip().split("::") data.append(item) return data # -------------------------开始------------------------------- start = time.clock() movies = readFile("/home/hadoop/Python/CF/movies.dat") ratings = readFile("/home/hadoop/Python/CF/ratings.dat") demo = CF(movies, ratings, k=20) demo.recommendByUser("100") print("推荐列表为:") demo.showTable() print("处理的数据为%d条" % (len(demo.ratings))) print("准确率: %.2f %%" % (demo.cost * 100)) end = time.clock() print("耗费时间: %f s" % (end - start))
Summary
and above That’s all the content of this article about the complete code example of implementing collaborative filtering recommendation algorithm in python. I hope it will be helpful to everyone. Related recommendations:implementation of php+mysql collaborative filtering algorithm
Tutorial on implementing collaborative filtering in Python
The above is the detailed content of Detailed explanation of collaborative filtering recommendation algorithm implemented in Python. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".
