0% found this document useful (0 votes)
9 views38 pages

LSTM Sunspot Forecasting Project Report

Uploaded by

Nistha choudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views38 pages

LSTM Sunspot Forecasting Project Report

Uploaded by

Nistha choudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

A Project Report

On

Sunspot Forecasting using LSTM Networks


Submitted in partial fulfilment of the requirements for the award of the
degree
of
Bachelor of Technology
(Artificial Intelligence and Machine Learning)

Submitted By:
NISTHA

Under the Guidance of:


Dr. Paulo Gil
Department of Computer Science and Engineering

Department of Computer Science & Engineering


Government Mahila Engineering College, Ajmer
(Affiliated to Bikaner Technical University, Rajasthan)
Academic Session: 2024–2025
Acknowledgement
I would like to express my sincere gratitude to all those who supported me throughout the
completion of this project on Sunspot Forecasting using LSTM Networks.

First and foremost, I am deeply thankful to my mentors, faculty members, and guides for their
valuable insights, encouragement, and constructive feedback, which helped me refine my
research and implementation.

I extend my appreciation to the Royal Observatory of Belgium (SILSO World Data Center)
for providing authentic sunspot datasets, which served as the foundation of this study.

I am also grateful for the availability of open-source tools and libraries such as TensorFlow,
Scikit-learn, NumPy, and Pandas, which played a crucial role in building, training, and
evaluating the forecasting model.

Finally, I would like to acknowledge the constant support of my peers, friends, and family
members, whose motivation and encouragement inspired me to carry this project to successful
completion.
Abstract
This project presents an LSTM-based approach for forecasting monthly solar sunspot activity.
The dataset, obtained from SILSO, was preprocessed using normalization techniques and
modeled with a sequence-learning architecture. A custom forecasting feature was implemented,
allowing users to input the last available month, year, and recent observations to predict future
sunspot counts, even when the dataset is incomplete. The model demonstrates high accuracy
within a defined tolerance, visualizes predictions against actual data, and provides flexible, user-
driven forecasts. This work highlights the effectiveness of LSTM networks in time-series
prediction tasks and their adaptability for real-world solar activity monitoring.
Overview of the Problem
Sunspots are dark spots that appear on the Sun’s surface because of strong magnetic activity.
Even though they look small, they have a big impact—they can affect the Sun’s radiation, space
weather, satellite communication, GPS navigation, and even the Earth’s climate.

What’s interesting is that sunspot activity is not random—it follows a regular cycle, usually
every 11 years, where the number of sunspots goes up and then down.

Scientists have been trying to predict sunspot numbers for a long time. Older methods like
ARIMA or exponential smoothing (statistical models) can give some predictions, but they
struggle with the complex patterns in the data.

That’s where deep learning comes in. New methods like Recurrent Neural Networks (RNNs),
and especially Long Short-Term Memory (LSTM) networks, are very good at handling time-
series data (data that changes over time). LSTMs are powerful because they can “remember”
patterns over long periods, which is important for capturing the natural cycles of sunspot activity.

The aim of this project is to build an LSTM-based prediction model that learns from past
sunspot records and produces accurate forecasts of future sunspot numbers. This can help in
understanding solar cycles and their impact on Earth.
Chapter:1
Tools and Libraries Used

Importing Required Libraries

Before building the prediction model, several Python libraries are imported. Each of them serves
a specific purpose:

1. import pandas as pd
o Purpose: Used for handling datasets.
o It allows us to load, clean, and organize sunspot data in the form of tables
(DataFrames).
2. import numpy as np
o Purpose: Provides mathematical tools and operations.
o It helps with handling arrays, numerical calculations, and data transformations
efficiently.
3. import [Link] as plt
o Purpose: A visualization library.
o It is used to create plots and graphs to better understand trends in sunspot data and
the predictions made by the model.

4. from [Link] import MinMaxScaler


