File size: 5,346 Bytes
9992228
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
---
language: en
tags:
  - crop-yield
  - agriculture
  - regression
  - classification
  - xgboost
  - tabular
license: mit
datasets:
  - fao
pipeline_tag: tabular-regression
---

# 🌾 CropYQ β€” Crop Yield & Quality Prediction Models

**Repository:** `BirendraSharma/cropyq`

This repository hosts two trained machine learning models for predicting agricultural crop yield and quality across India, Nepal, and the Netherlands, based on FAO crop production data.

---

## πŸ“¦ Models Included

| File | Type | Description |
|------|------|-------------|
| `regression_model.pkl` | Scikit-learn Regressor | Predicts crop yield in **kg/ha** (log-transformed target, inverse-transformed on output) |
| `xgboostClassification_model.pkl` | XGBoost Classifier | Predicts crop quality as **Low / Medium / High** |

---

## πŸ—‚οΈ Input Features

Both models share the same 5-feature input vector:

| Feature | Type | Description |
|---------|------|-------------|
| `Area` | Encoded int | Country (India=0, Nepal=1, Netherlands=2) |
| `Item` | Encoded int | Crop type (38 categories, e.g. Wheat=36, Rice=27) |
| `Crop Group` | Encoded int | Cereal=0, Fruit=1, Oilseed=2, Pulse=3, Root=4, Vegetable=5 |
| `Flag` | Encoded int | FAO data flag β€” A=0, E=1 |
| `Year` | int | Year offset from 1961 (e.g. 2020 β†’ 59) |

---

## 🌍 Supported Areas

- India
- Nepal
- Netherlands (Kingdom of the)

---

## 🌱 Supported Crops (38 total)

Apples, Bananas, Barley, Beans (dry), Broad beans, Cabbages, Carrots & turnips, Cassava, Cauliflowers & broccoli, Chick peas, Chillies & peppers, Eggplants, Grapes, Groundnuts, Lentils, Linseed, Maize (corn), Mangoes, Millet, Mustard seed, Oats, Onions & shallots, Oranges, Peas (dry), Pigeon peas, Potatoes, Rape/colza seed, Rice, Rye, Sesame seed, Sorghum, Soya beans, Sunflower seed, Sweet potatoes, Tomatoes, Triticale, Wheat, Yams

---

## πŸš€ Quickstart

### Install dependencies

```bash
pip install huggingface_hub scikit-learn xgboost numpy
```

### Load and use the models

```python
import pickle
import numpy as np
from huggingface_hub import hf_hub_download

REPO_ID = "BirendraSharma/cropyq"

# Download and load regression model
reg_path = hf_hub_download(repo_id=REPO_ID, filename="regression_model.pkl")
with open(reg_path, "rb") as f:
    reg_model = pickle.load(f)

# Download and load classification model
clf_path = hf_hub_download(repo_id=REPO_ID, filename="xgboostClassification_model.pkl")
with open(clf_path, "rb") as f:
    clf_model = pickle.load(f)

# Example: Wheat in India, Cereal group, Flag A, Year 2020
# area=0 (India), item=36 (Wheat), cropgroup=0 (Cereal), flag=0 (A), year=2020-1961=59
inputs = np.array([[0, 36, 0, 0, 59]], dtype=np.float32)

# Predict yield (kg/ha) β€” model was trained on log1p target
log_yield = reg_model.predict(inputs)[0]
yield_kgha = np.expm1(log_yield)
print(f"Predicted Yield: {yield_kgha:.2f} kg/ha")

# Predict quality
quality_map = {0: "Low", 1: "Medium", 2: "High"}
quality_pred = clf_model.predict(inputs)[0]
print(f"Predicted Quality: {quality_map[int(quality_pred)]}")
```

---

## πŸ–₯️ Desktop GUI App

A Tkinter-based desktop app is available that provides a point-and-click interface for running predictions.

### Run the app

```bash
pip install huggingface_hub scikit-learn xgboost numpy tkinter
python crop_yield_app.py
```

The app will automatically download both model files from this repository on first launch.

**Features:**
- Dropdown selectors for Area, Item, Crop Group, and Flag
- Text entry for Year
- **Predict Yield** button β†’ returns estimated kg/ha
- **Predict Quality** button β†’ returns Low / Medium / High

---

## πŸ”’ Encoding Reference

<details>
<summary>Area Encoding</summary>

| Area | Code |
|------|------|
| India | 0 |
| Nepal | 1 |
| Netherlands (Kingdom of the) | 2 |

</details>

<details>
<summary>Crop Group Encoding</summary>

| Crop Group | Code |
|------------|------|
| Cereal | 0 |
| Fruit | 1 |
| Oilseed | 2 |
| Pulse | 3 |
| Root | 4 |
| Vegetable | 5 |

</details>

<details>
<summary>Flag Encoding</summary>

| Flag | Code | Meaning |
|------|------|---------|
| A | 0 | Official figure |
| E | 1 | Estimated value |

</details>

<details>
<summary>Year Encoding</summary>

Year values are offset from 1961:

```
encoded_year = actual_year - 1961
# e.g. 2020 β†’ 59,  1990 β†’ 29,  1961 β†’ 0
```

</details>

---

## πŸ“Š Model Details

### Regression Model (`regression_model.pkl`)
- **Task:** Tabular regression
- **Target:** Log-transformed crop yield (`log1p(kg/ha)`), back-transformed with `expm1` at inference
- **Output:** Yield in kg/ha

### Classification Model (`xgboostClassification_model.pkl`)
- **Task:** Multi-class tabular classification
- **Framework:** XGBoost
- **Output classes:** Low (0), Medium (1), High (2)

---

## πŸ“ Repository Structure

```
BirendraSharma/cropyq/
β”œβ”€β”€ regression_model.pkl             # Sklearn regression model
β”œβ”€β”€ xgboostClassification_model.pkl  # XGBoost classification model
└── README.md                        # This file
```

---

## πŸ“œ License

This project is licensed under the [MIT License](https://opensource.org/licenses/MIT).

---

## πŸ™ Acknowledgements

Data sourced from the [FAO (Food and Agriculture Organization of the United Nations)](https://www.fao.org/faostat/) crop production statistics.