Briankabiru commited on
Commit
bb616d3
1 Parent(s): 113a857

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +217 -0
README.md ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ datasets:
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 Application 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
+ This script includes:
109
+
110
+ Loading the models and preprocessor.
111
+ Defining the categorical and numerical targets.
112
+ Loading the label encoders.
113
+ Creating a function make_predictions that processes the input data, makes predictions, and decodes the categorical predictions.
114
+
115
+ ### Inference Procedure
116
+
117
+ ```python
118
+ import pandas as pd
119
+ from joblib import load
120
+ from huggingface_hub import hf_hub_download
121
+ from sklearn.preprocessing import LabelEncoder
122
+
123
+ # Load models and preprocessor
124
+ preprocessor_path = hf_hub_download(repo_id='Briankabiru/FertiliserApplication', filename='preprocessor.joblib')
125
+ numerical_model_path = hf_hub_download(repo_id='Briankabiru/FertiliserApplication', filename='numerical_model.joblib')
126
+ categorical_model_path = hf_hub_download(repo_id='Briankabiru/FertiliserApplication', filename='categorical_model.joblib')
127
+
128
+ preprocessor = load(preprocessor_path)
129
+ numerical_model = load(numerical_model_path)
130
+ categorical_model = load(categorical_model_path)
131
+
132
+ # Define categorical targets
133
+ categorical_targets = [
134
+ 'Lime Application - Instruction',
135
+ 'Lime Application',
136
+ 'Organic Matter Application - Instruction',
137
+ 'Organic Matter Application',
138
+ '1st Application',
139
+ '1st Application - Type fertilizer (1)',
140
+ '1st Application - Type fertilizer (2)',
141
+ '2nd Application',
142
+ '2nd Application - Type fertilizer (1)',
143
+ '1st Application_1',
144
+ '1st Application - Type fertilizer (1)_3',
145
+ '1st Application - Type fertilizer (2)_5',
146
+ '2nd Application_6',
147
+ '1st Application_21',
148
+ '1st Application - Type fertilizer (1)_23',
149
+ '1st Application - Type fertilizer (2)_25',
150
+ '2nd Application_26',
151
+ '2nd Application - Type fertilizer (1)_28'
152
+ ]
153
+
154
+ # Define numerical targets
155
+ numerical_targets = [
156
+ 'Nitrogen (N) Need',
157
+ 'Phosphorus (P2O5) Need',
158
+ 'Potassium (K2O) Need',
159
+ 'Organic Matter Need',
160
+ 'Lime Need',
161
+ 'Lime Application - Requirement',
162
+ 'Organic Matter Application - Requirement',
163
+ '1st Application - Requirement (1)',
164
+ '1st Application - Requirement (2)',
165
+ '2nd Application - Requirement (1)'
166
+ ]
167
+
168
+ # Load label encoders
169
+ label_encoders = {col: load(hf_hub_download(repo_id='Briankabiru/FertiliserApplication', filename=f'label_encoder_{col}.joblib')) for col in categorical_targets}
170
+
171
+ def make_predictions(input_data):
172
+ # Convert input data to DataFrame
173
+ input_df = pd.DataFrame([input_data])
174
+
175
+ # Preprocess the input data
176
+ X_transformed = preprocessor.transform(input_df)
177
+
178
+ # Predict with numerical model
179
+ numerical_predictions = numerical_model.predict(X_transformed)
180
+
181
+ # Predict with categorical model
182
+ categorical_predictions_encoded = categorical_model.predict(X_transformed)
183
+
184
+ # Decode categorical predictions
185
+ categorical_predictions_decoded = {}
186
+ for i, col in enumerate(categorical_targets):
187
+ le = label_encoders[col]
188
+ try:
189
+ categorical_predictions_decoded[col] = le.inverse_transform(categorical_predictions_encoded[:, i])
190
+ except ValueError as e:
191
+ categorical_predictions_decoded[col] = ["Unknown"] * len(categorical_predictions_encoded[:, i])
192
+
193
+ # Combine numerical and categorical predictions into a dictionary
194
+ predictions_combined = {col: numerical_predictions[0, i] for i, col in enumerate(numerical_targets)}
195
+ predictions_combined.update({col: categorical_predictions_decoded[col][0] for col in categorical_targets})
196
+
197
+ return predictions_combined
198
+
199
+ # Example usage
200
+ input_data = {
201
+ 'Crop Name': 'maize(corn)',
202
+ 'Target Yield': 3600.0,
203
+ 'Field Size': 1.0,
204
+ 'pH (water)': 6.1,
205
+ 'Organic Carbon': 11.4,
206
+ 'Total Nitrogen': 1.1,
207
+ 'Phosphorus (M3)': 1.8,
208
+ 'Potassium (exch.)': 3.0,
209
+ 'Soil moisture': 20.0
210
+ }
211
+
212
+ predictions = make_predictions(input_data)
213
+
214
+ print("Predicted Fertilizer Requirements:")
215
+ for col, pred_value in predictions.items():
216
+ print(f"{col}: {pred_value}")
217
+