o Purpose: Scales (normalizes) the input data into a fixed range (usually 0 to 1).
o Scaling makes training faster and helps the LSTM model learn more effectively.
5. from [Link] import mean_absolute_error, mean_squared_error,
r2_score
o Purpose: These are evaluation metrics to check how well the model performs.
 MAE (Mean Absolute Error): Average of absolute errors between
predicted and actual values.
 MSE (Mean Squared Error): Average of squared errors, giving more
weight to large errors.
 R² Score: Measures how well the model explains variability in the data
(closer to 1 means better performance).
6. from [Link] import Sequential
o Purpose: Provides a way to build a neural network step by step, layer by layer, in
a simple sequence.
7. from [Link] import LSTM, Dense, Dropout
o LSTM (Long Short-Term Memory): The main deep learning layer used for
sequential/time-series data like sunspot numbers.
o Dense: Fully connected layer that makes final predictions.
o Dropout: A regularization technique that prevents overfitting by randomly
turning off some neurons during training.
8. from [Link] import EarlyStopping, ReduceLROnPlateau
o EarlyStopping: Stops training early if the model stops improving, to avoid
overfitting and save time.
o ReduceLROnPlateau: Reduces the learning rate automatically when progress
slows, helping the model learn more effectively.
Chapter:2
Data Preprocessing

 pd.read_csv(...) → Reads the dataset from the file SN_m_tot_V2.[Link].

 sep=';' → The dataset uses a semicolon (;) instead of a comma (,) to separate values.

 header=None → The file doesn’t have column names, so we manually define them.

 comment='#' → Lines starting with # are treated as comments and ignored.

 names=[...] → Assigns meaningful names to the dataset columns:

 Year → The year of observation


 Month → The month of observation
 DecimalDate → A decimal format of the date (e.g., 2000.5 means mid-year 2000)
 Sunspots → Monthly average sunspot count
 StdDev → Standard deviation (variation in data)
 Observations → Number of observations in that month
 Definitive → Indicator if the data is confirmed or provisional

 replace(-1, [Link]) → In the dataset, -1 is used to represent missing or invalid sunspot


counts. We replace these with NaN (Not a Number).

 dropna(subset=['Sunspots'], inplace=True) → Removes any rows where the Sunspots


value is missing. This ensures we only keep valid records.

 Combines Year and Month into a proper date format.

 .assign(DAY=1) → Adds a day column (always set to the 1st of the month).

 pd.to_datetime(...) → Converts year, month, and day into a standard datetime object (e.g.,
2000-05-01).

 This makes it easier to plot, filter, and analyze the data over time.
Output
Chapter:3
Preprocessing and Smoothning

 rolling(window=3).mean() → This applies a 3-month rolling average, meaning each


sunspot value is replaced by the average of itself and its two neighbors (previous
months).
o Why? Sunspot counts often show sudden fluctuations (spikes and dips). Rolling
average smooths out short-term noise and highlights long-term trends.
 [Link](inplace=True) → The first two rows don’t have enough previous values to
calculate a 3-month average, so they become NaN. We remove them to keep the dataset
clean.

Benefit: This step makes the data more stable and easier for the LSTM to learn patterns, instead
of being misled by sudden random changes.

 df['Sunspots'].values → Extracts the smoothed sunspot numbers as a NumPy array.


 reshape(-1, 1) → Converts the data into a 2D column vector, where -1 means "figure
out the number of rows automatically."
o LSTMs in TensorFlow/Keras expect input data in a specific shape:

[samples,timesteps,features][samples, timesteps,
features][samples,timesteps,features]

o This step prepares the data to later be split into sequences of sunspot values for
the LSTM model.

Example shape after reshaping:


If there are 3000 records, the array becomes (3000, 1) — 3000 samples, each with 1 feature
(sunspot count).

Output
Chapter:4
Normalize data

 scaler = MinMaxScaler()
o We create an object of the MinMaxScaler from [Link].
o It scales all values into a fixed range, usually 0 to 1.
 scaler.fit_transform(sunspots)
o fit → Learns the minimum and maximum values from the data.
o transform → Uses these values to scale all sunspot numbers.
o Formula:

