Skip to main content

Build With Me: SimuFarm

Updated 8 min readMarkdown
Part of seriesBuild With Me
Build With Me: SimuFarm

This past month has been hectic. Hectic is actually an understatement. It was my test loop (by test loop, I mean exam season), so forgive my absence. However, we're back with another project!

Welcome to another episode of the Build With Me series, where we explore some of the weird, creative, occasionally practical projects I've worked on, and the thought processes behind bringing them to life.

So, one of my closest friends had THE MOST AMAZING idea for our break. She suggested that we start doing coding challenges.

Here's how it works: one of us comes up with a theme. It could be AI in Agriculture, NLP in Africa, AI in Movies, Space Exploration, or pretty much anything else we can think of. We agree on a timeline, go our separate ways, develop an idea, actually build it, document the process properly, and then showcase our projects to each other.

I loved this idea for several reasons. Not only does it give us an opportunity to sharpen our technical skills, but it also pushes us to explore unfamiliar areas, think creatively, identify problems worth solving and, of course, build things that we can eventually add to our portfolios. With attachment opportunities around the corner, I would say the timing couldn't be better.

Anyway, onto the important part of this episode.

SIMUFARM.

Our first challenge theme was AI in Agriculture.

When I first considered the theme, honestly, the most obvious idea that came to mind was a crop disease detection CNN. And with every fibre of my being, I refused to accept it.

Why, you might ask?

Don't get me wrong. Crop disease detection using Convolutional Neural Networks is a perfectly valid application of AI in agriculture. However, it's also one of the first ideas that tends to come up whenever people think about combining the two. I wanted to explore something a little different. Something that would allow me to experiment with machine learning while tackling a problem that was a little more interesting to me.

And so, SimuFarm was born.

The idea was to create an interactive application where users could explore different maize-yield scenarios by adjusting a range of environmental, soil, planting and farm-management variables.

Think about it. What happens when you change the plant population? What if you adjust the nitrogen application? What if the soil characteristics or seasonal rainfall are different?

SimuFarm takes these inputs and passes them through a trained machine-learning pipeline to estimate the corresponding maize yield.

Essentially, I wanted to build something that would allow people to experiment with different combinations of agricultural conditions and see how a machine-learning model responds.

It wasn't going to be another application that simply identified something in an image. I wanted to build something interactive, something that involved data, experimentation and predictive modelling.

So, what exactly was I trying to build?

The initial idea was to create an interactive maize-yield prediction application. Users would be able to adjust different agricultural variables and observe how the model's predicted yield changed based on those inputs.

However, there was something I had to consider before getting carried away with the idea: the dataset.

Since the application would rely on a machine-learning model trained on an existing dataset, the variables available in the simulator had to be variables that were already present in that dataset. Otherwise, we'd be introducing inputs that the model had never encountered during training.

There was also another consideration. I didn't want the application to be filled with variables that a farmer had absolutely no control over. For example, while geographical location, elevation and seasonal temperature are important factors in agricultural production, they aren't exactly things someone can adjust at will.

I therefore wanted to focus on variables that a farmer could somewhat influence through their farming decisions. Things like plant population, row spacing, planting dates, fertiliser application, lime, compost and other management practices.

Of course, not every variable is completely within a farmer's control, and the application still includes environmental and soil characteristics to provide context for the predictions. But the idea was to strike a balance between the conditions influencing maize production and the decisions a farmer could potentially make.

With that in mind, I had a direction. Now came the actual work: finding out whether the data could support it.

Choosing my tools: The Tech Stack

Before diving into the actual development, I needed to settle on the tools I would use.

I wanted to keep the project relatively straightforward. My main priorities were exploring the data, experimenting with different machine-learning models and eventually making the predictions accessible through an interactive interface.

Python was an obvious choice. I'm comfortable working with it, and its ecosystem has pretty much everything needed for a data science and machine-learning project.

For the interface, I settled on Streamlit.

Why Streamlit? Well, I wanted to prototype quickly. One of the things I appreciate about Streamlit is how easily you can turn Python scripts into interactive applications. Rather than spending time setting up a separate frontend and backend, I could focus on the actual concept and demonstrate what the application was supposed to do.

It was also a good fit for the experimental nature of SimuFarm. I wasn't trying to build a fully-fledged commercial agricultural platform at this stage. I wanted a working prototype that could demonstrate the concept, allow users to experiment with different inputs and display the model's predictions.

The rest of the stack came together quite naturally:

  • Python: The primary programming language.
  • Pandas and NumPy: For data manipulation, cleaning and numerical operations.
  • Scikit-learn: For preprocessing, model benchmarking, cross-validation and evaluation.
  • CatBoost: For the final predictive model after experimenting with different algorithms.
  • Joblib: For exporting and loading the fitted machine-learning pipeline.
  • Streamlit: For building the interactive application.

With the tools sorted, it was time to get into the data and see what I was actually working with.

Getting to Know the Data

Now that I had an idea of what I wanted to build and the tools I would be using, it was time to get my hands dirty.

Before training any models, I needed to understand the dataset I was working with. After all, a machine-learning model is only as useful as the information it learns from.

I started with Exploratory Data Analysis (EDA) using Jupyter Notebook. This involved inspecting the dataset, examining the different variables, checking for missing values and duplicates, and exploring the distributions and relationships between features.

I was particularly interested in understanding how the different environmental, soil and farm-management variables related to maize yield.

This stage was important for another reason: I needed to determine which variables would eventually make their way into the application. Remember, the goal was to let users experiment with different combinations of inputs, so I needed to understand which features were available and which ones made sense for the intended experience.

I also had to pay attention to data quality. Duplicate observations, missing values and inconsistencies could affect how the models learned and how reliable their predictions were.

Rather than treating EDA as just another preliminary step, I used it to guide the decisions I would make throughout the modelling process.

Once I had a better understanding of the data, it was time to start experimenting with models.

Giving SimuFarm a Brain

With the data exploration out of the way, it was time to start experimenting with machine learning models.

I initially started with Linear Regression. It was a good starting point, but I quickly realised that it wasn't capturing the relationships in the data particularly well. Agricultural yield is influenced by several interacting factors, and the relationships aren't necessarily linear.

So, I started experimenting with other algorithms, including Random Forest, Extra Trees and CatBoost.

After benchmarking the different models using five-fold geographical cross-validation, CatBoost with engineered features came out on top, achieving a mean R² of 0.2141 and a mean MAE of approximately 1,096 kg/ha.

Was it perfect? Absolutely not. But it performed better than the baseline and gave me something to work with.

Bringing It to Life

Once I had a fitted model, the next step was getting it into the application.

I exported the trained pipeline using Joblib and connected it to my Streamlit interface. The application accepts 26 inputs covering environmental conditions, soil characteristics, planting decisions and farm-management practices.

And honestly, bringing it to life was surprisingly straightforward. I simply ran:

streamlit run app.py

And there it was. SimuFarm!

Users could now adjust the available inputs and see the model's predicted maize yield.

So, What's Next?

Getting a working prototype is one thing. Getting it to produce reliable predictions is another.

SimuFarm is still experimental, and its predictive performance leaves room for improvement. The next stage is to continue fine-tuning the model, investigating where its predictions go wrong, and evaluating how well it performs across different geographical groups.

Ultimately, I want to improve the reliability of the predictions and establish whether the simulation can meaningfully represent different agricultural scenarios.

For now, SimuFarm is a working prototype, a learning experience and a foundation for further experimentation. And that's perfectly fine. Not every project needs to start as a finished product.

Repo Link: SimuFarm

See you next time!

About the author

No comments yet. Be the first to comment.

More from this blog