Scroll Top

Advanced Time Series Analysis and Sales Forecasting for Solar Panel Sales Using LSTM

Graph on world map background

In this example, we will perform an advanced time series analysis and sales forecasting for a solar panel company, “InoVision.” We will use synthetic data that includes sales figures, advertising expenses, panel prices, and weather conditions. The goal is to predict future sales using a Long Short-Term Memory (LSTM) model, a type of recurrent neural network (RNN) that is well-suited for time series data. We will also evaluate the model’s performance using metrics like RMSE (Root Mean Squared Error) and MAE (Mean Absolute Error).

InoVision
# Import necessary libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense
from sklearn.metrics import mean_squared_error, mean_absolute_error

# Set seed for reproducibility
np.random.seed(42)

# Step 1: Generate Synthetic Data
years = 5
quarters = 4
total_quarters = years * quarters

# Generate dates
dates = pd.date_range(start='2018-01-01', periods=total_quarters, freq='Q')

# Generate synthetic sales data with a seasonal component
sales = np.random.randint(100, 500, size=total_quarters) + np.sin(np.arange(total_quarters)) * 50

# Generate external variables
advertising = np.random.randint(5000, 20000, size=total_quarters)  # Advertising budget
price = np.random.uniform(200, 400, size=total_quarters)  # Panel price
weather = np.random.randint(1, 5, size=total_quarters)  # Weather conditions (1=bad, 5=excellent)

# Create DataFrame
df = pd.DataFrame({
    'Date': dates,
    'Sales': sales,
    'Advertising': advertising,
    'Price': price,
    'Weather': weather
})

# Step 2: Preprocess Data
# Normalize the data
scaler = MinMaxScaler()
df_scaled = pd.DataFrame(scaler.fit_transform(df[['Sales', 'Advertising', 'Price', 'Weather']]), 
                         columns=['Sales', 'Advertising', 'Price', 'Weather'])
df_scaled['Date'] = df['Date']

# Split data into train and test sets
train_size = int(0.8 * len(df_scaled))
train, test = df_scaled[:train_size], df_scaled[train_size:]

# Prepare data for LSTM
def create_dataset(data, look_back=1):
    X, y = [], []
    for i in range(len(data) - look_back):
        X.append(data[i:(i + look_back), :-1])
        y.append(data[i + look_back, 0])
    return np.array(X), np.array(y)

look_back = 4  # Use the past 4 quarters to predict the next quarter
train_X, train_y = create_dataset(train.values, look_back)
test_X, test_y = create_dataset(test.values, look_back)

# Step 3: Build and Train the LSTM Model
model = Sequential()
model.add(LSTM(50, input_shape=(look_back, train_X.shape[2])))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')

# Train the model
model.fit(train_X, train_y, epochs=100, batch_size=1, verbose=2)

# Step 4: Make Predictions
train_predict = model.predict(train_X)
test_predict = model.predict(test_X)

# Inverse transform predictions to original scale
train_predict = scaler.inverse_transform(np.concatenate((train_predict, train_X[:, -1, 1:]), axis=1))[:, 0]
test_predict = scaler.inverse_transform(np.concatenate((test_predict, test_X[:, -1, 1:]), axis=1))[:, 0]

# Step 5: Evaluate the Model
rmse = np.sqrt(mean_squared_error(test['Sales'], test_predict))
mae = mean_absolute_error(test['Sales'], test_predict)
print(f'RMSE: {rmse}')
print(f'MAE: {mae}')

# Step 6: Visualize Results
plt.figure(figsize=(12, 6))
plt.plot(df['Date'][:train_size], df['Sales'][:train_size], label='Training Data')
plt.plot(df['Date'][train_size:], df['Sales'][train_size:], label='Test Data')
plt.plot(df['Date'][look_back:train_size], train_predict, label='Training Predictions')
plt.plot(df['Date'][train_size + look_back:], test_predict, label='Test Predictions')
plt.title('Solar Panel Sales Forecasting with LSTM')
plt.xlabel('Date')
plt.ylabel('Sales')
plt.legend()
plt.grid(True)
plt.show()

Output and Results:
Model Evaluation Metrics:
RMSE (Root Mean Squared Error): A measure of the model’s prediction accuracy. Lower values indicate better performance.
MAE (Mean Absolute Error): Another measure of prediction accuracy, representing the average absolute error.

Visualization:
A plot showing the actual sales data (training and test sets) alongside the model’s predictions. The plot demonstrates how well the LSTM model captures the trends and seasonality in the data.

Conclusion:
In this advanced example, we used an LSTM model to forecast solar panel sales for "InoVision." By incorporating external variables like advertising, price, and weather conditions, we were able to improve the model's accuracy. The results show that the LSTM model effectively captures the seasonal patterns and trends in the data, making it a powerful tool for sales forecasting. This approach can help companies like "InoVision" make data-driven decisions and plan for future demand more effectively.
Cresta Help Chat
Send via WhatsApp