RabbeTota/Soil_Quality_Prediction
Soil Quality Prediction — Bangladesh
A machine learning app that predicts the AP Ratio (Agricultural Production / Cultivated Area) as a proxy for soil productivity, trained on the SPAS-Dataset-BD dataset. The model is a Random Forest with Bayesian hyperparameter optimization via Optuna, augmented with three Bangladesh-specific engineered features and explained with SHAP.
Research project — Department of Computer Science and Engineering, Rangamati Science and Technology University, Bangladesh.
What the app does
Enter the district, crop, season, cultivated area, and climate readings. The app computes the three domain-specific features automatically and returns the predicted AP Ratio along with a plain-language interpretation.
AP Ratio = Total Production ÷ Cultivated Area. A higher ratio indicates better soil productivity for that crop under those conditions.
Bangladesh-specific features
Three features are computed from your inputs at inference time — they are not raw inputs you provide:
All three features were validated with SHAP and rank consistently among the top predictors.
Model performance
Exact values are loaded from model_card.json and displayed live in the app sidebar. The improvement over the baseline is statistically significant (paired t-test, p < 0.05).
Repository structure
.
├── app.py # Gradio inference app (this file drives the Space)
├── requirements.txt # Python dependencies
├── README.md # This file
│
├── optimized_rf.joblib # Bayesian-optimized Random Forest — PRIMARY model
├── baseline_rf.joblib # Default-param baseline Random Forest
├── knn_imputer.joblib # KNNImputer (k=5) fitted on the full feature matrix
├── label_encoders.joblib # Fitted LabelEncoders for District, Season, Crop Name
├── feature_list.json # Ordered list of 13 feature names (must match predict input)
└── model_card.json # Training metadata, best hyperparameters, and all metricsThe five `.joblib` / `.json` model files are not included in this repo by default. See the section below on how to add them.
How to deploy
Step 1 — Create the Space
- Go to huggingface.co/spaces and click Create new Space.
- Set Space SDK to Gradio.
- Give it a name, e.g.
soil-quality-prediction-bd. - Set visibility to Public or Private.
Step 2 — Upload the code files
Upload (or clone the repo and push) these three files:
app.py
requirements.txt
README.mdStep 3 — Upload the model artifacts
Run the notebook through Cell 21 to generate the six artifact files in soil_quality_outputs/. Then upload all six to the Space root:
optimized_rf.joblib
baseline_rf.joblib
knn_imputer.joblib
label_encoders.joblib
feature_list.json
model_card.jsonYou can upload via the Space Files tab in the browser, or via Git:
git clone https://huggingface.co/spaces/<your-username>/soil-quality-prediction-bd
cd soil-quality-prediction-bd
# Copy the six artifact files here, then:
git add .
git commit -m "add model artifacts"
git pushStep 4 — Wait for the build
Hugging Face will install dependencies from requirements.txt and launch the app automatically. The build log is visible in the Logs tab.
Running locally
# Clone or download the repo, then:
pip install -r requirements.txt
# Place all six model artifact files in the same directory as app.py, then:
python app.pyThe app opens at http://127.0.0.1:7860 by default.
Inference pipeline
The app follows the same pipeline as the training notebook:
raw inputs
│
├── LabelEncode(District, Season, Crop Name) # label_encoders.joblib
├── Compute Monsoon_Moisture_Index # from avg_humidity, avg_temp, season
├── Compute Saltwater_Intrusion_Risk # from district, season, max_humidity
└── Compute Seasonal_Soil_Stress # from temp/humidity range, season
│
└── Assemble 13-column row [feature_list.json order]
│
└── KNNImputer.transform() # knn_imputer.joblib
│
└── RF.predict() # optimized_rf.joblib
│
└── AP Ratio (float)Dataset
SPAS-Dataset-BD — Seasonal and district-level agricultural production data for Bangladesh, sourced from Mendeley Data. The dataset covers three seasons (Kharif 1, Kharif 2, Rabi), 64 districts, and multiple crops.
Preprocessing steps applied before training:
- AP Ratio coerced to float; rows with missing target or season dropped.
- Rows with
Crop Name == "#REF!"removed. - Extreme outliers in the target removed using a 3×IQR fence.
- Missing values in all features imputed with KNNImputer (k=5).
Citation
If you use this model or code in your research, please cite the associated paper (details in model_card.json).
License
MIT — see LICENSE for details.