Xscaled=X−XminXmax−XminX_{scaled} = \frac{X - X_{min}}{X_{max} -


X_{min}}Xscaled=Xmax−XminX−Xmin

o For example, if sunspot values range from 0 to 250, then:


 A sunspot count of 0 → becomes 0.0
 A sunspot count of 250 → becomes 1.0
 A sunspot count of 125 → becomes 0.5
 sunspots_scaled
o Stores the normalized data as a NumPy array.
o Now all values are between 0 and 1, making it easier for the LSTM to learn
patterns.

Normalization is Needed

1. Stabilizes training: Neural networks work better when input values are small and
consistent (0–1 range).
2. Faster convergence: Speeds up learning and reduces training time.
3. Avoids bias: Prevents features with larger numeric values from dominating smaller ones.
Output
Chapter:5
Creating Input-Output Pairs for Time-Series Forecasting

1. Function Definition – create_sequences


o This function splits the data into input sequences (X) and output labels (y).
o It uses a sliding window approach to create training samples.
2. Parameters
o data: The normalized sunspot values (already scaled between 0 and 1).
o window_size=60: Means the model will look at 60 previous months to predict
the next month’s sunspot number.
3. For Loop (Sliding Window)
o Takes 60 consecutive data points → stores them in X.
o Takes the next (61st) point → stores it in y.
o Then moves one step forward and repeats until all sequences are created.
4. Return
o Converts X and y into NumPy arrays for efficient training with LSTM.
5. Usage
o Now X contains shape (samples, 60, 1) → each sample is 60 time steps of
sunspot numbers.
o y contains shape (samples, 1) → the next value to be predicted.

Why This Step is Important?

 LSTM needs sequential input: Unlike regular models, LSTMs expect time-series data
in the form of sequences (windows).
 Captures temporal patterns: By looking back 60 months, the model learns seasonal
trends and long-term dependencies.
 Improves accuracy: Instead of predicting blindly, the model bases predictions on
meaningful historical data.
Chapter:6
Train-Test Data Split

1. Choosing Split Ratio


2. split_index = int(len(X) * 0.8)
o Takes 80% of the data for training and the remaining 20% for testing.
o split_index marks the boundary between training and testing samples.
3. Splitting Input Data (X)
X_train, X_test = X[:split_index], X[split_index:]

o X_train → contains the first 80% of input sequences.


o X_test → contains the last 20% of input sequences.
4. Splitting Output Data (y)
y_train, y_test = y[:split_index], y[split_index:]

o y_train → target values corresponding to X_train.


o y_test → target values corresponding to X_test.
5. Resulting Datasets
o X_train: Input data for training the model.
o y_train: Labels (true values) for training.
o X_test: Input data for testing.
o y_test: Labels for testing.

This Step is Important

 Prevents Overfitting → By holding out a separate test set, we can check if the model
generalizes well to unseen data.
 Ensures Fair Evaluation → Model performance is measured on data it has never seen
during training.
 80-20 Split → A widely accepted standard for time-series tasks when the dataset is
sufficiently large.
Chapter:7
Building the LSTM Model

1. Model Initialization
model = Sequential()

o Creates a Sequential model where layers are stacked one after another.
o Suitable for time-series forecasting since data flows forward in sequence.
2. First LSTM Layer
[Link](LSTM(128, return_sequences=True, input_shape=(WINDOW_SIZE,
1)))

o 128 units (neurons): Controls the learning capacity of this layer.


o input_shape=(WINDOW_SIZE, 1): Each sequence has WINDOW_SIZE time steps
with 1 feature (sunspots).
o return_sequences=True: Ensures the entire sequence is passed to the next
LSTM layer.
3. Dropout Layer
[Link](Dropout(0.3))

o Randomly “drops” 30% of the neurons during training.


o Prevents overfitting by forcing the network to generalize better.
4. Second LSTM Layer
[Link](LSTM(64))

o 64 units, processes the sequence from the previous LSTM layer.


o No return_sequences (default is False), so only the final output is passed
forward.
5. Second Dropout Layer
[Link](Dropout(0.3))

