├── biodiversity_loss_predictions.xlsx ├── README.md └── First Code /biodiversity_loss_predictions.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Geraldine-Winston/Biodiversity-Loss-Prediction-under-Climate-Change-using-ensemble-deep-learning-models./HEAD/biodiversity_loss_predictions.xlsx -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Biodiversity Loss Prediction under Climate Change 2 | 3 | This project predicts **biodiversity loss** under **climate change** scenarios using an ensemble of **deep learning models**. 4 | 5 | ## Project Overview 6 | 7 | We implemented an ensemble of three deep learning architectures: 8 | - **MLP** (**M**ulti-**L**ayer **P**erceptron) 9 | - **CNN** (**C**onvolutional **N**eural **N**etwork) 10 | - **LSTM** (**L**ong **S**hort-Term **M**emory Network) 11 | 12 | The models are trained on **climate** and **environmental data**, and their predictions are combined to improve **accuracy** and **robustness**. 13 | 14 | ## Project Structure 15 | 16 | ``` 17 | 📁 Biodiversity-Loss-Prediction 18 | ├── data/ 19 | │ └── biodiversity_climate_data.csv # Dataset (features + labels) 20 | ├── biodiversity_loss_prediction.py # Main training and evaluation script 21 | ├── biodiversity_loss_predictions.xlsx # Output prediction results 22 | ├── README.md # Project documentation 23 | ├── requirements.txt # Python dependencies 24 | └── .gitignore # Ignored files and folders 25 | ``` 26 | 27 | ## Requirements 28 | 29 | Install all necessary Python packages by running: 30 | 31 | ```bash 32 | pip install -r requirements.txt 33 | ``` 34 | 35 | **`requirements.txt` includes:** 36 | 37 | ``` 38 | torch 39 | scikit-learn 40 | pandas 41 | numpy 42 | openpyxl 43 | ``` 44 | 45 | ## How to Run 46 | 47 | 1. Place your **dataset** inside the `data/` directory as `biodiversity_climate_data.csv`. The dataset should include: 48 | - **Environmental features** (e.g., temperature, precipitation, CO₂ concentration, habitat index, etc.) 49 | - **Target variable**: `biodiversity_loss` 50 | 51 | 2. Run the script: 52 | 53 | ```bash 54 | python biodiversity_loss_prediction.py 55 | ``` 56 | 57 | 3. After running: 58 | - **Evaluation metrics** such as **MSE** (Mean Squared Error) and **R² Score** will be printed. 59 | - **Predictions** will be saved to `biodiversity_loss_predictions.xlsx`. 60 | 61 | ## Dataset Example 62 | 63 | ```csv 64 | temperature,precipitation,co2_concentration,habitat_index,biodiversity_loss 65 | 15.2,1200,400,0.8,5.1 66 | 16.0,1100,405,0.75,5.6 67 | 14.8,1300,398,0.85,4.9 68 | ``` 69 | 70 | ## Output 71 | 72 | - **`biodiversity_loss_predictions.xlsx`**: Contains the true and predicted biodiversity loss values. 73 | - **Console Output**: Displays evaluation metrics. 74 | 75 | ## Acknowledgements 76 | 77 | - Climate Data Sources: [**WorldClim**](https://www.worldclim.org/) 78 | - [**IPCC Data Distribution Centre**](https://www.ipcc-data.org/) 79 | - Biodiversity Data: [**GBIF**](https://www.gbif.org/), [**IUCN Red List**](https://www.iucnredlist.org/) 80 | 81 | ## Author 82 | 83 | **Ayebawanaemi Geraldine Winston** 84 | 85 | -------------------------------------------------------------------------------- /First Code: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import torch.optim as optim 4 | from torch.utils.data import DataLoader, Dataset 5 | import pandas as pd 6 | import numpy as np 7 | from sklearn.model_selection import train_test_split 8 | from sklearn.preprocessing import StandardScaler 9 | 10 | # Define Dataset 11 | class BiodiversityDataset(Dataset): 12 | def __init__(self, X, y): 13 | self.X = torch.tensor(X, dtype=torch.float32) 14 | self.y = torch.tensor(y, dtype=torch.float32) 15 | 16 | def __len__(self): 17 | return len(self.X) 18 | 19 | def __getitem__(self, idx): 20 | return self.X[idx], self.y[idx] 21 | 22 | # Define MLP Model 23 | class MLP(nn.Module): 24 | def __init__(self, input_dim): 25 | super(MLP, self).__init__() 26 | self.fc = nn.Sequential( 27 | nn.Linear(input_dim, 128), 28 | nn.ReLU(), 29 | nn.Linear(128, 64), 30 | nn.ReLU(), 31 | nn.Linear(64, 1) 32 | ) 33 | 34 | def forward(self, x): 35 | return self.fc(x) 36 | 37 | # Define CNN Model 38 | class CNN(nn.Module): 39 | def __init__(self, input_dim): 40 | super(CNN, self).__init__() 41 | self.conv1d = nn.Conv1d(1, 16, kernel_size=3, padding=1) 42 | self.relu = nn.ReLU() 43 | self.pool = nn.AdaptiveAvgPool1d(1) 44 | self.fc = nn.Linear(16, 1) 45 | 46 | def forward(self, x): 47 | x = x.unsqueeze(1) # Add channel dimension 48 | x = self.conv1d(x) 49 | x = self.relu(x) 50 | x = self.pool(x) 51 | x = x.view(x.size(0), -1) 52 | return self.fc(x) 53 | 54 | # Define LSTM Model 55 | class LSTMModel(nn.Module): 56 | def __init__(self, input_dim): 57 | super(LSTMModel, self).__init__() 58 | self.lstm = nn.LSTM(input_dim, hidden_size=64, batch_first=True) 59 | self.fc = nn.Linear(64, 1) 60 | 61 | def forward(self, x): 62 | x = x.unsqueeze(1) # (batch, seq_len=1, input_dim) 63 | out, (h_n, c_n) = self.lstm(x) 64 | return self.fc(h_n[-1]) 65 | 66 | # Ensemble Model 67 | class EnsembleModel(nn.Module): 68 | def __init__(self, models): 69 | super(EnsembleModel, self).__init__() 70 | self.models = models 71 | 72 | def forward(self, x): 73 | preds = [model(x) for model in self.models] 74 | return torch.stack(preds, dim=0).mean(0) 75 | 76 | # Load your dataset (replace this with your real data) 77 | df = pd.read_csv('biodiversity_climate_data.csv') 78 | X = df.drop('biodiversity_loss', axis=1).values 79 | y = df['biodiversity_loss'].values 80 | 81 | # Train-Test Split 82 | X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) 83 | 84 | # Standardize features 85 | scaler = StandardScaler() 86 | X_train = scaler.fit_transform(X_train) 87 | X_test = scaler.transform(X_test) 88 | 89 | # Dataset and DataLoader 90 | train_dataset = BiodiversityDataset(X_train, y_train) 91 | test_dataset = BiodiversityDataset(X_test, y_test) 92 | train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True) 93 | test_loader = DataLoader(test_dataset, batch_size=32) 94 | 95 | # Initialize models 96 | input_dim = X_train.shape[1] 97 | mlp = MLP(input_dim) 98 | cnn = CNN(input_dim) 99 | lstm = LSTMModel(input_dim) 100 | 101 | # Ensemble 102 | ensemble = EnsembleModel([mlp, cnn, lstm]) 103 | 104 | # Loss and Optimizer 105 | criterion = nn.MSELoss() 106 | optimizer = optim.Adam(ensemble.parameters(), lr=0.001) 107 | 108 | # Training Loop 109 | num_epochs = 50 110 | for epoch in range(num_epochs): 111 | ensemble.train() 112 | epoch_loss = 0 113 | for X_batch, y_batch in train_loader: 114 | optimizer.zero_grad() 115 | outputs = ensemble(X_batch) 116 | loss = criterion(outputs.squeeze(), y_batch) 117 | loss.backward() 118 | optimizer.step() 119 | epoch_loss += loss.item() 120 | print(f"Epoch [{epoch+1}/{num_epochs}], Loss: {epoch_loss/len(train_loader):.4f}") 121 | 122 | # Evaluation 123 | ensemble.eval() 124 | all_preds = [] 125 | all_labels = [] 126 | with torch.no_grad(): 127 | for X_batch, y_batch in test_loader: 128 | preds = ensemble(X_batch).squeeze() 129 | all_preds.append(preds.numpy()) 130 | all_labels.append(y_batch.numpy()) 131 | 132 | all_preds = np.concatenate(all_preds) 133 | all_labels = np.concatenate(all_labels) 134 | 135 | # Metrics 136 | from sklearn.metrics import mean_squared_error, r2_score 137 | mse = mean_squared_error(all_labels, all_preds) 138 | r2 = r2_score(all_labels, all_preds) 139 | print(f"Test MSE: {mse:.4f}") 140 | print(f"Test R2 Score: {r2:.4f}") 141 | --------------------------------------------------------------------------------