How to use the lifetimes.utils._check_inputs function in Lifetimes

To help you get started, we’ve selected a few Lifetimes examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github CamDavidsonPilon / lifetimes / tests / test_utils.py View on Github external
def test_check_inputs():
    frequency = np.array([0, 1, 2])
    recency = np.array([0, 1, 10])
    T = np.array([5, 6, 15])
    monetary_value = np.array([2.3, 490, 33.33])
    assert utils._check_inputs(frequency, recency, T, monetary_value) is None

    with pytest.raises(ValueError):
        bad_recency = T + 1
        utils._check_inputs(frequency, bad_recency, T)

    with pytest.raises(ValueError):
        bad_recency = recency.copy()
        bad_recency[0] = 1
        utils._check_inputs(frequency, bad_recency, T)

    with pytest.raises(ValueError):
        bad_freq = np.array([0, 0.5, 2])
        utils._check_inputs(bad_freq, recency, T)

    with pytest.raises(ValueError):
        bad_monetary_value = monetary_value.copy()
github CamDavidsonPilon / lifetimes / tests / test_utils.py View on Github external
def test_check_inputs():
    frequency = np.array([0, 1, 2])
    recency = np.array([0, 1, 10])
    T = np.array([5, 6, 15])
    monetary_value = np.array([2.3, 490, 33.33])
    assert utils._check_inputs(frequency, recency, T, monetary_value) is None

    with pytest.raises(ValueError):
        bad_recency = T + 1
        utils._check_inputs(frequency, bad_recency, T)

    with pytest.raises(ValueError):
        bad_recency = recency.copy()
        bad_recency[0] = 1
        utils._check_inputs(frequency, bad_recency, T)

    with pytest.raises(ValueError):
        bad_freq = np.array([0, 0.5, 2])
        utils._check_inputs(bad_freq, recency, T)

    with pytest.raises(ValueError):
        bad_monetary_value = monetary_value.copy()
        bad_monetary_value[0] = 0
        utils._check_inputs(frequency, recency, T, bad_monetary_value)
github CamDavidsonPilon / lifetimes / tests / test_utils.py View on Github external
T = np.array([5, 6, 15])
    monetary_value = np.array([2.3, 490, 33.33])
    assert utils._check_inputs(frequency, recency, T, monetary_value) is None

    with pytest.raises(ValueError):
        bad_recency = T + 1
        utils._check_inputs(frequency, bad_recency, T)

    with pytest.raises(ValueError):
        bad_recency = recency.copy()
        bad_recency[0] = 1
        utils._check_inputs(frequency, bad_recency, T)

    with pytest.raises(ValueError):
        bad_freq = np.array([0, 0.5, 2])
        utils._check_inputs(bad_freq, recency, T)

    with pytest.raises(ValueError):
        bad_monetary_value = monetary_value.copy()
        bad_monetary_value[0] = 0
        utils._check_inputs(frequency, recency, T, bad_monetary_value)
github CamDavidsonPilon / lifetimes / tests / test_utils.py View on Github external
def test_summary_data_from_transaction_data_obeys_data_contraints(example_summary_data):
    assert utils._check_inputs(example_summary_data['frequency'], example_summary_data['recency'], example_summary_data['T']) is None
github CamDavidsonPilon / lifetimes / tests / test_utils.py View on Github external
bad_recency = T + 1
        utils._check_inputs(frequency, bad_recency, T)

    with pytest.raises(ValueError):
        bad_recency = recency.copy()
        bad_recency[0] = 1
        utils._check_inputs(frequency, bad_recency, T)

    with pytest.raises(ValueError):
        bad_freq = np.array([0, 0.5, 2])
        utils._check_inputs(bad_freq, recency, T)

    with pytest.raises(ValueError):
        bad_monetary_value = monetary_value.copy()
        bad_monetary_value[0] = 0
        utils._check_inputs(frequency, recency, T, bad_monetary_value)
