The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Create a “who to follow” engine

A “Who to Follow” engine is a recommendation system designed to suggest relevant people or accounts to users based on various factors such as interests, behavior, network connections, or content engagement. Below is a conceptual breakdown and a simplified implementation using Python, ideal for a basic prototype or MVP.


1. Core Recommendation Logic

A. Data Requirements

  • User Profiles: user_id, interests, bio, location

  • User Connections: followers, following

  • Content Engagement: Likes, comments, shares

  • Tags or Topics: Followed or interacted topics


2. Recommendation Strategies

You can use a combination of the following:

  • Collaborative Filtering: Recommend users who are followed by similar users.

  • Content-Based Filtering: Recommend users who share common interests or post similar content.

  • Graph-Based (Social Graph): Recommend friends of friends.

  • Popularity-Based: Suggest trending or popular users within a domain.


3. Simple Python Implementation (Content + Social Graph Based)

python
import pandas as pd from collections import Counter from sklearn.metrics.pairwise import cosine_similarity from sklearn.feature_extraction.text import TfidfVectorizer # Example Data users = pd.DataFrame([ {'user_id': 1, 'bio': 'Tech enthusiast, Python developer', 'interests': 'Python,AI,Tech'}, {'user_id': 2, 'bio': 'Food blogger and photographer', 'interests': 'Food,Blogging,Photography'}, {'user_id': 3, 'bio': 'AI researcher and data scientist', 'interests': 'AI,Machine Learning,Python'}, {'user_id': 4, 'bio': 'Nature lover and travel writer', 'interests': 'Travel,Nature,Writing'}, {'user_id': 5, 'bio': 'Startup founder and coder', 'interests': 'Tech,Startups,Coding'} ]) # Simulate user follows follows = { 1: [3, 5], # user 1 follows user 3 and 5 2: [], 3: [1], 4: [], 5: [1, 3] } def generate_recommendations(target_user_id, top_n=3): # Get users not followed yet all_users = set(users['user_id']) followed_users = set(follows.get(target_user_id, [])) to_consider = list(all_users - followed_users - {target_user_id}) # Combine bio and interests for similarity users['profile'] = users['bio'] + ' ' + users['interests'] vectorizer = TfidfVectorizer() tfidf_matrix = vectorizer.fit_transform(users['profile']) # Find the target user index target_index = users[users['user_id'] == target_user_id].index[0] # Compute cosine similarity similarities = cosine_similarity(tfidf_matrix[target_index], tfidf_matrix).flatten() # Rank based on similarity sim_scores = list(zip(users['user_id'], similarities)) sim_scores = [u for u in sim_scores if u[0] in to_consider] sim_scores.sort(key=lambda x: x[1], reverse=True) # Return top N user_ids return [user_id for user_id, score in sim_scores[:top_n]] # Example Usage recommendations = generate_recommendations(target_user_id=1, top_n=3) print("Recommended users to follow:", recommendations)

4. Enhancements for Production Use

A. Machine Learning Models

Use collaborative filtering libraries like:

  • Surprise (for explicit/implicit ratings)

  • LightFM for hybrid recommendations

B. Graph Algorithms

Implement Personalized PageRank, HITS, or Community Detection to discover influential or contextually relevant users.

C. Real-time Personalization

Cache top recommendations and refresh dynamically based on:

  • Latest activity

  • Trending topics

  • Followed/following churn

D. Backend & API Integration

  • REST API or GraphQL to serve recommendations

  • Redis for caching frequent queries

  • Cron jobs for periodic model updates


5. Ranking Factors (Weights can be tuned)

FactorDescriptionWeight (Example)
Mutual connectionsCommon followers/followings0.4
Content similarityTF-IDF/NLP similarity0.3
Engagement overlapLikes/comments on same content0.2
Account popularityFollower count0.1

6. Final Output Format

For UI/UX integration, recommendations can include:

json
[ { "user_id": 3, "username": "ai_scientist", "bio": "AI researcher and data scientist", "mutual_friends": 2 }, { "user_id": 5, "username": "startup_guru", "bio": "Startup founder and coder", "mutual_friends": 1 } ]

Let me know if you’d like a version tailored for a specific framework (e.g., Django, Flask, Node.js, etc.) or with database integration (e.g., PostgreSQL, Neo4j, MongoDB).

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About