FertiliserAdvisor / README.md
Briankabiru's picture
Create README.md
f9ad69d verified
|
raw
history blame
4.54 kB
---
license: mit
datasets:
- custom
metrics:
- mean_squared_error
- mean_absolute_error
- r2_score
model_name: Fertilizer Recommendation System
tags:
- random-forest
- regression
- multioutput
- classification
- agriculture
- soil-nutrients
---
# Fertilizer Recommendation System
## Overview
This model predicts the fertilizer requirements for various crops based on input features such as crop type, target yield, field size, and soil properties. It utilizes a combination of Random Forest Regressor and Random Forest Classifier to predict both numerical values (e.g., nutrient needs) and categorical values (e.g., fertilizer application instructions).
## Training Data
The model was trained on a custom dataset containing the following features:
- Crop Name
- Target Yield
- Field Size
- pH (water)
- Organic Carbon
- Total Nitrogen
- Phosphorus (M3)
- Potassium (exch.)
- Soil moisture
The target variables include:
**Numerical Targets**:
- Nitrogen (N) Need
- Phosphorus (P2O5) Need
- Potassium (K2O) Need
- Organic Matter Need
- Lime Need
- Lime Application - Requirement
- Organic Matter Application - Requirement
- 1st Application - Requirement (1)
- 1st Application - Requirement (2)
- 2nd Application - Requirement (1)
**Categorical Targets**:
- Lime Application - Instruction
- Lime Application
- Organic Matter Application - Instruction
- Organic Matter Application
- 1st Application
- 1st Application - Type fertilizer (1)
- 1st Application - Type fertilizer (2)
- 2nd Application
- 2nd Application - Type fertilizer (1)
## Model Training
The model was trained using the following steps:
1. **Data Preprocessing**:
- Handling missing values
- Scaling numerical features using `StandardScaler`
- One-hot encoding categorical features
2. **Modeling**:
- Splitting the dataset into training and testing sets
- Training a `RandomForestRegressor` for numerical targets using a `MultiOutputRegressor`
- Training a `RandomForestClassifier` for categorical targets using a `MultiOutputClassifier`
3. **Evaluation**:
- Evaluating the models using the test set with metrics like Mean Squared Error (MSE), Mean Absolute Error (MAE), and R-squared (R2) Score for regression, and accuracy for classification.
## Evaluation Metrics
The model was evaluated using the following metrics:
- Mean Squared Error (MSE)
- Mean Absolute Error (MAE)
- R-squared (R2) Score
- Accuracy for categorical targets
## How to Use
### Input Format
The model expects input data in JSON format with the following fields:
- "Crop Name": String
- "Target Yield": Numeric
- "Field Size": Numeric
- "pH (water)": Numeric
- "Organic Carbon": Numeric
- "Total Nitrogen": Numeric
- "Phosphorus (M3)": Numeric
- "Potassium (exch.)": Numeric
- "Soil moisture": Numeric
### Preprocessing Steps
1. Load your input data.
2. Ensure all required fields are present and in the expected format.
3. Handle any missing values if necessary.
4. Scale numerical features based on the training data.
5. One-hot encode categorical features (if applicable).
### Inference Procedure
```python
from joblib import load
import pandas as pd
# Load the preprocessor and trained models
preprocessor = load('preprocessor.joblib')
numerical_model = load('numerical_model.joblib')
categorical_model = load('categorical_model.joblib')
# Example input data
new_data = {
'Crop Name': 'maize(corn)',
'Target Yield': 3600.0,
'Field Size': 1.0,
'pH (water)': 6.1,
'Organic Carbon': 11.4,
'Total Nitrogen': 1.1,
'Phosphorus (M3)': 1.8,
'Potassium (exch.)': 3.0,
'Soil moisture': 20.0
}
# Preprocess the input data
input_df = pd.DataFrame([new_data])
input_transformed = preprocessor.transform(input_df)
# Make numerical predictions
numerical_predictions = numerical_model.predict(input_transformed)
# Make categorical predictions
categorical_predictions = categorical_model.predict(input_transformed)
# Decode categorical predictions
label_encoders = {col: load(f'label_encoder_{col}.joblib') for col in categorical_targets}
categorical_predictions_decoded = {col: label_encoders[col].inverse_transform(categorical_predictions[:, i]) for i, col in enumerate(categorical_targets)}
# Combine predictions into a single dictionary
predictions_combined = {**{col: numerical_predictions[0, i] for i, col in enumerate(numerical_targets)}, **categorical_predictions_decoded}
print("Predicted Fertilizer Requirements:")
for col, pred_value in predictions_combined.items():
print(f"{col}: {pred_value}")