def calcItemInfo(item, user_ratings, users_info, alpha): '''Qi, Lii(i) for adjusted cosine''' Q = 0 L = 0 n = 0 # item popularity for rating in user_ratings: user = rating[0] ri = rating[1] ti = rating[2] t, avg, e = users_info[user] Q += (f(t, ti, alpha) * (ri - avg))**2 L += f(t, ti, alpha)**2 * (ri - avg) * e n += 1 return item, L, n, Q
def updateItemInfo(iid, prevL, prevn, prevQ, user_t, prev_ratings, fresh_ratings, rubar, user_e, alpha): '''Calculate the incrementaly update value of Qi and Lii(i)''' """calculate Q, L""" dL = 0 dQ = 0 dn = 0 for (user, item) in fresh_ratings: if item == iid: # u\epsilon\DeltaU_i^t rating, timestep = fresh_ratings[(user, item)] ru = rubar[user] e = user_e[user] t = user_t[user] dL += (rating - ru) * e dQ += (rating - ru)**2 dn += 1 if (user, item) in prev_ratings: # u\epsilon\U_i^{t-1} dL += 2 * f(t, timestep, alpha) * (rating - ru) * e L = dL + math.exp(-2*alpha) * prevL Q = dQ + math.exp(-2*alpha) * (prevQ - 2 * prevL) n = dn + prevn if Q < 0: Q = prevQ L = prevL return iid, L, n, Q
def compute_Sapprox(csv_file): starting_state, data = preprocessing_data(csv_file) #starting_state = np.array(pd.read_csv('startingstate.csv').iloc[:,1]) N = len(starting_state) Sapprox = simulated_annealing(starting_state, betas, n_iter, lambda_, data, verbose=True, plot=True) #Computing score of this run with the Sapprox found and save it in a file called Aquarium_submission_SCORE with SCORE being the score of the run. score = f(np.array(Sapprox), lambda_, data) submission(csv_file, Sapprox, "Aquarium_submission_N_{}_score_{}.csv".format(N, score))
def updateItemPairInfo(i, j, prevLi, prevLj, prevP, fresh_users, user_t, prev_ratings, fresh_ratings, rubar, user_e, alpha): '''Calculate the incrementaly update value of Pij, Lij(i), Lij(j)''' """calculate Pij, Lij(i), Lij(j)""" dP = 0 dLi = 0 dLj = 0 for user in fresh_users: ru = rubar[user] e = user_e[user] t = user_t[user] # u\epsilon\DeltaU_i^t \cap u\epsilon\U_j^{t-1} if (user, i) in fresh_ratings and (user, j) in prev_ratings: isRated = True ri, ti = fresh_ratings[(user, i)] rj, tj = prev_ratings[(user, j)] dP += (ri - ru) * f(t, tj, alpha) * (rj - ru) dLi += f(t, tj, alpha) * (ri - ru) * e dLj += f(t, tj, alpha) * (rj - ru) * e # u\epsilon\DeltaU_j^t \cap u\epsilon\U_i^{t-1} if (user, j) in fresh_ratings and (user, i) in prev_ratings: isRated = True rj, tj = fresh_ratings[(user, j)] ri, ti = prev_ratings[(user, i)] dP += f(t, ti, alpha) * (ri - ru) * (rj - ru) dLi += f(t, ti, alpha) * (ri - ru) * e dLj += f(t, ti, alpha) * (rj - ru) * e # u\epsilon\DeltaU_i^t \cap u\epsilon\DeltaU_j^t if (user, i) in fresh_ratings and (user, j) in fresh_ratings: isRated = True ri, ti = fresh_ratings[(user, i)] rj, tj = fresh_ratings[(user, j)] dP += (ri - ru) * (rj - ru) dLi += (ri - ru) * e dLj += (rj - ru) * e Li = dLi + math.exp(-2*alpha) * prevLi Lj = dLj + math.exp(-2*alpha) * prevLj P = dP + math.exp(-2*alpha) * (prevP - prevLi - prevLj) return i, j, Li, Lj, P
def calcItemPairInfo(itemi, itemj, rating_pairs, user_info, alpha): '''For each i-j item pair, return Pij, Lij(i), Lij(j)''' Pij = 0 Lij_i = 0 Lij_j = 0 for (user, i, j) in rating_pairs: t, avg, e = user_info[user] ri = np.float(i[0]) ti = i[1] rj = np.float(j[0]) tj = j[1] Pij += f(t, ti, alpha) * (ri - avg) * f(t, tj, alpha) * (rj - avg) Lij_i += f(t, ti, alpha) * f(t, tj, alpha) * (ri - avg) * e Lij_j += f(t, ti, alpha) * f(t, tj, alpha) * (rj - avg) * e return itemi, itemj, Lij_i, Lij_j, Pij