code: def user_diversity(
      train_df,
      reco_df,
      item_feature_df=None,
      item_sim_measure=DEFAULT_ITEM_SIM_MEASURE,
      col_item_features=DEFAULT_ITEM_FEATURES_COL,
      col_user=DEFAULT_USER_COL,
      col_item=DEFAULT_ITEM_COL,
      col_sim=DEFAULT_SIMILARITY_COL,
      col_relevance=None,
  ):
      """Calculate average diversity of recommendations for each user.
      The metric definition is based on formula (3) in the following reference:
  
      :Citation:
  
          Y.C. Zhang, D.Ó. Séaghdha, D. Quercia and T. Jambor, Auralist:
          introducing serendipity into music recommendation, WSDM 2012
  
      Args:
          train_df (pandas.DataFrame): Data set with historical data for users and items they have interacted with;
              contains col_user, col_item. Assumed to not contain any duplicate rows.
          reco_df (pandas.DataFrame): Recommender's prediction output, containing col_user, col_item, col_relevance (optional).
              Assumed to not contain any duplicate user-item pairs.
          item_feature_df (pandas.DataFrame): (Optional) It is required only when item_sim_measure='item_feature_vector'.
              It contains two columns: col_item and features (a feature vector).
          item_sim_measure (str): (Optional) This column indicates which item similarity measure to be used.
              Available measures include item_cooccurrence_count (default choice) and item_feature_vector.
          col_item_features (str): item feature column name.
          col_user (str): User id column name.
          col_item (str): Item id column name.
          col_sim (str): This column indicates the column name for item similarity.
          col_relevance (str): This column indicates whether the recommended item is actually relevant to the user or not.
  
      Returns:
          pandas.DataFrame: A dataframe with the following columns: col_user, user_diversity.
      """
  
      df_intralist_similarity = _get_intralist_similarity(
          train_df,
          reco_df,
          item_feature_df,
          item_sim_measure,
          col_item_features,
          col_user,
          col_item,
          col_sim,
      )
      df_user_diversity = df_intralist_similarity
      df_user_diversity["user_diversity"] = (
          1 - df_user_diversity["avg_il_sim"]
      )
      df_user_diversity = (
          df_user_diversity[[col_user, "user_diversity"]]
          .sort_values(col_user)
          .reset_index(drop=True)
      )
  
      return df_user_diversity
  
imports: import numpy
  import pandas
  from functools import wraps
  from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score, explained_variance_score, roc_auc_score, log_loss
  from recommenders.utils.constants import DEFAULT_USER_COL, DEFAULT_ITEM_COL, DEFAULT_RATING_COL, DEFAULT_PREDICTION_COL, DEFAULT_RELEVANCE_COL, DEFAULT_SIMILARITY_COL, DEFAULT_ITEM_FEATURES_COL, DEFAULT_ITEM_SIM_MEASURE, DEFAULT_K, DEFAULT_THRESHOLD
  from recommenders.datasets.pandas_df_utils import has_columns, has_same_base_dtype, lru_cache_df
label_desc: Use image node_id 24 for calling a global function with example usage: user_diversity(train_df, reco_df, item_feature_df, item_sim_measure, col_item_features, col_user, col_item, col_sim, col_relevance) and returns: df_user_diversity