本文整理汇总了Python中scipy.stats.boxcox方法的典型用法代码示例。如果您正苦于以下问题:Python stats.boxcox方法的具体用法?Python stats.boxcox怎么用?Python stats.boxcox使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.stats
的用法示例。
在下文中一共展示了stats.boxcox方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _estimate_lambda_single_y
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import boxcox [as 别名]
def _estimate_lambda_single_y(y):
"""Estimate lambda for a single y, given a range of lambdas
through which to search. No validation performed.
Parameters
----------
y : ndarray, shape (n_samples,)
The vector being estimated against
"""
# ensure is array
y = np.array(y)
# Use scipy's log-likelihood estimator
b = boxcox(y, lmbda=None)
# Return lambda corresponding to maximum P
return b[1]
示例2: _yeo_johnson_optimize
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import boxcox [as 别名]
def _yeo_johnson_optimize(self, x):
"""Find and return optimal lambda parameter of the Yeo-Johnson
transform by MLE, for observed data x.
Like for Box-Cox, MLE is done via the brent optimizer.
"""
def _neg_log_likelihood(lmbda):
"""Return the negative log likelihood of the observed data x as a
function of lambda."""
x_trans = self._yeo_johnson_transform(x, lmbda)
n_samples = x.shape[0]
loglike = -n_samples / 2 * np.log(x_trans.var())
loglike += (lmbda - 1) * (np.sign(x) * np.log1p(np.abs(x))).sum()
return -loglike
# the computation of lambda is influenced by NaNs so we need to
# get rid of them
x = x[~np.isnan(x)]
# choosing bracket -2, 2 like for boxcox
return optimize.brent(_neg_log_likelihood, brack=(-2, 2))
示例3: test_power_transformer_1d
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import boxcox [as 别名]
def test_power_transformer_1d():
X = np.abs(X_1col)
for standardize in [True, False]:
pt = PowerTransformer(method='box-cox', standardize=standardize)
X_trans = pt.fit_transform(X)
X_trans_func = power_transform(
X, method='box-cox',
standardize=standardize
)
X_expected, lambda_expected = stats.boxcox(X.flatten())
if standardize:
X_expected = scale(X_expected)
assert_almost_equal(X_expected.reshape(-1, 1), X_trans)
assert_almost_equal(X_expected.reshape(-1, 1), X_trans_func)
assert_almost_equal(X, pt.inverse_transform(X_trans))
assert_almost_equal(lambda_expected, pt.lambdas_[0])
assert len(pt.lambdas_) == X.shape[1]
assert isinstance(pt.lambdas_, np.ndarray)
示例4: test_alpha
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import boxcox [as 别名]
def test_alpha(self):
np.random.seed(1234)
x = stats.loggamma.rvs(5, size=50) + 5
# Some regular values for alpha, on a small sample size
_, _, interval = stats.boxcox(x, alpha=0.75)
assert_allclose(interval, [4.004485780226041, 5.138756355035744])
_, _, interval = stats.boxcox(x, alpha=0.05)
assert_allclose(interval, [1.2138178554857557, 8.209033272375663])
# Try some extreme values, see we don't hit the N=500 limit
x = stats.loggamma.rvs(7, size=500) + 15
_, _, interval = stats.boxcox(x, alpha=0.001)
assert_allclose(interval, [0.3988867, 11.40553131])
_, _, interval = stats.boxcox(x, alpha=0.999)
assert_allclose(interval, [5.83316246, 5.83735292])
示例5: fit
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import boxcox [as 别名]
def fit(self, x: np.ndarray, y=None):
"""Fit a Gaussianizing transformation to each variable/column in x."""
# Initialize coefficients again with an empty list. Otherwise
# calling .fit() repeatedly will augment previous .coefs_ list.
self.coefs_ = []
x = _update_x(x)
if self.verbose:
print("Gaussianizing with strategy='%s'" % self.strategy)
if self.strategy == "lambert":
_get_coef = lambda vec: igmm(vec, self.tol, max_iter=self.max_iter)
elif self.strategy == "brute":
_get_coef = lambda vec: None # TODO: In principle, we could store parameters to do a quasi-invert
elif self.strategy == "boxcox":
_get_coef = lambda vec: boxcox(vec)[1]
else:
raise NotImplementedError("stategy='%s' not implemented." % self.strategy)
for x_i in x.T:
self.coefs_.append(_get_coef(x_i))
return self
示例6: fit
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import boxcox [as 别名]
def fit(self, y, exogenous=None):
"""Fit the transformer
Learns the value of ``lmbda``, if not specified in the constructor.
If defined in the constructor, is not re-learned.
Parameters
----------
y : array-like or None, shape=(n_samples,)
The endogenous (time-series) array.
exogenous : array-like or None, shape=(n_samples, n_features), optional
The exogenous array of additional covariates. Not used for
endogenous transformers. Default is None, and non-None values will
serve as pass-through arrays.
"""
lam1 = self.lmbda
lam2 = self.lmbda2
if lam2 < 0:
raise ValueError("lmbda2 must be a non-negative scalar value")
if lam1 is None:
y, _ = self._check_y_exog(y, exogenous)
_, lam1 = stats.boxcox(y + lam2, lmbda=None, alpha=None)
self.lam1_ = lam1
self.lam2_ = lam2
return self
示例7: test_boxcox_bad_arg
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import boxcox [as 别名]
def test_boxcox_bad_arg():
"""Raise ValueError if any data value is negative."""
x = np.array([-1])
assert_raises(ValueError, stats.boxcox, x)
示例8: _fit
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import boxcox [as 别名]
def _fit(self, X, y=None, force_transform=False):
X = self._check_input(X, check_positive=True, check_method=True)
if not self.copy and not force_transform: # if call from fit()
X = X.copy() # force copy so that fit does not change X inplace
optim_function = {'box-cox': self._box_cox_optimize,
'yeo-johnson': self._yeo_johnson_optimize
}[self.method]
with np.errstate(invalid='ignore'): # hide NaN warnings
self.lambdas_ = np.array([optim_function(col) for col in X.T])
if self.standardize or force_transform:
transform_function = {'box-cox': boxcox,
'yeo-johnson': self._yeo_johnson_transform
}[self.method]
for i, lmbda in enumerate(self.lambdas_):
with np.errstate(invalid='ignore'): # hide NaN warnings
X[:, i] = transform_function(X[:, i], lmbda)
if self.standardize:
self._scaler = StandardScaler(copy=False)
if force_transform:
X = self._scaler.fit_transform(X)
else:
self._scaler.fit(X)
return X
示例9: transform
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import boxcox [as 别名]
def transform(self, X):
"""Apply the power transform to each feature using the fitted lambdas.
Parameters
----------
X : array-like, shape (n_samples, n_features)
The data to be transformed using a power transformation.
Returns
-------
X_trans : array-like, shape (n_samples, n_features)
The transformed data.
"""
check_is_fitted(self, 'lambdas_')
X = self._check_input(X, check_positive=True, check_shape=True)
transform_function = {'box-cox': boxcox,
'yeo-johnson': self._yeo_johnson_transform
}[self.method]
for i, lmbda in enumerate(self.lambdas_):
with np.errstate(invalid='ignore'): # hide NaN warnings
X[:, i] = transform_function(X[:, i], lmbda)
if self.standardize:
X = self._scaler.transform(X)
return X
示例10: _box_cox_optimize
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import boxcox [as 别名]
def _box_cox_optimize(self, x):
"""Find and return optimal lambda parameter of the Box-Cox transform by
MLE, for observed data x.
We here use scipy builtins which uses the brent optimizer.
"""
# the computation of lambda is influenced by NaNs so we need to
# get rid of them
_, lmbda = stats.boxcox(x[~np.isnan(x)], lmbda=None)
return lmbda
示例11: test_power_transformer_boxcox_strictly_positive_exception
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import boxcox [as 别名]
def test_power_transformer_boxcox_strictly_positive_exception():
# Exceptions should be raised for negative arrays and zero arrays when
# method is boxcox
pt = PowerTransformer(method='box-cox')
pt.fit(np.abs(X_2d))
X_with_negatives = X_2d
not_positive_message = 'strictly positive'
assert_raise_message(ValueError, not_positive_message,
pt.transform, X_with_negatives)
assert_raise_message(ValueError, not_positive_message,
pt.fit, X_with_negatives)
assert_raise_message(ValueError, not_positive_message,
power_transform, X_with_negatives, 'box-cox')
assert_raise_message(ValueError, not_positive_message,
pt.transform, np.zeros(X_2d.shape))
assert_raise_message(ValueError, not_positive_message,
pt.fit, np.zeros(X_2d.shape))
assert_raise_message(ValueError, not_positive_message,
power_transform, np.zeros(X_2d.shape), 'box-cox')
示例12: fit_transform
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import boxcox [as 别名]
def fit_transform(self, X: dt.Frame, y: np.array = None):
XX = X.to_pandas().iloc[:, 0].values
is_na = np.isnan(XX)
self._offset = -np.nanmin(XX) if np.nanmin(XX) < 0 else 0
self._offset += 1e-3
self._lmbda = None
if not any(~is_na):
return X
x = self._offset + XX[~is_na]
x = np.asarray(x)
x[x <= 0] = 1e-3
self._lmbda = boxcox(x, lmbda=self._lmbda)[1] # compute lambda
return self.transform(X)
示例13: transform
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import boxcox [as 别名]
def transform(self, X: dt.Frame):
XX = X.to_pandas().iloc[:, 0].values
is_na = np.isnan(XX) | np.array(XX <= -self._offset)
if not any(~is_na) or self._lmbda is None:
return X
x = self._offset + XX[~is_na]
x = np.asarray(x)
x[x <= 0] = 1e-3 # don't worry if not invertible, just ensure can transform and valid transforms are kept valid
ret = boxcox(x, lmbda=self._lmbda) # apply transform with pre-computed lambda
XX[~is_na] = ret
return XX
示例14: test_2d_input
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import boxcox [as 别名]
def test_2d_input(self):
# Note: boxcox_llf() was already working with 2-D input (sort of), so
# keep it like that. boxcox() doesn't work with 2-D input though, due
# to brent() returning a scalar.
np.random.seed(54321)
x = stats.norm.rvs(size=100, loc=10)
lmbda = 1
llf = stats.boxcox_llf(lmbda, x)
llf2 = stats.boxcox_llf(lmbda, np.vstack([x, x]).T)
assert_allclose([llf, llf], llf2, rtol=1e-12)
示例15: test_fixed_lmbda
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import boxcox [as 别名]
def test_fixed_lmbda(self):
np.random.seed(12345)
x = stats.loggamma.rvs(5, size=50) + 5
xt = stats.boxcox(x, lmbda=1)
assert_allclose(xt, x - 1)
xt = stats.boxcox(x, lmbda=-1)
assert_allclose(xt, 1 - 1/x)
xt = stats.boxcox(x, lmbda=0)
assert_allclose(xt, np.log(x))
# Also test that array_like input works
xt = stats.boxcox(list(x), lmbda=0)
assert_allclose(xt, np.log(x))