Report
Report
TRIBHUVAN UNIVERSITY
AMRIT SCIENCE CAMPUS
Lainchaur, Kathmandu
Project Report on
Crop Recommendation System
With integrated crop cure and marketplace functionality
Submitted By:
Ashish Khatri 23123/076
Kritan Sitaula 23149/076
Nischal Kafle 23165/076
Submitted To:
Tribhuvan University
Institute of Science and Technology
Date: __________________
AMRIT CAMPUS
Affiliated to Tribhuvan University
SUPERVISOR’S RECOMMENDATION
I hereby recommend that this project prepared under my supervision by Ashish Khatri
(23123/076), Kritan Sitaula (23149/076), and Nischal Kafle(23165/076) entitled “Crop
Recommendation System – with integrated cure recommendation and marketplace” in partial
fulfillment of requirement of the degree of BSc. In Computer Science and Information
Technology (B.Sc. CSIT) be processed for evaluation.
_________________________________
Mr. Rakesh Kumar Bachan
Supervisor
AMRIT CAMPUS
Affiliated to Tribhuvan University
CERTIFICATE OF APPROVAL
This is to certify that the project report entitles “Crop Recommendation System – with
integrated Cure recommendation and marketplace” submitted by:
Ashish Khatri 23123/076
Kritan Sitaula 23149/076
Nischal Kafle 23165/076
are bonafide students of this institute and the work has been carried out by them under the
supervision of Mr. Rakesh Kumar Bachhan and it is approved for the partial fulfillment of the
requirement of Tribhuvan University, for the award of the degree of B.Sc. Computer Science
and Information technology.
Cultivation
1. The first stage of our software allows users to input data regarding soil nutrients, such as
Nitrogen, Phosphorous, Potassium, and pH levels, alongside climatic parameters including
temperature, humidity, and rainfall patterns. Utilizing a Random Forest classification
algorithm, the software intelligently analyzes this data to recommend suitable crops for
cultivation, tailored to the specific conditions of the farm using huge dataset. Furthermore, our
software also simplifies online ordering of seeds for the recommended crops.
2. Our software extends support to farmers during the cultivation phase by providing plant care
recommendations. By inputting soil nutrient levels and the name of the cultivated crop,
farmers can receive tailored suggestions for optimal fertilization and pest management
strategies, optimizing crop health and productivity.
3. Moreover, upon harvesting, our software facilitates direct online sales of farm produce to
consumers through a user-friendly e-commerce platform. By cutting out intermediaries and
enabling direct-to-consumer transactions, farmers can establish stronger connections with
consumers, enhance market reach, and command better prices for their produce.
Figure
1: Use Case Diagram
• Admin
1. Use ‘Crop Recommendation System” and “Crop Cure Recommendation System”
2. Create seeds that is recommended for purchase to any user who obtains prediction
of the crop
3. Modify/Delete the created seed
4. Create farm products to list in market place from where any user can make an
order
5. Modify/delete products
6. Create another admin
7. Modify/Delete users
8. Manage Orders
• Farmer
1. Use ‘Crop Recommendation System” and “Crop Cure Recommendation System”
2. Make order for seeds of crop recommended by crop recommendation system
3. Create farm products to list in market place from where any user can make an
order
4. Modify/delete products
5. Modify/Delete users
6. Manage Orders
• Customer
1. Create Account
2. Make orders for seeds or farm products
Figure 4: ER Diagram
Figure 4: ER Diagram
Data Flow Diagram
1. DFD 0
The context level DFD describes the whole system. Below DFD of the Kishan Saathi App
shows three users can operate the system; Admin, Farmer and Customer.
Forms Design
For our system, following forms will be necessary:
1. Login and Registration form
2. Crop recommendation form
3. Cure Recommendation form
4. Checkout form
5. Banner, Seed, Product, User creation form in CMS
Dialog Design
Each decision tree in the random forest is constructed using a subset of the training data and a
random subset of features introducing diversity among the trees, making the model more robust
and less prone to overfitting.
During the training phase, each tree is built by recursively partitioning the data based on the
features. At each split, the algorithm selects the best feature from the random subset, optimizing
for information gain or Gini impurity. The process continues until a predefined stopping criterion
is met, such as reaching a maximum depth or having a minimum number of samples in each leaf
node.
For creating Decision Tree, we used CART Algorithm. CART stands for Classification and
Regression Tree Algorithm.
CART Algorithm
Classification and Regression Trees (CART) is a decision tree algorithm that is used for both
classification and regression tasks. It is a supervised learning algorithm that learns from labelled
data to predict unseen data.
Tree structure: CART builds a tree-like structure consisting of nodes and branches. The nodes
represent different decision points, and the branches represent the possible outcomes of those
decisions. The leaf nodes in the tree contain a predicted class label or value for the target
variable.
Splitting criteria: CART uses a greedy approach to split the data at each node. It evaluates all
possible splits and selects the one that best reduces the impurity of the resulting subsets. For
classification tasks, CART uses Gini impurity as the splitting criterion. The lower the Gini
impurity, the more pure the subset is. For regression tasks, CART uses residual reduction as the
splitting criterion. The lower the residual reduction, the better the fit of the model to the data.
Pruning: To prevent overfitting of the data, pruning is a technique used to remove the nodes that
contribute little to the model accuracy. Cost complexity pruning and information gain pruning
are two popular pruning techniques. Cost complexity pruning involves calculating the cost of
each node and removing nodes that have a negative cost. Information gain pruning involves
calculating the information gain of each node and removing nodes that have a low information
gain.
The CART algorithm works via the following process:
• The best-split point of each input is obtained.
• Based on the best-split points of each input in Step 1, the new “best” split point is
identified.
• Split the chosen input according to the “best” split point.
• Continue splitting until a stopping rule is satisfied or no further desirable splitting is
available.
CART algorithm uses Gini Impurity to split the dataset into a decision tree. It does that by
searching for the best homogeneity for the sub nodes, with the help of the Gini index criterion.
Chapter 5: Implementation and Testing
5.1 Implementation
5.1.1 Tools Used
1. Visual Studio Code:
We used VS Code IDE for development of the project. Visual Studio Code (VS Code) is
a free source-code editor developed by Microsoft for Windows, Linux, and macOS. It
offers a wide range of features for developers, including support for debugging, syntax
highlighting, intelligent code completion, code refactoring, and Git integration.
One of the key features of Visual Studio Code is its extensibility, which allows developers
to customize and enhance their coding experience through various extensions available in
the Visual Studio Code Marketplace. These extensions can add support for additional
programming languages, provide new themes, integrate with other tools and services, and
more.
4. Database:
MongoDB database was used as database. MongoDB is a popular open-source NoSQL
database management system that stores data in flexible, JSON-like documents. It's
known for its scalability, flexibility, and performance, making it suitable for a wide range
of applications, from small startups to large enterprises.
data = []
data.append(line.strip().split(','))
return data
data_array = np.array(data)
X = data_array[:, :-1].astype(float)
y = data_array[:, -1]
return X, y
Now the data is splitted into training and testing data. Train size was was taken 0.8 part of size of
features and test size was 0.2 part.
train_size = int(0.8 * len(features))
X_train, X_test = features[:train_size], features[train_size:]
y_train, y_test = labels[:train_size], labels[train_size:]
This fit function creates object of Decision tree. The related Decision Tree Class is:
class DecisionTree:
def __init__(self, max_depth=None):
self.max_depth = max_depth
# Stopping criteria
if (self.max_depth is not None and depth >= self.max_depth) or num_classes == 1:
return {'class': np.bincount(y).argmax(), 'num_samples': num_samples, 'depth': depth}
if best_gini == np.inf:
return {'class': np.bincount(y).argmax(), 'num_samples': num_samples, 'depth': depth}
city = request.form.get("city")
prediction = crop_recommendation(single_pred)
response = {
"result": prediction[0],
"message": "Crop Predicted"
}
return json.dumps(response)
N = int(request.form['nitrogen'])
P = int(request.form['phosphorous'])
K = int(request.form['potassium'])
We extract suitable value of N, P, K as nr, pr, kr from file. Then we find n, p, k which is the
difference between actual N, P, K and suitable nr, pr, kr respectively. Based on that we can
determine which of the parameter is abundance or low in soil.
df = pd.read_csv('Data/fertilizer.csv')
nr = df[df['Crop'] == crop_name]['N'].iloc[0]
pr = df[df['Crop'] == crop_name]['P'].iloc[0]
kr = df[df['Crop'] == crop_name]['K'].iloc[0]
n = nr - N
p = pr - P
k = kr - K
temp = {abs(n): "N", abs(p): "P", abs(k): "K"}
max_value = temp[max(temp.keys())]
if max_value == "N":
if n < 0:
key = 'NHigh'
else:
key = "Nlow"
elif max_value == "P":
if p < 0:
key = 'PHigh'
else:
key = "Plow"
else:
if k < 0:
key = 'KHigh'
else:
key = "Klow"
We create a dictionary of cure for each case of high and low. We markup the dictionaries content
and dumps the final cure recommendation.
response = Markup(str(fertilizer_dic[key]))
res = {
"result": response,
"message": "Recommended cure"
}
return json.dumps(res)
3. For ecommerce functionality:
No major algorithm was used in this functionality. An express
app was setup in server file and the system was broken down
into modules; auth, banner, cart, dashboard, product, seed, and
user. MVC pattern was followed. Folder were organized into
modular architecture. Each folder of modules contain file for
router, controller, service validator and model. Common
middleware, routes and config are stored in separate folders.
Project variable are controlled from ‘.env’ file.
5.2 Testing
5.2.1 Test Cases for Unit Testing
Table 1: Unit Testing of Authentication and Authorization
Test Description Test Step Expected Result Status
TA1 User Registration Go to sign up page, fill After filling registration pass
test registration form form, user will get
verification link at specified
email
TA2 Verification Check verification email If verified, user will redirect pass
and click on the link to password set page
provided to verify
TA3 Set Password In password set page, Password should be stored pass
create password. Only in database in encrypted
password with Uppercase, form and user should be
Lowercase, Number, redirect into login page.
Symbol and at least 8
characters are allowed.
TA4 Login Provide login credential in For Admin, redirect in pass
login page. ADMIN CMS.
For Farmer, redirect in
FARMER CMS.
For Consumer, redirect in
HOMEPAGE.
TA5 Permission Test Browse Admin Block Admin Dashboard pass
Dashboard URL, while and redirect in homepage
login as User
TA6 Logout Click on logout User should be logged out pass
Seed:
Product:
New Admin:
2. A new farmer user is signed up with the required credentials. Then the user first uses the crop
recommendation system. A random data will be provided to the crop recommendation form.
To get the crop recommendation. User must be able to order the recommended crop from the
recommendation. The order must be shown in admin panel. Then farmer user will test cure
recommendation system. A test data will be fed and the result for curing the crop should be
obtained. From farmer CMS, we will create few products, which must be displayed in Farmer
Marketplace.
Result:
New Farmer account:
Crop recommendation:
Cure recommendation:
3. A new customer user is signed up with the required credentials. Then the customer user will
navigate to farmer market place. Customer user must be able to place order. The order should
be updated in farmer’s panel.
The feature_list is fed into the model which has 100 decision tree. Each decision tree derive it’s
own conclusion. The conclusion with highest majority which is “apple” is resulted.