Instructions to use SMHunain/crop-yield-drought-pakistan with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Scikit-learn
How to use SMHunain/crop-yield-drought-pakistan with Scikit-learn:
from huggingface_hub import hf_hub_download import joblib model = joblib.load( hf_hub_download("SMHunain/crop-yield-drought-pakistan", "sklearn_model.joblib") ) # only load pickle files from sources you trust # read more about it here https://skops.readthedocs.io/en/stable/persistence.html - Notebooks
- Google Colab
- Kaggle
Crop yield model for drought scenarios in Pakistan (CT-378)
A scikit-learn pipeline that predicts the yield of 13 crops in Punjab, Sindh and Balochistan from annual climate and soil wetness, and ranks the crops by how much of a typical year's yield each keeps in a scenario. It is a university course project (CT-378 Machine Learning).
- Model: Gradient Boosting inside a scikit-learn
Pipeline(median imputation, standard scaling, one-hot encoding, regressor fitted on the log of the yield). - Inputs (annual): rain, mean/max/min temperature, evapotranspiration, solar radiation, rain hours, root-zone soil wetness (mean and wettest month), plus the crop and the province. Features: precipitation_sum, temperature_2m_mean, temperature_2m_max, temperature_2m_min, fao_evapotranspiration, shortwave_radiation, precipitation_hours, soil_wetness_mean, soil_wetness_max, rain_deficit_pct, water_balance_mm, aridity_index, Crop, Province.
- Output: yield in dataset yield units (the data source does not state the unit).
- Code and app: the project repository (https://github.com/SyedMuhammadHunain/Climate-Machine-Learning) has a FastAPI app, a Streamlit app and this model's training code.
Intended use
Studying how a simple model responds to drought-like scenarios, and demonstrating an end-to-end machine learning lifecycle. Not for real farm or policy decisions.
Training data
Two public sources are merged by province and year:
- crop yield and climate: Pakistan crop yield vs climate (GitHub Abdullah-Masood-05, origin Zenodo 15486470). Licence: MIT (stated by the repository). File SHA-256
998ac2e23c39581a.... - soil wetness: NASA POWER (Prediction Of Worldwide Energy Resources), monthly point API. Licence: License not specified in the POWER catalog entry. Free to use; POWER asks users to cite its Reference and Data Reference.. Accessed 2026-11-19 (API v2.10.1, 9 points). File SHA-256
30646a48ac2e35e2....
The source file has 12 monthly rows per crop, province and year with one yearly yield repeated on all 12, so the model trains on one row per crop, province and year: 773 rows after dropping apple 22, grapes 41, pomegranate 22 rows with zero area (not grown or not reported). Split by year: train 2001-2016 (565 rows), validation 2017-2019 (106 rows), test 2020-2022 (102 rows).
Training procedure
GridSearchCV with 5-fold GroupKFold grouped by year, so a year never appears in two folds. Candidates: Ridge, Random Forest, Gradient Boosting, plus three baselines (overall mean, crop-province mean, and crop-province mean plus a year trend with no climate). Each model learns the log-scale gap between a year's yield and the crop-province average of the training years, plus a straight-line year trend picked on the validation years (chosen: per crop province). Year is not a model input: it is pinned to 2022 at prediction, which cancels out of the crop ranking. The model was chosen on the validation years; the test years were scored once and not used to choose. Random seed 42. Trained 2026-11-19T09:24:29+00:00 with scikit-learn 1.9.1.
Evaluation
Yields are in dataset yield units.
| Model | Val R² | Val MAE | Test R² | Test MAE | Test RMSE | Test skill vs crop-province mean |
|---|---|---|---|---|---|---|
| Baseline: overall mean yield | -0.002 | 91.7 | -0.030 | 100.4 | 173.6 | -6.96 |
| Baseline: crop-province mean yield | 0.877 | 20.9 | 0.832 | 34.7 | 70.1 | +0.00 |
| Baseline: crop-province mean + year trend (no climate) | 0.919 | 14.8 | 0.875 | 30.3 | 60.5 | -0.43 |
| Ridge (regularised linear regression) | 0.924 | 15.2 | 0.878 | 29.5 | 59.7 | -0.39 |
| Random Forest | 0.917 | 15.2 | 0.873 | 30.6 | 60.9 | -0.54 |
| Gradient Boosting (chosen) | 0.924 | 14.9 | 0.885 | 29.7 | 58.0 | -0.38 |
On the 2020-2022 test years, compared with using each crop's average yield per province, the model was better on R² 0.885 against 0.832, MAE 29.7 against 34.7 and RMSE 58.0 against 70.1; it was not better on log-MAE 0.341 against 0.312. The pass test (R² and MAE) was chosen after seeing the test results. A year trend alone, with no climate inputs, scores R² 0.875. Small differences between crops are weak evidence.
Absolute yields are rough: the model is pinned to 2022, and its typical-year prediction, set against the mean actual yield of 2020-2022, was 0.80 to 1.15 times the actual for the middle half of the 34 crop-province pairs, but 0.21 times for mango in Balochistan and 2.19 times for cotton in Sindh. Trust the ends of the ranking more than a single predicted number.
"Skill" compares a model with the crop-province mean baseline on a log scale; above 0 means better than that baseline. Read the baseline row and the skill column before treating small differences between crops as meaningful. Most of the model's signal is which crop it is.
Prediction interval
The "likely range" is the empirical 80% spread (10% to 90% quantiles of log(actual / predicted)) of the model's errors on the validation years. On the 2020-2022 test rows it covered 58% of the actual yields, below the nominal 80%, so treat it as optimistic.
How to use
Download the files of this repository, install requirements.txt, and run the example (it adds this folder to the Python path, so the bundled climate_machine_learning code is used):
pip install -r requirements.txt
python inference_example.py
import json, joblib
from climate_machine_learning import inference as I
pipeline = joblib.load("crop_yield_pipeline.joblib")
reference = json.load(open("reference.json"))
metrics = json.load(open("metrics.json"))
result = I.predict_crops(
pipeline, reference, "Sindh",
{"precipitation_sum": 150.0, "temperature_2m_mean": 27.5}, # other inputs: province median
log_quantiles=metrics["prediction_interval"]["log_residual_quantiles"],
)
for item in result.items:
print(item.crop, round(item.pct_of_normal, 1), "% of a typical year")
Inputs outside the range seen in training are refused (OutOfRangeError) because a tree model cannot extrapolate. reference.json holds the trained ranges, province medians and real driest/wettest training years.
Limitations
- Few rows: 773 usable yearly rows from 22 years and 3 provinces.
- Crop identity dominates; weather and soil wetness change predictions only a little. The soil wetness source adds little.
- The win is narrower than it looks (see the paragraph under the evaluation table): a year trend alone, with no climate inputs, scores almost the same R², so the weather inputs add little beyond it.
- Weather and soil wetness are province-level (soil wetness from 3 hand-picked farming-area points per province), the same for every crop. There is no district or farm detail.
- Annual mean temperature varies by only about 1.5 deg C within a province in training, so many inputs fall outside the trained range and are refused.
- Some crops are missing years (grapes, pomegranate, apple) and are noisier.
- The yield unit is not confirmed by the data source.
Licence
The group has not chosen a licence for this model yet. Data licences: the crop dataset is MIT (stated by its repository); the NASA POWER catalog entry says "License not specified" (free to use, citation requested: https://power.larc.nasa.gov/docs/referencing/).
References
- Pakistan crop yield vs climate, https://github.com/Abdullah-Masood-05/pakistan-crop-yield-vs-climate (origin: Climate & Agriculture in Pakistan, Zenodo, https://zenodo.org/records/15486470).
- NASA Langley Research Center POWER Project, https://power.larc.nasa.gov/.
- Pedregosa et al. (2011). Scikit-learn: Machine Learning in Python. JMLR 12, 2825-2830.
Course project of a group of three students.
- Downloads last month
- -