-
-
Notifications
You must be signed in to change notification settings - Fork 414
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1140 from kanchanrai7/main
Crop Recommendation Using ML
- Loading branch information
Showing
17 changed files
with
7,637 additions
and
1 deletion.
There are no files selected for viewing
2,201 changes: 2,201 additions & 0 deletions
2,201
...ng and Data Science/Intermediate/Crop Recommedation using ML/Data/Crop_recommendation.csv
Large diffs are not rendered by default.
Oops, something went wrong.
1,094 changes: 1,094 additions & 0 deletions
1,094
...g and Data Science/Intermediate/Crop Recommedation using ML/EDA_crop_recommendation.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file added
BIN
+161 KB
...ing and Data Science/Intermediate/Crop Recommedation using ML/Images/image1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+130 KB
...ing and Data Science/Intermediate/Crop Recommedation using ML/Images/image2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+42.6 KB
...Data Science/Intermediate/Crop Recommedation using ML/Images/mod_comparison.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
151 changes: 151 additions & 0 deletions
151
...Learning and Data Science/Intermediate/Crop Recommedation using ML/Model Comparison.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
2,201 changes: 2,201 additions & 0 deletions
2,201
... and Data Science/Intermediate/Crop Recommedation using ML/Models/Crop_recommendation.csv
Large diffs are not rendered by default.
Oops, something went wrong.
359 changes: 359 additions & 0 deletions
359
...ning and Data Science/Intermediate/Crop Recommedation using ML/Models/Decision Tree.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
318 changes: 318 additions & 0 deletions
318
...d Data Science/Intermediate/Crop Recommedation using ML/Models/Gaussian Naive Bayes.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,318 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"id": "074109be-9caa-4f91-a3cf-45d8d7c7789a", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import pandas as pd\n", | ||
"import numpy as np\n", | ||
"from sklearn.metrics import accuracy_score \n", | ||
"from sklearn.model_selection import train_test_split\n", | ||
"from sklearn.svm import SVC\n", | ||
"from sklearn.preprocessing import MinMaxScaler\n", | ||
"from sklearn import metrics\n", | ||
"from sklearn.metrics import classification_report,confusion_matrix,ConfusionMatrixDisplay\n", | ||
"from sklearn.model_selection import cross_val_score\n", | ||
"from sklearn.naive_bayes import GaussianNB" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"id": "d5649b12-bfdc-4013-a112-ee8928d0fb0b", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"df=pd.read_csv(\"Crop_recommendation.csv\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"id": "33a2b758-7ace-4970-9864-386fed83a88c", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"features = df[['N', 'P','K','temperature', 'humidity', 'ph', 'rainfall']]\n", | ||
"target = df['label']\n", | ||
"#features = df[['temperature', 'humidity', 'ph', 'rainfall']]\n", | ||
"labels = df['label']" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"id": "98bb3aee-2b27-4e84-934c-65426e1d6f0d", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"Xtrain, Xtest, Ytrain, Ytest = train_test_split(features,target,test_size = 0.3,random_state =2)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"id": "643cb2a4-22b8-4831-8a3b-b09924df8fe7", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"<style>#sk-container-id-1 {color: black;background-color: white;}#sk-container-id-1 pre{padding: 0;}#sk-container-id-1 div.sk-toggleable {background-color: white;}#sk-container-id-1 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-1 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-1 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-1 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-1 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-1 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-1 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-1 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-1 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-1 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-1 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-1 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-1 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-1 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-1 div.sk-item {position: relative;z-index: 1;}#sk-container-id-1 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-1 div.sk-item::before, #sk-container-id-1 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-1 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-1 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-1 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-1 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-1 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-1 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-1 div.sk-label-container {text-align: center;}#sk-container-id-1 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-1 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-1\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>GaussianNB()</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-1\" type=\"checkbox\" checked><label for=\"sk-estimator-id-1\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">GaussianNB</label><div class=\"sk-toggleable__content\"><pre>GaussianNB()</pre></div></div></div></div></div>" | ||
], | ||
"text/plain": [ | ||
"GaussianNB()" | ||
] | ||
}, | ||
"execution_count": 7, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"NaiveBayes=GaussianNB()\n", | ||
"NaiveBayes.fit(Xtrain,Ytrain)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 9, | ||
"id": "a977bbfd-30b2-4ac6-86a8-fbda16112a55", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"predicted_values=NaiveBayes.predict(Xtest)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 12, | ||
"id": "fbb637e3-cc3c-4479-8b0f-1077e88d0aae", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Naive Bayes's Accuracy is: 0.9924242424242424\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"x=metrics.accuracy_score(Ytest,predicted_values)\n", | ||
"print(\"Naive Bayes's Accuracy is: \",x)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 13, | ||
"id": "518a369d-785b-430e-8525-1b088590a9a6", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
" precision recall f1-score support\n", | ||
"\n", | ||
" apple 1.00 1.00 1.00 28\n", | ||
" banana 1.00 1.00 1.00 26\n", | ||
" blackgram 1.00 1.00 1.00 28\n", | ||
" chickpea 1.00 1.00 1.00 29\n", | ||
" coconut 1.00 1.00 1.00 31\n", | ||
" coffee 1.00 1.00 1.00 33\n", | ||
" cotton 1.00 1.00 1.00 31\n", | ||
" grapes 1.00 1.00 1.00 29\n", | ||
" jute 0.89 0.97 0.93 33\n", | ||
" kidneybeans 1.00 1.00 1.00 30\n", | ||
" lentil 1.00 1.00 1.00 32\n", | ||
" maize 1.00 1.00 1.00 32\n", | ||
" mango 1.00 1.00 1.00 33\n", | ||
" mothbeans 1.00 1.00 1.00 29\n", | ||
" mungbean 1.00 1.00 1.00 32\n", | ||
" muskmelon 1.00 1.00 1.00 30\n", | ||
" orange 1.00 1.00 1.00 42\n", | ||
" papaya 1.00 1.00 1.00 30\n", | ||
" pigeonpeas 1.00 1.00 1.00 31\n", | ||
" pomegranate 1.00 1.00 1.00 19\n", | ||
" rice 0.96 0.87 0.91 30\n", | ||
" watermelon 1.00 1.00 1.00 22\n", | ||
"\n", | ||
" accuracy 0.99 660\n", | ||
" macro avg 0.99 0.99 0.99 660\n", | ||
"weighted avg 0.99 0.99 0.99 660\n", | ||
"\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"print(classification_report(Ytest,predicted_values))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 14, | ||
"id": "341b0753-d59b-48cf-b53f-1907fd1733cf", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"array([0.99772727, 0.99545455, 0.99545455, 0.99545455, 0.99090909])" | ||
] | ||
}, | ||
"execution_count": 14, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"# Cross validation score (NaiveBayes)\n", | ||
"score = cross_val_score(NaiveBayes,features,target,cv=5)\n", | ||
"score" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 15, | ||
"id": "9ef017fb-6420-47e6-98b2-b5c17fd46134", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"0.9950000000000001\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"print(score.mean())" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 19, | ||
"id": "ff557e0b-96a4-45c6-9ef4-76c115018456", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Predicted Crop Label: jute\n", | ||
"Predicted Crop Label: mothbeans\n" | ||
] | ||
}, | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"C:\\Users\\kanch\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\sklearn\\base.py:439: UserWarning: X does not have valid feature names, but GaussianNB was fitted with feature names\n", | ||
" warnings.warn(\n", | ||
"C:\\Users\\kanch\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\sklearn\\base.py:439: UserWarning: X does not have valid feature names, but GaussianNB was fitted with feature names\n", | ||
" warnings.warn(\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"def predict_crop_label(input_features):\n", | ||
" \n", | ||
" \n", | ||
" input_features = np.array(input_features).reshape(1, -1) \n", | ||
" \n", | ||
" # Predict the crop label using the LogReg model\n", | ||
" predicted_label = NaiveBayes.predict(input_features)\n", | ||
" \n", | ||
" return predicted_label[0] # Return the predicted label\n", | ||
"\n", | ||
"# Example usage:\n", | ||
"user_input = [83, 45, 60, 28, 70.3, 7.0, 150.9] # Example input features (N, P, K, pH, temperature, humidity, rainfall)\n", | ||
"predicted_crop_label = predict_crop_label(user_input)\n", | ||
"print(\"Predicted Crop Label:\", predicted_crop_label)\n", | ||
"\n", | ||
"user_input = [83, 45, 60, 28, 3.3, 2.0, 0.9] # Example input features (N, P, K, pH, temperature, humidity, rainfall)\n", | ||
"predicted_crop_label = predict_crop_label(user_input)\n", | ||
"print(\"Predicted Crop Label:\", predicted_crop_label)\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 20, | ||
"id": "97cfcbf6-4635-4b5d-9131-dcf9c2e2ec2a", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"nb_train_accuracy = NaiveBayes.score(Xtrain, Ytrain)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 21, | ||
"id": "8ec62797-daba-4365-b225-d83e6f5a37cc", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"0.9961038961038962\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"print(nb_train_accuracy)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "72348713-8497-406b-99bb-258a458c3949", | ||
"metadata": {}, | ||
"source": [ | ||
"### Training Accuracy: 0.9961\n", | ||
"### Cross-Validation Accuracy: 0.9950\n", | ||
"### Test Accuracy: 0.9924" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "2d16a23f-0b9b-42ae-bb72-8f4cecd70a74", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "3f13635b-5f36-4bbf-842b-1957cf7dc028", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.11.3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
Oops, something went wrong.