o Again applies 30% dropout to prevent overfitting in deeper layers.


6. Dense (Output) Layer
[Link](Dense(1))

o Fully connected layer with 1 neuron.


o Outputs a single predicted sunspot value for the next time step.
7. Compile Model
[Link](loss='mse', optimizer='adam')

o Loss = MSE (Mean Squared Error): Suitable for regression tasks.


o Optimizer = Adam: An adaptive optimizer that efficiently updates weights.

LSTM Architecture

 Captures Long-Term Dependencies: LSTM (Long Short-Term Memory) is designed


for sequential data like time-series.
 Handles Nonlinear Patterns: Sunspot numbers are irregular; LSTM can learn complex
temporal relationships.
 Dropout Layers: Reduce overfitting and improve generalization.
 Stacked LSTMs: Improves the model’s ability to capture both short-term and long-term
trends.
Chapter:8
Model Training

1. Early Stopping
early_stop = EarlyStopping(patience=10, restore_best_weights=True)

o Monitors validation loss during training.


o If loss does not improve for 10 consecutive epochs, training stops early.
o restore_best_weights=True ensures the model reverts to the best performance
weights (avoiding overfitting).
2. Learning Rate Reduction
reduce_lr = ReduceLROnPlateau(patience=5, factor=0.5, verbose=1)

o If validation loss does not improve for 5 epochs, the learning rate is reduced by
50%.
o Helps the optimizer make finer adjustments as it approaches the minimum.
3. Model Fitting
history = [Link](
X_train, y_train,
validation_data=(X_test, y_test),
epochs=20,
batch_size=16,
callbacks=[early_stop, reduce_lr],
verbose=1
)

o X_train, y_train: Training data.


o validation_data=(X_test, y_test): Used to check generalization after each epoch.
o epochs=20: Maximum training cycles. (May stop earlier due to EarlyStopping).
o batch_size=16: Data is split into batches of 16 samples for weight updates.
o callbacks: Uses both early stopping and learning rate adjustment.
o verbose=1: Shows progress bar and details during training.
4. History Object
history

o Stores training progress, including loss and validation loss for each epoch.
o Useful for plotting learning curves later.
Chapter:9
Model Predictions and Rescaling

1. Generate Predictions
y_pred_scaled = [Link](X_test)

oThe trained LSTM model is used to predict future values based on test
sequences.
o Predictions are in the scaled range (0–1) because we normalized the data earlier.
2. Inverse Transform for Predictions
y_pred = scaler.inverse_transform(y_pred_scaled)

oConverts predictions back to the original sunspot scale.


oThis makes results interpretable (actual number of sunspots instead of normalized
values).
3. Inverse Transform for True Values
y_true = scaler.inverse_transform(y_test)

o
Similarly, the actual test values are also converted back to the original scale.
o
This ensures a fair comparison between predicted vs actual values.
4. Output
y_true

o Displays the actual sunspot numbers (ground truth) for evaluation.

 Scaling was necessary for stable LSTM training.


 But predictions need to be converted back to original scale for interpretation.
 This step bridges the gap between model outputs and real-world values.
Chapter:10
Model Evaluation and Accuracy Measurement

 Mean Absolute Error (MAE)


mae = mean_absolute_error(y_true, y_pred)

 Calculates the average absolute difference between actual and predicted sunspot
numbers.
 Lower MAE = better accuracy.

 Root Mean Squared Error (RMSE)


rmse = [Link](mean_squared_error(y_true, y_pred))

 Measures the square-root of average squared error.


 More sensitive to large errors compared to MAE.

 R² Score (Coefficient of Determination)


r2 = r2_score(y_true, y_pred)

 Explains how much variance in the actual data is captured by the model.
 Value close to 1.0 means strong predictive power.

 Custom Accuracy Function


def accuracy_within_percent(y_true, y_pred, tolerance=15):
y_true, y_pred = y_true.flatten(), y_pred.flatten()
error = [Link]((y_true - y_pred) / y_true) * 100
accurate_count = [Link](error <= tolerance)
return (accurate_count / len(y_true)) * 100

 Defines a function to calculate percentage accuracy within a given error tolerance.


 Here, predictions within ±15% of actual values are considered “accurate.”

 Compute Accuracy
