building a recommender system

I’m building a recommender system based of a Netflix dataset. I seem to be having trouble with my reccomender function.
It seems to be taking long to run & it might just time out on me.

So far this is the code I have:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code># Function to get collaborative recommendations for a user
def get_collaborative_recommendations(user_id, svd, movies_df, ratings_df, n=10):
recommendations = []
rated_movie_ids = ratings_df[ratings_df['userId'] == user_id]['movieId'].unique()
movie_ids = [m for m in movies_df['movieId'].unique() if m not in rated_movie_ids]
for movie_id in movie_ids:
prediction = svd.predict(user_id, movie_id)
recommendations.append((movie_id, prediction.est))
recommendations = sorted(recommendations, key=lambda x: x[1], reverse=True)
top_recommendations = recommendations[:n]
recommendations_info = [
(movies_df[movies_df['movieId'] == movie_id]['title'].values[0], est_rating)
for movie_id, est_rating in top_recommendations
]
return recommendations_info
# Function to generate recommendations for a given user (for parallel processing)
def generate_user_recommendations(user_id):
recommendations = get_collaborative_recommendations(user_id, svd, movies_df, train_df)
user_recommendations = []
for title, predicted_rating in recommendations:
user_recommendations.append({
'userId': user_id,
'Recommended Movie': title,
'Predicted_Rating': predicted_rating
})
return user_recommendations
# Get unique user IDs from the train dataset
user_ids = train_df['userId'].unique()
# Use parallel processing to generate recommendations for all users
with Pool() as pool:
all_recommendations = pool.map(generate_user_recommendations, user_ids)
# Flatten the list of lists
all_recommendations = [rec for sublist in all_recommendations for rec in sublist]
# Convert the list of recommendations to a DataFrame
recommendations_df = pd.DataFrame(all_recommendations)
# Display the recommendations DataFrame
print(recommendations_df.head())```
The function itself is taking long to run, so I'm not sure if it's the fact that I need to have 5 million data points.
I'm still new to this, this is my first real world project.
</code>
<code># Function to get collaborative recommendations for a user def get_collaborative_recommendations(user_id, svd, movies_df, ratings_df, n=10): recommendations = [] rated_movie_ids = ratings_df[ratings_df['userId'] == user_id]['movieId'].unique() movie_ids = [m for m in movies_df['movieId'].unique() if m not in rated_movie_ids] for movie_id in movie_ids: prediction = svd.predict(user_id, movie_id) recommendations.append((movie_id, prediction.est)) recommendations = sorted(recommendations, key=lambda x: x[1], reverse=True) top_recommendations = recommendations[:n] recommendations_info = [ (movies_df[movies_df['movieId'] == movie_id]['title'].values[0], est_rating) for movie_id, est_rating in top_recommendations ] return recommendations_info # Function to generate recommendations for a given user (for parallel processing) def generate_user_recommendations(user_id): recommendations = get_collaborative_recommendations(user_id, svd, movies_df, train_df) user_recommendations = [] for title, predicted_rating in recommendations: user_recommendations.append({ 'userId': user_id, 'Recommended Movie': title, 'Predicted_Rating': predicted_rating }) return user_recommendations # Get unique user IDs from the train dataset user_ids = train_df['userId'].unique() # Use parallel processing to generate recommendations for all users with Pool() as pool: all_recommendations = pool.map(generate_user_recommendations, user_ids) # Flatten the list of lists all_recommendations = [rec for sublist in all_recommendations for rec in sublist] # Convert the list of recommendations to a DataFrame recommendations_df = pd.DataFrame(all_recommendations) # Display the recommendations DataFrame print(recommendations_df.head())``` The function itself is taking long to run, so I'm not sure if it's the fact that I need to have 5 million data points. I'm still new to this, this is my first real world project. </code>
# Function to get collaborative recommendations for a user
def get_collaborative_recommendations(user_id, svd, movies_df, ratings_df, n=10):
    recommendations = []
    rated_movie_ids = ratings_df[ratings_df['userId'] == user_id]['movieId'].unique()
    movie_ids = [m for m in movies_df['movieId'].unique() if m not in rated_movie_ids]

    for movie_id in movie_ids:
        prediction = svd.predict(user_id, movie_id)
        recommendations.append((movie_id, prediction.est))

    recommendations = sorted(recommendations, key=lambda x: x[1], reverse=True)

    top_recommendations = recommendations[:n]
    recommendations_info = [
        (movies_df[movies_df['movieId'] == movie_id]['title'].values[0], est_rating)
        for movie_id, est_rating in top_recommendations
    ]

    return recommendations_info

# Function to generate recommendations for a given user (for parallel processing)
def generate_user_recommendations(user_id):
    recommendations = get_collaborative_recommendations(user_id, svd, movies_df, train_df)
    user_recommendations = []
    for title, predicted_rating in recommendations:
        user_recommendations.append({
            'userId': user_id,
            'Recommended Movie': title,
            'Predicted_Rating': predicted_rating
        })
    return user_recommendations

# Get unique user IDs from the train dataset
user_ids = train_df['userId'].unique()

# Use parallel processing to generate recommendations for all users
with Pool() as pool:
    all_recommendations = pool.map(generate_user_recommendations, user_ids)

# Flatten the list of lists
all_recommendations = [rec for sublist in all_recommendations for rec in sublist]

# Convert the list of recommendations to a DataFrame
recommendations_df = pd.DataFrame(all_recommendations)

# Display the recommendations DataFrame
print(recommendations_df.head())```

The function itself is taking long to run, so I'm not sure if it's the fact that I need to have 5 million data points.

I'm still new to this, this is my first real world project.

New contributor

Glen Wallace is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật