admin管理员组

文章数量:1123145

Here is the type of data I have:

   date a_kg    b_kg    a_gbp   b_gbp   t_kg    year
2020-01-05T00:00:00.000+00:00   4123620.356542087   5430179.407605753   2777354.579378964   2298985.614845754   11652081.086148437  2020
2020-01-12T00:00:00.000+00:00   5852142.919229749   3682096.494749166   2542698.063547792   3783111.968057487   12272639.099464454  2020
2020-01-19T00:00:00.000+00:00   5195981.825434215   3260206.371941118   3657475.0183038586  3901962.6215422433  8226024.324803047   2020
2020-01-26T00:00:00.000+00:00   4795975.45259111    6795542.149013333   2713506.6533871787  3403192.9939237405  6770363.471693737   2020
2020-02-02T00:00:00.000+00:00   3468055.9213273097  6862528.132298238   2561869.0193747617  3927417.9498864026  7595546.137793591   2020
2020-02-09T00:00:00.000+00:00   3467983.561008608   6233589.392465845   3085392.166316497   3234488.990910977   8989754.520383794   2020
2020-02-16T00:00:00.000+00:00   3174250.836504598   4218455.076693483   2281848.4499495253  3306832.0734549854  11726103.361457452  2020
2020-02-23T00:00:00.000+00:00   5598528.437324805   3390688.4560255352  3604393.9615080794  3068852.545896374   12025114.082794404  2020
2020-03-01T00:00:00.000+00:00   4803345.035229626   5736932.106048628   2149101.2873595417  2063547.816860238   6048664.913718335   2020
2020-03-08T00:00:00.000+00:00   5124217.733388137   4760609.974958405   3973773.8732010345  2269728.567483261   9575231.11804296    2020
2020-03-15T00:00:00.000+00:00   3061753.4828874073  3488152.939379115   3544489.5385933146  2078572.9642168356  8921877.022041453   2020
2020-03-22T00:00:00.000+00:00   5909729.556485983   4980707.640445081   2397431.363068345   3591026.0281594507  7554754.673295111   2020
2020-03-29T00:00:00.000+00:00   5497327.922401265   3137554.0844608736  2011044.2342472048  2785889.9526908165  6839057.5713357795  2020
2020-04-05T00:00:00.000+00:00   3637017.3320348286  6637281.608315129   3630922.856909668   3271426.727911757   8363306.199825396   2020
2020-04-12T00:00:00.000+00:00   3545474.9016213017  4035119.9264000678  3413714.687695234   4268916.184815233   12600367.927387634  2020
2020-04-19T00:00:00.000+00:00   3550213.5295603015  5650089.137415928   3458014.3360819747  2623230.5728721875  8262420.5241452865  2020
2020-04-26T00:00:00.000+00:00   3912726.728878613   4246844.304357644   3542540.6933718915  3025957.3075890746  9631534.352203563   2020
2020-05-03T00:00:00.000+00:00   4574269.294896713   5080272.084711243   2148089.303468181   3888877.8463576217  10921132.712266244  2020
2020-05-10T00:00:00.000+00:00   4295835.055926347   5186841.117373118   2716931.457088545   2571995.4137290563  8545407.216655057   2020
2020-05-17T00:00:00.000+00:00   3873687.420594126   3739417.822102108   2231738.1190502592  2192449.7745719827  12802474.579046724  2020
2020-05-24T00:00:00.000+00:00   4835558.684167138   6878338.511058234   3726206.851751187   2724378.63228442    12737131.06459478   2020
2020-05-31T00:00:00.000+00:00   3418481.5819561253  6100531.293444458   3246596.253655116   2403053.218135011   7762476.070777549   2020
2020-06-07T00:00:00.000+00:00   3876433.9456056543  6757995.766256757   2661796.0497052986  4324244.130856432   9480739.541246697   2020
2020-06-14T00:00:00.000+00:00   4099085.529881075   6579309.401710596   2127116.7005720474  4020300.9489110424  8106148.168717388   2020
2020-06-21T00:00:00.000+00:00   4368209.952651108   5391599.915244341   2621964.6434313245  3583509.3912760587  7993883.460642274   2020
2020-06-28T00:00:00.000+00:00   5355527.884179041   6687496.940092467   2650366.644053494   4178651.4754692945  6258208.631481729   2020
2020-07-05T00:00:00.000+00:00   3599021.346475079   3353970.008207678   3459212.356676128   4009180.192247786   10266950.337859277  2020
2020-07-12T00:00:00.000+00:00   4542703.315240835   3783931.4496765807  3275114.942710426   2466425.1472150898  9518753.16260203    2020
2020-07-19T00:00:00.000+00:00   4777243.706586127   3180909.1556421523  3774425.4851526534  4231397.4962249445  6360351.2587499255  2020
2020-07-26T00:00:00.000+00:00   3139351.238159993   4301321.323053057   2944429.8503238987  3348355.6047891267  7950525.2496562805  2020

