Skip to content

Instantly share code, notes, and snippets.

@mikkohei13
Created August 25, 2023 09:24
Show Gist options
  • Save mikkohei13/be91ef70e56f847352eadaa968b18543 to your computer and use it in GitHub Desktop.
Save mikkohei13/be91ef70e56f847352eadaa968b18543 to your computer and use it in GitHub Desktop.
Estimate future observation counts based on laji.fi data
import json
from statsmodels.tsa.holtwinters import Holt
# Load the JSON data
with open("pyjamalude-years.json", "r") as file:
data = json.load(file)
'''
Estimate future observation counts based on laji.fi data
Made with GPT-4 code interpreter. Unvalidated.
Input: yearly aggregate of of observation count for a taxon.
e.g.
https://laji.fi/api/warehouse/query/unit/aggregate?target=MX.230561&countryId=ML.206&aggregateBy=gathering.conversions.year&selected=gathering.conversions.year&orderBy=gathering.conversions.year&page=1&pageSize=10000&onlyCount=false
'''
# Extract the results and organize the data
results = data["results"]
year_observation_data = [(int(item["aggregateBy"]["gathering.conversions.year"]), item["count"]) for item in results]
year_observation_data.sort(key=lambda x: x[0])
years, observations = zip(*year_observation_data)
# Fit the Holt's Linear Exponential Smoothing model
model = Holt(observations).fit()
# Forecast the next 7 years (2024-2030)
forecast = model.forecast(7)
forecast_years = list(range(2024, 2031))
forecast_values = list(forecast)
# Print the forecasted observations
for year, value in zip(forecast_years, forecast_values):
print(f"{year}: Approximately {round(value)} observations")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment