A clean, reproducible benchmark for the classic Crop Recommendation problem. This project implements and evaluates multiple machine learning baselines and a deep learning model to predict the optimal crop based on environmental and soil features.
This repository provides an end-to-end, ready-to-run solution for crop recommendation.
- Comprehensive EDA: Includes visualizations like a correlation heatmap to understand feature relationships.
- Multiple Baselines: Compares classic ML models: Logistic Regression, KNN, SVM, Decision Tree, Random Forest, and XGBoost.
- Deep Learning Model: Features a custom Artificial Neural Network (ANN) built with TensorFlow/Keras.
- Thorough Evaluation: Assesses models using accuracy, F1-score, confusion matrices, and ROC/AUC curves.
- Reproducible Environment: Comes with a
requirements.txtfile for easy setup.
The model is trained on the Crop Recommendation Dataset, which contains the following features:
- Soil Nutrients: Nitrogen (N), Phosphorous (P), Potassium (K)
- Soil Condition: pH value
- Environmental Factors: Temperature, Humidity, Rainfall
The target variable is the recommended crop label (e.g., Rice, Maize, Chickpea, etc.).
To Use:
The notebook is configured to load Crop_recommendation.csv from a data/ directory.
- Create a folder named
datain the root of the project. - Download the dataset from Kaggle and place it inside the
data/folder. - Alternatively, update the path in the "Data Load" cell of the notebook to your specific file location.
Get up and running in a few simple steps.
-
Clone the repository:
git clone [https://github.com/meanderinghuman/AgriRecoNet.git](https://github.com/meanderinghuman/AgriRecoNet.git) cd AgriRecoNet -
Create and activate a virtual environment:
# Create the environment python -m venv .venv # Activate it (Linux/Mac) source .venv/bin/activate # Activate it (Windows) .\.venv\Scripts\activate
-
Install the required packages:
pip install -r requirements.txt
-
Launch the notebook: Open the
AgriRecoNet.ipynbnotebook in VS Code or Jupyter and run the cells.- In VS Code: Ensure you have the Python and Jupyter extensions installed. Select the
.venvinterpreter from the command palette (Ctrl+Shift+P).
- In VS Code: Ensure you have the Python and Jupyter extensions installed. Select the
- Logistic Regression
- K-Nearest Neighbors (KNN)
- Support Vector Machine (SVM)
- Decision Tree
- Random Forest
- XGBoost
- Artificial Neural Network (ANN)
The models were evaluated based on their accuracy on the test set.
| Model | Accuracy (%) |
|---|---|
| Naive Bayes | 99.64 |
| Random Forest | 99.45 |
| XGBoost | 99.45 |
| Gradient Boosting | 99.09 |
| Decision Tree | 98.73 |
| Artificial Neural Network (ANN) | 98.18 |
| Support Vector Machine (SVM) | 98.00 |
| Logistic Regression | 97.27 |
| K-Nearest Neighbors (KNN) | 95.82 |
Evaluation is performed using:
- Accuracy Score
- F1 Score (Macro/Weighted)
- Confusion Matrix
- ROC / AUC Curves (One-vs-Rest for multi-class classification)
AgriRecoNet/
├── AgriRecoNet.ipynb
├── requirements.txt
├── .gitignore
└── README.md
Future enhancements planned for this project:
- Implement hyperparameter tuning sweeps (Optuna/Scikit-Optimize).
- Integrate cross-validation with stratification for more robust evaluation.
- Add model explainability using SHAP across all models.
- Create a script to export the best-performing model as a saved artifact for deployment.
- TensorFlow Errors: If you encounter GPU errors with TensorFlow, try running on a CPU or ensure you have the correct CUDA/cuDNN versions installed for your hardware.
- XGBoost: This library is optional. If you face installation issues, you can comment out the XGBoost-related cells in the notebook.
agriculture machine learning, crop recommendation, soil nutrients, nitrogen, phosphorous, potassium, pH, rainfall, temperature, humidity, XGBoost, Random Forest, SVM, KNN, ANN, TensorFlow, Keras, EDA, classification.