accuracy = accuracy_within_percent(y_true, y_pred, tolerance=15)

 Applies the custom function to measure realistic prediction accuracy.

 Print Final Results


print(f"MAE: {mae:.2f}")
print(f"RMSE: {rmse:.2f}")
print(f"R² Score: {r2:.4f}")
print(f"Accuracy (within ±15%): {accuracy:.2f}% ✅")

 Summarizes all metrics to clearly evaluate the model’s performance.


Output
Chapter:11
Visualization of Predictions

 Figure Size
[Link](figsize=(12, 6))

 Makes the plot wide enough for clear comparison.

 Plot Actual Sunspot Numbers


[Link](y_true, label='Actual Sunspots', color='blue')

 Draws the ground truth values in blue.

 Plot Predicted Sunspot Numbers


[Link](y_pred, label='Predicted Sunspots', color='orange')

 Draws the model predictions in orange.


 Allows visual comparison of trend alignment.

 Title and Labels


[Link]("🌞 Sunspot Prediction - Actual vs Predicted")
[Link]("Time (months)")
[Link]("Sunspot Count")

 Adds a clear title and axis labels for better readability.

 Legend & Grid


[Link]()
[Link](True)

 Legend distinguishes actual vs predicted curves.


 Grid makes it easier to compare values.
 Tight Layout & Show Plot
plt.tight_layout()
[Link]()

 Prevents overlap and displays the final plot.

Output
Chapter:12
Percentage-Based Accuracy Evaluation

 Flatten Inputs
Ensures both y_true and y_pred are 1D arrays:
y_true = y_true.flatten()
y_pred = y_pred.flatten()

 Compute Percentage Error


percent_error = [Link]((y_true - y_pred) / y_true) * 100

 Calculates error relative to the actual value.


 Expresses it in percentage terms.

 Count Accurate Predictions


accurate_count = [Link](percent_error <= tolerance_percent)

 Finds predictions where error is within tolerance (default ±10%).

 Convert to Percentage Accuracy


accuracy_percent = (accurate_count / len(y_true)) * 100

 Gives a final accuracy percentage.


Chapter:13
Final Accuracy Calculation with Tolerance

1. Commented Line
#accuracy = calculate_accuracy_percentage(actual_sunspots,
predicted_sunspots, tolerance_percent=10)

 This line (currently commented out) calls the function


calculate_accuracy_percentage.
 It checks how many predictions are within ±10% of the actual sunspot values.
 actual_sunspots → real observed data.
 predicted_sunspots → values predicted by the LSTM model.
 tolerance_percent=10 → we allow a 10% error margin.

2. Printing Accuracy
print(f"Prediction Accuracy (within ±10% tolerance): {accuracy:.2f}%")

 Displays the calculated accuracy on the screen.


 :.2f ensures the result is shown with two decimal places.
Output
Chapter:14
Future Sunspot Forecasting and Visualization

Forecasting Function – forecast_future()

This function is the heart of the forecasting process. It uses your trained LSTM model to predict
future sunspot counts beyond the available dataset.

How it Works:

1. Inputs:
o data: Your scaled sunspot series (values between 0 and 1).
o model: The trained LSTM model.
o scaler: Used to reverse scaling back to original sunspot numbers.
o window_size: Number of past time steps (months) the model needs for prediction
(here, 60).
o future_steps: How many months into the future you want to forecast (here, 4).
2. Select Last Sequence:
last_sequence = data[-window_size:]

o Takes the last 60 months from the dataset.


o This acts as the initial input for forecasting.
3. Prediction Loop:
for _ in range(future_steps):
input_seq = last_sequence.reshape(1, window_size, 1)
next_pred_scaled = [Link](input_seq, verbose=0)
[Link](next_pred_scaled[0, 0])
last_sequence = [Link](last_sequence, next_pred_scaled)[-
window_size:]