github CamDavidsonPilon / lifetimes / tests / test_utils.py View on Github external
def test_check_inputs():
    frequency = np.array([0, 1, 2])
    recency = np.array([0, 1, 10])
    T = np.array([5, 6, 15])
    monetary_value = np.array([2.3, 490, 33.33])
    assert utils._check_inputs(frequency, recency, T, monetary_value) is None

    with pytest.raises(ValueError):
        bad_recency = T + 1
        utils._check_inputs(frequency, bad_recency, T)

    with pytest.raises(ValueError):
        bad_recency = recency.copy()
        bad_recency[0] = 1
        utils._check_inputs(frequency, bad_recency, T)

    with pytest.raises(ValueError):
        bad_freq = np.array([0, 0.5, 2])
        utils._check_inputs(bad_freq, recency, T)

    with pytest.raises(ValueError):
        bad_monetary_value = monetary_value.copy()
        bad_monetary_value[0] = 0
        utils._check_inputs(frequency, recency, T, bad_monetary_value)
github CamDavidsonPilon / lifetimes / lifetimes / fitters / pareto_nbd_fitter.py View on Github external
Returns
        -------
        ParetoNBDFitter
            with additional properties like ``params_`` and methods like ``predict``
        """

        frequency = asarray(frequency).astype(int)
        recency = asarray(recency)
        T = asarray(T)

        if weights is None:
            weights = np.ones(recency.shape[0], dtype=np.int64)
        else:
            weights = asarray(weights)

        _check_inputs(frequency, recency, T)

        self._scale = _scale_time(T)
        scaled_recency = recency * self._scale
        scaled_T = T * self._scale

        params, self._negative_log_likelihood_ = self._fit(
            (frequency, scaled_recency, scaled_T, weights, self.penalizer_coef),
            iterative_fitting,
            initial_params,
            4,
            verbose,
            tol,
            fit_method,
            maxiter,
            **kwargs
        )
github CamDavidsonPilon / lifetimes / lifetimes / fitters / gamma_gamma_fitter.py View on Github external
index: array_like, optional
            index for resulted DataFrame which is accessible via self.data
        q_constraint: bool, optional
            when q < 1, population mean will result in a negative value
            leading to negative CLV outputs. If True, we penalize negative values of q to avoid this issue.
        kwargs:
            key word arguments to pass to the scipy.optimize.minimize
            function as options dict

        Returns
        -------
        GammaGammaFitter
            fitted and with parameters estimated
        """

        _check_inputs(frequency, monetary_value=monetary_value)

        frequency = np.asarray(frequency).astype(float)
        monetary_value = np.asarray(monetary_value).astype(float)

        if weights is None:
            weights = np.ones_like(frequency, dtype=int)
        else:
            weights = np.asarray(weights)

        log_params, self._negative_log_likelihood_, self._hessian_ = self._fit(
            (frequency, monetary_value, weights, self.penalizer_coef),
            initial_params,
            3,
            verbose,
            tol=tol,
            bounds=((None, None), (0, None), (None, None)) if q_constraint else None,
github CamDavidsonPilon / lifetimes / lifetimes / fitters / beta_geo_fitter.py View on Github external
index: array_like, optional
            index for resulted DataFrame which is accessible via self.data
        kwargs:
            key word arguments to pass to the scipy.optimize.minimize
            function as options dict

        Returns
        -------
        BetaGeoFitter
            with additional properties like ``params_`` and methods like ``predict``
        """

        frequency = np.asarray(frequency).astype(int)
        recency = np.asarray(recency)
        T = np.asarray(T)
        _check_inputs(frequency, recency, T)

        if weights is None:
            weights = np.ones_like(recency, dtype=int)
        else:
            weights = np.asarray(weights)

        self._scale = _scale_time(T)
        scaled_recency = recency * self._scale
        scaled_T = T * self._scale

        log_params_, self._negative_log_likelihood_, self._hessian_ = self._fit(
            (frequency, scaled_recency, scaled_T, weights, self.penalizer_coef),
            initial_params,
            4,
            verbose,
            tol,