Skip to content

Commit 72eb110

Browse files
committed
update UserKNN algorithm
1 parent 6d609cd commit 72eb110

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

lkauto/algorithms/user_knn.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,40 @@
1-
from lenskit.algorithms import user_knn
2-
from ConfigSpace import Integer, Float
1+
from lenskit.knn.user import UserKNNScorer, UserKNNConfig
2+
from ConfigSpace import UniformIntegerHyperparameter, UniformFloatHyperparameter
33
from ConfigSpace import ConfigurationSpace
44

55

6-
class UserUser(user_knn.UserUser):
7-
def __init__(self, nnbrs, **kwargs):
8-
super().__init__(nnbrs=nnbrs, **kwargs)
6+
class UserUser(UserKNNScorer):
7+
def __init__(self, max_nbrs, min_nbrs=1, min_sim=1e-6, **kwargs):
8+
config= UserKNNConfig(
9+
max_nbrs=max_nbrs,
10+
min_nbrs=min_nbrs,
11+
min_sim=min_sim,
12+
**kwargs
13+
)
14+
super().__init__(config=config, **kwargs)
915

1016
@staticmethod
1117
def get_default_configspace(**kwargs):
1218
"""
1319
return default configuration spaces for hyperparameter
1420
"""
1521
"""
16-
The nnbrs hyperparameter is set to 10000 in LensKit-Auto. Generally speaking, the higher the nnbrs
22+
The max_nbrs hyperparameter is set to 10000 in LensKit-Auto. Generally speaking, the higher the max_nbrs
1723
hyperparameter value, the better the performance. But the computational cost will increase
18-
exponentially by increasing the nnbrs hyperparameter value. 10000 is a reasonable value for nnbrs
24+
exponentially by increasing the max_nbrs hyperparameter value. 10000 is a reasonable value for max_nbrs
1925
hyperparameter since it has relatively good performance and is still able
2026
to run in a reasonable amount of time.
2127
"""
22-
nnbrs = Integer('nnbrs', bounds=(1, 10000), default=1000, log=True) # No default value given by LensKit
28+
max_nbrs = UniformIntegerHyperparameter('max_nbrs', lower=1, upper=10000, default_value=1000, log=True)
2329

2430
"""
25-
The min_sim hyperparameter describes the minimum number of neighbors for scoring each item.
31+
The min_nbrs hyperparameter describes the minimum number of neighbors for scoring each item.
2632
Since the LensKit default value for the min_nbrs hyperparameter is 1, we set the lower bound to 1.
27-
The upper bound is set to the nnbrs hyperparameter value.
33+
The upper bound is set to the max_nbrs hyperparameter value.
2834
Therefore, the upper bound of min_nbrs is set to 10000 to cover the full possible range of the
2935
min_nbrs hyperparameter.
3036
"""
31-
min_nbrs = Integer('min_nbrs', bounds=(1, 1000), default=1, log=True)
37+
min_nbrs = UniformIntegerHyperparameter('min_nbrs', lower=1, upper=1000, default_value=1, log=True)
3238

3339
"""
3440
The min_sim hyperparameter describes the minimum threshold for similarity between items. It is commonly
@@ -44,9 +50,9 @@ def get_default_configspace(**kwargs):
4450
Since the paper already states that it is very difficult to find the best value, we define a large bound around
4551
the default LensKit value.
4652
"""
47-
min_sim = Float('min_sim', bounds=(1.0e-10, 1.0e-2), default=1.0e-6)
53+
min_sim = UniformFloatHyperparameter('min_sim', lower=1.0e-10, upper=1.0e-2, default_value=1.0e-6, log=True)
4854

4955
cs = ConfigurationSpace()
50-
cs.add_hyperparameters([min_nbrs, min_sim, nnbrs])
56+
cs.add([max_nbrs, min_nbrs, min_sim])
5157

5258
return cs

0 commit comments

Comments
 (0)