> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mathfi.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Agriculture crop classification

> Agriculture classification use case to predict crop or soil outcomes from structured field data with MathFi.ai.

### The problem: maximize yield of crops

Farmers primary goal is to maximize crop yield in each season. To achieve that farmers need to make a crucial decision to select the best crop depending on soil conditions such as the levels of nitrogen, phosphorous, potassium, and the pH value of the soil to ensure optimal growth and yield.

### The data

For this project, the goal is to develop a **multi-class classification model** using MathFi.ai Platform to predict the most suitable crop based on the provided features.

The present dataset (<a href="/files/crops/crops-labelled-dataset.csv" target="_blank">Crop.csv</a>) includes the following features:

| Parameter     | Units           | Description                                                |
| ------------- | --------------- | ---------------------------------------------------------- |
| `N`           | % (ratio)       | Nitrogen content ratio in the soil.                        |
| `P`           | % (ratio)       | Phosphorous content ratio in the soil.                     |
| `K`           | % (ratio)       | Potassium content ratio in the soil.                       |
| `ph`          | 0–14 (unitless) | pH value (acidity/alkalinity) of the soil.                 |
| `temperature` | °C              | Ambient/soil temperature measured in degrees Celsius.      |
| `humidity`    | %               | Relative humidity.                                         |
| `rainfall`    | mm              | Total rainfall (e.g., over the sampling period).           |
| `label`       | categorical     | Target variable: crop label for the given soil conditions. |

<br />

<img src="https://mintcdn.com/mathficast/vsPVxXb4l3VcjgSi/images/usecases/crops/crops-csv.png?fit=max&auto=format&n=vsPVxXb4l3VcjgSi&q=85&s=79ffae0796074f5e3b828c0ea797ae9e" alt="Crops CSV" width="670" height="695" data-path="images/usecases/crops/crops-csv.png" />

<br />

### Dataset creation

Use the following parameters for dataset creation:

* **number of buckets**: `20`

<img src="https://mintcdn.com/mathficast/vsPVxXb4l3VcjgSi/images/usecases/crops/crops-dataset.png?fit=max&auto=format&n=vsPVxXb4l3VcjgSi&q=85&s=c4f393a2de873c11c03ad9c2b648c1db" alt="Dataset creation" width="1378" height="622" data-path="images/usecases/crops/crops-dataset.png" />

<br />

### Training

This is the best training attempt:

* **scaling factor**: `20`
* **performance threshold**: `0.955`

<img src="https://mintcdn.com/mathficast/XHYtrrIKg310YHlO/images/usecases/crops/crops-training-1.png?fit=max&auto=format&n=XHYtrrIKg310YHlO&q=85&s=5c69100084f25cbc65956dc1f78f7c44" alt="Training params" width="1375" height="631" data-path="images/usecases/crops/crops-training-1.png" />

which obtained maximum performance (`0.9491283` vs previous `0.9434465`)

<img src="https://mintcdn.com/mathficast/XHYtrrIKg310YHlO/images/usecases/crops/crops-training-2.png?fit=max&auto=format&n=XHYtrrIKg310YHlO&q=85&s=b6587753a4cd6dce7970933f3949927f" alt="Training best" width="1376" height="636" data-path="images/usecases/crops/crops-training-2.png" />

<br />

This good performance of `0.955` was achieved after few iterations of [hyperparameter tuning](/guides/hyperparameter-tuning):

|  #  | Number of Buckets | Scaling Factor | Performance Threshold |
| :-: | ----------------: | -------------: | --------------------: |
|  1  |                20 |             19 |                  0.80 |
|  2  |                20 |             20 |                  0.95 |
|  3  |                10 |             20 |                  0.96 |
|  4  |                10 |             20 |                 0.955 |

<br />

### Final result

Using <a href="/files/crops/crops-unseen.csv" target="_blank">this unseen unlabelled data</a>

<img src="https://mintcdn.com/mathficast/vsPVxXb4l3VcjgSi/images/usecases/crops/crops-prediction.png?fit=max&auto=format&n=vsPVxXb4l3VcjgSi&q=85&s=b1c6e2922d237274fe8d655bea641f83" alt="Prediction 1" width="1376" height="623" data-path="images/usecases/crops/crops-prediction.png" />

As this is a multi-class classification, a probability is assigned to each target label. In this case, `Probability1` belongs to the first label, `Probability2` belongs to the second label... the label order is established following their appearance order in the original training file.
For example, in the *Crops* training file used for this example, the `Probability18=lentil` as `lentil` is the 18th distinct label that appears in the file. The classification result is the label with the highest probability of all.

The <a href="/files/crops/crops-prediction-result.csv" target="_blank">resulting labelled CSV</a> looks like this:

<img src="https://mintcdn.com/mathficast/vsPVxXb4l3VcjgSi/images/usecases/crops/crops-prediction-result.png?fit=max&auto=format&n=vsPVxXb4l3VcjgSi&q=85&s=51843a98b3c5cd4874bc39d5262bc86a" alt="Prediction 2" width="1909" height="911" data-path="images/usecases/crops/crops-prediction-result.png" />

***

**Build this yourself** — Follow the [Quickstart](/quickstart) to run your first prediction, or go straight to [API Recipes](/developers/api-recipes) to integrate programmatically.
