Coder Social home page Coder Social logo

crop-rotation-planner's Introduction

Crop-Rotation-Planner

This project is a Crop Rotation Planner designed to assist farmers in optimizing their crop rotations for sustainable and efficient farming practices. It provides recommendations for crop sequences based on various factors such as soil health, environmental conditions, and nutrient balance.

Features

  • Recommends crop rotation plans based on user input and historical data.
  • Takes into account soil health, nutrient levels, and climatic conditions.
  • Provides visual representations of data.
  • Allows users to customize and prioritize specific crops in their rotation.

Requirements

  • Python 3.x
  • Required Python packages: streamlit, pandas, numpy, matplotlib, scikit-learn

Architecture Diagram/Flow

1

Program:

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression

from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.cluster import KMeans

# Load the CSV file data into data variable using pandas
PATH = 'Crop_recommendation.csv'
data = pd.read_csv(PATH)

data.head()
data.info()
data.describe()
data['label'].unique()
data['label'].value_counts()

import matplotlib.pyplot as plt
import seaborn as sns

plt.rcParams['figure.figsize'] = (10, 10)
plt.rcParams['figure.dpi'] = 60

features = ['N', 'P', 'K', 'temperature',
            'humidity', 'ph', 'rainfall']

for i, feat in enumerate(features):
    plt.subplot(4, 2, i + 1)
    sns.histplot(data[feat], color='greenyellow', kde=True)  # You can use sns.histplot for histograms
    if i < 3:
        plt.title(f'Ratio of {feat}', fontsize=12)
    else:
        plt.title(f'Distribution of {feat}', fontsize=12)
    plt.tight_layout()
    plt.grid()

plt.show()

fig, ax = plt.subplots(1, 1, figsize=(15, 9))
sns.heatmap(data.corr(),
			annot=True,
			cmap='viridis')
ax.set(xlabel='features')
ax.set(ylabel='features')

plt.title('Correlation between different features',
		fontsize=15,
		c='black')
plt.show()

# Put all the input variables into features vector
features = data[['N', 'P', 'K', 'temperature', 'humidity', 'ph', 'rainfall']]

# Put all the output into labels array
labels = data['label']

# Split the data into training and testing sets
X_train, X_test, Y_train, Y_test = train_test_split(features, labels, test_size=0.2, random_state=42)

# Decision Tree
dt_model = DecisionTreeClassifier(random_state=42)
dt_model.fit(X_train, Y_train)
dt_predicted_values = dt_model.predict(X_test)

# Random Forest
rf_model = RandomForestClassifier(random_state=42)
rf_model.fit(X_train, Y_train)
rf_predicted_values = rf_model.predict(X_test)

# Support Vector Machine (SVM)
svm_model = SVC(random_state=42)
svm_model.fit(X_train, Y_train)
svm_predicted_values = svm_model.predict(X_test)

# Logistic Regression
lr_model = LogisticRegression(random_state=42)
lr_model.fit(X_train, Y_train)
lr_predicted_values = lr_model.predict(X_test)

# K-Nearest Neighbors
knn_model = KNeighborsClassifier()
knn_model.fit(X_train, Y_train)
knn_predicted_values = knn_model.predict(X_test)

# Naive Bayes
nb_model = GaussianNB()
nb_model.fit(X_train, Y_train)
nb_predicted_values = nb_model.predict(X_test)


# Evaluate and compare the models
models = {
    'Decision Tree': dt_predicted_values,
    'Random Forest': rf_predicted_values,
    'SVM': svm_predicted_values,
    'Logistic Regression': lr_predicted_values,
    'KNN': knn_predicted_values,
    'Naive Bayes': nb_predicted_values
}

best_model_name = None
best_accuracy = 0.0

for model_name, predictions in models.items():
    accuracy = accuracy_score(Y_test, predictions)
    print(f"{model_name} accuracy: {accuracy}")
    
    if accuracy > best_accuracy:
        best_accuracy = accuracy
        best_model_name = model_name

if best_model_name == 'Decision Tree':
    best_model = dt_model
elif best_model_name == 'Random Forest':
    best_model = rf_model
elif best_model_name == 'SVM':
    best_model = svm_model
elif best_model_name == 'Logistic Regression':
    best_model = lr_model
elif best_model_name == 'KNN':
    best_model = knn_model
elif best_model_name == 'Naive Bayes':
    best_model = nb_model

# Streamlit App
st.title("Crop Rotation Planner")

# User Input for Features
st.header("Enter the Input Features")
N = st.number_input("Nitrogen (N)", min_value=0.0, max_value=150.0, step=1.0)
P = st.number_input("Phosphorous (P)", min_value=0.0, max_value=150.0, step=1.0)
K = st.number_input("Potassium (K)", min_value=0.0, max_value=150.0, step=1.0)
temperature = st.number_input("Temperature", min_value=0.0, max_value=100.0, step=1.0)
humidity = st.number_input("Humidity", min_value=0.0, max_value=100.0, step=1.0)
ph = st.number_input("pH Level", min_value=0.0, max_value=14.0, step=0.1)
rainfall = st.number_input("Rainfall", min_value=0.0, max_value=300.0, step=1.0)

# Predict using the best model
if st.button("Predict"):
    new_data_values = [[N, P, K, temperature, humidity, ph, rainfall]]
    prediction = best_model.predict(new_data_values)
    st.success(f"The predicted crop label is {prediction[0]}")

Output:

image

image

image

image

image

image

Result:

The integration of diverse machine learning models, including Decision Trees, Random Forest, SVM, Logistic Regression, K-Nearest Neighbors, and Naive Bayes, has resulted in precise and data-driven crop rotation recommendations. This achievement enhances the efficiency of agricultural planning and resource allocation.

This Crop Rotation Planner project provides farmers with data-driven recommendations for optimal crop rotations. By leveraging machine learning models, it takes into account various factors to suggest rotations that enhance soil health and overall agricultural productivity.

crop-rotation-planner's People

Contributors

harshini1331 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.