<jupyter_start><jupyter_text>Import<jupyter_code>import random
import pandas as pd
import umap
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt

from recoxplainer.config import cfg
from recoxplainer.data_reader.data_reader import DataReader
from recoxplainer.models.emf_model import EMFModel

from recoxplainer.recommender import Recommender 
from recoxplainer.evaluator import Splitter, Evaluator, ExplanationEvaluator
from recoxplainer.explain import EMFExplainer<jupyter_output>/opt/anaconda3/envs/recoxplainer2/lib/python3.6/site-packages/umap/__init__.py:9: UserWarning: Tensorflow not installed; ParametricUMAP will be unavailable
  warn("Tensorflow not installed; ParametricUMAP will be unavailable")<jupyter_text>Import data<jupyter_code>data = DataReader(**cfg.ml100k)
data.make_consecutive_ids_in_dataset()
sp = Splitter()
train, test = sp.split_leave_n_out(data, frac=0.1)<jupyter_output><empty_output><jupyter_text>Model training & recommending<jupyter_code>emf = EMFModel(**cfg.model.emf)
emf.fit(train)
rec = Recommender(train, emf)
rec = rec.recommend_all()<jupyter_output><empty_output><jupyter_text>Generating Explanations<jupyter_code>explanations = EMFExplainer(emf, rec, data)
res = explanations.explain_recommendations()
ex = ExplanationEvaluator(train.num_user)<jupyter_output><empty_output><jupyter_text>Evaluationg an explanation:<jupyter_code>ex.mean_explaianable_precision(res, emf.explainability_matrix)
def plot_model(model, dt, user_X):
    
    dim_red_model = umap.UMAP().fit_transform(model.item_embedding())
    dim_red_model_usr = umap.UMAP().fit_transform(model.user_embedding())

    rated_items = dt.dataset[dt.dataset.userId == user_X].itemId

    item_emb_for_user_X = pd.DataFrame.from_dict({
        'x': dim_red_model[:,0],
        'y': dim_red_model[:,1],
        'expl': model.explainability_matrix[user_X, :]})

    item_emb_for_user_X = item_emb_for_user_X.sort_values(by=['expl'])

    sns.scatterplot(data=item_emb_for_user_X, x="x", y="y", hue="expl")
    
    plt.scatter(dim_red_model[rated_items,0], 
            dim_red_model[rated_items,1], 
            marker='o', c='#c3e0f4')

    plt.scatter(dim_red_model_usr[user_X,0], 
            dim_red_model_usr[user_X,1], 
            marker='X', s=100, c='#419ede')
rand_usr = random.choice(data.dataset.userId.unique())
plot_model(emf, train, rand_usr)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11341919401744911, 'fraction_numerical': 0.01038637307852098, 'mean_word_length': 6.319148936170213, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '661281', 'license': 'MIT', 'n_tokens_mistral': 913, 'n_tokens_neox': 883, 'n_words': 129, 'path': 'notebooks/2. EMF.ipynb', 'repo_name': 'ludovikcoba/recoxplainer', 'size': '2407'}