A machine learning-based crop prediction system that helps farmers make informed decisions about crop selection. The model uses Deep Neural Networks (DNNs) trained on historical crop and weather data to predict the most suitable crop based on soil conditions, location, and seasonal factors.
🎯 Accuracy: 99% on both training and test data!
- 🧪 Soil Analysis - Considers N, P, K, and pH content of the soil
- 🌡️ Weather Integration - Fetches real-time temperature and humidity via API
- 🌧️ Rainfall Prediction - Uses historical rainfall data by region
- 📍 Location-Based - State and district-specific recommendations
- 🖥️ User-Friendly Interface - Interactive web interface for easy input
- ⚡ Fast API Backend - Quick and reliable predictions
| Category | Technologies |
|---|---|
| ML Framework | PyTorch |
| Backend | FastAPI, Uvicorn |
| Frontend | HTML, CSS, JavaScript |
| Data Processing | Pandas, NumPy, Scikit-learn |
| Visualization | Matplotlib |
The prediction model uses a Deep Neural Network with:
Input Layer (7 features)
↓
Hidden Layer 1 (64 neurons, SeLU activation)
↓
Hidden Layer 2 (128 neurons, SeLU activation)
↓
Hidden Layer 3 (64 neurons, SeLU activation)
↓
Output Layer (22 neurons, Softmax activation)
- Nitrogen (N) - Soil nitrogen content
- Phosphorus (P) - Soil phosphorus content
- Potassium (K) - Soil potassium content
- pH - Soil pH level
- Temperature - Regional temperature
- Humidity - Regional humidity
- Rainfall - Expected rainfall
![]() |
![]() |
| Pairwise Correlation Plots | Correlation Matrix |
- Python 3.8+
- pip
-
Clone the repository
git clone https://github.com/yourusername/crop_prediction.git cd crop_prediction/crp-main -
Create virtual environment (recommended)
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies
pip install -r requirements.txt
-
Run the application
uvicorn main:app --reload
-
Open the interface
- Navigate to
http://localhost:8000 - Or open
index.htmlin your browser
- Navigate to
Request Body:
{
"nitrogen": 90,
"phosphorous": 42,
"potassium": 43,
"ph": 6.5,
"state": "Karnataka",
"district": "Bangalore",
"month": "June"
}Response:
{
"result": "rice"
}crop_prediction/
├── crp-main/
│ ├── data/ # Dataset files
│ ├── model/ # Trained models and architecture
│ │ ├── net.py # Neural network definition
│ │ └── *.hdf5 # Saved model weights
│ ├── utils/ # Utility functions
│ │ ├── pred_crop.py # Crop prediction logic
│ │ ├── pred_rainfall.py # Rainfall data fetching
│ │ └── pred_temp_hum.py # Temperature & humidity API
│ ├── main.py # FastAPI application
│ ├── index.html # Web interface
│ ├── index.js # Frontend logic
│ ├── style.css # Styling
│ ├── requirements.txt # Python dependencies
│ └── *.ipynb # Jupyter notebooks for analysis
└── README.md
- Crop Recommendation Dataset - 2200 samples of 22 crops
- Rainfall in India - Historical rainfall data
| API | Purpose |
|---|---|
| OpenWeather API | Temperature & humidity data |
| LatLong.net | Geolocation coordinates |
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Made with ❤️ for farmers



