Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: mit
|
3 |
+
datasets:
|
4 |
+
- custom
|
5 |
+
metrics:
|
6 |
+
- mean_squared_error
|
7 |
+
- mean_absolute_error
|
8 |
+
- r2_score
|
9 |
+
model_name: Random Forest Regressor for Crop Nutrient Prediction
|
10 |
+
tags:
|
11 |
+
- random-forest
|
12 |
+
- regression
|
13 |
+
- agriculture
|
14 |
+
- soil-nutrients
|
15 |
+
---
|
16 |
+
|
17 |
+
# Random Forest Regressor for Crop Nutrient Prediction
|
18 |
+
|
19 |
+
## Overview
|
20 |
+
|
21 |
+
This model predicts the nutrient needs (Nitrogen, Phosphorus, Potassium) for various crops based on features like crop type, target yield, field size, and soil properties. It is trained using a Random Forest Regressor.
|
22 |
+
|
23 |
+
## Training Data
|
24 |
+
|
25 |
+
The model was trained on a custom dataset containing the following features:
|
26 |
+
- Crop Name
|
27 |
+
- Target Yield
|
28 |
+
- Field Size
|
29 |
+
- pH (water)
|
30 |
+
- Organic Carbon
|
31 |
+
- Total Nitrogen
|
32 |
+
- Phosphorus (M3)
|
33 |
+
- Potassium (exch.)
|
34 |
+
- Soil moisture
|
35 |
+
|
36 |
+
The target variables are:
|
37 |
+
- Nitrogen (N) Need
|
38 |
+
- Phosphorus (P2O5) Need
|
39 |
+
- Potassium (K2O) Need
|
40 |
+
|
41 |
+
## Model Training
|
42 |
+
|
43 |
+
The model was trained using a Random Forest Regressor. Below are the steps taken for training:
|
44 |
+
|
45 |
+
1. Data preprocessing: handling missing values, scaling numerical features, and one-hot encoding categorical features.
|
46 |
+
2. Splitting the dataset into training and testing sets.
|
47 |
+
3. Training the Random Forest model on the training set.
|
48 |
+
4. Evaluating the model on the test set.
|
49 |
+
|
50 |
+
## Evaluation Metrics
|
51 |
+
|
52 |
+
The model was evaluated using the following metrics:
|
53 |
+
- Mean Squared Error (MSE)
|
54 |
+
- Mean Absolute Error (MAE)
|
55 |
+
- R-squared (R2) Score
|
56 |
+
|
57 |
+
## How to Use
|
58 |
+
|
59 |
+
### Input Format
|
60 |
+
|
61 |
+
The model expects input data in JSON format with the following fields:
|
62 |
+
- "Crop Name": String
|
63 |
+
- "Target Yield": Numeric
|
64 |
+
- "Field Size": Numeric
|
65 |
+
- "pH (water)": Numeric
|
66 |
+
- "Organic Carbon": Numeric
|
67 |
+
- "Total Nitrogen": Numeric
|
68 |
+
- "Phosphorus (M3)": Numeric
|
69 |
+
- "Potassium (exch.)": Numeric
|
70 |
+
- "Soil moisture": Numeric
|
71 |
+
|
72 |
+
### Preprocessing Steps
|
73 |
+
|
74 |
+
1. Load your input data.
|
75 |
+
2. Ensure all required fields are present and in the expected format.
|
76 |
+
3. Handle any missing values if necessary.
|
77 |
+
4. Scale numerical features based on the training data.
|
78 |
+
5. One-hot encode categorical features (if applicable).
|
79 |
+
|
80 |
+
### Inference Procedure
|
81 |
+
|
82 |
+
#### Example Code:
|
83 |
+
|
84 |
+
```python
|
85 |
+
from huggingface_hub import hf_hub_download
|
86 |
+
import joblib
|
87 |
+
import pandas as pd
|
88 |
+
|
89 |
+
# Download the model file from the Hugging Face hub
|
90 |
+
model_path = hf_hub_download(repo_id="DNgigi/NPKRecommendation", filename="ModelV2.joblib")
|
91 |
+
|
92 |
+
# Load the trained model
|
93 |
+
model = joblib.load(model_path)
|
94 |
+
|
95 |
+
# Example input data
|
96 |
+
new_data = {
|
97 |
+
'Crop Name': 'coffee',
|
98 |
+
'Target Yield': 1200.0,
|
99 |
+
'Field Size': 1.0,
|
100 |
+
'pH (water)': 5.76,
|
101 |
+
'Organic Carbon': 12.9,
|
102 |
+
'Total Nitrogen': 1.1,
|
103 |
+
'Phosphorus (M3)': 1.2,
|
104 |
+
'Potassium (exch.)': 1.7,
|
105 |
+
'Soil moisture': 11.4
|
106 |
+
}
|
107 |
+
|
108 |
+
# Preprocess the input data
|
109 |
+
input_df = pd.DataFrame([new_data])
|
110 |
+
|
111 |
+
# Ensure the same columns as in training
|
112 |
+
input_df = pd.get_dummies(input_df, columns=['Crop Name'])
|
113 |
+
# Assuming X is your feature set used during training
|
114 |
+
for col in X.columns:
|
115 |
+
if col not in input_df.columns:
|
116 |
+
input_df[col] = 0
|
117 |
+
|
118 |
+
# Make predictions
|
119 |
+
predictions = model.predict(input_df)
|
120 |
+
|
121 |
+
print("Predicted nutrient needs:")
|
122 |
+
print(predictions)
|
123 |
+
|