File size: 2,837 Bytes
3570633
 
 
 
 
 
 
 
 
 
 
 
 
 
 
654d84b
3570633
654d84b
3570633
654d84b
3570633
654d84b
 
 
3570633
 
 
 
 
 
 
 
 
 
654d84b
3570633
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7d2a6fa
3570633
7d2a6fa
 
 
 
 
 
 
 
 
 
3570633
7d2a6fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93b6e33
7d2a6fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
---
license: mit
datasets:
- custom
metrics:
- mean_squared_error
- mean_absolute_error
- r2_score
model_name: Random Forest Regressor for Crop Nutrient Prediction
tags:
- random-forest
- regression
- agriculture
- soil-nutrients
---

# Random Forest Regressor for Crop Nutrient Prediction

## Overview

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.

## 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 are:
- Nitrogen (N) Need
- Phosphorus (P2O5) Need
- Potassium (K2O) Need

## Model Training

The model was trained using a Random Forest Regressor. Below are the steps taken for training:

1. Data preprocessing: handling missing values, scaling numerical features, and one-hot encoding categorical features.
2. Splitting the dataset into training and testing sets.
3. Training the Random Forest model on the training set.
4. Evaluating the model on the test set.

## Evaluation Metrics

The model was evaluated using the following metrics:
- Mean Squared Error (MSE)
- Mean Absolute Error (MAE)
- R-squared (R2) Score

## 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

#### Example Code:

```python
from sklearn.externals import joblib
import pandas as pd

# Load the trained model
model = joblib.load('ModelV2.joblib')

# Example input data
new_data = {
    'Crop Name': 'apple',
    'Target Yield': 1200.0,
    'Field Size': 1.0,
    'pH (water)': 5.76,
    'Organic Carbon': 12.9,
    'Total Nitrogen': 1.1,
    'Phosphorus (M3)': 1.2,
    'Potassium (exch.)': 1.7,
    'Soil moisture': 11.4
}

# Preprocess the input data
input_df = pd.DataFrame([new_data])

# Ensure the same columns as in training
input_df = pd.get_dummies(input_df, columns=['Crop Name'])
for col in X.columns:
    if col not in input_df.columns:
        input_df[col] = 0

# Make predictions
predictions = model.predict(input_df)

print("Predicted nutrient needs:")
print(predictions)