BracketError: The algorithm terminated without finding a valid bracket. Consider trying different initial points

945 Views Asked by At
# Stats
from scipy.stats import skew
from scipy.special import boxcox1p
from scipy.stats import boxcox_normmax

numeric_features = df_train.dtypes[df_train.dtypes != "object"].index
skewed_features = df_train[numeric_features].apply(lambda x: skew(x)).sort_values(ascending=False)
print(skewed_features)

high_skewness = skewed_features[abs(skewed_features) > 0.9]
skewed_features = high_skewness.index

for feature in skewed_features:
    # Calculate the lambda parameter using boxcox_normmax
    lam = boxcox_normmax(df_train[feature] + 1)
    
    # Apply the Box-Cox transformation using boxcox1p
    df_train[feature] = boxcox1p(df_train[feature], lam)

I tried the above code but unfortunately I am facing few problems. Here, I am applying box cox transformation on skewed features.

---------------------------------------------------------------------------
BracketError                              Traceback (most recent call last)
Cell In[208], line 15
     11 skewed_features = high_skewness.index
     13 for feature in skewed_features:
     14     # Calculate the lambda parameter using boxcox_normmax
---> 15     lam = boxcox_normmax(df_train[feature] + 1)
     17     # Apply the Box-Cox transformation using boxcox1p
     18     df_train[feature] = boxcox1p(df_train[feature], lam)

File /opt/conda/lib/python3.10/site-packages/scipy/stats/_morestats.py:1276, in boxcox_normmax(x, brack, method, optimizer)
   1273     raise ValueError("Method %s not recognized." % method)
   1275 optimfunc = methods[method]
-> 1276 res = optimfunc(x)
   1277 if res is None:
   1278     message = ("`optimizer` must return an object containing the optimal "
   1279                "`lmbda` in attribute `x`")

File /opt/conda/lib/python3.10/site-packages/scipy/stats/_morestats.py:1254, in boxcox_normmax.<locals>._pearsonr(x)
   1251     r, prob = _stats_py.pearsonr(xvals, yvals)
   1252     return 1 - r
-> 1254 return _optimizer(_eval_pearsonr, args=(xvals, x))

File /opt/conda/lib/python3.10/site-packages/scipy/stats/_morestats.py:1223, in boxcox_normmax.<locals>._optimizer(func, args)
   1222 def _optimizer(func, args):
-> 1223     return optimize.brent(func, args=args, brack=brack)

File /opt/conda/lib/python3.10/site-packages/scipy/optimize/_optimize.py:2642, in brent(func, args, brack, tol, full_output, maxiter)
   2570 """
   2571 Given a function of one variable and a possible bracket, return
   2572 a local minimizer of the function isolated to a fractional precision
   (...)
   2638 
   2639 """
   2640 options = {'xtol': tol,
   2641            'maxiter': maxiter}
-> 2642 res = _minimize_scalar_brent(func, brack, args, **options)
   2643 if full_output:
   2644     return res['x'], res['fun'], res['nit'], res['nfev']

File /opt/conda/lib/python3.10/site-packages/scipy/optimize/_optimize.py:2679, in _minimize_scalar_brent(func, brack, args, xtol, maxiter, disp, **unknown_options)
   2676 brent = Brent(func=func, args=args, tol=tol,
   2677               full_output=True, maxiter=maxiter, disp=disp)
   2678 brent.set_bracket(brack)
-> 2679 brent.optimize()
   2680 x, fval, nit, nfev = brent.get_result(full_output=True)
   2682 success = nit < maxiter and not (np.isnan(x) or np.isnan(fval))

File /opt/conda/lib/python3.10/site-packages/scipy/optimize/_optimize.py:2449, in Brent.optimize(self)
   2446 def optimize(self):
   2447     # set up for optimization
   2448     func = self.func
-> 2449     xa, xb, xc, fa, fb, fc, funcalls = self.get_bracket_info()
   2450     _mintol = self._mintol
   2451     _cg = self._cg

File /opt/conda/lib/python3.10/site-packages/scipy/optimize/_optimize.py:2418, in Brent.get_bracket_info(self)
   2416     xa, xb, xc, fa, fb, fc, funcalls = bracket(func, args=args)
   2417 elif len(brack) == 2:
-> 2418     xa, xb, xc, fa, fb, fc, funcalls = bracket(func, xa=brack[0],
   2419                                                xb=brack[1], args=args)
   2420 elif len(brack) == 3:
   2421     xa, xb, xc = brack

File /opt/conda/lib/python3.10/site-packages/scipy/optimize/_optimize.py:3048, in bracket(func, xa, xb, args, grow_limit, maxiter)
   3046     e = BracketError(msg)
   3047     e.data = (xa, xb, xc, fa, fb, fc, funcalls)
-> 3048     raise e
   3050 return xa, xb, xc, fa, fb, fc, funcalls

BracketError: The algorithm terminated without finding a valid bracket. Consider trying different initial points.

But, I got the above mentioned error. Here, compiler said algorithm terminated without finding a valid bracket. But, as far as I guess code is ok. So, I don't understand what is the main problem here.

0

There are 0 best solutions below