Skip to content

addy0328p/Crop_Prediction_using_ML

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

🌾 Crop Prediction using Machine Learning

Crops Banner

Python FastAPI PyTorch License

📖 Overview

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!

Preview


✨ Features

  • 🧪 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

🛠️ Tech Stack

Category Technologies
ML Framework PyTorch
Backend FastAPI, Uvicorn
Frontend HTML, CSS, JavaScript
Data Processing Pandas, NumPy, Scikit-learn
Visualization Matplotlib

📊 Model Architecture

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)

Input Features

  1. Nitrogen (N) - Soil nitrogen content
  2. Phosphorus (P) - Soil phosphorus content
  3. Potassium (K) - Soil potassium content
  4. pH - Soil pH level
  5. Temperature - Regional temperature
  6. Humidity - Regional humidity
  7. Rainfall - Expected rainfall

📈 Data Visualization

Pairwise Correlation Correlation Matrix
Pairwise Correlation Plots Correlation Matrix

🚀 Installation

Prerequisites

  • Python 3.8+
  • pip

Setup

  1. Clone the repository

    git clone https://github.com/yourusername/crop_prediction.git
    cd crop_prediction/crp-main
  2. Create virtual environment (recommended)

    python -m venv venv
    source venv/bin/activate  # On Windows: venv\Scripts\activate
  3. Install dependencies

    pip install -r requirements.txt
  4. Run the application

    uvicorn main:app --reload
  5. Open the interface

    • Navigate to http://localhost:8000
    • Or open index.html in your browser

📡 API Usage

Endpoint: POST /predict/

Request Body:

{
  "nitrogen": 90,
  "phosphorous": 42,
  "potassium": 43,
  "ph": 6.5,
  "state": "Karnataka",
  "district": "Bangalore",
  "month": "June"
}

Response:

{
  "result": "rice"
}

📁 Project Structure

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

📚 Datasets


🌐 External APIs

API Purpose
OpenWeather API Temperature & humidity data
LatLong.net Geolocation coordinates

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Made with ❤️ for farmers

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages