Species Distribution Modeling in Scikit Learn
Last Updated :
07 Jun, 2024
Species Distribution Modeling (SDM) is a crucial tool in conservation biology, ecology, and related fields. It involves predicting the geographic distribution of species based on environmental variables and species occurrence data. This article explores how to implement SDM using Scikit-Learn, a popular machine learning library in Python.
Introduction to Species Distribution Modeling
Species Distribution Models (SDMs) predict the spatial distribution of species by correlating species occurrence data with environmental variables. This correlation enables scientists to infer where species are likely to be found based on the environmental characteristics of a given area.
These models are essential for understanding species habitats, planning conservation efforts, and studying the impacts of climate change on biodiversity.
- Species Distribution Modeling (SDM) is a pivotal tool in ecology and conservation biology, allowing researchers to anticipate and map the spatial distribution of species across landscapes.
- By integrating species occurrence data with environmental variables such as temperature, precipitation, elevation, and land cover, SDMs unveil the ecological niches and habitat preferences of organisms.
Why Use Scikit-Learn for SDM?
Scikit-Learn offers a robust set of tools for machine learning, including various algorithms that can be applied to SDM. Its ease of use, extensive documentation, and active community make it an excellent choice for implementing SDMs.
Workflow for Species Distribution Modeling
The typical workflow for SDM in Scikit-Learn involves several steps:
- Data Collection: Gather species occurrence data and environmental variables.
- Data Preprocessing: Clean and prepare the data for modeling.
- Model Training: Train a machine learning model using the prepared data.
- Model Evaluation: Assess the model's performance using appropriate metrics.
- Prediction and Mapping: Use the model to predict species distribution and visualize the results.
Step-by-Step Guide for Building an Species Distribution Model
Let's create a Species Distribution Model (SDM) using a dataset from Kaggle, we need to select a dataset that is relatively small in size (in kilobytes). Based on the provided search results, the "Bird Sightings Dataset" from Kaggle seems to be a suitable choice as it includes information on different bird species, their locations, dates, and times of sighting, as well as descriptions of the birds.
Step 1: Load Necessary Libraries
Python
import pandas as pd
from sklearn.preprocessing import StandardScaler, LabelEncoder, OneHotEncoder
from sklearn.impute import SimpleImputer
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.svm import OneClassSVM
from sklearn.metrics import roc_auc_score
from sklearn.preprocessing import label_binarize
import matplotlib.pyplot as plt
Step 2: Load and inspect the dataset
Python
data = pd.read_csv('birdsoftheworld-unprocessed.csv')
print(data.columns)
Output:
Index(['species', 'location', 'time', 'description of bird', 'sex',
'feather color', 'Unnamed: 6', 'Unnamed: 7', 'Unnamed: 8', 'Unnamed: 9',
'Unnamed: 10', 'Unnamed: 11'],
dtype='object')
Step 3: Data Preprocessing
We'll use the 'location' feature and other relevant features. We will need to encode categorical features and handle any missing values.
Python
Select relevant columns
features = data[['location', 'sex', 'feather color']]
labels = data['species']
# Handle missing values and encode categorical features
preprocessor = ColumnTransformer(
transformers=[
('cat', Pipeline([
('imputer', SimpleImputer(strategy='most_frequent')),
('encoder', OneHotEncoder(handle_unknown='ignore'))
]), ['location', 'sex', 'feather color'])
])
# Standardize the features
pipeline = Pipeline(steps=[
('preprocessor', preprocessor),
('scaler', StandardScaler(with_mean=False))
])
features_processed = pipeline.fit_transform(features)
# Encode labels
label_encoder = LabelEncoder()
labels_encoded = label_encoder.fit_transform(labels)
Step 4: Model Training
Train a One-Class SVM model to predict species distribution
Python
model = OneClassSVM(kernel='rbf', gamma=0.1, nu=0.1)
model.fit(features_processed)
Output:
OneClassSVM
OneClassSVM(gamma=0.1, nu=0.1)
Step 5: Model Evaluation
Evaluate the model using the Area Under the ROC Curve (AUC) metric for multi-class classification.
Python
labels_binarized = label_binarize(labels_encoded, classes=range(len(label_encoder.classes_)))
# Predict the species distribution
predictions = model.decision_function(features_processed)
# Reshape predictions to match the shape of labels_binarized
predictions_reshaped = predictions.reshape(-1, 1)
auc_score = roc_auc_score(labels_binarized, predictions_reshaped, average='macro', multi_class='ovr')
print(f'Area under the ROC curve: {auc_score:.4f}')
Output:
Area under the ROC curve: 0.0038
Step 6: Prediction and Mapping
Since we don't have geographic coordinates, we will visualize the predictions using a simple scatter plot.
Python
# Predict the species distribution
predictions = model.predict(features_processed)
plt.figure(figsize=(10, 6))
plt.scatter(range(len(predictions)), predictions, c=predictions, cmap='coolwarm', alpha=0.5)
plt.title('Bird Species Distribution Predictions')
plt.xlabel('Sample Index')
plt.ylabel('Prediction')
plt.show()
Output:
Species Distribution ModelingThe scatter plot provides a clear visualization of the model's binary predictions for bird species distribution. The distinct separation between the two clusters of points indicates that the model is making confident predictions. This visualization is valuable for understanding species distribution patterns and informing conservation efforts.
- The prediction values are binary, with -1.0 indicating one class (likely absence or a negative prediction) and 1.0 indicating another class (likely presence or a positive prediction).
- Each point on the x-axis corresponds to a different sample or observation in the dataset.
- The plot shows two distinct clusters of points: one at y = -1.0 (blue) and another at y = 1.0 (red). This indicates that the model has made clear binary predictions for each sample, classifying them into two distinct groups.
Conclusion
Species Distribution Modeling is a powerful tool for understanding and conserving biodiversity. Scikit-Learn provides a flexible and efficient framework for implementing SDMs. By following the workflow outlined in this article, you can leverage Scikit-Learn's machine learning capabilities to predict and visualize species distributions.
Similar Reads
Learning Model Building in Scikit-learn Building machine learning models from scratch can be complex and time-consuming. Scikit-learn which is an open-source Python library which helps in making machine learning more accessible. It provides a straightforward, consistent interface for a variety of tasks like classification, regression, clu
8 min read
Top Inbuilt DataSets in Scikit-Learn Library Scikit-Learn is one of the most popular libraries of Python for machine learning. This library comes equipped with various inbuilt datasets perfect for practising and experimenting with different algorithms. These datasets cover a range of applications, from simple classification tasks to more compl
6 min read
Feature Selection in Python with Scikit-Learn Feature selection is a crucial step in the machine learning pipeline. It involves selecting the most important features from your dataset to improve model performance and reduce computational cost. In this article, we will explore various techniques for feature selection in Python using the Scikit-L
4 min read
Identifying Overfitting in Machine Learning Models Using Scikit-Learn Overfitting is a critical issue in machine learning that can significantly impact the performance of models when applied to new, unseen data. Identifying overfitting in machine learning models is crucial to ensuring their performance generalizes well to unseen data. In this article, we'll explore ho
7 min read
Project | Scikit-learn - Whisky Clustering Introduction | Scikit-learnScikit-learn is a machine learning library for Python.It features various classification, regression and clustering algorithms including support vector machines, random forests, gradient boosting, k-means and DBSCAN, and is designed to interoperate with the Python numerica
4 min read
Multiclass classification using scikit-learn Multiclass classification is a popular problem in supervised machine learning.Problem - Given a dataset of m training examples, each of which contains information in the form of various features and a label. Each label corresponds to a class, to which the training example belongs. In multiclass clas
5 min read