Skip to content

Instantly share code, notes, and snippets.

View victorkohler's full-sized avatar

Victor Köhler victorkohler

View GitHub Profile
import pandas as pd
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
from scipy import sparse
#------------------
# LOAD THE DATASET
#------------------
#------------------------
# USER-ITEM CALCULATIONS
#------------------------
user = 5985 # The id of the user for whom we want to generate recommendations
user_index = data[data.user == user].index.tolist()[0] # Get the frame index
# Get the artists the user has likd.
known_user_likes = data_items.ix[user_index]
#------------------------
# USER-ITEM CALCULATIONS
#------------------------
# Construct a new dataframe with the 10 closest neighbours (most similar)
# for each artist.
data_neighbours = pd.DataFrame(index=data_matrix.columns, columns=range(1,11))
for i in xrange(0, len(data_matrix.columns)):
data_neighbours.ix[i,:10] = data_matrix.ix[0:,i].sort_values(ascending=False)[:10].index
We can't make this file beautiful and searchable because it's too large.
user,a perfect circle,abba,ac/dc,adam green,aerosmith,afi,air,alanis morissette,alexisonfire,alicia keys,all that remains,amon amarth,amy macdonald,amy winehouse,anti-flag,aphex twin,apocalyptica,arcade fire,arch enemy,arctic monkeys,as i lay dying,atb,atreyu,audioslave,avril lavigne,babyshambles,bad religion,beastie boys,beatsteaks,beck,beirut,belle and sebastian,beyonce,billy talent,bjork,black eyed peas,black sabbath,blind guardian,blink-182,bloc party,bloodhound gang,blur,boards of canada,bob dylan,bob marley,bob marley & the wailers,breaking benjamin,bright eyes,bring me the horizon,britney spears,bruce springsteen,bullet for my valentine,caliban,cascada,cat power,children of bodom,chimaira,christina aguilera,clueso,cocorosie,coldplay,crystal castles,cypress hill,daft punk,damien rice,dark tranquillity,david bowie,david guetta,death cab for cutie,deep purple,deftones,deichkind,depeche mode,dido,die apokalyptischen reiter,die toten hosen,digitalism,dimmu borgir,dire straits,disturbed,dream theater,dredg,d
def implicit_als(sparse_data, alpha_val=40, iterations=10, lambda_val=0.1, features=10):
""" Implementation of Alternating Least Squares with implicit data. We iteratively
compute the user (x_u) and item (y_i) vectors using the following formulas:
x_u = ((Y.T*Y + Y.T*(Cu - I) * Y) + lambda*I)^-1 * (X.T * Cu * p(u))
y_i = ((X.T*X + X.T*(Ci - I) * X) + lambda*I)^-1 * (Y.T * Ci * p(i))
Args:
""" Continuation of implicit_als function"""
# Start main loop. For each iteration we first compute X and then Y
for i in xrange(iterations):
print 'iteration %d of %d' % (i+1, iterations)
# Precompute Y-transpose-Y and X-transpose-X
yTy = Y.T.dot(Y)
xTx = X.T.dot(X)
user_vecs, item_vecs = implicit_als(data_sparse, iterations=20, features=20, alpha_val=40)
#------------------------------
# FIND SIMILAR ITEMS
#------------------------------
# Let's find similar artists to Jay-Z.
# Note that this ID might be different for you if you're using
# the full dataset or if you've sliced it somehow.
item_id = 10277
# Let's say we want to recommend artists for user with ID 2023
user_id = 2023
#------------------------------
# GET ITEMS CONSUMED BY USER
#------------------------------
# Let's print out what the user has listened to
consumed_idx = data_sparse[user_id,:].nonzero()[1].astype(str)
def nonzeros(m, row):
for index in xrange(m.indptr[row], m.indptr[row+1]):
yield m.indices[index], m.data[index]
def implicit_als_cg(Cui, features=20, iterations=20, lambda_val=0.1):
user_size, item_size = Cui.shape
X = np.random.rand(user_size, features) * 0.01