用python+mongodb写一个简单的旅游推荐系统
时间: 2024-03-26 20:36:06 浏览: 64
python+vue知识图谱的智能旅游推荐系统源码+文档说明(高分项目)
好的,以下是一个简单的基于Python和MongoDB的旅游推荐系统的示例代码:
1. 安装MongoDB驱动
```
pip install pymongo
```
2. 连接MongoDB数据库
```
from pymongo import MongoClient
client = MongoClient()
db = client['travel_recommendation']
```
3. 创建用户和旅游地点数据集合
```
users = db['users']
places = db['places']
```
4. 插入用户数据
```
user1 = {"name": "Alice", "age": 25, "interests": ["beach", "shopping", "sightseeing"]}
user2 = {"name": "Bob", "age": 30, "interests": ["mountain", "hiking", "photography"]}
users.insert_many([user1, user2])
```
5. 插入旅游地点数据
```
place1 = {"name": "Hawaii", "type": "beach", "rating": 4.5}
place2 = {"name": "Yosemite", "type": "mountain", "rating": 4.2}
places.insert_many([place1, place2])
```
6. 根据用户偏好推荐旅游地点
```
user_interests = users.find_one({"name": "Alice"})['interests']
recommended_places = places.find({"type": {"$in": user_interests}}).sort([("rating", -1)]).limit(5)
for place in recommended_places:
print(place['name'])
```
以上代码是一个简单的旅游推荐系统的示例,根据用户的兴趣爱好推荐旅游地点。实际的旅游推荐系统可能需要考虑更多的因素,并且需要更复杂的算法和数据处理。
阅读全文