Assignment 07: A First Machine Learning SDM
BIO597 Spatial Analysis of Biodiversity
Repeat the workflow from Lab 07 for a Maine amphibian species of your choice. Focus on understanding what the model learns and what its predictions mean. You may reuse code from the lab.
Deliverable
Submit this notebook with your completed code, figures, and short written answers, plus a saved suitability raster. There is no need to tune model settings or compare additional algorithms.
1. Import packages and load the data
Run this notebook from docs/assignments. Import the packages used in Lab 07. Load the two supplied files and display species counts.
# Import the packages used in Lab 07.
amphibians = gpd.read_file("../data/amphibians+bioclim.geojson")
climate = xr.open_dataset("../data/maine_bioclim.nc")
# Display species counts.
2. Choose your species
Choose a common species other than the wood frog, with at least 500 records. Keep records with coordinate uncertainty below 100 m, as in the lab.
target_species = "YOUR SPECIES HERE"
# Select your species and apply the uncertainty filter.
# Store the result as presence and print its record count.
Why did you choose this species? Write one or two sentences.
3. Keep all climate variables available
As in Lab 07, make a list called climate_variables containing bio_1 through bio_19. We will choose the model predictors after preparing the modeling table.
# Create climate_variables and display these columns at your species' records.
4. Prepare presence and background tables
Use the prepared climate values at the occurrence records directly, as in the revised lab. We do not need to match records to raster cells or join tables by coordinates.
The code below prepares the climate grid for background sampling and mapping. Each grid row is a cell, but each presence row is an occurrence record. Several presence records may therefore describe the same climate cell.
grid = climate[climate_variables].to_dataframe().reset_index()
grid = grid[["x", "y"] + climate_variables]
grid = grid.dropna(subset=climate_variables)
grid.head()
# Keep the prepared predictors and remove records with missing climate values.
presence_data = presence[climate_variables].dropna().copy()
presence_data["observed"] = 1
print("Usable presence records:", len(presence_data))
presence_data.head()
# Sample available environments, using one background row per presence record.
background = grid.sample(n=len(presence_data), random_state=597).copy()
background["observed"] = 0
model_data = pd.concat([presence_data, background], ignore_index=True)
model_data["observed"].value_counts()
The missing-value cleanup keeps incomplete presence records out of both models. random_state=597 makes the background sample repeatable. Background is sampled from the full valid grid, so it can include cells where the species has been recorded. It represents available environments, not confirmed absences.
5. Define predictors and response
Start with features = ["bio_1"], annual mean temperature, to connect with last week's model. Set X to this predictor column and y to the observed labels. Only the selected climate columns belong in X; the background's coordinate columns are for mapping.
# Set features to bio_1 only.
# Define X and y, following Lab 07.
6. Split, fit, and predict
Split model_data into train_data (75%) and test_data (25%) with `random_state=597``, as in the lab
Fit a RandomForestRegressor using only bio_1, with 100 trees, min_samples_leaf=5, and random_state=597.
# Split model_data into train_data and test_data.
# Define X_train, X_test, y_train, and y_test using features and observed.
# Create model and fit it to the training data.
# Predict suitability for X_test.
7. Check the BIO1-only predictions
Calculate mean absolute error and store it as bio1_error. Plot test predictions for presence and background cells in overlapping histograms, as in the lab.
# Calculate and print bio1_error.
# Plot the predicted score distributions for the two test labels.
Interpretation: Do test presence cells generally receive higher scores? Why should we use withheld cells to check the model?
8. Add precipitation and compare MAE
Choose three new bioclim variables to test (you can look up the meanings of the variables in the bioclimatic variables documentation)
Change features to ["bio_X", "bio_XX", "bio_XX"] (replacing the "X" values with the integer
index of the bioclim variables you chose). Create a new feature table (X) selecting just the features of interest and perform a new test/train split. Fit a new forest with these three predictors and calculate three_variable_error. Print both MAEs together.
# Select the three predictor columns from the existing training and test tables.
# Fit three_variable_model and predict the same withheld cells.
# Calculate three_variable_error and print it beside bio1_error.
Interpretation: Did the three new bioclim variables improve prediction error for your species? Explain why the new conditions you chose may or may not be relevant biologically.
9. Make a suitability map
Fit a new final_model using all modeling rows and the three predictors. Predict scores for grid[features]. Use the plotting recipe from Lab 07 to turn the suitability column into a raster and map it from 0 to 1.
# Update X to include all three predictors.
# Fit final_model with the same forest settings.
# Add grid["suitability"], convert it to a raster named suitability, and plot it.
Interpretation: Which parts of Maine receive high scores? Why should we describe these as relative suitability rather than probability of occurrence?
10. Plot variable importance
Use final_model.feature_importances_ and pd.Series() as in the lab to make a bar plot for the three predictors.
# Make and label a variable-importance plot.
Interpretation: Which variable did your forest use most? Give one reason why this does not establish an ecological cause.
11. Save your map
Save suitability as Assignment07_suitability.tif using the same CRS and export commands as the lab.
# Assign EPSG:4326 and save the suitability raster.
12. Run the training and prediction again including all 19 bioclim variables
Think about: Do you predict that more variables will improve the MAE of the model?
# Run the full analysis again but this time include all 19 bioclim variables
# feel free to copy/paste from cells above
Final reflection
Answer in two or three sentences per question.
- How does this week's BIO1-only random forest differ from last week's hand-built temperature curve?
- What did the comparison with three predictors teach you? Would you expect more predictors always to reduce test error?
- Several presence records can share a climate cell. Why might a random split then make prediction look easier than predicting a new location?
- Name one habitat variable or sampling issue missing from your model. How might it affect the map?
Submit your work
Restart the kernel and run the notebook from top to bottom. Check your figures, written answers, both MAEs, and saved raster. Commit and push the notebook and raster to your class GitHub repository.
git add docs/assignments/Assignment-07-MachineLearningSDM.ipynb docs/assignments/Assignment07_suitability.tif
git commit -m "Complete machine learning SDM assignment"
git push