Building Recommender Systems with Python

giahan110

New member
## Xây dựng các hệ thống đề xuất với Python

Các hệ thống đề xuất là một loại thuật toán học máy dự đoán các mục mà người dùng sẽ thích dựa trên hành vi trong quá khứ của họ.Chúng được sử dụng trong một loạt các ứng dụng, chẳng hạn như:

*** Thương mại điện tử: ** Đề xuất sản phẩm cho khách hàng dựa trên các giao dịch mua trước của họ
*** Dịch vụ phát trực tuyến: ** Giới thiệu phim và chương trình truyền hình cho người dùng dựa trên lịch sử xem của họ
*** Phương tiện truyền thông xã hội: ** Đề xuất bạn bè, nhóm và nội dung cho người dùng dựa trên các tương tác của họ

Xây dựng một hệ thống đề xuất có thể là một nhiệm vụ phức tạp, nhưng nó cũng là một nhiệm vụ rất bổ ích.Trong hướng dẫn này, chúng tôi sẽ chỉ cho bạn cách xây dựng một hệ thống đề xuất với Python bằng thư viện [bất ngờ] (https://surprise.readthedocs.io/en/stable/) thư viện.

### 1. Bắt đầu

Bước đầu tiên là cài đặt thư viện bất ngờ.Bạn có thể làm điều này bằng cách sử dụng PIP:

`` `
Pip cài đặt bất ngờ
`` `

Sau khi thư viện được cài đặt, chúng tôi có thể tạo một bộ dữ liệu xếp hạng người dùng.Đối với hướng dẫn này, chúng tôi sẽ sử dụng [Bộ dữ liệu Movielens 100K] (https://grouplens.org/datasets/movielens/100k/).Bộ dữ liệu này chứa 100.000 xếp hạng của 1.682 bộ phim từ 943 người dùng.

Chúng ta có thể tải bộ dữ liệu vào DataFrame Pandas bằng mã sau:

`` `
nhập khẩu gấu trúc dưới dạng PD

df = pd.read_csv ('ml-100k/xếp hạng.csv'))
`` `

DataFrame có các cột sau:

*** userid: ** ID của người dùng đã đưa ra xếp hạng
*** Movieid: ** Id của bộ phim được đánh giá
*** Xếp hạng: ** Xếp hạng mà người dùng đã đưa cho bộ phim (từ 1 đến 5)

### 2. Đào tạo mô hình

Khi chúng tôi có một bộ dữ liệu xếp hạng người dùng, chúng tôi có thể đào tạo một mô hình hệ thống đề xuất.Có nhiều loại hệ thống đề xuất khác nhau, nhưng chúng tôi sẽ sử dụng mô hình [bộ lọc hợp tác] (https://en.wikipedia.org/wiki/collaborative_filtering).

Các mô hình lọc hợp tác hoạt động bằng cách tìm kiếm sự tương đồng giữa người dùng và các mục.Sau đó, họ sử dụng những điểm tương đồng này để dự đoán các mục mà người dùng sẽ thích.

Có nhiều cách khác nhau để đào tạo mô hình lọc hợp tác.Trong hướng dẫn này, chúng tôi sẽ sử dụng [phân tách giá trị số ít (SVD)] (https://en.wikipedia.org/wiki/singular_value_decompation) thuật toán.

Thuật toán SVD hoạt động bằng cách phân tách ma trận xếp hạng mục người dùng thành ba ma trận:

*** u: ** Một ma trận của vectơ người dùng
*** V: ** Một ma trận của các vectơ vật phẩm
*** σ: ** Một ma trận đường chéo của các giá trị số ít

Các vectơ người dùng và vectơ mục sau đó có thể được sử dụng để dự đoán các mục mà người dùng sẽ thích.

Chúng tôi có thể đào tạo mô hình SVD bằng mã sau:

`` `
từ SVD nhập bất ngờ

model = svd ()
model.fit (DF)
`` `

### 3. Đưa ra đề xuất

Khi chúng tôi đã đào tạo một mô hình hệ thống đề xuất, chúng tôi có thể sử dụng nó để đưa ra đề xuất.Để làm điều này, chúng tôi chỉ cần cung cấp cho mô hình ID người dùng và nó sẽ trả về danh sách các mục mà người dùng có thể thích.

Chúng tôi có thể đưa ra đề xuất bằng mã sau:

`` `
user_id = 1

# Nhận danh sách các mục được đề xuất cho người dùng
đề xuất_items = model.ReCommend (user_id, df ['movieId']. độc đáo ())

# In danh sách các mục được đề xuất
in (khuyến nghị_items)
`` `

Đầu ra của mã sẽ là một danh sách các bộ dữ liệu, trong đó mỗi tuple chứa ID của một bộ phim và xếp hạng dự đoán cho bộ phim đó.

### 4. Kết luận

Trong hướng dẫn này, chúng tôi đã chỉ cho bạn cách xây dựng một hệ thống đề xuất với Python bằng thư viện bất ngờ.Chúng tôi đã đề cập đến những điều cơ bản của các hệ thống đề xuất, cách đào tạo mô hình lọc hợp tác và cách đưa ra đề xuất.

Nếu bạn quan tâm đến việc tìm hiểu thêm về các hệ thống đề xuất, có nhiều tài nguyên có sẵn trực tuyến.Dưới đây là một vài liên kết đến
=======================================
## Building Recommender Systems with Python

Recommender systems are a type of machine learning algorithm that predicts the items a user will like based on their past behavior. They are used in a variety of applications, such as:

* **E-commerce:** Recommending products to customers based on their previous purchases
* **Streaming services:** Recommending movies and TV shows to users based on their viewing history
* **Social media:** Recommending friends, groups, and content to users based on their interactions

Building a recommender system can be a complex task, but it is also a very rewarding one. In this tutorial, we will show you how to build a recommender system with Python using the [surprise](https://surprise.readthedocs.io/en/stable/) library.

### 1. Getting Started

The first step is to install the surprise library. You can do this using pip:

```
pip install surprise
```

Once the library is installed, we can create a dataset of user ratings. For this tutorial, we will use the [MovieLens 100K dataset](https://grouplens.org/datasets/movielens/100k/). This dataset contains 100,000 ratings of 1,682 movies from 943 users.

We can load the dataset into a pandas dataframe using the following code:

```
import pandas as pd

df = pd.read_csv('ml-100k/ratings.csv')
```

The dataframe has the following columns:

* **userId:** The ID of the user who made the rating
* **movieId:** The ID of the movie that was rated
* **rating:** The rating that the user gave to the movie (from 1 to 5)

### 2. Training the Model

Once we have a dataset of user ratings, we can train a recommender system model. There are many different types of recommender systems, but we will use a [collaborative filtering](https://en.wikipedia.org/wiki/Collaborative_filtering) model.

Collaborative filtering models work by finding similarities between users and items. They then use these similarities to predict the items that a user will like.

There are many different ways to train a collaborative filtering model. In this tutorial, we will use the [Singular Value Decomposition (SVD)](https://en.wikipedia.org/wiki/Singular_value_decomposition) algorithm.

The SVD algorithm works by decomposing the user-item rating matrix into three matrices:

* **U:** A matrix of user vectors
* **V:** A matrix of item vectors
* **Σ:** A diagonal matrix of singular values

The user vectors and item vectors can then be used to predict the items that a user will like.

We can train a SVD model using the following code:

```
from surprise import SVD

model = SVD()
model.fit(df)
```

### 3. Making Recommendations

Once we have trained a recommender system model, we can use it to make recommendations. To do this, we simply need to provide the model with a user ID and it will return a list of items that the user is likely to like.

We can make recommendations using the following code:

```
user_id = 1

# Get the list of recommended items for the user
recommended_items = model.recommend(user_id, df['movieId'].unique())

# Print the list of recommended items
print(recommended_items)
```

The output of the code will be a list of tuples, where each tuple contains the ID of a movie and the predicted rating for that movie.

### 4. Conclusion

In this tutorial, we showed you how to build a recommender system with Python using the surprise library. We covered the basics of recommender systems, how to train a collaborative filtering model, and how to make recommendations.

If you are interested in learning more about recommender systems, there are many resources available online. Here are a few links to
 
Join ToolsKiemTrieuDoGroup
Back
Top
AdBlock Detected

We get it, advertisements are annoying!

Sure, ad-blocking software does a great job at blocking ads, but it also blocks useful features of our website. For the best site experience please disable your AdBlocker.

I've Disabled AdBlock