-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
50 lines (41 loc) · 1.95 KB
/
main.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
from recommenders.collaborative_filtering import CollaborativeFiltering
from recommenders.collaborative_filtering import similarity_cosine
from recommenders.collaborative_filtering import similarity_pearson
import csv
# Read dataset
def read_dataset(path):
dataset = []
with open(path, newline='') as file:
reader = csv.reader(file, delimiter='\t', quotechar='|')
for row in reader:
(user_id, movie_id, rating) = (row[0], row[1], float(row[2]))
dataset.append((user_id, movie_id, rating))
return dataset
# Write dataset
def write_dataset(dataset, path):
with open(path, 'w', newline='') as file:
writer = csv.writer(file, delimiter='\t', quotechar='|', quoting=csv.QUOTE_MINIMAL)
for instance in dataset:
writer.writerow(instance)
return dataset
# Cross validation
rmse = CollaborativeFiltering.cross_validate('data/training.dat',
item_based=True,
k=2,
similarity=similarity_cosine,
n_folds=2,
# models_directory='models', # when provided, the models would be saved
models_directory='',
load_models=False)
print("RMSE: %.3f" % rmse)
# Prediction
ibcf = CollaborativeFiltering(k=2, similarity=similarity_pearson)
ibcf.load_dataset('data/training.dat')
ibcf.train(item_based=True)
# Model can be trained and saved and then loaded again without training
# ibcf.save_model('models/model_sim_{}.csv'.format(ibcf.similarity.__name__))
# ibcf.load_model('models/model_sim_{}.csv'.format(ibcf.similarity.__name__))
ibcf.predict_missing_ratings(item_based=True)
predictions = ibcf.predict_for_set_with_path('data/predict.dat')
write_dataset(predictions, 'output/predictions.dat')
print(predictions)