forked from federicoBetti/Recommender-System-Competition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cooccurrences_matrix.py
61 lines (47 loc) · 1.9 KB
/
cooccurrences_matrix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import gc
import itertools
import os
import pickle
from scipy.sparse import csr_matrix
import scipy.sparse as sparse
from Dataset.RS_Data_Loader import RS_Data_Loader
import pandas as pd
import numpy as np
if __name__ == '__main__':
target_playlist = pd.read_csv(os.path.join("Dataset", "target_playlists.csv"))
train = pd.read_csv(os.path.join("Dataset", "train.csv"))
tracks = pd.read_csv(os.path.join("Dataset", "tracks.csv"))
target_playlist_m = np.array(target_playlist.playlist_id.as_matrix())
songs_per_playlist = pd.Series(train.groupby(train.playlist_id).track_id.apply(list))
songs_number = train.groupby(train.track_id).track_id.groups
relation_matrix = np.zeros([len(tracks), len(tracks)], dtype=int)
all_combinations = []
for pl in songs_per_playlist:
# sort it to have the pairs in order.
# In this way you'll build a traingular matrix and and the end you can easilly sum it with its transpose
# to create the final one (in which diagonal values will not be present yet)
pl.sort()
all_combinations.append(list(itertools.combinations(pl, 2)))
all_combinations = [item for sublist in all_combinations for item in sublist]
i = 0
# simply, add 1 in all pairs that have been found
for el in all_combinations:
i += 1
relation_matrix[el] += 1
relation_matrix_complete = relation_matrix + relation_matrix.transpose()
del relation_matrix
print(np.nonzero(relation_matrix_complete))
i = 0
for line in relation_matrix_complete:
# fill the diagonal
relation_matrix_complete[i, i] = len(songs_number[i])
i += 1
del tracks
del train
del target_playlist
del target_playlist_m
del songs_number
del songs_per_playlist
gc.collect()
cooc_csr = csr_matrix(relation_matrix_complete)
sparse.save_npz(os.path.join("Dataset", "cooc_matrix.npz"), cooc_csr)