o Reshape sequence for LSTM input → (1, 60, 1) (batch=1, time=60,


features=1).
o Predicts the next month’s sunspot value (scaled).
o Appends it to predictions.
o Updates last_sequence by sliding the window (drop oldest, add newest
prediction).
o Repeats this until all future months are predicted.

🔑 This is called recursive forecasting (feeding predictions back as input).

4. Inverse Transform:
predictions = scaler.inverse_transform([Link](predictions).reshape(-
1, 1))

o Converts predictions from scaled values (0–1) back to actual sunspot counts.
5. Output:
Returns a list of future sunspot forecasts in original scale.

2. Forecasting Next 4 Months


future_predictions = forecast_future(sunspots_scaled, model, scaler,
WINDOW_SIZE, future_steps=4)

 Calls the function to forecast 4 months into the future.


 Example Output:
 🌞 Forecast for next 4 months:
 Month +1: 92.34 sunspots
 Month +2: 110.45 sunspots
 Month +3: 125.12 sunspots
 Month +4: 130.89 sunspots
 These numbers represent the predicted sunspot activity trend.
3. Visualization of Future Predictions

This part shows predictions visually compared to historical data.

Steps:

1. Plot Historical Data (last 200 months):


[Link](df['Date'][-200:], df['Sunspots'][-200:], label="Historical
Sunspots", color="blue")

o
Shows the recent sunspot pattern in blue.
o
Helps compare the new forecast with recent history.
2. Generate Future Dates:
future_dates = pd.date_range(start=df['Date'].iloc[-1] +
[Link](months=1)
periods=future_months, freq="MS")

oCreates a timeline for future months.


oStarts from the month after the last known date.
oGenerates exactly future_months new dates.
3. Plot Predictions:
[Link](future_dates, future_predictions, marker="o", linestyle="--",
color="red", label="Forecasted Sunspots")

o
Forecasted values are plotted in red dashed line with circle markers.
o
Makes it easy to spot the future trend.
4. Add Labels, Grid, Legend:
o Title: "🌞 Sunspot Forecast - Next Few Months".
o X-axis → Date, Y-axis → Sunspot Count.
o Grid improves readability.
o Legend distinguishes actual vs forecasted.

 The blue line (past data) shows real sunspot cycles.


 The red dashed line (future predictions) shows what the model thinks will happen next.
 This helps scientists, researchers, and analysts anticipate sunspot activity in upcoming
months.

Output
Chapter:15
User-Input Driven LSTM Predictions
Import required tools
from [Link] import relativedelta
import calendar

 relativedelta: lets us easily move backward or forward by months/years.


 calendar: used to get month names (like January, February).

2. Function Definition
def forecast_from_custom_date(model, scaler, window_size=60,
future_steps=4):

 model: trained LSTM model.


 scaler: fitted MinMaxScaler used during training for scaling/unscaling values.
 window_size: number of past months the LSTM expects as input (default = 60).
 future_steps: how many future months to forecast (default = 4).

3. User Input for Last Known Date


last_year = int(input("Enter last available year (e.g., 2025): "))
last_month = int(input("Enter last available month (1-12): "))
last_date = pd.to_datetime(f"{last_year}-{last_month}-01")
 User specifies last available year & month.
 last_date becomes the starting point of forecasting. Example: 2025-08-01.

4. User Input for Recent Sunspot Counts


num_inputs = int(input("Enter how many last months of data you want to
provide (2 or 3): "))
 Model normally expects 60 months of data, but here we allow user to give just 2 or 3
months manually.
last_values = []
for i in range(num_inputs):
input_date = last_date - relativedelta(months=num_inputs - i - 1)
month_name = calendar.month_name[input_date.month]
val = float(input(f"Enter sunspot count for {month_name}
{input_date.year}: "))
last_values.append(val)
 Example: If last date is Aug 2025, and user chooses 3 months, they’ll enter values for:
o June 2025
o July 2025
o August 2025
 These are stored in last_values.

5. Scaling User Input


last_values_scaled = [Link]([Link](last_values).reshape(-
1, 1))
 Since model was trained on scaled values, we scale user’s inputs.

5. Padding to Match
window_size
if len(last_values_scaled) < window_size:
padded = [Link]((window_size, 1))
padded[-len(last_values_scaled):] = last_values_scaled
last_sequence = padded
else:
last_sequence = last_values_scaled[-window_size:]
 Model expects exactly window_size months (e.g., 60).
 If user provides fewer months (2 or 3), we pad with zeros at the beginning so length
becomes 60.
 Example: [0,0,...,0,scaled(June),scaled(July),scaled(August)].

7. Forecasting Future Months


predictions = []
for _ in range(future_steps):
input_seq = last_sequence.reshape(1, window_size, 1)
next_pred_scaled = [Link](input_seq, verbose=0)
[Link](next_pred_scaled[0, 0])
last_sequence = [Link](last_sequence, next_pred_scaled)[-
window_size:]

 Loop runs future_steps times (e.g., 4 months).


 Each iteration:
1. Reshape input to LSTM format (1, 60, 1).
2. Model predicts next month’s scaled sunspot count.
3. Store prediction in predictions.
4. Slide window → append prediction, drop oldest value.

8. Inverse Scaling
predictions = scaler.inverse_transform([Link](predictions).reshape(-
1, 1)).flatten()
 Convert predictions back to original sunspot scale.
 Example: 0.45 → 120 sunspots.

9. Print Forecast with Dates


print("\n🌞 Predicted Sunspot Counts:")
for i, val in enumerate(predictions, 1):
future_date = last_date + relativedelta(months=i)
month_name = calendar.month_name[future_date.month]
print(f"{month_name} {future_date.year}: {val:.2f} sunspots")
 Prints each forecasted month with date + predicted sunspot count.
Output
Model Architecture

The final model is:


Input Layer (60 timesteps, 1 feature)

LSTM Layer (128 units, return_sequences=True)

Dropout (30%)

LSTM Layer (64 units)

Dropout (30%)

Dense Layer (1 unit → predicted sunspot count)
Conclusion
 The custom date-based LSTM method allows users to enter the last known month/year
and recent sunspot values.
 It generates reliable forecasts even if the dataset is incomplete or outdated.
 This makes the system adaptable to real-world scenarios.
 Enhances user-friendliness and practical usability.
 Demonstrates the strength of LSTM models for time-series forecasting, especially solar
sunspot prediction.
Appendix
Dataset Information
 Source: SILSO (Sunspot Index and Long-term Solar Observations).
 Data Type: Monthly total sunspot numbers.
 Time Range: Varies (e.g., 1749 – present).
 Format: Date (YYYY-MM) and Sunspot Count.
Tools & Libraries Used
 Python – Programming language.
 Pandas, NumPy – Data processing and manipulation.
 Matplotlib – Visualization of trends and forecasts.
 Scikit-learn (MinMaxScaler) – Scaling and normalization.
 TensorFlow/Keras – Building and training the LSTM model.
 Dateutil & Calendar – Handling custom dates for forecasting.
LSTM Model Details
 Input Shape: (window_size, 1)
 Layers:
o LSTM layers for sequence learning.
o Dense output layer for prediction.
 Loss Function: Mean Squared Error (MSE).
 Optimizer: Adam.
Custom Forecasting Features
 User specifies last available year and month.
 Option to input last 2–3 months of sunspot data.
 Model forecasts future sunspots (e.g., next 4 months).
 Works even if official dataset is incomplete or not updated.
E. Visualization
 Historical vs. Predicted Sunspot Graphs.
 Highlight of future predicted points on timeline.
References
1. SILSO World Data Center – Sunspot Number and Long-term Solar Observations. Royal
Observatory of Belgium, Brussels. Available: [Link]
2. Y. LeCun, Y. Bengio, and G. Hinton, “Deep learning,” Nature, vol. 521, pp. 436–444,
2015.
3. S. Hochreiter and J. Schmidhuber, “Long Short-Term Memory,” Neural Computation,
vol. 9, no. 8, pp. 1735–1780, 1997.
4. I. Goodfellow, Y. Bengio, and A. Courville, Deep Learning. Cambridge, MA: MIT Press,
2016.
5. G. Box, G. Jenkins, G. Reinsel, and G. Ljung, Time Series Analysis: Forecasting and
Control, 5th ed. Hoboken, NJ: Wiley, 2015.
6. S. Makridakis, E. Spiliotis, and V. Assimakopoulos, “Statistical and Machine Learning
forecasting methods: Concerns and ways forward,” PLOS ONE, vol. 13, no. 3, e0194889,
2018.
7. F. Chollet, Deep Learning with Python, 2nd ed. Shelter Island, NY: Manning
Publications, 2021.
8. TensorFlow Documentation – [Link]
9. Scikit-learn Documentation – [Link]
10. NumPy Documentation – [Link]

Common questions

Powered by AI

The method used is known as the sliding window approach, which creates input-output pairs by assembling consecutive data points into sequences. Each sequence consists of a defined number of past months, while the subsequent data point acts as the output label. This approach is essential as it captures temporal patterns, allowing the model to learn seasonal trends and long-term dependencies critical for accurate time-series forecasting .

The primary reason for using an 80-20 train-test data split is to prevent overfitting and ensure that the model's performance is evaluated fairly on unseen data. By holding out a separate test set, the model's ability to generalize well beyond the training data is assessed. This split is standard in time-series tasks to validate the model effectively when the dataset is sufficiently large .

Evaluation metrics such as Mean Absolute Error (MAE) provide insights into the average prediction error, offering a direct measure of model accuracy in terms of the deviations from actual sunspot counts. The R² Score, on the other hand, indicates how well the variance in the observed data is captured by the model's predictions. Together, these metrics allow a comprehensive assessment of both the precision and explanatory power of the LSTM model, ensuring reliable forecast quality .

Scaling data into a fixed range, such as 0 to 1, is necessary because it stabilizes training, speeds up the learning process, and prevents features with larger numeric values from dominating others. This ensures that the neural networks, including LSTMs, work more effectively by making input values small and consistent, leading to faster convergence and a reduction in overfitting .

LSTMs are particularly well-suited for predicting sunspot activity due to their ability to 'remember' patterns over long periods of time, which is crucial for capturing the natural cycles of sunspot activity. This memory feature allows LSTMs to learn from past sunspot records and produce accurate forecasts of future sunspot numbers. This is significant for understanding solar cycles and their impact on Earth .

The MinMaxScaler transforms sunspot data into a standardized range, typically between 0 and 1, facilitating faster and more stable training for the LSTM model. This transformation avoids bias in training due to large numeric variations, allowing the model to learn more effectively from balanced input values by maintaining proportionate influence across all features .

Recursive forecasting enhances prediction outcomes by constantly feeding model predictions back into the system as inputs for future step forecasts. This method builds upon each previous step's forecast, allowing the LSTM model to anticipate future trends more accurately. By updating the input sequence iteratively, the model adapts to evolving patterns, thus producing a more dynamic and realistic projection of future sunspot counts .

Applying a rolling average to the sunspot data helps in smoothing out short-term fluctuations and noise, allowing the model to focus on long-term trends instead. These smoothed trends are more stable and easier for the LSTM model to learn, thereby improving its ability to discern patterns in historically fluctuating data like sunspot counts .

Dropout layers are integrated into the LSTM model as a regularization technique to prevent overfitting. By randomly turning off a fraction of neurons during training, dropout layers promote redundancy and robustness in the network's function. This forces the model to learn multiple independent representations, thereby improving its generalization ability on unseen data .

The function forecast_from_custom_date enhances user-friendliness by allowing users to specify the last known month/year and optionally provide recent sunspot data. This adaptability lets the model generate forecasts even when the dataset is incomplete or outdated, which is crucial in real-world applications where timely data may not always be available. This interaction allows users to easily input new data points and obtain updated predictions, demonstrating the flexibility of the forecasting system .

You might also like