Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 35/optimize personalization calculation #36

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ upload: clean

# Remove files from repo
clean:
python setup.py clean --all
python3 setup.py clean --all
rm -rf *.pyc __pycache__ build dist recmetrics.egg-info recmetrics/__pycache__ tests/__pycache__ tests/reports docs/build .pytest_cache .tox .coverage

# Download MovieLens data to repo
Expand Down
6 changes: 2 additions & 4 deletions recmetrics/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,9 @@ def make_rec_matrix(predicted: List[list]) -> sp.csr_matrix:
#calculate similarity for every user's recommendation list
similarity = cosine_similarity(X=rec_matrix_sparse, dense_output=False)

#get indicies for upper right triangle w/o diagonal
upper_right = np.triu_indices(similarity.shape[0], k=1)

#calculate average similarity
personalization = np.mean(similarity[upper_right])
dim = similarity.shape[0]
personalization = (similarity.sum() - dim) / (dim * (dim - 1))
return 1-personalization

def _single_list_similarity(predicted: list, feature_df: pd.DataFrame, u: int) -> float:
Expand Down