Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Silencing Warning and printing #62

Open
raedbsili1991 opened this issue Jul 28, 2023 · 7 comments
Open

Silencing Warning and printing #62

raedbsili1991 opened this issue Jul 28, 2023 · 7 comments
Assignees
Labels
enhancement New feature or request

Comments

@raedbsili1991
Copy link

Hello,

How can I silence some warnings ?

/config/workspace/my_venv/lib/python3.11/site-packages/scalecast/Forecaster.py:1902: Warning: Trend decomposition did not work and raised this error: You must specify a period or x must be a pandas object with a PeriodIndex or a DatetimeIndex with a freq not set to None Switching to the non-decomp method.
warnings.warn(
/config/workspace/my_venv/lib/python3.11/site-packages/scalecast/Forecaster.py:2034: Warning: No seasonalities are currently associated with the None frequency.
warnings.warn(
/config/workspace/my_venv/lib/python3.11/site-packages/scalecast/Forecaster.py:1902: Warning: Trend decomposition did not work and raised this error: You must specify a period or x must be a pandas object with a PeriodIndex or a DatetimeIndex with a freq not set to None Switching to the non-decomp method.
warnings.warn(
/config/workspace/my_venv/lib/python3.11/site-packages/scalecast/Forecaster.py:2034: Warning: No seasonalities are currently associated with the None frequency.
warnings.warn(
/config/workspace/my_venv/lib/python3.11/site-packages/scalecast/Forecaster.py:1902: Warning: Trend decomposition did not work and raised this error: You must specify a period or x must be a pandas object with a PeriodIndex or a DatetimeIndex with a freq not set to None Switching to the non-decomp method.
warnings.warn(
/config/workspace/my_venv/lib/python3.11/site-packages/scalecast/Forecaster.py:2034: Warning: No seasonalities are currently associated with the None frequency.
warnings.warn(
/config/workspace/my_venv/lib/python3.11/site-packages/scalecast/Forecaster.py:1902: Warning: Trend decomposition did not work and raised this error: You must specify a period or x must be a pandas object with a PeriodIndex or a DatetimeIndex with a freq not set to None Switching to the non-decomp method.
warnings.warn(
/config/workspace/my_venv/lib/python3.11/site-packages/scalecast/Forecaster.py:2034: Warning: No seasonalities are currently associated with the None frequency.
warnings.warn(
/config/workspace/my_venv/lib/python3.11/site-packages/scalecast/Forecaster.py:1902: Warning: Trend decomposition did not work and raised this error: You must specify a period or x must be a pandas object with a PeriodIndex or a DatetimeIndex with a freq not set to None Switching to the non-decomp method.
warnings.warn(
/config/workspace/my_venv/lib/python3.11/site-packages/scalecast/Forecaster.py:2034: Warning: No seasonalities are currently associated with the None frequency.

This is my code:

`
df = load_data(database_filename,FILTERED_ITEM_NUMBER)

#GridGenerator.get_example_grids() # example hyperparameter grids

data = df
f = Forecaster(
y = data['Monthly _ Ordered quantity _ basic U_M'], # required
current_dates = data['Month Number'], # required
#future_dates = length_forecast_horizon_training , # length of the forecast horizon, 
#test_length = 30, # set a test set length or fraction to validate all models if desired
cis = False, # choose whether or not to evaluate confidence intervals for all models, 
metrics = ['rmse','mae','mape','r2'], # the metrics to evaluate when testing/tuning models
)

f.add_metric(custom_metric_3)
f.set_validation_metric('custom_metric_3')

#f.generate_future_dates(length_forecast_horizon_deployement)
f.set_last_future_date(FORECAST_DATE)
f.get_freq()
f.infer_freq()

f.set_validation_length(length_forecast_horizon_training)
f.set_test_length(.25)
f.eval_cis() # tell the object to build confidence intervals for all models
f.add_ar_terms(8)
f.add_AR_terms((4,8))

f.add_seasonal_regressors('month','quarter','week','dayofyear',raw=False,sincos=True)
f.add_seasonal_regressors('dayofweek','is_leap_year','week',raw=False,dummy=True,drop_first=True)
f.add_seasonal_regressors('year')

f.add_sklearn_estimator(StackingRegressor,called='stacking')
f.add_sklearn_estimator(AdaBoostRegressor,called='adaboost')

models = ('elasticnet', 'knn', 'xgboost', 'lasso', 'ridge','mlp')

for m in models:

    f.set_estimator(m)
    f.auto_Xvar_select(estimator=m) 
    f.determine_best_series_length(estimator =m)
    f.tune() # by default, will pull the grid with the same name as the estimator (mlr will pull the mlr grid, etc.)
    f.cross_validate(k = 2,verbose=False) 
    f.auto_forecast()

f.infer_freq()
f.plot_fitted(order_by='TestSetR2') # plot fitted values of all models ordered by r2
models = plot_test_export_summaries(f)
#f.plot(order_by='TestSetR2') # plot fitted values of all models ordered by


data_forecast = f.export(to_excel=True,excel_name=forecasting_file, cis = True)
display(models)

`

Also, is there a progression bar ?

Thank you in advance,

@mikekeith52
Copy link
Owner

Hi, you can use the warnings package to shut off warnings:

import warnings
warnings.simplefilter("ignore")

But the warnings you are seeing are because of the package not being able to associate seasonalities consistently with the dates you have passed to the object. I think part of the problem may be the repeated use of f.auto_Xvar_select(). I had envisioned only using that function once before modeling. The way you are using it, I'd suggest calling f.drop_all_Xvars() in each iteration before calling f.auto_Xvar_select(). That way, every search is a new search and the same variables aren't being added and tested against each other over and over.

The repated use of f.determine_best_series_length() should also only ever be used once when modeling as it only ever shortens a series. So using it over and over may just shorten the series until there's nothing left. In fact, I've never written about that function because I'm not convinced it's ever a good idea to use it, and I don't have a good way to use it repeatedly. Maybe if I created a f.restore_series_length() that you could call in each iteration it would work better. Is that something you'd find helpful?

For progression bars, use the tqdm package.

from tqdm import tqdm

for i in tqdm(some_loop):
  do_something(i)

If you are using notebook, you can call from tqdm.notebook import tqdm for a more visually appealing effect.

Finally, I know you didn't ask about it, but to increase modeling accuracy, I recommend series transformations and pipelines. Optimal series transformations can be found with this function. More than anything, transformations are emerging as the number 1 way to juice forecasting prowess according to the most recent research, at least from what I'm seeing.

Please follow up with additional questions. Thanks for raising the issue!

@raedbsili1991
Copy link
Author

Thank you for the feedback.

I see it more clear now.

So, if I understand also well, If the auto Xvar select is called, there is no need to add before the AR terms and seasonalities, right ?

I think the function you proposed could be very useful.

Otherwise, maybe use it in Tune_cast function that take the model lists.

I have a question regarding the best series length, is it about determining best training length?

I have few other questions, better to add them in another different threads.

Thank you again.

@mikekeith52 mikekeith52 self-assigned this Jul 29, 2023
@mikekeith52 mikekeith52 added the enhancement New feature or request label Jul 29, 2023
@mikekeith52
Copy link
Owner

mikekeith52 commented Jul 29, 2023

If auto_Xvar_select() is called, you should only add regressors that are not related to seasonality, trend, or AR terms. Or if you add those other terms, turn the search for them off in the function.

An alternative method for auto-selecting variables is to add all of them you think you might need and drop them one at a time using reduce_Xvars() to find the optimal set. It's a pretty good function to use with boosted tree algorithms and shap feature importance.

I will add the restore_series_length() function in the next update.

There is a tune_test_forecast() function but it doesn't support auto_Xvar_select() nor determine_best_series_length().

As to your last question, it is about finding the best total length for the series in the object, taking into consideration whatever validation/test sets you also have loaded. It's not any more complicated than trying a bunch of difference lengths and choosing the one where the out-of-sample error/accuracy metric returns the best score. It supports cross-validation as well.

Feel free to ask any questions you want in this or other threads. It really helps me improve the package. Thanks!

mikekeith52 added a commit that referenced this issue Jul 30, 2023
- Added the `restore_series_length()` function (#62).
- Changed how in-sample metrics are evaluated. If the series length currently in the object and the predictions are different lengths, the prediction-length is truncated so that an in-sample metric can still be evaluated.
- Fixed documentation typos.
@mikekeith52
Copy link
Owner

mikekeith52 commented Jul 30, 2023

I added the restore_series_length() method. I'd recommend using it like this:

for m in models:
    f.drop_all_Xvars()
    f.set_estimator(m)
    f.auto_Xvar_select(estimator=m)
    f.determine_best_series_length(estimator=m)
    f.cross_validate()
    f.auto_forecast()
    f.restore_series_length()

@raedbsili1991
Copy link
Author

Thank you,

  • Shall I do an upgrade then to the library ?
  • Do you have a list of the regressors that are not related to seasonality, trend, or AR terms.

@mikekeith52
Copy link
Owner

Yes, please upgrade the package: pip install --upgrade scalecast

Other regressors would be any you don't add using the following functions:

f.add_ar_terms()
f.add_AR_terms()
f.add_seasonal_regressors()
f.add_time_trend()

All of that will be searched for in the f.auto_Xvar_select() function and you can specify the parameters of the function to search for them in different ways (such as adding seasonality as dummy variables instead of wave functions).

@mikekeith52
Copy link
Owner

Are we okay to close this one?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants