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

About get_coef in older version #276

Closed
kennis222 opened this issue Oct 28, 2022 · 3 comments
Closed

About get_coef in older version #276

kennis222 opened this issue Oct 28, 2022 · 3 comments
Labels
question Further information is requested

Comments

@kennis222
Copy link

Hi developers,

The function "get_coef" is deprecated since version 4.3.0, but I would like to use the get_coef with an older version since it easier to interpret to people without machine learning or statistical knowledge compared to use the "impurity-based feature importance" . However, when I use the function "get_coef" in the older version either 4.2.0 or 4.1.0, the program reports error with "AttributeError: 'ForecasterAutoregMultiOutput' object has no attribute 'get_coef'"

Here is the codes I calls for the function:
coef_ = []
for i,j in zip(range(1,7),pd.date_range(start='2022-01',periods=6,freq='M')):
<space>df = pd.DataFrame()
<space>df['feature'] = model_direct.get_coef(step=i)['feature']
<space>df['coef'] = model_direct.get_coef(step=i)['coef']
<space> df['date'] = pd.to_datetime(j)
<space>coef_.append(df)

Thanks.

@JavierEscobarOrtiz
Copy link
Collaborator

Hello @kennis222,

Thank you for opening the issue, a similar example works for me in version 0.4.3, here is:

# Libraries
# ==============================================================================
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from skforecast.ForecasterAutoregMultiOutput import ForecasterAutoregMultiOutput
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

# Create and fit forecaster
# ==============================================================================
y = pd.Series(list(range(50)))

forecaster = ForecasterAutoregMultiOutput(
                 regressor = LinearRegression(),
                 steps     = 5,
                 lags      = 2
             )

forecaster.fit(y=y)

# Get coef for step 1
# ==============================================================================
display(forecaster.get_coef(step=1))

# Get list of df
# ==============================================================================
coef_ = []

for i in list(range(forecaster.steps)):
    df = forecaster.get_coef(step=i+1) # First step is 1 not 0
    coef_.append(df)
    
# print Get coef for step 1
display(coef_[0])

From forecaster.get_coef(step=1):

feature coef
0 lag_1 0.5
1 lag_2 0.5

From list:

feature coef
0 lag_1 0.5
1 lag_2 0.5

FYI, the get_coef method was deprecated because it was merged into the get_feature_importance method. get_feature_importance accesses the method coef_ or feature_importances_ depending on the type of the regressor.

So in the latest version, 0.5.1 it would be:

Note that ForecasterAutoregMultiOutput has been renamed to ForecasterAutoregDirect

# Libraries
# ==============================================================================
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from skforecast.ForecasterAutoregDirect import ForecasterAutoregDirect
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

# Create and fit forecaster
# ==============================================================================
y = pd.Series(list(range(50)))

forecaster = ForecasterAutoregDirect(
                 regressor = LinearRegression(),
                 steps     = 5,
                 lags      = 2
             )

forecaster.fit(y=y)

# Get coef for step 1
# ==============================================================================
display(forecaster.get_feature_importance(step=1))

# Get list of df
# ==============================================================================
coef_ = []

for i in list(range(forecaster.steps)):
    df = forecaster.get_feature_importance(step=i+1) # First step is 1 not 0
    coef_.append(df)
    
# print Get coef for step 1
display(coef_[0])

From forecaster.get_feature_importance(step=1):

feature importance
0 lag_1 0.5
1 lag_2 0.5

From list:

feature importance
0 lag_1 0.5
1 lag_2 0.5

I recommend using the latest version as we have fixed some bugs and improved efficiency. 😄

Thank you!

@JavierEscobarOrtiz JavierEscobarOrtiz added the question Further information is requested label Oct 28, 2022
@kennis222
Copy link
Author

Hello @JavierEscobarOrtiz,

Thank you for the reply, but the case you show me in the version 0.4.3 still not works for my environment. However, since merging the the method coef_ or feature_importances_ depending on the type of the regressor, can I understand in this way: if using LinearRegression, the feature_importances_ means the coefficients, but if using the random forest, the feature_importances_ means the impurity-based feature importance? If "Yes", I can directly use the latest version : )

@JoaquinAmatRodrigo
Copy link
Owner

Hi @kennis222,
Yes, you understood it well :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants