-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recommend.py
1128 lines (949 loc) · 34.7 KB
/
recommend.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Board game recommenders."""
import csv
import logging
import os
import sys
import tempfile
# from datetime import date
from typing import Any, Dict, FrozenSet, Iterable, Optional, Tuple, Type
import numpy as np
import turicreate as tc
from pytility import arg_to_iter, clear_list
from board_game_recommender.base import BaseGamesRecommender, GameKeyType, UserKeyType
from board_game_recommender.utils import (
condense_csv,
filter_sframe,
format_from_path,
percentile_buckets,
star_rating,
)
csv.field_size_limit(sys.maxsize)
LOGGER = logging.getLogger(__name__)
def make_cluster(data, item_id, target, target_dtype=str):
"""take an SFrame and cluster by target"""
if not data or item_id not in data.column_names():
return tc.SArray(dtype=list)
target = [t for t in arg_to_iter(target) if t in data.column_names()]
target_dtype = list(arg_to_iter(target_dtype))
target_dtype += [str] * (len(target) - len(target_dtype))
if not target:
return tc.SArray(dtype=list)
graph = tc.SGraph()
for tar, tdt in zip(target, target_dtype):
def _convert(item, dtype=tdt):
try:
return dtype(item)
except Exception:
pass
return None
tdata = data[[item_id, tar]].dropna()
tdata[tar] = tdata[tar].apply(
lambda x: [i for i in map(_convert, x or ()) if i is not None],
dtype=list,
skip_na=True,
)
tdata = tdata.stack(
column_name=tar,
new_column_name=tar,
new_column_type=tdt,
drop_na=True,
)
if not tdata:
continue
graph = graph.add_edges(edges=tdata, src_field=item_id, dst_field=tar)
del tdata, _convert
if not graph.edges:
return tc.SArray(dtype=list)
components_model = tc.connected_components.create(graph)
clusters = components_model.component_id.groupby(
"component_id",
{"cluster": tc.aggregate.CONCAT("__id")},
)["cluster"]
return clusters.filter(lambda x: x is not None and len(x) > 1)
class GamesRecommender(BaseGamesRecommender):
"""games recommender"""
logger = logging.getLogger("GamesRecommender")
id_field: str
id_type: Type = str
user_id_field: str
user_id_type: Type = str
rating_id_field: str
rating_id_type: Type = float
columns_games: Dict[str, Type]
columns_ratings: Dict[str, Type]
default_filters: Dict[str, Any]
cluster_fields: Optional[Tuple[str, ...]] = None
cluster_field_types: Optional[Tuple[Type, ...]] = None
compilation_field: Optional[str] = "compilation"
cooperative_field: Optional[str] = "cooperative"
_rated_games = None
_known_games = None
_known_users = None
_num_games = None
_clusters = None
_game_clusters = None
_compilations = None
_cooperatives = None
def __init__(
self: "GamesRecommender",
model,
similarity_model=None,
games=None,
ratings=None,
clusters=None,
compilations=None,
):
self.model = model
self.similarity_model = similarity_model
self.games = games
self.ratings = ratings
# pylint: disable=len-as-condition
if clusters is not None and len(clusters):
self._clusters = clusters
if compilations is not None and len(compilations):
self._compilations = compilations
@property
def rated_games(self: "GamesRecommender") -> FrozenSet[GameKeyType]:
"""rated games"""
if self._rated_games is None:
self._rated_games = frozenset(
self.model.coefficients[self.id_field][self.id_field]
)
return self._rated_games
@property
def known_games(self: "GamesRecommender") -> FrozenSet[GameKeyType]:
"""known games"""
if self._known_games is None:
self._known_games = (
frozenset(self.ratings[self.id_field] if self.ratings else ())
| frozenset(self.games[self.id_field] if self.games else ())
| self.rated_games
)
return self._known_games
@property
def known_users(self: "GamesRecommender") -> FrozenSet[UserKeyType]:
"""known users"""
if self._known_users is None:
self._known_users = frozenset(
self.ratings[self.user_id_field] if self.ratings else ()
) | frozenset(
self.model.coefficients[self.user_id_field][self.user_id_field]
)
return self._known_users
@property
def num_games(self: "GamesRecommender") -> int:
"""total number of games known to the recommender"""
if self._num_games is None:
self._num_games = len(self.known_games)
return self._num_games
@property
def clusters(self: "GamesRecommender"):
"""game implementation clusters"""
if self._clusters is None:
self._clusters = make_cluster(
data=self.games,
item_id=self.id_field,
target=self.cluster_fields,
target_dtype=self.cluster_field_types,
)
return self._clusters
@property
def compilations(self: "GamesRecommender"):
"""compilation games"""
if self._compilations is None:
self._compilations = (
self.games[self.games[self.compilation_field]][self.id_field]
if self.games
and self.compilation_field
and self.compilation_field in self.games.column_names()
else tc.SArray(dtype=self.id_type)
)
return self._compilations
@property
def cooperatives(self: "GamesRecommender"):
"""cooperative games"""
if self._cooperatives is None:
self._cooperatives = (
self.games[self.games[self.cooperative_field]][self.id_field]
if self.games
and self.cooperative_field
and self.cooperative_field in self.games.column_names()
else tc.SArray(dtype=self.id_type)
)
return self._cooperatives
def filter_games(self: "GamesRecommender", **filters):
"""return games filtered by given criteria"""
return filter_sframe(self.games, **filters)
def cluster(self: "GamesRecommender", game_id):
"""get implementation cluster for a given game"""
# pylint: disable=len-as-condition
if self.clusters is None or not len(self.clusters):
return (game_id,)
if self._game_clusters is None:
self._game_clusters = {
id_: cluster
for cluster in self.clusters
for id_ in cluster
if cluster is not None and len(cluster) > 1
}
return self._game_clusters.get(game_id) or (game_id,)
def _process_games(self: "GamesRecommender", games=None, games_filters=None):
games = (
games[self.id_field].astype(self.id_type, True)
if isinstance(games, tc.SFrame)
else arg_to_iter(games)
if games is not None
else None
)
games = (
games
if isinstance(games, tc.SArray) or games is None
else tc.SArray(tuple(games), dtype=self.id_type)
)
if games_filters and self.games:
games = tc.SArray(dtype=self.id_type) if games is None else games
in_field = f"{self.id_field}__in"
game_id_in = frozenset(games_filters.get(in_field) or ())
games_filters[in_field] = (
game_id_in & self.rated_games if game_id_in else self.rated_games
)
self.logger.debug(
"games filters: %r",
{
k: f"[{len(v)} games]" if k == in_field else v
for k, v in games_filters.items()
},
)
filtered_games = self.filter_games(**games_filters)
games = games.append(filtered_games[self.id_field]).unique()
del games_filters, filtered_games
return games
def _process_exclude(
self: "GamesRecommender",
users,
exclude=None,
exclude_known=True,
exclude_clusters=True,
exclude_compilations=True,
):
if exclude_known and self.ratings:
for user in users:
if not user:
continue
rated = self.ratings.filter_by([user], self.user_id_field)[
self.id_field,
self.user_id_field,
]
exclude = rated.copy() if exclude is None else exclude.append(rated)
del rated
if exclude_clusters and exclude:
grouped = exclude.groupby(
self.user_id_field,
{"game_ids": tc.aggregate.CONCAT(self.id_field)},
)
for user, game_ids in zip(grouped[self.user_id_field], grouped["game_ids"]):
game_ids = frozenset(game_ids)
if not user or not game_ids:
continue
game_ids = {
linked
for game_id in game_ids
for linked in self.cluster(game_id)
if linked not in game_ids
}
clusters = tc.SFrame(
{
self.id_field: tc.SArray(list(game_ids), dtype=self.id_type),
self.user_id_field: tc.SArray.from_const(
user,
len(game_ids),
self.user_id_type,
),
}
)
exclude = exclude.append(clusters)
del clusters
del grouped
# pylint: disable=len-as-condition
if exclude_compilations and len(self.compilations):
comp = tc.SFrame({self.id_field: self.compilations})
for user in users:
comp[self.user_id_field] = tc.SArray.from_const(
user,
len(self.compilations),
self.user_id_type,
)
exclude = comp.copy() if exclude is None else exclude.append(comp)
del comp
return exclude
def _post_process_games(
self: "GamesRecommender",
games,
columns,
join_on=None,
sort_by="rank",
star_percentiles=None,
ascending=True,
):
if join_on and self.games:
games = games.join(self.games, on=join_on, how="left")
else:
games["name"] = None
if star_percentiles:
columns.append("stars")
buckets = tuple(percentile_buckets(games["score"], star_percentiles))
games["stars"] = [
star_rating(score=score, buckets=buckets, low=1.0, high=5.0)
for score in games["score"]
]
return games.sort(sort_by, ascending=ascending)[columns]
def process_user_id(self: "GamesRecommender", user_id):
"""process user ID"""
return user_id or None
def recommend(
self: "GamesRecommender",
users: Iterable[UserKeyType],
*,
similarity_model=False,
games=None,
games_filters=None,
exclude=None,
exclude_known=True,
exclude_clusters=True,
exclude_compilations=True,
num_games=None,
ascending=True,
columns=None,
star_percentiles=None,
**kwargs,
) -> tc.SFrame:
"""recommend games"""
users = [self.process_user_id(user) for user in arg_to_iter(users)] or [None]
self.logger.info("Calculating recommendations for %d users", len(users))
items = kwargs.pop("items", None)
assert games is None or items is None, "cannot use <games> and <items> together"
games = items if games is None else games
games = self._process_games(games, games_filters)
if games is not None:
self.logger.info("Restrict recommendations to %d games", len(games))
exclude = self._process_exclude(
users,
exclude,
exclude_known,
exclude_clusters,
exclude_compilations,
)
if exclude is not None:
self.logger.info(
"Exclude %d game-user pairs from recommendations",
len(exclude),
)
kwargs["k"] = (
kwargs.get("k", self.num_games) if num_games is None else num_games
)
self.logger.info("Recommending %d games per user", kwargs["k"])
columns = list(arg_to_iter(columns)) or ["rank", "name", self.id_field, "score"]
if len(users) > 1 and self.user_id_field not in columns:
columns.insert(0, self.user_id_field)
model = (
self.similarity_model
if similarity_model and self.similarity_model
else self.model
)
self.logger.info("Making recommendations using <%s>", model)
recommendations = model.recommend(
users=users,
items=games,
exclude=exclude,
exclude_known=exclude_known,
**kwargs,
)
self.logger.info("Calculated %d recommendations", len(recommendations))
del users, items, games, exclude, model
return self._post_process_games(
games=recommendations,
columns=columns,
join_on=self.id_field,
sort_by=[self.user_id_field, "rank"]
if self.user_id_field in columns
else "rank",
star_percentiles=star_percentiles,
ascending=ascending,
)
def recommend_as_numpy(
self: "GamesRecommender",
users: Iterable[str],
games: Iterable[int],
) -> np.ndarray:
"""Calculate recommendations for certain users and games as a numpy array."""
users = list(users)
users_sf = tc.SFrame(
{
self.user_id_field: users,
"sort_users": range(len(users)),
}
)
games = list(games)
games_sf = tc.SFrame(
{
self.id_field: games,
"sort_games": range(len(games)),
}
)
recommendations = self.model.recommend(
users=users,
items=games,
exclude_known=False,
k=len(games),
)
assert len(recommendations) == len(users) * len(games)
result = (
recommendations.join(users_sf)
.join(games_sf)
.sort(["sort_users", "sort_games"])
)
return result["score"].to_numpy().reshape(len(users), len(games))
def recommend_group(
self: "GamesRecommender",
users: Iterable[UserKeyType],
*,
games=None,
games_filters=None,
exclude=None,
exclude_clusters=True,
exclude_compilations=True,
ascending=True,
star_percentiles=None,
**kwargs,
) -> tc.SFrame:
"""Recommend games for a group of users."""
users = [self.process_user_id(user) for user in arg_to_iter(users)] or [None]
self.logger.info("Calculating recommendations for %d users", len(users))
items = kwargs.pop("items", None)
assert games is None or items is None, "cannot use <games> and <items> together"
games = items if games is None else games
games = self._process_games(games, games_filters)
if games is not None:
self.logger.info("Restrict recommendations to %d games", len(games))
exclude = self._process_exclude(
users,
exclude,
False,
exclude_clusters,
exclude_compilations,
)
if exclude is not None:
self.logger.info(
"Exclude %d game-user pairs from recommendations",
len(exclude),
)
kwargs["k"] = self.num_games
recommendations = (
self.model.recommend(
users=users,
items=games,
exclude=exclude,
exclude_known=False,
**kwargs,
)
.groupby(
key_column_names="bgg_id",
operations={"score": tc.aggregate.MEAN("score")},
)
.sort("score", ascending=False)
)
recommendations["rank"] = range(1, len(recommendations) + 1)
self.logger.info("Calculated %d recommendations", len(recommendations))
del users, items, games, exclude
return self._post_process_games(
games=recommendations,
columns=["rank", "name", self.id_field, "score"],
join_on=self.id_field,
sort_by="rank",
star_percentiles=star_percentiles,
ascending=ascending,
)
def recommend_group_as_numpy(
self: "GamesRecommender",
users: Iterable[str],
games: Iterable[int],
) -> np.ndarray:
"""Calculate recommendations for a group of users and games as a numpy array."""
users = list(users)
games = list(games)
games_sf = tc.SFrame(
{
self.id_field: games,
"sort_games": range(len(games)),
}
)
recommendations = self.model.recommend(
users=users,
items=games,
exclude_known=False,
k=len(games),
).groupby(
key_column_names=self.id_field,
operations={"score": tc.aggregate.MEAN("score")},
)
assert len(recommendations) == len(games)
result = recommendations.join(games_sf).sort("sort_games")
return result["score"].to_numpy().reshape(1, len(games))
def recommend_similar(
self: "GamesRecommender",
games: Iterable[GameKeyType],
*,
items=None,
games_filters=None,
threshold=0.001,
num_games=None,
columns=None,
**kwargs,
) -> tc.SFrame:
"""recommend games similar to given ones"""
games = list(arg_to_iter(games))
items = self._process_games(items, games_filters)
kwargs["k"] = (
kwargs.get("k", self.num_games) if num_games is None else num_games
)
columns = list(arg_to_iter(columns)) or ["rank", "name", self.id_field, "score"]
model = self.similarity_model or self.model
self.logger.debug("recommending games similar to %s using %s", games, model)
recommendations = model.recommend_from_interactions(
observed_items=games,
items=items,
**kwargs,
)
recommendations = (
recommendations[recommendations["score"] >= threshold]
if threshold
else recommendations
)
del games, items, model
return self._post_process_games(
games=recommendations,
columns=columns,
join_on=self.id_field,
)
def similar_games(
self: "GamesRecommender",
games: Iterable[GameKeyType],
*,
num_games=10,
columns=None,
**kwargs,
) -> tc.SFrame:
"""find similar games"""
games = list(arg_to_iter(games))
columns = list(arg_to_iter(columns)) or ["rank", "name", "similar", "score"]
if len(games) > 1 and self.id_field not in columns:
columns.insert(0, self.id_field)
model = self.similarity_model or self.model
self.logger.debug("finding similar games using %s", model)
sim_games = model.get_similar_items(items=games, k=num_games or self.num_games)
del games, model
return self._post_process_games(
games=sim_games,
columns=columns,
join_on={"similar": self.id_field},
sort_by=[self.id_field, "rank"] if self.id_field in columns else "rank",
)
def lead_game(
self: "GamesRecommender",
game_id,
user=None,
exclude_known=False,
exclude_compilations=True,
**kwargs,
):
"""find the highest rated game in a cluster"""
cluster = frozenset(self.cluster(game_id)) & self.rated_games
if exclude_compilations:
cluster -= frozenset(self.compilations)
other_games = cluster - {game_id}
if not other_games:
return game_id
if len(cluster) == 1:
return next(iter(cluster))
cluster = sorted(cluster)
kwargs.pop("items", None)
recommendations = self.recommend(
user,
items=cluster,
exclude_known=exclude_known,
exclude_compilations=exclude_compilations,
**kwargs,
)
if recommendations:
return recommendations[self.id_field][0]
if not self.games or "rank" not in self.games.column_names():
return game_id
ranked = self.games.filter_by(cluster, self.id_field).sort("rank")
return ranked[self.id_field][0] if ranked else game_id
def save(
self: "GamesRecommender",
path,
dir_model="recommender",
dir_similarity="similarity",
dir_games="games",
dir_ratings="ratings",
dir_clusters="clusters",
dir_compilations="compilations",
):
"""save all recommender data to files in the give dir"""
path_model = os.path.join(path, dir_model, "")
self.logger.info("saving model to <%s>", path_model)
self.model.save(path_model)
if dir_similarity and self.similarity_model:
path_similarity = os.path.join(path, dir_similarity, "")
self.logger.info("saving similarity model to <%s>", path_similarity)
self.similarity_model.save(path_similarity)
if dir_games and self.games:
path_games = os.path.join(path, dir_games, "")
self.logger.info("saving games to <%s>", path_games)
self.games.save(path_games)
if dir_ratings and self.ratings:
path_ratings = os.path.join(path, dir_ratings, "")
self.logger.info("saving ratings to <%s>", path_ratings)
self.ratings.save(path_ratings)
# pylint: disable=len-as-condition
if dir_clusters and self.clusters is not None and len(self.clusters):
path_clusters = os.path.join(path, dir_clusters, "")
self.logger.info("saving clusters to <%s>", path_clusters)
self.clusters.save(path_clusters)
if (
dir_compilations
and self.compilations is not None
and len(self.compilations)
):
path_compilations = os.path.join(path, dir_compilations, "")
self.logger.info("saving compilations to <%s>", path_compilations)
self.compilations.save(path_compilations)
@classmethod
def load(
cls,
path,
dir_model="recommender",
dir_similarity="similarity",
dir_games="games",
dir_ratings="ratings",
dir_clusters="clusters",
dir_compilations="compilations",
):
"""load all recommender data from files in the give dir"""
path_model = os.path.join(path, dir_model, "")
cls.logger.info("loading model from <%s>", path_model)
model = tc.load_model(path_model)
if dir_similarity:
path_similarity = os.path.join(path, dir_similarity, "")
cls.logger.info("loading similarity model from <%s>", path_similarity)
try:
similarity_model = tc.load_model(path_similarity)
except Exception:
similarity_model = None
else:
similarity_model = None
if dir_games:
path_games = os.path.join(path, dir_games, "")
cls.logger.info("loading games from <%s>", path_games)
try:
games = tc.load_sframe(path_games)
except Exception:
games = None
else:
games = None
if dir_ratings:
path_ratings = os.path.join(path, dir_ratings, "")
cls.logger.info("loading ratings from <%s>", path_ratings)
try:
ratings = tc.load_sframe(path_ratings)
except Exception:
ratings = None
else:
ratings = None
if dir_clusters:
path_clusters = os.path.join(path, dir_clusters, "")
cls.logger.info("loading clusters from <%s>", path_clusters)
try:
clusters = tc.SArray(path_clusters)
except Exception:
clusters = None
else:
clusters = None
if dir_compilations:
path_compilations = os.path.join(path, dir_compilations, "")
cls.logger.info("loading compilations from <%s>", path_compilations)
try:
compilations = tc.SArray(path_compilations)
except Exception:
compilations = None
else:
compilations = None
return cls(
model=model,
similarity_model=similarity_model,
games=games,
ratings=ratings,
clusters=clusters,
compilations=compilations,
)
@classmethod
def train(
cls,
games,
ratings,
*,
side_data_columns=None,
similarity_model=False,
num_factors=32,
max_iterations=100,
verbose=False,
defaults=True,
**filters,
):
"""train recommender from data"""
filters.setdefault(f"{cls.id_field}__apply", bool)
if defaults:
for column, values in cls.default_filters.items():
filters.setdefault(column, values)
filters = {k: v for k, v in filters.items() if k and v is not None}
columns = clear_list(k.split("__")[0] for k in filters)
all_games = games
games = filter_sframe(games[columns].dropna(), **filters)
side_data_columns = list(arg_to_iter(side_data_columns))
if cls.id_field not in side_data_columns:
side_data_columns.append(cls.id_field)
if len(side_data_columns) > 1:
cls.logger.info("using game side features: %r", side_data_columns)
item_data = all_games[side_data_columns].dropna()
else:
item_data = None
ratings_filtered = ratings.filter_by(games[cls.id_field], cls.id_field)
cls.logger.info(
"Using %d latent factors in collaborative filtering",
num_factors,
)
model = tc.ranking_factorization_recommender.create(
observation_data=ratings_filtered,
user_id=cls.user_id_field,
item_id=cls.id_field,
target=cls.rating_id_field,
num_factors=num_factors,
item_data=item_data,
max_iterations=max_iterations,
verbose=verbose,
)
sim_model = (
tc.item_similarity_recommender.create(
observation_data=ratings_filtered,
user_id=cls.user_id_field,
item_id=cls.id_field,
target=cls.rating_id_field,
item_data=item_data,
verbose=verbose,
)
if similarity_model
else None
)
return cls(
model=model,
similarity_model=sim_model,
games=all_games,
ratings=ratings,
)
@classmethod
def load_games_csv(cls, games_csv, columns=None):
"""load games from CSV"""
columns = cls.columns_games if columns is None else columns
_, csv_cond = tempfile.mkstemp(text=True)
num_games = condense_csv(games_csv, csv_cond, columns.keys())
cls.logger.info("condensed %d games into <%s>", num_games, csv_cond)
games = tc.SFrame.read_csv(
csv_cond,
column_type_hints=columns,
usecols=columns.keys(),
)
try:
os.remove(csv_cond)
except Exception as exc:
cls.logger.exception(exc)
if cls.compilation_field in columns:
# pylint: disable=unexpected-keyword-arg
games[cls.compilation_field] = games[cls.compilation_field].apply(
bool,
skip_na=False,
)
if cls.cooperative_field in columns:
# pylint: disable=unexpected-keyword-arg
games[cls.cooperative_field] = games[cls.cooperative_field].apply(
bool,
skip_na=False,
)
return games
@classmethod
def load_games_json(cls, games_json, columns=None, orient="lines"):
"""load games from JSON"""
cls.logger.info("reading games from JSON file <%s>", games_json)
columns = cls.columns_games if columns is None else columns
games = tc.SFrame.read_json(url=games_json, orient=orient)
for col in columns:
if col not in games.column_names():
games[col] = None
if cls.compilation_field in games.column_names():
# pylint: disable=unexpected-keyword-arg
games[cls.compilation_field] = games[cls.compilation_field].apply(
bool,
skip_na=False,
)
if cls.cooperative_field in games.column_names():
games[cls.cooperative_field] = games[cls.cooperative_field].apply(
bool,
skip_na=False,
)
return games
# pylint: disable=unused-argument
@classmethod
def process_ratings(cls, ratings, **kwargs):
"""process ratings"""
return ratings
@classmethod
def load_ratings_csv(cls, ratings_csv, columns=None, **kwargs):
"""load ratings from CSV"""
columns = cls.columns_ratings if columns is None else columns
ratings = tc.SFrame.read_csv(
ratings_csv,
column_type_hints=columns,
usecols=columns.keys(),
).dropna()
return cls.process_ratings(ratings, **kwargs)
@classmethod
def load_ratings_json(cls, ratings_json, columns=None, orient="lines", **kwargs):
"""load ratings from JSON"""
columns = cls.columns_ratings if columns is None else columns
ratings = tc.SFrame.read_json(url=ratings_json, orient=orient)
ratings = ratings[columns].dropna()
return cls.process_ratings(ratings, **kwargs)
@classmethod
def train_from_files(
cls,
games_file,
ratings_file,
games_columns=None,
ratings_columns=None,
side_data_columns=None,
similarity_model=False,
num_factors=32,
max_iterations=100,
verbose=False,
defaults=True,