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asets:
|
| 4 |
+
- custom
|
| 5 |
+
metrics:
|
| 6 |
+
- mean_squared_error
|
| 7 |
+
- mean_absolute_error
|
| 8 |
+
- r2_score
|
| 9 |
+
model_name: Fertilizer Recommendation System
|
| 10 |
+
tags:
|
| 11 |
+
- random-forest
|
| 12 |
+
- regression
|
| 13 |
+
- multioutput
|
| 14 |
+
- classification
|
| 15 |
+
- agriculture
|
| 16 |
+
- soil-nutrients
|
| 17 |
+
---
|
| 18 |
+
|
| 19 |
+
# Fertilizer Recommendation System
|
| 20 |
+
|
| 21 |
+
## Overview
|
| 22 |
+
|
| 23 |
+
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).
|
| 24 |
+
|
| 25 |
+
## Training Data
|
| 26 |
+
|
| 27 |
+
The model was trained on a custom dataset containing the following features:
|
| 28 |
+
|
| 29 |
+
- Crop Name
|
| 30 |
+
- Target Yield
|
| 31 |
+
- Field Size
|
| 32 |
+
- pH (water)
|
| 33 |
+
- Organic Carbon
|
| 34 |
+
- Total Nitrogen
|
| 35 |
+
- Phosphorus (M3)
|
| 36 |
+
- Potassium (exch.)
|
| 37 |
+
- Soil moisture
|
| 38 |
+
|
| 39 |
+
The target variables include:
|
| 40 |
+
|
| 41 |
+
**Numerical Targets**:
|
| 42 |
+
- Nitrogen (N) Need
|
| 43 |
+
- Phosphorus (P2O5) Need
|
| 44 |
+
- Potassium (K2O) Need
|
| 45 |
+
- Organic Matter Need
|
| 46 |
+
- Lime Need
|
| 47 |
+
- Lime Application - Requirement
|
| 48 |
+
- Organic Matter Application - Requirement
|
| 49 |
+
- 1st Application - Requirement (1)
|
| 50 |
+
- 1st Application - Requirement (2)
|
| 51 |
+
- 2nd Application - Requirement (1)
|
| 52 |
+
|
| 53 |
+
**Categorical Targets**:
|
| 54 |
+
- Lime Application - Instruction
|
| 55 |
+
- Lime Application
|
| 56 |
+
- Organic Matter Application - Instruction
|
| 57 |
+
- Organic Matter Application
|
| 58 |
+
- 1st Application
|
| 59 |
+
- 1st Application - Type fertilizer (1)
|
| 60 |
+
- 1st Application - Type fertilizer (2)
|
| 61 |
+
- 2nd Application
|
| 62 |
+
- 2nd Application - Type fertilizer (1)
|
| 63 |
+
|
| 64 |
+
## Model Training
|
| 65 |
+
|
| 66 |
+
The model was trained using the following steps:
|
| 67 |
+
|
| 68 |
+
1. **Data Preprocessing**:
|
| 69 |
+
- Handling missing values
|
| 70 |
+
- Scaling numerical features using `StandardScaler`
|
| 71 |
+
- One-hot encoding categorical features
|
| 72 |
+
|
| 73 |
+
2. **Modeling**:
|
| 74 |
+
- Splitting the dataset into training and testing sets
|
| 75 |
+
- Training a `RandomForestRegressor` for numerical targets using a `MultiOutputRegressor`
|
| 76 |
+
- Training a `RandomForestClassifier` for categorical targets using a `MultiOutputClassifier`
|
| 77 |
+
|
| 78 |
+
3. **Evaluation**:
|
| 79 |
+
- 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.
|
| 80 |
+
|
| 81 |
+
## Evaluation Metrics
|
| 82 |
+
|
| 83 |
+
The model was evaluated using the following metrics:
|
| 84 |
+
|
| 85 |
+
- Mean Squared Error (MSE)
|
| 86 |
+
- Mean Absolute Error (MAE)
|
| 87 |
+
- R-squared (R2) Score
|
| 88 |
+
- Accuracy for categorical targets
|
| 89 |
+
|
| 90 |
+
## How to Use
|
| 91 |
+
|
| 92 |
+
### Input Format
|
| 93 |
+
|
| 94 |
+
The model expects input data in JSON format with the following fields:
|
| 95 |
+
|
| 96 |
+
- "Crop Name": String
|
| 97 |
+
- "Target Yield": Numeric
|
| 98 |
+
- "Field Size": Numeric
|
| 99 |
+
- "pH (water)": Numeric
|
| 100 |
+
- "Organic Carbon": Numeric
|
| 101 |
+
- "Total Nitrogen": Numeric
|
| 102 |
+
- "Phosphorus (M3)": Numeric
|
| 103 |
+
- "Potassium (exch.)": Numeric
|
| 104 |
+
- "Soil moisture": Numeric
|
| 105 |
+
|
| 106 |
+
### Preprocessing Steps
|
| 107 |
+
|
| 108 |
+
1. Load your input data.
|
| 109 |
+
2. Ensure all required fields are present and in the expected format.
|
| 110 |
+
3. Handle any missing values if necessary.
|
| 111 |
+
4. Scale numerical features based on the training data.
|
| 112 |
+
5. One-hot encode categorical features (if applicable).
|
| 113 |
+
|
| 114 |
+
### Inference Procedure
|
| 115 |
+
|
| 116 |
+
```python
|
| 117 |
+
from joblib import load
|
| 118 |
+
import pandas as pd
|
| 119 |
+
|
| 120 |
+
# Load the preprocessor and trained models
|
| 121 |
+
preprocessor = load('preprocessor.joblib')
|
| 122 |
+
numerical_model = load('numerical_model.joblib')
|
| 123 |
+
categorical_model = load('categorical_model.joblib')
|
| 124 |
+
|
| 125 |
+
# Example input data
|
| 126 |
+
new_data = {
|
| 127 |
+
'Crop Name': 'maize(corn)',
|
| 128 |
+
'Target Yield': 3600.0,
|
| 129 |
+
'Field Size': 1.0,
|
| 130 |
+
'pH (water)': 6.1,
|
| 131 |
+
'Organic Carbon': 11.4,
|
| 132 |
+
'Total Nitrogen': 1.1,
|
| 133 |
+
'Phosphorus (M3)': 1.8,
|
| 134 |
+
'Potassium (exch.)': 3.0,
|
| 135 |
+
'Soil moisture': 20.0
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
# Preprocess the input data
|
| 139 |
+
input_df = pd.DataFrame([new_data])
|
| 140 |
+
input_transformed = preprocessor.transform(input_df)
|
| 141 |
+
|
| 142 |
+
# Make numerical predictions
|
| 143 |
+
numerical_predictions = numerical_model.predict(input_transformed)
|
| 144 |
+
|
| 145 |
+
# Make categorical predictions
|
| 146 |
+
categorical_predictions = categorical_model.predict(input_transformed)
|
| 147 |
+
|
| 148 |
+
# Decode categorical predictions
|
| 149 |
+
label_encoders = {col: load(f'label_encoder_{col}.joblib') for col in categorical_targets}
|
| 150 |
+
categorical_predictions_decoded = {col: label_encoders[col].inverse_transform(categorical_predictions[:, i]) for i, col in enumerate(categorical_targets)}
|
| 151 |
+
|
| 152 |
+
# Combine predictions into a single dictionary
|
| 153 |
+
predictions_combined = {**{col: numerical_predictions[0, i] for i, col in enumerate(numerical_targets)}, **categorical_predictions_decoded}
|
| 154 |
+
|
| 155 |
+
print("Predicted Fertilizer Requirements:")
|
| 156 |
+
for col, pred_value in predictions_combined.items():
|
| 157 |
+
print(f"{col}: {pred_value}")
|