def outputSparseMatrixStats(spars_matrix, dict_item_id_to_users): """ Analyzing for Matrix :param spars_matrix: :param dict_item_id_to_users: """ ecc_item_sim_nonzero = spars_matrix.nonzero() dict_names=create_dict_names() dict_item_ecc=create_dict_ecc() i_item_index = ecc_item_sim_nonzero[0] j_item_index = ecc_item_sim_nonzero[1] results_to_print=list() for index in range(len(i_item_index)): results_to_print.append([i_item_index[index], j_item_index[index], spars_matrix[i_item_index[index], j_item_index[index]]]) results_to_print=sorted(results_to_print, key=itemgetter(2)) for item_pair in results_to_print[-100:]: print(50*"#") item_names_print([item_pair[0],item_pair[1]],dict_names,dict_item_ecc) print("THEIR VALUE IS:",item_pair[2]) a=set(dict_item_id_to_users[int(item_pair[0])]) b=set(dict_item_id_to_users[int(item_pair[1])]) intersection= a&b print("Intersection Length:",len(intersection)) print(50*"#")
def print_maxes(mat): """ prints variouse informations about the martrix :param mat: matrix to print """ u_to_likes = load_or_create("/Matrix/UserIdToLikes.matrix", create_matrix_user_likes) dict_names = load_or_create('/DICT/MovieIdToName.dict', create_dict_names) dict_ecc = load_or_create('/DICT/MovieIdToItemEccentricity.dict', create_dict_ecc) user_to_ecc = load_or_create('/DICT/UserIdToUserEccentricity.dict', createDictUserIdToUserEccentricity) dict_userid_to_moviesliked = load_or_create( '/DICT/UserIdToLikedMovies.dict', create_dict_user_id_to_liked_items) dict_userid_to_recommends = dict() print("STARTING ECC CALC") recommends = [] for i in range(int(mat.shape[0] * 0.5)): row = mat.getrow(i) if len(row.nonzero()[0]) != 0: # print(u_to_likes.getrow(i).nonzero()[1]) if len(u_to_likes.getrow(i).nonzero()[1]) < 10 and user_to_ecc[ i + 1] > 0: # print("Amount of recommends:",len(row.nonzero()[0])) row = row.toarray()[0].tolist() max_val = max(val for val in row if str(row.index(val) + 1) not in dict_userid_to_moviesliked[i + 1]) print( 'SUM is:', sum(val for val in row if str(row.index(val) + 1) not in dict_userid_to_moviesliked[i + 1])) print('SUM with all is:', sum(val for val in row)) index_max = row.index(max_val) + 1 recommends.append([ max_val, row.index(max_val) + 1, i + 1, [i + 1 for i in u_to_likes.getrow(i).nonzero()[1]], [row.index(max_val) + 1], user_to_ecc[i + 1] ]) recommends = sorted(recommends, key=itemgetter(0)) for i in recommends[-100:]: print("MAX id:", i[1]) print("MAX val:", i[0]) print("Users ECC:", i[5]) print("for user:"******"MOVIES HE ALREADY LIKED", 50 * "=") item_names_print(i[3], dict_names, dict_ecc) print("Movie Well recommend:" + 50 * '*') item_names_print(i[4], dict_names, dict_ecc) print(50 * "#")
def print_max_similar(): dict_names = load_or_create('/DICT/MovieIdToName.dict', create_dict_names) dict_ecc = load_or_create('/DICT/MovieIdToItemEccentricity.dict', create_dict_ecc) mat = load_or_create("/Matrix/ItemSimilarityEccentricity.matrix", create_matrix_item_similarity) maxes = [] for i in range(int(mat.shape[1] * 0.1)): row = mat.getrow(i) if len(row) > 0: max_curr = max(i for i in row) maxes.append([i + 1, row.index(max_curr) + 1, max_curr]) maxes = sorted(maxes, key=itemgetter(2)) for i, j, val in maxes: print("Val is:", val) item_names_print([i, j], dict_names, dict_ecc)
def print_maxes(): u_to_likes = load_or_create("/Matrix/UserIdToLikes.matrix", create_matrix_user_likes) dict_names = load_or_create('/DICT/MovieIdToName.dict', create_dict_names) dict_ecc = load_or_create('/DICT/MovieIdToItemEccentricity.dict', create_dict_ecc) mat = get_recommendation_matrix() user_to_ecc = load_or_create('/DICT/UserIdToUserEccentricity.dict', createDictUserIdToUserEccentricity) dict_userid_to_recommends = dict() recommends = [] for i in range(int(mat.shape[0] * 0.5)): row = mat.getrow(i) if len(row.nonzero()[0]) != 0: # print(u_to_likes.getrow(i).nonzero()[1]) if len(u_to_likes.getrow(i).nonzero()[1]) < 10 and user_to_ecc[ i + 1] > 0: # print("Amount of recommends:",len(row.nonzero()[0])) row = row.toarray()[0].tolist() max_val = max(val for val in row) print('SUM with all is:', sum(val for val in row)) recommends.append([ max_val, row.index(max_val) + 1, i + 1, [i + 1 for i in u_to_likes.getrow(i).nonzero()[1]], [row.index(max_val) + 1], user_to_ecc[i + 1] ]) recommends = sorted(recommends, key=itemgetter(0)) for i in recommends[-100:]: print("MAX id:", i[1]) print("MAX val:", i[0]) print("Users ECC:", i[5]) print("for user:"******"MOVIES HE ALREADY LIKED", 50 * "=") item_names_print(i[3], dict_names, dict_ecc) print("Movie Well recommend:" + 50 * '*') item_names_print(i[4], dict_names, dict_ecc) print(50 * "#")