Here is the code I have:

   # Ensure the 'date' column exists in the DataFrame
    if 'date' in weekly_dt_filt.columns:
        # Convert 'date' column to datetime
        weekly_dt_filt['date'] = pd.to_datetime(weekly_dt_filt['date'])
        # Set 'date' column as the index
        weekly_dt_filt.set_index('date', inplace=True)
    else:
        raise KeyError("The 'date' column is not found in the DataFrame")
    
    # Aggregate weekly data to monthly
    df_monthly = weekly_dt_filt.groupby(['year', 'month']).agg({'a_kg': 'sum'}).reset_index()
    
    # Convert DataFrame to Darts TimeSeries
    series_weekly = TimeSeries.from_dataframe(weekly_dt_filt, value_cols='a_kg')
    series_monthly = TimeSeries.from_dataframe(df_monthly, value_cols='a_kg')
    
    # Split into train-test sets
    train_weekly, test_weekly = series_weekly.split_before(0.8)
    train_monthly, test_monthly = series_monthly.split_before(0.8)

This is another code

# Define models
models = {
   'ExponentialSmoothing': [
       ExponentialSmoothing(trend='add', seasonal='add', seasonal_periods=52),
       ExponentialSmoothing(trend='add', seasonal='mul', seasonal_periods=12)
   ],
   'SeasonalARIMA': [
        ARIMA(p=1, d=1, q=1, seasonal_order=(1, 1, 1, 52)),
        ARIMA(p=1, d=1, q=1, seasonal_order=(1, 1, 1, 12))
   ],
   'FFT': [
       FFT(nr_freqs_to_keep=10),
       FFT(nr_freqs_to_keep=5)
   ]
}

def evaluate_models(train, test, model_list):
   performance = []
   for model in model_list:
       start_time = time.time()
       model.fit(train)
       forecast = model.predict(len(test))
       end_time = time.time()
       # Ensure forecast and test are TimeSeries objects
       if not isinstance(forecast, TimeSeries):
           raise ValueError(f"Forecast is not a TimeSeries object: {forecast}")
       if not isinstance(test, TimeSeries):
           raise ValueError(f"Test is not a TimeSeries object: {test}")
       performance.append({
           'Model': type(model).__name__,
           'MAE': mae(test, forecast),
           'MSE': mse(test, forecast),
           'MASE': mase(test, forecast, train),
           'Forecast Bias': (forecast.mean() - test.mean()).values()[0],
           'Time Elapsed (s)': end_time - start_time
       })
   return pd.DataFrame(performance)

# Evaluate weekly data
performance_weekly = {}
for name, model_list in models.items():
   performance_weekly[name] = evaluate_models(train_weekly, test_weekly, model_list)

# Evaluate monthly data
performance_monthly = {}
for name, model_list in models.items():
   performance_monthly[name] = evaluate_models(train_monthly, test_monthly, model_list)

# Display results
display(pd.concat(performance_weekly.values()))
display(pd.concat(performance_monthly.values()))

I get an error like this:

 AttributeError: 'str' object has no attribute 'value'
File <command-3594900232608958>, line 42
     40 performance_weekly = {}
     41 for name, model_list in models.items():
---> 42    performance_weekly[name] = evaluate_models(train_weekly, test_weekly, model_list)
     44 # Evaluate monthly data
     45 performance_monthly = {}
File /local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.11/site-packages/darts/models/forecasting/exponential_smoothing.py:123, in ExponentialSmoothing.fit(self, series)
    118 if self.seasonal_periods is None and series.has_range_index:
    119     seasonal_periods_param = 12
    121 hw_model = hw.ExponentialSmoothing(
    122     series.values(copy=False),
--> 123     trend=self.trend if self.trend is None else self.trend.value,
    124     damped_trend=self.damped,
    125     seasonal=self.seasonal if self.seasonal is None else self.seasonal.value,
    126     seasonal_periods=seasonal_periods_param,
    127     freq=series.freq if series.has_datetime_index else None,
    128     dates=series.time_index if series.has_datetime_index else None,
    129     **self.constructor_kwargs,
    130 )
    131 hw_results = hw_model.fit(**self.fit_kwargs)
    132 self.model = hw_results

The context:

I do timeseries forecasting.

Is this because of the methodology I have in splitting the training and test dataset?

本文标签: pythonWhy I get the following errorAttributeError 39str39 object has no attribute 39value39Stack Overflow