No project description provided
Project description
DesmoothForecasterModel Documentation
Overview
The DesmoothForecasterModel class provides a framework for time-series forecasting using a method that allows for original future timestep predictions to be made based on predictions on the smoothed version of the data. Currently, the model uses a simple LSTM with an option to add a custom model. It applies smoothing techniques to the target variable before training the provided model on predicting smoothed values, and then extrapolates unsmoothed, original predictions from them.
Class: DesmoothForecasterModel
Description
A class to create and train a model that uses smoothed time-series data for forecasting. The model supports LSTM or custom neural network architectures and can apply exponential smoothing or moving average smoothing to the target variable before training.
Attributes
model_type(str): The type of model to use ('LSTM'or'custom'). If custom, a custom model must be provided.custom_model(keras.Model, optional): A custom model to use instead of the default LSTM model. model_type must be set to'custom'.smoothing(str): The smoothing technique to apply ('exp_smoothing'or'moving_average').smoothing_param(floatorint): The parameter for the chosen smoothing method. For exponential smoothing, this is the smoothing level (alpha). For moving average, this is the window size.scaler(StandardScaler): A scaler instance used to normalize the data.model(keras.Model, optional): The neural network model to be trained.trainX(np.ndarray, optional): The input training data prepared for the model.df_for_training(pd.DataFrame, optional): The DataFrame containing smoothed and normalized data used for training.n_future(int, optional): The number of future timesteps the model is trained to predict.Y(pd.Series, optional): The original target variable before smoothing.lookback(int, optional): The number of past timesteps used to predict the future.
Methods
__init__(model='LSTM', custom_model=None, smoothing="exp_smoothing", smoothing_param=-1)
Description:
Initializes the DesmoothForecasterModel with the specified parameters.
Parameters:
model(str, optional): The type of model to use ('LSTM'by default, or'custom').custom_model(keras.Model, optional): A custom model to use instead of the default LSTM model (default isNone).smoothing(str, optional): The smoothing technique to apply ('exp_smoothing'by default, or'moving_average').smoothing_param(floatorint, optional): The parameter for the smoothing method. Default is-1, which auto-sets the value based on the smoothing method:0.01for exponential smoothing and10for moving average.
smooth_data(Y) -> pd.Series
Description:
Applies the chosen smoothing technique to the target variable.
Parameters:
Y(pd.Series): The target variable to be smoothed.
Returns:
pd.Series: The smoothed target variable.
prepare_data(time_column, X, Y, lookback, n_future) -> np.ndarray
Description:
Prepares the input and output data for training the model by applying smoothing, normalizing, and reshaping it into sequences.
Parameters:
time_column(pd.Seriesorpd.Index): The time indices or datetime values for the data.X(pd.DataFrame): The input feature columns.Y(pd.Series): The target variable column.lookback(int): The number of past timesteps used to predict the future.n_future(int): The number of future timesteps to predict.
Returns:
np.ndarray: The prepared output data (target variable) for training.
build_model(input_shape) -> Sequential
Description:
Builds the neural network model based on the specified model type.
Parameters:
input_shape(tuple): The shape of the input data for the model.
Returns:
keras.Model: The compiled neural network model.
train_model(time_column, X, Y, lookback, n_future, epochs, batch_size=32, validation_split=0.2) -> Sequential
Description:
Trains the neural network model on the prepared data.
Parameters:
time_column(pd.Seriesorpd.Index): The time indices or datetime values for the data.X(np.ndarray): The input data for training, prepared by theprepare_datamethod.Y(np.ndarray): The target variable for training.lookback(int): The number of past timesteps used to predict the future.n_future(int): The number of future timesteps to predict.epochs(int, optional): The number of epochs to train the model (default is100).batch_size(int, optional): The batch size used during training (default is32).validation_split(float, optional): The proportion of data to use for validation (default is0.2).
Returns:
keras.Model: The trained neural network model.
predict(n_timesteps) -> pd.DataFrame
Description:
Predicts future values based on the trained model and prepared data.
Parameters:
- 'data' (
pd.DataFrameorNone, optional): The DataFrame containing the prepared data for prediction. If not provided, the last n_timesteps of the training data will be used. n_timesteps(int, optional): The number of future timesteps to predict.
Returns:
pd.DataFrame: A DataFrame containing the actual and predicted values with the corresponding time indices.
save_trained_model(file_path: str) -> None
Description:
Saves the trained neural network model to the specified file path. This function can be used to persist the model after training so that it can be loaded and used later without retraining.
Parameters:
file_path(str): The file path where the trained model will be saved. This should include the file name and appropriate extension (e.g.,.h5for Keras models).
Returns:
None
Installation
To install the DesmoothForecaster package, you can simply run the following command in your terminal or command prompt:
pip install DesmoothForecaster
This will download and install the package along with its dependencies. The package is compatible with Python 3.6 and above, and works on windows, linux, and macos.
Usage
To use the DesmoothForecaster package, you can import the DesmoothForecasterModel class and create an instance of it. Here's a simple example with pdw data from radars (loaded from google drive):
import gdown
import pandas as pd
from DesmoothForecaster import DesmoothForecasterModel
file_id = '1-3DGmcR8PP9k_HSMmH8B7Lu8XK8vPyKF'
download_url = f'https://drive.google.com/uc?id={file_id}'
output = 'file.csv'
gdown.download(download_url, output, quiet=False)
df = pd.read_csv(output)
Create an instance of the DesmoothForecasterModel class with the desired configuration:
desmooth_instance = DesmoothForecasterModel(
model_type='LSTM', # Model type: 'LSTM' or 'custom'
custom_model=None, # Provide a custom model if using 'custom'
smoothing="exp_smoothing", # Smoothing method: 'exp_smoothing' or 'moving_average'
smoothing_param=-1 # Smoothing parameter: set to -1 for automatic selection
)
Train the model using the train_model method:
model = desmooth_instance.train_model(
X=df.index, # Index or time column
y=df['PW'], # Target variable (in this case, 'PW')
features=df[features], # Feature columns
lookback=100, # Number of past timesteps to consider
n_future=n_future, # Number of future timesteps to predict
epochs=7 # Number of training epochs
)
Use the trained model to predict future values and save it in the current directory.
predictions = desmooth_instance.predict(n_future * 6) # Predicting 6 times the future steps
desmooth_instance.save_trained_model('desmooth_model.h5')
Contact
If you have any questions or feedback, please feel free to contact me at shandixit2002@gmail.com.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file desmoothforecaster-0.3.tar.gz.
File metadata
- Download URL: desmoothforecaster-0.3.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
631d93fad3b0be71dd4b91fc9f304aa18bf530ae754547e6edc3b2544edfbd0a
|
|
| MD5 |
5dc3071d3e0603d6e0bbfbef38623b67
|
|
| BLAKE2b-256 |
ea165bec7ae3345553bfa1b2656e00a528a4b3224c98c4c57a0404b7e9b39d59
|
File details
Details for the file DesmoothForecaster-0.3.0-py3-none-any.whl.
File metadata
- Download URL: DesmoothForecaster-0.3.0-py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
29e26c4fa4cebd6e6fd26a2ec2b3026986e762500a89063a6bd01a7221fb282b
|
|
| MD5 |
f52f0f48136dba8aa8a8cc5356b2dcbc
|
|
| BLAKE2b-256 |
bff9d591704905e1fdb0f8fc769f6e0a52b57038e0f9be45a5299f21302c7522
|
File details
Details for the file DesmoothForecaster-0.3-py3-none-any.whl.
File metadata
- Download URL: DesmoothForecaster-0.3-py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f1584d046dd9ceb72aeec7c6e52d3d206f6e2f1016b5ff263ce3c2a79f41bbb6
|
|
| MD5 |
232f9b119db7c7c30c02d2fa91203b1b
|
|
| BLAKE2b-256 |
d91ce669e3b334e0c4358437d248056ec9d8c3f6231ff650d536d149803f461d
|