Skip to content

Commit 4fabb55

Browse files
committed
update ItemKNN
1 parent c9f08f8 commit 4fabb55

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

lkauto/algorithms/item_knn.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,44 @@
1-
from lenskit.algorithms import item_knn
2-
from ConfigSpace import Integer, Float
1+
from lenskit.knn.item import ItemKNNScorer, ItemKNNConfig
2+
from ConfigSpace import UniformIntegerHyperparameter, UniformFloatHyperparameter
33
from ConfigSpace import ConfigurationSpace
44

55

6-
class ItemItem(item_knn.ItemItem):
7-
def __init__(self, nnbrs, **kwargs):
8-
super().__init__(nnbrs=nnbrs, **kwargs)
6+
class ItemItem(ItemKNNScorer):
7+
def __init__(self, max_nbrs, min_nbrs=1, min_sim=1e-6, save_nbrs=None, feedback='explicit', block_size=250, **kwargs):
8+
config = ItemKNNConfig(
9+
max_nbrs=max_nbrs,
10+
min_nbrs=min_nbrs,
11+
min_sim=min_sim,
12+
save_nbrs=save_nbrs,
13+
feedback=feedback,
14+
block_size=block_size
15+
)
16+
super().__init__(config=config, **kwargs)
917

1018
@staticmethod
1119
def get_default_configspace(**kwargs):
1220
"""
13-
return default configurationspace
14-
Default configuration spaces for hyperparameters are defined here.
21+
return default configurationspace
22+
Default configuration spaces for hyperparameters are defined here.
1523
"""
1624

1725
"""
18-
The nnbrs hyperparameter is set to 10000 in LensKit-Auto. Generally speaking, the higher the nnbrs
26+
The max_nbrs hyperparameter is set to 10000 in LensKit-Auto. Generally speaking, the higher the max_nbrs
1927
hyperparameter value, the better the performance. But the computational cost will increase
20-
exponentially by increasing the nnbrs hyperparameter value. 10000 is a reasonable value for nnbrs
28+
exponentially by increasing the max_nbrs hyperparameter value. 10000 is a reasonable value for max_nbrs
2129
hyperparameter since it has relatively good performance and is still able
2230
to run in a reasonable amount of time.
2331
"""
24-
nnbrs = Integer('nnbrs', bounds=(1, 10000), default=1000, log=True) # No default value given by LensKit
32+
max_nbrs = UniformIntegerHyperparameter('max_nbrs', lower=1, upper=10000, default_value=1000, log=True)
2533

2634
"""
2735
The min_sim hyperparameter describes the minimum number of neighbors for scoring each item.
2836
Since the LensKit default value for the min_nbrs hyperparameter is 1, we set the lower bound to 1.
29-
The upper bound is set to the nnbrs hyperparameter value.
37+
The upper bound is set to the max_nbrs hyperparameter value.
3038
Therefore, the upper bound of min_nbrs is set to 10000 to cover the full possible range of the
3139
min_nbrs hyperparameter.
3240
"""
33-
min_nbrs = Integer('min_nbrs', bounds=(1, 1000), default=1, log=True)
41+
min_nbrs = UniformIntegerHyperparameter('min_nbrs', lower=1, upper=1000, default_value=1, log=True)
3442

3543
"""
3644
The min_sim hyperparameter describes the minimum threshold for similarity between items. It is commonly
@@ -46,9 +54,9 @@ def get_default_configspace(**kwargs):
4654
Since the paper already states that it is very difficult to find the best value, we define a large bound around
4755
the default LensKit value.
4856
"""
49-
min_sim = Float('min_sim', bounds=(1.0e-10, 1.0e-2), default=1.0e-6)
57+
min_sim = UniformFloatHyperparameter('min_sim', lower=1.0e-10, upper=1.0e-2, default_value=1.0e-6, log=True)
5058

5159
cs = ConfigurationSpace()
52-
cs.add_hyperparameters([min_nbrs, min_sim, nnbrs])
60+
cs.add([max_nbrs, min_nbrs, min_sim])
5361

5462
return cs

0 commit comments

Comments
 (0)