Skip to content

Instantly share code, notes, and snippets.

@wenming
Created May 20, 2012 23:53
Show Gist options
  • Select an option

  • Save wenming/2759966 to your computer and use it in GitHub Desktop.

Select an option

Save wenming/2759966 to your computer and use it in GitHub Desktop.
MSD sparse matrix read
#!/usr/bin/python
#opens the triplets, saves song & user keys, writes out a simpler matrix based on int user, int song, int rating.
dataset_file = open('train_triplets.txt', 'r') #http://labrosa.ee.columbia.edu/millionsong/sites/default/files/challenge/train_triplets.txt.zip
songs_count = 0
users_count = 0
dataset_count = 0
users_dict = dict()
songs_dict = dict()
file_out = open('results_int.txt', 'w')
file_songs = open('songs.txt', 'w')
file_users = open('users.txt', 'w')
while 1:
lines = dataset_file.readlines(1000000)
if not lines:
break
for line in lines:
line = line.strip()
(user, song, freq) = line.split()
user = user.strip()
if (users_dict.has_key(user) == False):
users_dict[user] = users_count
file_users.write(user)
file_users.write('\n')
users_count = users_count + 1
song = song.strip()
if (songs_dict.has_key(song) == False):
songs_dict[song] = songs_count
file_songs.write(song)
file_songs.write('\n')
songs_count = songs_count + 1
freq = freq.strip()
out_line = [str(users_dict[user]), " ", str(songs_dict[song]), " ", str(freq), '\n']
file_out.writelines(out_line)
dataset_file.close()
file_songs.close()
file_users.close()
file_out.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment