'numpy.ndarray' object has no attribute '_validate_params'

3.6k views Asked by At

I'm forecasting a univariant time series using darts Regression model with scikit.learn's Linear Regression. The ts I'm working with is from 2019 until 2023 with a monthly granularity.

It seems the Darts framework is asking for an attribute that's not provided by scikit.learn's Linear Regression, as following, when I try .fit() I get AttributeError: 'numpy.ndarray' object has no attribute '_validate_params.

Update: I'm adding a data chunk that reproduces the error so anyone could tell if the same error would happen with him?

import pandas as pd
from darts import TimeSeries

index = pd.date_range(start='2019-01-01', end='2023-07-01', freq='M', inclusive='both')

values = [80, 65, 65, 73, 73, 73, 72, 72, 80, 80, 80, 72, 80, 80, 80, 80, 80, 80, 40, 40, 40, 80, 48, 48, 48, 48, 48, 48, 48, 48, 48, 56, 48, 48, 48, 48, 42, 42, 42, 42, 42, 42, 51, 42, 50, 42, 42, 50, 34, 30, 10, 10, 9, 8]

example_df = pd.DataFrame(values, index, columns=['util'])

ts = TimeSeries.from_dataframe(example_df,
                              value_cols=['util'],
                              fill_missing_dates=True)


# Create training data, hold out last 10 data points for test
y_train = ts[:-10]
y_test = ts[-10:]

# Specify and train the model
from darts.models import RegressionModel
from sklearn.linear_model import LinearRegression

model = RegressionModel(lags = [-1, -2, -3],
                       model = LinearRegression)

model.fit(series=y_train)

The error appears only when I use model=LinearRegression, if model not passed in argument the error doesn't appear. I expect after model.fit() would run successfully I would follow with model.predict()

AttributeError Traceback (most recent call last)
Cell In[27], line 8
      4 # Specify and train model
      5 model = RegressionModel(lags=[-1, -2, -3],
      6                        model=LinearRegression)
----> 8 model.fit(series=y_train)
     10 # Forecast
     11 y_pred = model.predict(n=10, series=y_train)

File ~\PycharmProjects\OPSDN\venv\Lib\site-packages\darts\models\forecasting\regression_model.py:548, in RegressionModel.fit(self, series, past_covariates, future_covariates, max_samples_per_ts, n_jobs_multioutput_wrapper, **kwargs)
    540     logger.warning("Provided `n_jobs_multioutput_wrapper` wasn't used.")
    542 super().fit(
    543     series=seq2series(series),
    544     past_covariates=seq2series(past_covariates),
    545     future_covariates=seq2series(future_covariates),
    546 )
--> 548 self._fit_model(
    549     series, past_covariates, future_covariates, max_samples_per_ts, **kwargs
    550 )
    552 return self

File ~\PycharmProjects\OPSDN\venv\Lib\site-packages\darts\models\forecasting\regression_model.py:416, in RegressionModel._fit_model(self, target_series, past_covariates, future_covariates, max_samples_per_ts, **kwargs)
    414 if len(training_labels.shape) == 2 and training_labels.shape[1] == 1:
    415     training_labels = training_labels.ravel()
--> 416 self.model.fit(training_samples, training_labels, **kwargs)
    418 # generate and store the lagged components names (for feature importance analysis)
    419 self._lagged_feature_names, _ = create_lagged_component_names(
    420     target_series=target_series,
    421     past_covariates=past_covariates,
   (...)
    428     use_static_covariates=self.uses_static_covariates,
    429 )

File ~\PycharmProjects\OPSDN\venv\Lib\site-packages\sklearn\base.py:1144, in _fit_context.<locals>.decorator.<locals>.wrapper(estimator, *args, **kwargs)
   1139 partial_fit_and_fitted = (
   1140     fit_method.__name__ == "partial_fit" and _is_fitted(estimator)
   1141 )
   1143 if not global_skip_validation and not partial_fit_and_fitted:
-> 1144     estimator._validate_params()
   1146 with config_context(
   1147     skip_parameter_validation=(
   1148         prefer_skip_nested_validation or global_skip_validation
   1149     )
   1150 ):
   1151     return fit_method(estimator, *args, **kwargs)

AttributeError: 'numpy.ndarray' object has no attribute '_validate_params'
1

There are 1 answers

0
Mohamed Essam On

Passing sklearn's LinearRegression() as an instance solved this problem:

model = RegressionModel(lags=[-1, -2, -3], model=LinearRegression())