FarmEasy: Crop Recommendation for Farmers made easy | by Darshan Gandhi | Towards Data Science

Please leave a clap if you found this article useful, it helps me a lot! :D
In order to mitigate the agrarian crisis in the current status quo, there is a need for better recommendation systems to alleviate the crisis by helping the farmers to make an informed decision before starting the cultivation of crops.
The major parameters considered here are:
1. Crop name
2. Sowing Time (Month)
3. Region
4. Temperature - Minimum & Maximum
5. Rainfall - Minimum & Maximum
6. pH value of the Soil
7. Soil Type
Step 1: Data Collection
This was by far the most important and crucial step during the project building.
Since there was no specific dataset available I had to work on building that dataset from scratch and had to choose the parameters accordingly. Additionally, I had several other parameters in mind such as Nitrogen level of Soil, Irrigation Facilities, Crop Rotation Cycle but due to data paucity was not able to take them into account.
The data obtained was distributed across different files such as:
Soil TypeCash-cropsFlowerFruitGrainHerbsShrubsSpicesTreesVegetablesAverage RainfallAverage Temperature
Soil Type
Cash-crops
Flower
Fruit
Grain
Herbs
Shrubs
Spices
Trees
Vegetables
Average Rainfall
Average Temperature
Step 2: Data Transformation
The data also had several inconsistencies since it was obtained from several different sources such as :
Missing Values
Missing Values
The missing values had to be removed from the dataset since they would lead to data inconsistencies and eventually to the wrong prediction.
2. Spelling Aberrations
The spellings were not consistent throughout the files and had to be normalised to a single value to improve the model accuracy.
3. Redundant Data
The redundant data had to be discarded since they would not add any additional significance to the process.
The data was eventually coalesced into one dataset to ease the process of model building and computation.
Once, the data was collected the data types were checked and changes were made such as changing from String -> Integer where required.
Also since a machine learning model works only with numbers, the string values need to be converted into numerical values. For this, I made use of Label Encoder Technique.
“Label Encoder is a tool provided by Scikit Learn which helps one to encode the categorical features/variables into their numeric features. The encoder targets variables between 0 to n_classes-1 where n is the number of distinct labels.”
More can be learnt about Label Encoding by referring to the official Scikit Learn documentation here.
Step 3: Model Building
The model used here is: Ensemble Learning Model.
Okay first let us understand what ensemble learning is all about!
According to Wikipedia ensemble learning is stated follows :
“Ensemble Learning approach uses multiple learning algorithms in statistics and machine learning to achieve greater predictive efficiency than either of the constituent learning algorithms alone might obtain.”
Here, I have made use of the functionality of Voting Classifier provided by Scikit Learn.
from sklearn.ensemble import VotingClassifiermodel=VotingClassifier(estimators=[('rf', random_forest),('gnb',gaussian_naive_bayes),('knn',k_nearest_neighbours), ('dt',decision_tree)  ], voting=''hard)
As seen in the above code snippet the model was built using algorithms listed below :
Random Forest Algorithm
Gaussian Naive Bayes Algorithm
K-Nearest Neighbours Algorithm
Decision Tree Algorithm
Voting parameter is set to “hard” here since the model uses predicted class labels for majority rule voting.
Step 4: Deployment of the model
In order to deploy the trained model for the farmers to use off, we would need an application with a simple user interface which the farmers can utilise.
Thus, here I made a simple web interface using HTML, CSS & Bootstrap.
The next step was to have a database to store the data for which I made use of : MongoDb
Lastly, I wanted to predict the results for the obtained values from the user for which I made use of Flask framework to integrate the backend and the front end. Also,I generated the pickle file for our model to generate the predictions for the input data. Okay, so you would be wondering what is a pickle file? Let’s have a look at it as well.
According to the official Python documentation available at docs.python.org :
“The pickle module implements binary protocols for serialising and de-serialising a Python object structure. “Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the inverse operation, whereby a byte stream (from a binary file or bytes-like object) is converted back into an object hierarchy.”
In simple words, the pickled file converts the trained model into a byte stream and is sent to the website where it is unpickled again where it is used to train the incoming data from the farmers.
Login Page for the Farmer
The farmer needs to enter their details such as :
Name
Email Id
Password
Services offered
Prediction
Recommendation
Information
Predict now & Searching a Crop
If the farmer would like to search for more information about a specific crop the “Search Crop Section” would be there to the rescue.
It helps the farmer with detailed information about the crop for better understanding.
“Predict Now Section”
The predict now takes in the following parameters for the farmer:
Month
pH Scale
Region
Soil Type
Additionally, 2 parameters are taken into account from the database itself accordingly to the region mentioned by the farmer
Temperature
Rainfall
After inputting the values, the farmer can click on the “Predict Now” button.
The optimum Crop Name is displayed to the farmer for cultivation.
RSS News Feed
Added the RSS functionality for farmers to get informative news for them to read and be updated!
Voice Based Modelling System
Additionally, I have tried to implement a question-answer system model where I give a voice input as a question and I get an answer back in voice.
The system has been incorporated in the website to ease the process of the farmer for feeding in the application. The farmer just needs to click on the 🎙️ below the input field for talking!
Concepts and Libraries used here :
iNLTK (Natural Language Tool Kit)- Taking input in the regional language (Hindi in this case)
SpeechRecognition- For receiving speech input from the farmer
gTTS (Google Text to Speech)- Providing farmer the output via voice
Scikit Learn- For making use of NLP libraries
I have built a portal for the Farmers in collaboration with my colleague Nipun Iyer which would help them to get assistance regarding crop cultivation strategies, primarily for the services of: prediction, recommendation, information of crops.
Thank you for taking the time out and reading this article! 🙂
If you have got any doubts or would love to discuss more and understand about the project, please reach out to me here, would be happy to chat!