Skip to content

Instantly share code, notes, and snippets.

@bowdenk7
Last active November 26, 2018 21:50
Show Gist options
  • Save bowdenk7/cf716ab8c9d0b73caa39a8d21b22dd3c to your computer and use it in GitHub Desktop.
Save bowdenk7/cf716ab8c9d0b73caa39a8d21b22dd3c to your computer and use it in GitHub Desktop.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import sklearn.metrics as metrics
import numpy as np
from sklearn.neighbors import NearestNeighbors
# This contains a function `find_similar_items` which calculates the most similar items using cosine similarity.
# I figured this made more sense than user based recommendations because someone who hasn't bought any items before would have
# zero similarity to other users, right?
#
# I generally followed a tutorial here: https://github.com/csaluja/JupyterNotebooks-Medium/blob/master/CF%20Recommendation%20System-Examples.ipynb
# user/product array - 1 for purchase, 0 for no purchase history
# presumably could have another service that constructs this and then load it from a csv?
M = np.asarray([[1, 0, 0, 1, 1, 1, 1, 0],
[0, 1, 0, 0, 1, 0, 1, 0],
[0, 1, 0, 0, 1, 1, 1, 1],
[0, 0, 0, 1, 1, 1, 1, 0],
[1, 0, 1, 1, 1, 1, 0, 1],
[1, 0, 0, 1, 1, 1, 0, 0]])
M = pd.DataFrame(M)
def find_similar_items(item_id, ratings, k=3):
similarities = []
indices = []
ratings = ratings.T
model_knn = NearestNeighbors(metric='cosine', algorithm='brute')
model_knn.fit(ratings)
distances, indices = model_knn.kneighbors(
ratings.iloc[item_id-1, :].values.reshape(1, -1), n_neighbors=k+1)
similarities = 1-distances.flatten()
print('{0} most similar items for item {1}:\n'.format(k, item_id))
for i in range(0, len(indices.flatten())):
if indices.flatten()[i]+1 == item_id:
continue
else:
print('{0}: Item {1} :, with similarity of {2}'.format(i, indices.flatten()[i]+1, similarities.flatten()[i]))
return similarities, indices
similarities, indices = find_similar_items(3, M)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment