Skip to content

Item Profiling

ItemProfilingRecommender

A recommender system that builds and maintains detailed item profiles for context-aware recommendations.

This recommender system creates comprehensive profiles for items by analyzing their features, usage patterns, and performance across different contexts. It uses these profiles to make more accurate recommendations based on contextual similarities.

Attributes:

Name Type Description
item_profiles Dict[int, Dict]

Detailed profiles for each item containing: - Static features (inherent item characteristics) - Dynamic features (usage patterns, ratings) - Contextual performance metrics

context_performance Dict[int, Dict]

Performance metrics for items in different contexts

feature_importance Dict[str, float]

Learned importance of different features

Methods:

Name Description
build_profile

Creates or updates an item's profile

update_context_performance

Updates item performance metrics for specific contexts

get_context_similarity

Calculates similarity between contexts

recommend

Generates recommendations using item profiles and current context

Example

profiler = ItemProfilingRecommender() profiler.build_profile(item_id=123, features={...}, context_data={...}) recommendations = profiler.recommend(user_id=456, context={"time": "evening"})

Source code in engines/contentFilterEngine/context_personalization/item_profiling.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class ItemProfilingRecommender:
    """
    A recommender system that builds and maintains detailed item profiles for context-aware recommendations.

    This recommender system creates comprehensive profiles for items by analyzing their features,
    usage patterns, and performance across different contexts. It uses these profiles to make
    more accurate recommendations based on contextual similarities.

    Attributes:
        item_profiles (Dict[int, Dict]): Detailed profiles for each item containing:
            - Static features (inherent item characteristics)
            - Dynamic features (usage patterns, ratings)
            - Contextual performance metrics
        context_performance (Dict[int, Dict]): Performance metrics for items in different contexts
        feature_importance (Dict[str, float]): Learned importance of different features

    Methods:
        build_profile: Creates or updates an item's profile
        update_context_performance: Updates item performance metrics for specific contexts
        get_context_similarity: Calculates similarity between contexts
        recommend: Generates recommendations using item profiles and current context

    Example:
        >>> profiler = ItemProfilingRecommender()
        >>> profiler.build_profile(item_id=123, features={...}, context_data={...})
        >>> recommendations = profiler.recommend(user_id=456, context={"time": "evening"})
    """
    def __init__(self):
        """
        Initialize the item profiling recommender.
        """
        self.item_profiles: Dict[int, Dict[str, Any]] = {}

    def fit(self, data: Dict[int, List[int]], item_features: Dict[int, Dict[str, Any]]):
        """
        Train the recommender system by building item profiles.

        Parameters:
        - data (dict): The data used for training the model, containing user interactions.
        - item_features (dict): A dictionary mapping item IDs to their features.
        """
        # Example: Count-based item profiling
        for user_id, items in data.items():
            for item_id in items:
                if item_id not in self.item_profiles:
                    self.item_profiles[item_id] = {}
                for feature, value in item_features.get(item_id, {}).items():
                    self.item_profiles[item_id][feature] = self.item_profiles[item_id].get(feature, 0) + 1

    def recommend(self, query: str, top_n: int = 10) -> List[int]:
        """
        Recommend items based on the similarity of the query to the documents.

        Parameters:
        - query (str): The query text for which to generate recommendations.
        - top_n (int): Number of top recommendations to return.

        Returns:
        - List[int]: List of recommended item indices.
        """
        logger.info("Generating recommendations using LSA.")
        query_vec = self.transform([query])
        doc_vecs = self.lsa_model.transform(self.vectorizer.transform(self.vectorizer.get_feature_names_out()))
        similarity_scores = (doc_vecs @ query_vec.T).flatten()
        top_indices = similarity_scores.argsort()[::-1][:top_n]
        logger.info(f"Top {top_n} recommendations generated using LSA.")
        return top_indices.tolist()

__init__()

Initialize the item profiling recommender.

Source code in engines/contentFilterEngine/context_personalization/item_profiling.py
34
35
36
37
38
def __init__(self):
    """
    Initialize the item profiling recommender.
    """
    self.item_profiles: Dict[int, Dict[str, Any]] = {}

fit(data, item_features)

Train the recommender system by building item profiles.

Parameters: - data (dict): The data used for training the model, containing user interactions. - item_features (dict): A dictionary mapping item IDs to their features.

Source code in engines/contentFilterEngine/context_personalization/item_profiling.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def fit(self, data: Dict[int, List[int]], item_features: Dict[int, Dict[str, Any]]):
    """
    Train the recommender system by building item profiles.

    Parameters:
    - data (dict): The data used for training the model, containing user interactions.
    - item_features (dict): A dictionary mapping item IDs to their features.
    """
    # Example: Count-based item profiling
    for user_id, items in data.items():
        for item_id in items:
            if item_id not in self.item_profiles:
                self.item_profiles[item_id] = {}
            for feature, value in item_features.get(item_id, {}).items():
                self.item_profiles[item_id][feature] = self.item_profiles[item_id].get(feature, 0) + 1

recommend(query, top_n=10)

Recommend items based on the similarity of the query to the documents.

Parameters: - query (str): The query text for which to generate recommendations. - top_n (int): Number of top recommendations to return.

Returns: - List[int]: List of recommended item indices.

Source code in engines/contentFilterEngine/context_personalization/item_profiling.py
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def recommend(self, query: str, top_n: int = 10) -> List[int]:
    """
    Recommend items based on the similarity of the query to the documents.

    Parameters:
    - query (str): The query text for which to generate recommendations.
    - top_n (int): Number of top recommendations to return.

    Returns:
    - List[int]: List of recommended item indices.
    """
    logger.info("Generating recommendations using LSA.")
    query_vec = self.transform([query])
    doc_vecs = self.lsa_model.transform(self.vectorizer.transform(self.vectorizer.get_feature_names_out()))
    similarity_scores = (doc_vecs @ query_vec.T).flatten()
    top_indices = similarity_scores.argsort()[::-1][:top_n]
    logger.info(f"Top {top_n} recommendations generated using LSA.")
    return top_indices.tolist()