--- license: apache-2.0 library_name: pytorch tags: - tabular-classification --- ## Model Documentation: Space Weather Severity Classification ### Overview This document provides an overview of the space weather severity classification model developed using PyTorch. The model is designed to classify space weather conditions into four severity levels (classes) based on various solar and geomagnetic parameters. The training data was collected from the DSCOVR satellite, which monitors solar wind conditions from the L1 point, approximately 1.5 million kilometers from Earth. ### Dataset The dataset used for training the model consists of hourly observations between 2016 and 2018. Each observation includes solar wind parameters and geomagnetic field measurements. The data contains the following features: - **year**: Year of the observation - **month**: Month of the observation - **bx_gsm**: X component of the interplanetary magnetic field in the GSM coordinate system - **by_gsm**: Y component of the interplanetary magnetic field in the GSM coordinate system - **bz_gsm**: Z component of the interplanetary magnetic field in the GSM coordinate system - **bt**: Magnitude of the interplanetary magnetic field - **intensity**: Total intensity of the Earth's geomagnetic field - **declination**: Angle between magnetic north and true north - **inclination**: Angle of the Earth's magnetic field relative to the horizontal plane - **north**: North component of the Earth's magnetic field - **east**: East component of the Earth's magnetic field - **vertical**: Vertical component of the Earth's magnetic field - **horizontal**: Horizontal component of the Earth's magnetic field - **class**: Space weather severity level (target variable) **Source**: https://huggingface.co/datasets/nayem-ng/space-weather-severity-dscovr-2016-18 ### Preprocessing - **Data Filling**: Missing values in the dataset were filled with zeros. - **Feature Selection**: The features listed above were selected for model training. - **Train-Test Split**: The data was split into training (80%) and testing (20%) sets using `train_test_split` from Scikit-learn. - **Standardization**: Features were standardized using `StandardScaler` to ensure that they have a mean of 0 and a standard deviation of 1. ### Model Architecture The model is a fully connected neural network (also known as a feedforward neural network) implemented in PyTorch. The architecture consists of the following layers: - **Input Layer**: 13 input features - **Hidden Layer 1**: Fully connected layer with 64 neurons and ReLU activation - **Hidden Layer 2**: Fully connected layer with 32 neurons and ReLU activation - **Hidden Layer 3**: Fully connected layer with 16 neurons and ReLU activation - **Output Layer**: Fully connected layer with 4 output neurons (one for each class) ### Training - **Loss Function**: CrossEntropyLoss, suitable for multiclass classification tasks. - **Optimizer**: Adam optimizer with a learning rate of 0.001. - **Training Process**: The model was trained for 100 epochs with a batch size of 64. During each epoch, the model parameters were updated based on the computed loss. ### Evaluation After training, the model was evaluated on the test set, achieving an accuracy of **100%**. The evaluation was done using the model's predictions compared to the actual labels in the test set. ### Model Saving The trained model's state dictionary was saved to a file named `space-weather-severity-multiclass.pth`. This file contains the learned parameters of the model and can be loaded later for inference or further training. ### Code The following Python code was used to develop, train, and evaluate the model: ```python from sklearnex import patch_sklearn patch_sklearn() import pandas as pd from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.data import DataLoader, TensorDataset # Load the dataset data = pd.read_csv('./spaceWeatherSeverityMulticlass.csv') data = data.fillna(0) # Features and labels X = data[['year', 'month', 'bx_gsm', 'by_gsm', 'bz_gsm', 'bt', 'intensity', 'declination', 'inclination', 'north', 'east', 'vertical', 'horizontal']].values y = data['class'].values # Split the data into train and test sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Standardize the features scaler = StandardScaler() X_train = scaler.fit_transform(X_train) X_test = scaler.transform(X_test) # Convert to PyTorch tensors X_train = torch.tensor(X_train, dtype=torch.float32) y_train = torch.tensor(y_train, dtype=torch.long) X_test = torch.tensor(X_test, dtype=torch.float32) y_test = torch.tensor(y_test, dtype=torch.long) # Create data loaders train_dataset = TensorDataset(X_train, y_train) test_dataset = TensorDataset(X_test, y_test) train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True) test_loader = DataLoader(test_dataset, batch_size=64, shuffle=False) class SpaceWeatherNet(nn.Module): def __init__(self): super(SpaceWeatherNet, self).__init__() self.fc1 = nn.Linear(13, 64) self.fc2 = nn.Linear(64, 32) self.fc3 = nn.Linear(32, 16) self.fc4 = nn.Linear(16, 4) # 4 output classes def forward(self, x): x = F.relu(self.fc1(x)) x = F.relu(self.fc2(x)) x = F.relu(self.fc3(x)) x = self.fc4(x) # No activation here, as we'll use CrossEntropyLoss return x model = SpaceWeatherNet() # Define loss function and optimizer criterion = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters(), lr=0.001) # Training loop num_epochs = 100 for epoch in range(num_epochs): model.train() running_loss = 0.0 for inputs, labels in train_loader: # Zero the parameter gradients optimizer.zero_grad() # Forward pass outputs = model(inputs) loss = criterion(outputs, labels) # Backward pass and optimization loss.backward() optimizer.step() running_loss += loss.item() # Print the loss for this epoch print(f'Epoch {epoch + 1}/{num_epochs}, Loss: {running_loss / len(train_loader)}') model.eval() correct = 0 total = 0 with torch.no_grad(): for inputs, labels in test_loader: outputs = model(inputs) _, predicted = torch.max(outputs, 1) total += labels.size(0) correct += (predicted == labels).sum().item() print(f'Accuracy on test set: {100 * correct / total}%') # Save the model torch.save(model.state_dict(), 'space-weather-severity-multiclass.pth') ``` ### Conclusion This model provides a reliable method for classifying space weather severity based on solar wind and geomagnetic field data. It can be utilized for real-time space weather forecasting and risk assessment, offering significant value to researchers and engineers working in space weather monitoring.