Predicting Environment Impact of Food Production caused by C02 Emission
Tags: hacktoberfest, mindsdb, machinelearning, aws
Published: 2022-10-31 22:56:08

## Introduction

In this tutorial, we will be predicting the C02 emissions and their impact on food production with MindsDB. MindsDB is an open-source machine-learning tool that brings automated machine learning to your database. MindsDB offers predictive capabilities in your database. 

To complete this tutorial, you are required to have a working MindsDB connection, either locally or via [cloud.mindsdb.com](http://cloud.mindsdb.com/). You can use this [guide](https://docs.mindsdb.com/connect/mindsdb_editor/) to connect to the MindsDB cloud.

## **Data Setup**

### **Connecting the data as a file**

Follow the steps below to upload a file to MindsDB Cloud.

- Log in to your [MindsDB Cloud](https://cloud.mindsdb.com/login) account to open the MindsDB Editor.
- Navigate to `Add data` the section by clicking the `Add data` button located in the top right corner.

![Add Data](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/crrhqczsc8ioijbayn8d.png)
- Choose the File tab
![File Upload](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dbi0vfbblwjw5at3vacb.png)
- Choose the `Import File` option.
- Upload a file [Food_Production.csv](https://docs.google.com/spreadsheets/d/1BxW9Pg_43ctPXaDLN33gLN7cgem4sPYclimjIVIElVo/edit?usp=sharing), name a table used to store the file data (here it is `Food_Production`), and click the `Save and Continue` button.
![File Upload](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4pvwd4uj7km5ceibrwgh.png)
- Once you are done uploading, you can query the data directly with the;

```sql
SELECT * FROM files.Food_Production LIMIT 10;
```
![Table Query](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bwioz3s3bry4ngwlklbf.png)
## Understanding the Dataset

### **Context**

As the world’s population has expanded and gotten richer, the demand for food, energy, and water has seen a rapid increase. Not only has demand for all three increased, but they are also strongly interlinked: food production requires water and energy; traditional energy production demands water resources; agriculture provides a potential energy source. This article focuses on the environmental impacts of food. Ensuring everyone in the world has access to a nutritious diet in a sustainable way is one of the greatest challenges we face.

### **Content**

This dataset contains most 43 most common foods grown across the globe and 23 columns as their respective land, water usage, and carbon footprints.

Columns

1. Land use change - Kg CO2 - equivalents per kg product
2. Animal Feed - Kg CO2 - equivalents per kg product
3. Farm - Kg CO2 - equivalents per kg product
4. Processing - Kg CO2 - equivalents per kg product
5. Transport - Kg CO2 - equivalents per kg product
6. Packaging - Kg CO2 - equivalents per kg product
7. Retail - Kg CO2 - equivalents per kg product

These represent greenhouse gas emissions per kg of food product(Kg CO2 - equivalents per kg product) across different stages in the lifecycle of food production.

Eutrophication – the pollution of water bodies and ecosystems with excess nutrients – is a major environmental problem. The runoff of nitrogen and other nutrients from agricultural production systems is a leading contributor.

### Acknowledgments and Credits

[https://ourworldindata.org](https://ourworldindata.org/)

Credit: [Vivek](https://www.kaggle.com/selfvivek)

Original Source: ****[Environment Impact of Food Production](https://www.kaggle.com/datasets/selfvivek/environment-impact-of-food-production)****

## Creating the Predictor

To being, let’s create a predictor that uses the columns to predict CO2 emission. You can learn more about creating a predictor by checking [here](https://docs.mindsdb.com/sql/create/predictor/). You can predict a classification series model using the following syntax

```sql
CREATE PREDICTOR mindsdb.[predictor_name]
FROM [integration_name]
    (SELECT [sequential_column], [partition_column], [other_column], [target_column]
     FROM [table_name])
PREDICT [target_column]
```

- `CREATE PREDICTOR`: Creates a predictor with the name `predictor_name` in the `mindsdb` table.
- `FROM files`: Points to the table containing the data.
- `PREDICT`: Dictates the column to predict.

```sql
CREATE PREDICTOR mindsdb.CO2_emission
FROM files (SELECT * FROM files.Food_Produuction)
PREDICT Total_emissions
```

On execution we get:

```sql
Query successfully completed
```

## **Status of a Predictor**

A predictor may take a couple of minutes for the training to complete. You can monitor the status of the predictor by using this SQL command:

```sql
SELECT status
FROM mindsdb.predictors
WHERE name='CO2_emission';
```

Your output should be:

```sql
+------------+
| status     |
+------------+
| complete   |
+------------+
```

## **Making Predictions**

Now that we have our Prediction Model, we can simply execute some simple SQL query statements to predict the target value based on the feature parameters:

### **Making a Single Prediction**

You can make predictions by querying the predictor as if it were a table. The **[SELECT](https://docs.mindsdb.com/sql/api/select/)** statement lets you make predictions for the `CO2_emission` on the chosen feature parameter.

```sql
SELECT Total_emissions, Total_emissions_explain
FROM mindsdb.CO2_emission
WHERE Food_Product = 'Rice'
AND Land_use_charge = 0
AND Animal_Feed = 0
AND Farm = 1.4
AND Processing = 0.1
AND Transport = 0.1
AND Packaging = 0.1
AND Retail = 0.3
```
![Single Prediction](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ego95hkzfh5v6nku7um.png)
### **Making a Bulk Prediction**

Now let’s make bulk predictions or multiple predictions to predict the `CO2_emission` by joining our table with the model.

```sql
SELECT * FROM mindsdb.CO2_emission
JOIN files.Food_Production
LIMIT 100;
```

Expected output should be:
![Batch Predictions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h3besamtdtn2uub8tal2.png)
## What’s Next?

Have fun while trying it out yourself!

- Star the **[MindsDB repository on GitHub](https://github.com/mindsdb/mindsdb)**.
- Sign up for a **[free MindsDB account](https://cloud.mindsdb.com/register?_ga=2.126448781.1875715573.1663498526-358045687.1658244666&_gl=1*1us5k78*_ga*MzU4MDQ1Njg3LjE2NTgyNDQ2NjY.*_ga_7LGFPGV6XV*MTY2MzYwNjcxNi4zNi4xLjE2NjM2MDY3MzUuMC4wLjA)**
- Engage with the MindsDB community on **[Slack](https://mindsdb.com/joincommunity?_ga=2.126448781.1875715573.1663498526-358045687.1658244666&_gl=1*1us5k78*_ga*MzU4MDQ1Njg3LjE2NTgyNDQ2NjY.*_ga_7LGFPGV6XV*MTY2MzYwNjcxNi4zNi4xLjE2NjM2MDY3MzUuMC4wLjA)** or **[GitHub](https://github.com/mindsdb/mindsdb/discussions)** to ask questions and share your ideas and thoughts.

Give a like or a comment if this tutorial was helpful