Skip to content

neighbors.models.Mean

The Mean algorithm simply uses the mean of other users to make predictions about items. It's primarily useful as a good baseline model.

Source code in neighbors/models.py
class Mean(Base):
    """
    The Mean algorithm simply uses the mean of other users to make predictions about items. It's primarily useful as a good baseline model.
    """

    def __init__(
        self, data, mask=None, n_mask_items=None, verbose=True, random_state=None
    ):
        """
        Args:
            data (pd.DataFrame): users x items dataframe
            mask (pd.DataFrame, optional): A boolean dataframe used to split the data into 'observed' and 'missing' datasets. Defaults to None.
            n_mask_items (int/float, optional): number of items to mask out, while the rest are treated as observed; Defaults to None.
            data_range (int/float, optional): max - min of the data; Default computed from the input data. This is useful to set manually in case the input data do not span the full range of possible values
            random_state (None, int, RandomState): a seed or random state used for all internal random operations (e.g. randomly mask half the data given n_mask_item = .05). Passing None will generate a new random seed. Default None.
            verbose (bool; optional): print any initialization warnings; Default True

        """
        super().__init__(
            data, mask, n_mask_items, random_state=random_state, verbose=verbose
        )
        self.mean = None

    def fit(self, dilate_by_nsamples=None, axis=0, **kwargs):
        """Fit model to train data. Simply learns item-wise mean using observed (non-missing) values.

        Args:
            dilate_ts_n_samples (int): will dilate masked samples by n_samples to leverage auto-correlation in estimating time-series data
            axis (int): dimension along which to compute mean, 0 = mean across users separately by item, 1 = mean across items separately by user; Default 0

        """

        # Call parent fit which acts as a guard for non-masked data
        super().fit()

        self.dilate_mask(n_samples=dilate_by_nsamples)
        self.mean = self.masked_data.mean(skipna=True, axis=axis)
        self._predict()
        self.is_fit = True

    def _predict(self):
        """Predict missing items using other subject's item means."""

        # Always predict mean (learned on observed values) for observed and missing values
        self.predictions = pd.concat([self.mean] * self.data.shape[0], axis=1).T
        self.predictions.index = self.data.index
        self.predictions.columns = self.data.columns

__init__(self, data, mask=None, n_mask_items=None, verbose=True, random_state=None) special

Parameters:

Name Type Description Default
data pd.DataFrame

users x items dataframe

required
mask pd.DataFrame

A boolean dataframe used to split the data into 'observed' and 'missing' datasets. Defaults to None.

None
n_mask_items int/float

number of items to mask out, while the rest are treated as observed; Defaults to None.

None
data_range int/float

max - min of the data; Default computed from the input data. This is useful to set manually in case the input data do not span the full range of possible values

required
random_state None, int, RandomState

a seed or random state used for all internal random operations (e.g. randomly mask half the data given n_mask_item = .05). Passing None will generate a new random seed. Default None.

None
verbose bool; optional

print any initialization warnings; Default True

True
Source code in neighbors/models.py
def __init__(
    self, data, mask=None, n_mask_items=None, verbose=True, random_state=None
):
    """
    Args:
        data (pd.DataFrame): users x items dataframe
        mask (pd.DataFrame, optional): A boolean dataframe used to split the data into 'observed' and 'missing' datasets. Defaults to None.
        n_mask_items (int/float, optional): number of items to mask out, while the rest are treated as observed; Defaults to None.
        data_range (int/float, optional): max - min of the data; Default computed from the input data. This is useful to set manually in case the input data do not span the full range of possible values
        random_state (None, int, RandomState): a seed or random state used for all internal random operations (e.g. randomly mask half the data given n_mask_item = .05). Passing None will generate a new random seed. Default None.
        verbose (bool; optional): print any initialization warnings; Default True

    """
    super().__init__(
        data, mask, n_mask_items, random_state=random_state, verbose=verbose
    )
    self.mean = None

fit(self, dilate_by_nsamples=None, axis=0, **kwargs)

Fit model to train data. Simply learns item-wise mean using observed (non-missing) values.

Parameters:

Name Type Description Default
dilate_ts_n_samples int

will dilate masked samples by n_samples to leverage auto-correlation in estimating time-series data

required
axis int

dimension along which to compute mean, 0 = mean across users separately by item, 1 = mean across items separately by user; Default 0

0
Source code in neighbors/models.py
def fit(self, dilate_by_nsamples=None, axis=0, **kwargs):
    """Fit model to train data. Simply learns item-wise mean using observed (non-missing) values.

    Args:
        dilate_ts_n_samples (int): will dilate masked samples by n_samples to leverage auto-correlation in estimating time-series data
        axis (int): dimension along which to compute mean, 0 = mean across users separately by item, 1 = mean across items separately by user; Default 0

    """

    # Call parent fit which acts as a guard for non-masked data
    super().fit()

    self.dilate_mask(n_samples=dilate_by_nsamples)
    self.mean = self.masked_data.mean(skipna=True, axis=axis)
    self._predict()
    self.is_fit = True