irspack.recommenders.NMFRecommender#

class irspack.recommenders.NMFRecommender(X_train_all, n_components=64, alpha=0.01, l1_ratio=0.01, beta_loss='frobenius', init=None)[source]#

Bases: BaseRecommender

Parameters:
  • X_train_all (csr_matrix)

  • n_components (int)

  • alpha (float)

  • l1_ratio (float)

  • beta_loss (str)

  • init (str | None)

__init__(X_train_all, n_components=64, alpha=0.01, l1_ratio=0.01, beta_loss='frobenius', init=None)[source]#
Parameters:
  • X_train_all (csr_matrix | csc_matrix)

  • n_components (int)

  • alpha (float)

  • l1_ratio (float)

  • beta_loss (str)

  • init (str | None)

Methods

__init__(X_train_all[, n_components, alpha, ...])

default_suggest_parameter(trial, ...)

from_config(X_train_all, config)

get_score(user_indices)

Compute the item recommendation score for a subset of users.

get_score_block(begin, end)

Compute the score for a block of the users.

get_score_cold_user(X)

Compute the item recommendation score for unseen users whose profiles are given as another user-item relation matrix.

get_score_cold_user_remove_seen(X)

Compute the item recommendation score for unseen users whose profiles are given as another user-item relation matrix.

get_score_remove_seen(user_indices)

Compute the item score and mask the item in the training set.

get_score_remove_seen_block(begin, end)

Compute the score for a block of the users, and mask the items in the training set.

learn()

Learns and returns itself.

learn_with_optimizer(evaluator, trial[, ...])

Learning procedures with early stopping and pruning.

tune(data, evaluator[, study, n_trials, ...])

Perform the optimization step.

Attributes

default_tune_range

X_train_all

The matrix to feed into recommender.

X_train_all: sps.csr_matrix#

The matrix to feed into recommender.

config_class#

alias of NMFConfig

get_score(user_indices)[source]#

Compute the item recommendation score for a subset of users.

Parameters:

user_indices (ndarray) – The index defines the subset of users.

Returns:

The item scores. Its shape will be (len(user_indices), self.n_items)

Return type:

ndarray

get_score_block(begin, end)#

Compute the score for a block of the users.

Parameters:
  • begin (int) – where the evaluated user block begins.

  • end (int) – where the evaluated user block ends.

Returns:

The item scores. Its shape will be (end - begin, self.n_items)

Return type:

ndarray

get_score_cold_user(X)[source]#

Compute the item recommendation score for unseen users whose profiles are given as another user-item relation matrix.

Parameters:

X (csr_matrix | csc_matrix) – The profile user-item relation matrix for unseen users. Its number of rows is arbitrary, but the number of columns must be self.n_items.

Returns:

Computed item scores for users. Its shape is equal to X.

Return type:

ndarray

get_score_cold_user_remove_seen(X)#

Compute the item recommendation score for unseen users whose profiles are given as another user-item relation matrix. The score will then be masked by the input.

Parameters:

X (csr_matrix | csc_matrix) – The profile user-item relation matrix for unseen users. Its number of rows is arbitrary, but the number of columns must be self.n_items.

Returns:

Computed & masked item scores for users. Its shape is equal to X.

Return type:

ndarray

get_score_remove_seen(user_indices)#

Compute the item score and mask the item in the training set. Masked items will have the score -inf.

Parameters:

user_indices (ndarray) – Specifies the subset of users.

Returns:

The masked item scores. Its shape will be (len(user_indices), self.n_items)

Return type:

ndarray

get_score_remove_seen_block(begin, end)#

Compute the score for a block of the users, and mask the items in the training set. Masked items will have the score -inf.

Parameters:
  • begin (int) – where the evaluated user block begins.

  • end (int) – where the evaluated user block ends.

Returns:

The masked item scores. Its shape will be (end - begin, self.n_items)

Return type:

ndarray

learn()#

Learns and returns itself.

Returns:

The model after fitting process.

Parameters:

self (R)

Return type:

R

learn_with_optimizer(evaluator, trial, max_epoch=128, validate_epoch=5, score_degradation_max=5)#

Learning procedures with early stopping and pruning.

Parameters:
  • evaluator (ForwardRef('evaluation.Evaluator') | None) – The evaluator to measure the score.

  • trial (ForwardRef('Trial') | None) – The current optuna trial under the study (if any.)

  • max_epoch (int) – Maximal number of epochs. If iterative learning procedure is not available, this parameter will be ignored. Defaults to 128.

  • validate_epoch (int) – The frequency of validation score measurement. If iterative learning procedure is not available, this parameter will be ignored. Defaults to 5.

  • validate_epoch – The frequency of validation score measurement. If iterative learning procedure is not available, this parameter will be ignored. Defaults to 5.

  • score_degradation_max (int) – Maximal number of allowed score degradation. If iterative learning procedure is not available, this parameter will be ignored. Defaults to 5.

Return type:

None

classmethod tune(data, evaluator, study=None, n_trials=20, timeout=None, data_suggest_function=None, parameter_suggest_function=None, tuning_random_seed=None, prunning_n_startup_trials=10, max_epoch=128, validate_epoch=5, score_degradation_max=5, logger=None, **recommender_params)#

Perform the optimization step. An optuna.Study object can be supplied or created inside this function.

Parameters:
  • data (csr_matrix | csc_matrix | None) – The training data. You can also provide tunable parameter dependent training data by providing data_suggest_function. In that case, data must be None.

  • evaluator (evaluation.Evaluator) – The validation evaluator that measures the performance of the recommenders.

  • study (ForwardRef('Study') | None) – An existing Optuna study. If None, a new study is created.

  • n_trials (int) – The number of expected trials (including pruned ones). Defaults to 20.

  • timeout (int | None) – If set to some value (in seconds), the study will exit after that time period. Note that the running trials is not interrupted, though. Defaults to None.

  • data_suggest_function (Callable[[ForwardRef('Trial')], csr_matrix | csc_matrix] | None) – If not None, this must be a function which takes optuna.Trial as its argument and returns training data. Defaults to None.

  • parameter_suggest_function (Callable[[ForwardRef('Trial')], Dict[str, Any]] | None) – If not None, this must be a function which takes optuna.Trial as its argument and returns Dict[str, Any] (i.e., some keyword arguments of the recommender class). If None, cls.default_suggest_parameter will be used for the parameter suggestion. Defaults to None.

  • tuning_random_seed (int | None) – The random seed to control optuna.samplers.TPESampler. Defaults to None. Ignored when study is provided.

  • prunning_n_startup_trials (int) – n_startup_trials argument passed to the constructor of optuna.pruners.MedianPruner.

  • max_epoch (int) – The maximal number of epochs for the training. If iterative learning procedure is not available, this parameter will be ignored.

  • validate_epoch (int, optional) – The frequency of validation score measurement. If iterative learning procedure is not available, this parameter will be ignored. Defaults to 5.

  • score_degradation_max (int, optional) – Maximal number of allowed score degradation. If iterative learning procedure is not available, this parameter will be ignored. Defaults to 5. Defaults to 5.

  • **recommender_params (Any) – Fixed keyword arguments passed to the recommender constructor for every trial. These override suggested parameters with the same name, and are not included in the returned best parameters.

  • logger (Logger | None)

  • **recommender_params

Returns:

A tuple that consists of

  1. A dict containing the best suggested and learnt parameters. Fixed recommender_params are not included.

  2. A pandas.DataFrame that contains the history of optimization.

Return type:

Tuple[Dict[str, Any], DataFrame]