ai_predictive_forecaster
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| ai_predictive_forecaster [2025/05/29 15:15] – [Class Overview] eagleeyenebula | ai_predictive_forecaster [2025/05/29 15:50] (current) – [Class Overview] eagleeyenebula | ||
|---|---|---|---|
| Line 83: | Line 83: | ||
| * List or pandas Series containing numeric time-series values. | * List or pandas Series containing numeric time-series values. | ||
| - | **Method: " | + | **Method:** " |
| **Signature**: | **Signature**: | ||
| < | < | ||
| Line 96: | Line 96: | ||
| **Process**: | **Process**: | ||
| - | 1. Ensures the model is pre-fitted (**fit()** called prior). | + | * Ensures the model is pre-fitted (**fit()** called prior). |
| - | 2. Uses the **forecast()** method of the fitted **ARIMA** model to provide future predictions. | + | |
| **Parameters**: | **Parameters**: | ||
| Line 103: | Line 103: | ||
| **Error Handling**: | **Error Handling**: | ||
| - | * Raises errors if the model is not fitted before calling | + | * Raises errors if the model is not fitted before calling |
| ===== Workflow ===== | ===== Workflow ===== | ||
| - | ### Step-by-Step Usage Workflow: | + | **Step-by-Step Usage Workflow:** |
| 1. **Import the Class**: | 1. **Import the Class**: | ||
| - | Import the `PredictiveForecaster` module into your script: | + | |
| - | ```python | + | < |
| + | | ||
| from ai_predictive_forecaster import PredictiveForecaster | from ai_predictive_forecaster import PredictiveForecaster | ||
| - | ``` | + | </ |
| 2. **Initialize the Forecaster**: | 2. **Initialize the Forecaster**: | ||
| - | Set ARIMA parameters and instantiate the `PredictiveForecaster`: | + | |
| - | ```python | + | < |
| + | | ||
| | | ||
| - | ``` | + | </ |
| 3. **Fit the Model**: | 3. **Fit the Model**: | ||
| - | Provide time-series historical data to train the model: | + | |
| - | ```python | + | < |
| + | | ||
| | | ||
| | | ||
| - | ``` | + | </ |
| 4. **Forecast Future Values**: | 4. **Forecast Future Values**: | ||
| | | ||
| - | ```python | + | < |
| + | python | ||
| | | ||
| | | ||
| - | ``` | + | </ |
| - | + | ||
| - | --- | + | |
| ===== Advanced Examples ===== | ===== Advanced Examples ===== | ||
| Line 142: | Line 144: | ||
| These advanced examples illustrate real-world scenarios and optimizations for predictive forecasting: | These advanced examples illustrate real-world scenarios and optimizations for predictive forecasting: | ||
| - | --- | ||
| ==== Example 1: Custom ARIMA Parameters for Complex Data ==== | ==== Example 1: Custom ARIMA Parameters for Complex Data ==== | ||
| Experiment with various ARIMA configurations to improve forecast accuracy: | Experiment with various ARIMA configurations to improve forecast accuracy: | ||
| - | ```python | + | < |
| - | # Data | + | python |
| + | </ | ||
| + | **Data** | ||
| + | < | ||
| sales_data = [400, 420, 460, 510, 490, 550, 580, 600] | sales_data = [400, 420, 460, 510, 490, 550, 580, 600] | ||
| - | + | </ | |
| - | # Advanced forecaster with custom ARIMA parameters | + | **Advanced forecaster with custom |
| + | < | ||
| forecaster = PredictiveForecaster(model_order=(2, | forecaster = PredictiveForecaster(model_order=(2, | ||
| forecaster.fit(sales_data) | forecaster.fit(sales_data) | ||
| - | + | </ | |
| - | # Generate a forecast for the next 7 days | + | **Generate a forecast for the next 7 days** |
| + | < | ||
| custom_predictions = forecaster.forecast(steps=7) | custom_predictions = forecaster.forecast(steps=7) | ||
| print(f" | print(f" | ||
| - | ``` | + | </ |
| - | + | ||
| - | --- | + | |
| ==== Example 2: Use with pandas DataFrames ==== | ==== Example 2: Use with pandas DataFrames ==== | ||
| Include timestamps in your dataset for indexed prediction: | Include timestamps in your dataset for indexed prediction: | ||
| - | ```python | + | < |
| + | python | ||
| import pandas as pd | import pandas as pd | ||
| - | + | </ | |
| - | # Generate time-indexed data | + | **Generate time-indexed data** |
| + | < | ||
| data_index = pd.date_range(start=" | data_index = pd.date_range(start=" | ||
| data_values = [100, 120, 140, 130, 150, 160, 180, 200, 210, 230] | data_values = [100, 120, 140, 130, 150, 160, 180, 200, 210, 230] | ||
| time_series_data = pd.Series(data=data_values, | time_series_data = pd.Series(data=data_values, | ||
| - | + | </ | |
| - | # Fit and forecast | + | **Fit and forecast** |
| + | < | ||
| forecaster = PredictiveForecaster(model_order=(5, | forecaster = PredictiveForecaster(model_order=(5, | ||
| forecaster.fit(time_series_data) | forecaster.fit(time_series_data) | ||
| - | + | </ | |
| - | # Predict next 5 periods | + | **Predict next 5 periods** |
| + | < | ||
| future_data = forecaster.forecast(steps=5) | future_data = forecaster.forecast(steps=5) | ||
| print(f" | print(f" | ||
| - | ``` | + | </ |
| - | + | ||
| - | --- | + | |
| ==== Example 3: Using on Seasonal Data ==== | ==== Example 3: Using on Seasonal Data ==== | ||
| Adapt ARIMA to handle seasonality by preprocessing data with differencing: | Adapt ARIMA to handle seasonality by preprocessing data with differencing: | ||
| - | ```python | + | < |
| + | python | ||
| def seasonal_differencing(data, | def seasonal_differencing(data, | ||
| return [data[i] - data[i - lag] for i in range(lag, len(data))] | return [data[i] - data[i - lag] for i in range(lag, len(data))] | ||
| - | + | </ | |
| - | # Seasonal data simulation | + | **Seasonal data simulation** |
| + | < | ||
| seasonal_data = [10, 20, 30, 40, 50, 60, 30, 20, 10, 40, 50] | seasonal_data = [10, 20, 30, 40, 50, 60, 30, 20, 10, 40, 50] | ||
| seasonally_differenced = seasonal_differencing(seasonal_data, | seasonally_differenced = seasonal_differencing(seasonal_data, | ||
| Line 200: | Line 208: | ||
| print(" | print(" | ||
| - | ``` | + | </ |
| - | + | ||
| - | --- | + | |
| ==== Example 4: Evaluating Model Accuracy ==== | ==== Example 4: Evaluating Model Accuracy ==== | ||
| - | Assess forecast accuracy using metrics such as Mean Absolute Error (MAE): | + | * Assess forecast accuracy using metrics such as Mean Absolute Error (MAE): |
| - | ```python | + | < |
| + | python | ||
| from sklearn.metrics import mean_absolute_error | from sklearn.metrics import mean_absolute_error | ||
| - | + | </ | |
| - | # Training Data | + | **Training Data** |
| + | < | ||
| train_data = [10, 20, 30, 40, 50] | train_data = [10, 20, 30, 40, 50] | ||
| test_data = [60, 70, 80] # Known test values | test_data = [60, 70, 80] # Known test values | ||
| - | + | </ | |
| - | # Fit and predict | + | **Fit and predict** |
| + | < | ||
| forecaster = PredictiveForecaster(model_order=(3, | forecaster = PredictiveForecaster(model_order=(3, | ||
| forecaster.fit(train_data) | forecaster.fit(train_data) | ||
| predictions = forecaster.forecast(steps=3) | predictions = forecaster.forecast(steps=3) | ||
| - | + | </ | |
| - | # Evaluate using MAE | + | **Evaluate using MAE** |
| + | < | ||
| mae = mean_absolute_error(test_data, | mae = mean_absolute_error(test_data, | ||
| print(f" | print(f" | ||
| - | ``` | + | </ |
| - | + | ||
| - | --- | + | |
| ===== Best Practices ===== | ===== Best Practices ===== | ||
| 1. **Optimize ARIMA Parameters**: | 1. **Optimize ARIMA Parameters**: | ||
| - | Experiment with parameters (`p`, `d`, `q`) to find the best-fitting model for your data. | + | |
| 2. **Consider Seasonality**: | 2. **Consider Seasonality**: | ||
| - | Ensure seasonal trends are accounted for before fitting. Use differencing to remove seasonal effects. | + | |
| 3. **Evaluate Predictions**: | 3. **Evaluate Predictions**: | ||
| - | Assess prediction quality with metrics such as MAE, RMSE, or MAPE to measure errors and refine parameters. | + | |
| 4. **Data Preprocessing**: | 4. **Data Preprocessing**: | ||
| - | Clean data by removing outliers, handling missing values, and normalizing trends for better results. | + | |
| 5. **Integrate into Pipelines**: | 5. **Integrate into Pipelines**: | ||
| - | Use the forecaster in orchestration tools or CI/CD pipelines for recurring forecast updates. | + | |
| - | + | ||
| - | --- | + | |
| ===== Extensibility ===== | ===== Extensibility ===== | ||
| - | ### Adding Alternative Models | + | **Adding Alternative Models** |
| - | Extend support to models like SARIMA, Prophet, or neural network-based models: | + | * Extend support to models like **SARIMA**, Prophet, or neural network-based models: |
| - | ```python | + | |
| + | < | ||
| + | python | ||
| from fbprophet import Prophet | from fbprophet import Prophet | ||
| Line 263: | Line 271: | ||
| forecast = self.model.predict(future) | forecast = self.model.predict(future) | ||
| return forecast.tail(steps) | return forecast.tail(steps) | ||
| - | ``` | + | </ |
| - | + | ||
| - | --- | + | |
| ===== Conclusion ===== | ===== Conclusion ===== | ||
| - | The **AI Predictive Forecaster** | + | The **AI Predictive Forecaster** |
| + | |||
| + | Beyond its core functionality, | ||
ai_predictive_forecaster.1748531705.txt.gz · Last modified: 2025/05/29 15:15 by eagleeyenebula
