Built to order

Crop Recommendation System using Machine Learning (Soil and Climate Dataset)

Choosing the right crop from soil-test numbers is still mostly tradition or last season's prices. This project trains a Random Forest on the public Crop Recommendation Dataset (2,200 soil-climate records, 22 crops) and serves it through a web app: enter N, P, K, temperature, humidity, pH and rainfall to get the best-match crop with runner-up scores. A full training notebook, honest evaluation report and explainability breakdown make every choice viva-ready. Suitable for B.E./B.Tech final-year projects in Computer Science, IT and AI & Machine Learning.

AI & Machine Learning Jupyter notebookGitPython 3pandas · NumPyHTML · CSS · JavaScript demoscikit-learn · Random ForestMatplotlib · SeabornCrop Recommendation DatasetSoftware
Crop Recommendation System using Machine Learning (Soil and Climate Dataset) — project thumbnail preview
More project photos (2)

The problem

Choosing the right crop for a field is one of the highest-stakes decisions in farming, yet it is still made mostly by tradition, word of mouth or last season's prices. A soil health card gives the farmer NPK and pH numbers, but those numbers are hard to translate into a crop choice, and a mismatch between crop and soil-climate conditions means wasted fertilizer, poor germination and a season of lost income. Machine learning can bridge this gap: crop-requirement knowledge, compiled over years of agronomy into reference tables, can be encoded in a dataset and learned by a classifier that maps seven soil and climate measurements to the most suitable of 22 crops. This project builds exactly that pipeline — the public Crop Recommendation Dataset (2,200 records, 22 balanced crop classes) is explored, split and used to train a Random Forest classifier, compared against SVM, k-NN and Naive Bayes baselines, evaluated on a held-out test set, and served through a web demo where any soil-climate profile returns a ranked recommendation.

SYSTEM FLOW

4 components 4 connections

Live flow
Drag to explore
100%
userFarmer / user

Enters soil nutrients, temperature, humidity, pH and rainfall to get a crop recommendation.

Follow the working sequence

How it works

  1. The 2,200-row Crop Recommendation Dataset is loaded: 7 numeric features (N, P, K, temperature, humidity, pH, rainfall) and a label column with 22 crop classes, 100 rows each, no missing values.
  2. Exploratory analysis checks feature distributions and class balance; the data is split into a stratified 80/20 train/test split (1,760 train, 440 test) so every crop is represented in both sets.
  3. A Random Forest classifier is trained on the training split, with tree count and maximum depth tuned; SVM, k-NN and Naive Bayes are trained on the same split as baselines.
  4. All models are evaluated once on the held-out test set: accuracy, per-class precision/recall/F1 and a 22x22 confusion matrix.
  5. The winning model is saved as a .pkl file together with the exact feature schema (names, order, valid ranges).
  6. In the web demo, the 7 entered values are validated against the dataset's observed ranges, passed to the saved model, and the app returns the predicted crop with class probabilities and the top-3 ranked crops.
  7. The demo additionally shows each input next to the predicted crop's typical profile, giving a plain-language "why this crop" explanation.

Tech stack:

  • Python 3, scikit-learn (Random Forest)
  • Pandas, NumPy (data handling)
  • Matplotlib, Seaborn (EDA & evaluation plots)
  • Jupyter Notebook (training & evaluation)
  • HTML5, CSS, JavaScript (recommendation web demo)
  • Crop Recommendation Dataset (Kaggle, atharvaingle)
  • Git

Dataset & model details

  • Dataset: Crop Recommendation Dataset (Kaggle, published by atharvaingle) — 2,200 rows, 7 numeric features (N, P, K, temperature, humidity, ph, rainfall), target column "label" with 22 crop classes: rice, maize, chickpea, kidneybeans, pigeonpeas, mothbeans, mungbean, blackgram, lentil, pomegranate, banana, mango, grapes, watermelon, muskmelon, apple, orange, papaya, coconut, cotton, jute, coffee. 100 samples per class (perfectly balanced), zero missing values. Class list verified against the dataset file itself.
  • Task: 22-class tabular classification; input = 7 soil/climate numbers, output = predicted crop label plus a probability distribution over all 22 crops.
  • Model: Random Forest classifier (ensemble of decision trees; number of trees and max depth tuned in the notebook). SVM, k-NN and Naive Bayes are trained as baselines on the same stratified split. Training (offline, notebook) and inference (live, web app) are separate phases.
  • Metrics: Test accuracy, per-class precision/recall/F1 and a 22x22 confusion matrix on the stratified held-out test split. Accuracy is reported as a design target (≥98% expected — this benchmark is well-separated in published notebooks), never as a measured claim; the built-to-order training run's actual figures are documented in the delivered report.
Parameter Value
Input features 7 (N, P, K, temperature, humidity, pH, rainfall)
Classes 22 crop labels
Dataset size 2,200 rows, 100 per class (balanced)
Train/test split 80/20 stratified (design target)
Test accuracy ≥98% (design target, not a measured claim)
Inference latency Approximately under 50 ms per prediction on CPU (expected)
Saved model size Approximately 5–20 MB (.pkl, expected — depends on tree count)
Demo Single-file web app, runs offline after download

Project features

  • [Random Forest crop classifier] Ensemble of decision trees trained on the 2,200-row dataset; tree count and depth tuned in the notebook. Outputs the predicted crop plus a full probability distribution over all 22 classes.
  • [Live recommendation web demo] Single-file web app: enter the 7 soil/climate values and get the recommended crop instantly, with suitability scores and runner-up crops shown as bars.
  • [Explainability breakdown] The demo compares your inputs against the predicted crop's typical profile feature-by-feature, so the recommendation is traceable rather than a black box.
  • [Full training & evaluation notebook] Data loading, EDA, stratified train-test split, model training, hyperparameter notes and every evaluation plot in one reproducible Jupyter notebook.
  • [Baseline model comparison] SVM, k-NN and Naive Bayes trained on the same split in the same notebook, so the report can justify the Random Forest choice with numbers.
  • [Confusion matrix & per-class report] Precision, recall and F1 for each of the 22 crops, making it clear exactly which crop pairs the model confuses.
  • [Input validation & range checks] The demo validates every input against the dataset's observed ranges (e.g. pH 3.5-9.9) and flags out-of-range values before prediction.

Optional extensions (configurable at order time, not in the base build)

  • [Regional variety mapping + multilingual demo UI] Mapping the 22 generic crop labels to locally grown varieties for a chosen region, and a Hindi/Marathi demo interface alongside English.

Future scope (not implemented)

  • Live weather-API inputs, market-price-aware ranking, and a mobile app wrapper.

What is included

  • Complete training & evaluation Jupyter notebook
  • Trained Random Forest model file (.pkl) with the input feature schema
  • Working crop-recommendation web demo wired to the trained model
  • EDA plots, confusion matrix and per-class metrics
  • Project report PDF (background, methodology, evaluation, honest limitations)
  • PPT presentation for final review
  • Viva Q&A preparation document (Random Forest, overfitting, feature importance, evaluation metrics)

Limitations & prerequisites

  • The recommendation is advisory, not agronomic advice — it cannot replace a soil agronomist or a local extension officer.
  • Predictions are only as good as the input soil-test values; wrong NPK/pH readings produce wrong recommendations.
  • The dataset encodes reference crop-requirement tables, not a specific region's cultivars — local variety differences are not modeled.
  • No pest, disease, irrigation-availability or market-price modeling — the model optimizes agronomic fit only.
  • Only 22 crops are covered; anything outside this set is out of scope (extension listed under optional).
  • ≥98% test accuracy is a design target for the built-to-order training run; the delivered report documents the actual achieved figure.

Frequently Asked Questions

Which dataset is used?

The Crop Recommendation Dataset (Kaggle, published by atharvaingle): 2,200 rows, 7 numeric features (N, P, K, temperature, humidity, pH, rainfall) and 22 crop classes with 100 samples each and no missing values. The class list was verified against the dataset file itself before writing this page.

Which model is used, and why Random Forest?

Random Forest is the primary model — it handles unscaled tabular features well, resists overfitting through bagging, and yields feature importance for the viva. The notebook also trains SVM, k-NN and Naive Bayes on the same stratified split so the choice is justified with numbers.

Is the accuracy guaranteed?

No measured accuracy is claimed. ≥98% test accuracy is the design target for the built-to-order training run (this benchmark is well-separated in published notebooks); the delivered report documents the actual confusion matrix and per-class precision/recall/F1 from the run.

Can new crops be added?

Yes — as a configurable extension: labeled rows for the new crop are added to the dataset and the pipeline retrains. The demo and feature schema already handle an arbitrary class list, so no code changes are needed beyond retraining.

Does it work with a real soil-test report?

Yes. N, P, K and pH come straight from a soil health card; temperature, humidity and rainfall from local seasonal normals. The demo validates every input against the ranges observed in the training data and flags out-of-range values.

Is this project suitable for a final-year project?

Yes — for Computer Science, IT and AI/ML programs. It covers the complete ML lifecycle: data understanding, preprocessing, model training, comparative evaluation, deployment through a working demo, and an honest limitations analysis.

Components & software requirements
  • Python 3, scikit-learn (Random Forest)
  • Pandas, NumPy (data handling)
  • Matplotlib, Seaborn (EDA & evaluation plots)
  • Jupyter Notebook (training & evaluation)
  • HTML5, CSS, JavaScript (recommendation web demo)
  • Crop Recommendation Dataset (Kaggle, atharvaingle)
  • Git

Dataset & model details

  • Dataset: Crop Recommendation Dataset (Kaggle, published by atharvaingle) — 2,200 rows, 7 numeric features (N, P, K, temperature, humidity, ph, rainfall), target column "label" with 22 crop classes: rice, maize, chickpea, kidneybeans, pigeonpeas, mothbeans, mungbean, blackgram, lentil, pomegranate, banana, mango, grapes, watermelon, muskmelon, apple, orange, papaya, coconut, cotton, jute, coffee. 100 samples per class (perfectly balanced), zero missing values. Class list verified against the dataset file itself.
  • Task: 22-class tabular classification; input = 7 soil/climate numbers, output = predicted crop label plus a probability distribution over all 22 crops.
  • Model: Random Forest classifier (ensemble of decision trees; number of trees and max depth tuned in the notebook). SVM, k-NN and Naive Bayes are trained as baselines on the same stratified split. Training (offline, notebook) and inference (live, web app) are separate phases.
  • Metrics: Test accuracy, per-class precision/recall/F1 and a 22x22 confusion matrix on the stratified held-out test split. Accuracy is reported as a design target (≥98% expected — this benchmark is well-separated in published notebooks), never as a measured claim; the built-to-order training run's actual figures are documented in the delivered report.
Delivery information

Built-to-order project. Delivery timeline is shared after order confirmation based on current queue.

Support terms

Complete documentation, setup guide, and viva preparation included. Support for setup and explanation provided.

Download free synopsis (PDF)

Enter your name and email — the synopsis PDF downloads immediately. We may send occasional project updates; no spam, unsubscribe anytime.

Get a quotation