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name": "stderr",
19
+ "output_type": "stream",
20
+ "text": [
21
+ "C:\\Users\\Noor Saeed\\anaconda3\\lib\\site-packages\\pandas\\core\\computation\\expressions.py:21: UserWarning: Pandas requires version '2.8.4' or newer of 'numexpr' (version '2.8.1' currently installed).\n",
22
+ " from pandas.core.computation.check import NUMEXPR_INSTALLED\n",
23
+ "C:\\Users\\Noor Saeed\\anaconda3\\lib\\site-packages\\pandas\\core\\arrays\\masked.py:60: UserWarning: Pandas requires version '1.3.6' or newer of 'bottleneck' (version '1.3.4' currently installed).\n",
24
+ " from pandas.core import (\n"
25
+ ]
26
+ }
27
+ ],
28
+ "source": [
29
+ "import numpy as np\n",
30
+ "import pandas as pd"
31
+ ]
32
+ },
33
+ {
34
+ "cell_type": "markdown",
35
+ "id": "2822305c",
36
+ "metadata": {},
37
+ "source": [
38
+ "# Importing Data"
39
+ ]
40
+ },
41
+ {
42
+ "cell_type": "code",
43
+ "execution_count": 2,
44
+ "id": "5b6f8884",
45
+ "metadata": {},
46
+ "outputs": [
47
+ {
48
+ "data": {
49
+ "text/html": [
50
+ "<div>\n",
51
+ "<style scoped>\n",
52
+ " .dataframe tbody tr th:only-of-type {\n",
53
+ " vertical-align: middle;\n",
54
+ " }\n",
55
+ "\n",
56
+ " .dataframe tbody tr th {\n",
57
+ " vertical-align: top;\n",
58
+ " }\n",
59
+ "\n",
60
+ " .dataframe thead th {\n",
61
+ " text-align: right;\n",
62
+ " }\n",
63
+ "</style>\n",
64
+ "<table border=\"1\" class=\"dataframe\">\n",
65
+ " <thead>\n",
66
+ " <tr style=\"text-align: right;\">\n",
67
+ " <th></th>\n",
68
+ " <th>N</th>\n",
69
+ " <th>P</th>\n",
70
+ " <th>K</th>\n",
71
+ " <th>temperature</th>\n",
72
+ " <th>humidity</th>\n",
73
+ " <th>ph</th>\n",
74
+ " <th>rainfall</th>\n",
75
+ " <th>label</th>\n",
76
+ " </tr>\n",
77
+ " </thead>\n",
78
+ " <tbody>\n",
79
+ " <tr>\n",
80
+ " <th>0</th>\n",
81
+ " <td>90</td>\n",
82
+ " <td>42</td>\n",
83
+ " <td>43</td>\n",
84
+ " <td>20.879744</td>\n",
85
+ " <td>82.002744</td>\n",
86
+ " <td>6.502985</td>\n",
87
+ " <td>202.935536</td>\n",
88
+ " <td>rice</td>\n",
89
+ " </tr>\n",
90
+ " <tr>\n",
91
+ " <th>1</th>\n",
92
+ " <td>85</td>\n",
93
+ " <td>58</td>\n",
94
+ " <td>41</td>\n",
95
+ " <td>21.770462</td>\n",
96
+ " <td>80.319644</td>\n",
97
+ " <td>7.038096</td>\n",
98
+ " <td>226.655537</td>\n",
99
+ " <td>rice</td>\n",
100
+ " </tr>\n",
101
+ " <tr>\n",
102
+ " <th>2</th>\n",
103
+ " <td>60</td>\n",
104
+ " <td>55</td>\n",
105
+ " <td>44</td>\n",
106
+ " <td>23.004459</td>\n",
107
+ " <td>82.320763</td>\n",
108
+ " <td>7.840207</td>\n",
109
+ " <td>263.964248</td>\n",
110
+ " <td>rice</td>\n",
111
+ " </tr>\n",
112
+ " <tr>\n",
113
+ " <th>3</th>\n",
114
+ " <td>74</td>\n",
115
+ " <td>35</td>\n",
116
+ " <td>40</td>\n",
117
+ " <td>26.491096</td>\n",
118
+ " <td>80.158363</td>\n",
119
+ " <td>6.980401</td>\n",
120
+ " <td>242.864034</td>\n",
121
+ " <td>rice</td>\n",
122
+ " </tr>\n",
123
+ " <tr>\n",
124
+ " <th>4</th>\n",
125
+ " <td>78</td>\n",
126
+ " <td>42</td>\n",
127
+ " <td>42</td>\n",
128
+ " <td>20.130175</td>\n",
129
+ " <td>81.604873</td>\n",
130
+ " <td>7.628473</td>\n",
131
+ " <td>262.717340</td>\n",
132
+ " <td>rice</td>\n",
133
+ " </tr>\n",
134
+ " </tbody>\n",
135
+ "</table>\n",
136
+ "</div>"
137
+ ],
138
+ "text/plain": [
139
+ " N P K temperature humidity ph rainfall label\n",
140
+ "0 90 42 43 20.879744 82.002744 6.502985 202.935536 rice\n",
141
+ "1 85 58 41 21.770462 80.319644 7.038096 226.655537 rice\n",
142
+ "2 60 55 44 23.004459 82.320763 7.840207 263.964248 rice\n",
143
+ "3 74 35 40 26.491096 80.158363 6.980401 242.864034 rice\n",
144
+ "4 78 42 42 20.130175 81.604873 7.628473 262.717340 rice"
145
+ ]
146
+ },
147
+ "execution_count": 2,
148
+ "metadata": {},
149
+ "output_type": "execute_result"
150
+ }
151
+ ],
152
+ "source": [
153
+ "crop = pd.read_csv(\"Crop_recommendation.csv\")\n",
154
+ "crop.head()"
155
+ ]
156
+ },
157
+ {
158
+ "cell_type": "markdown",
159
+ "id": "e9ddfb22",
160
+ "metadata": {},
161
+ "source": [
162
+ "# Asq Six Question to yourself"
163
+ ]
164
+ },
165
+ {
166
+ "cell_type": "code",
167
+ "execution_count": 3,
168
+ "id": "3ca70c00",
169
+ "metadata": {},
170
+ "outputs": [
171
+ {
172
+ "data": {
173
+ "text/plain": [
174
+ "(2200, 8)"
175
+ ]
176
+ },
177
+ "execution_count": 3,
178
+ "metadata": {},
179
+ "output_type": "execute_result"
180
+ }
181
+ ],
182
+ "source": [
183
+ "crop.shape"
184
+ ]
185
+ },
186
+ {
187
+ "cell_type": "code",
188
+ "execution_count": 4,
189
+ "id": "e2ae9b60",
190
+ "metadata": {},
191
+ "outputs": [
192
+ {
193
+ "name": "stdout",
194
+ "output_type": "stream",
195
+ "text": [
196
+ "<class 'pandas.core.frame.DataFrame'>\n",
197
+ "RangeIndex: 2200 entries, 0 to 2199\n",
198
+ "Data columns (total 8 columns):\n",
199
+ " # Column Non-Null Count Dtype \n",
200
+ "--- ------ -------------- ----- \n",
201
+ " 0 N 2200 non-null int64 \n",
202
+ " 1 P 2200 non-null int64 \n",
203
+ " 2 K 2200 non-null int64 \n",
204
+ " 3 temperature 2200 non-null float64\n",
205
+ " 4 humidity 2200 non-null float64\n",
206
+ " 5 ph 2200 non-null float64\n",
207
+ " 6 rainfall 2200 non-null float64\n",
208
+ " 7 label 2200 non-null object \n",
209
+ "dtypes: float64(4), int64(3), object(1)\n",
210
+ "memory usage: 137.6+ KB\n"
211
+ ]
212
+ }
213
+ ],
214
+ "source": [
215
+ "crop.info()"
216
+ ]
217
+ },
218
+ {
219
+ "cell_type": "code",
220
+ "execution_count": 5,
221
+ "id": "9efad4c4",
222
+ "metadata": {},
223
+ "outputs": [
224
+ {
225
+ "data": {
226
+ "text/plain": [
227
+ "N 0\n",
228
+ "P 0\n",
229
+ "K 0\n",
230
+ "temperature 0\n",
231
+ "humidity 0\n",
232
+ "ph 0\n",
233
+ "rainfall 0\n",
234
+ "label 0\n",
235
+ "dtype: int64"
236
+ ]
237
+ },
238
+ "execution_count": 5,
239
+ "metadata": {},
240
+ "output_type": "execute_result"
241
+ }
242
+ ],
243
+ "source": [
244
+ "crop.isnull().sum()"
245
+ ]
246
+ },
247
+ {
248
+ "cell_type": "code",
249
+ "execution_count": 6,
250
+ "id": "1f7bf8c5",
251
+ "metadata": {},
252
+ "outputs": [
253
+ {
254
+ "data": {
255
+ "text/plain": [
256
+ "0"
257
+ ]
258
+ },
259
+ "execution_count": 6,
260
+ "metadata": {},
261
+ "output_type": "execute_result"
262
+ }
263
+ ],
264
+ "source": [
265
+ "crop.duplicated().sum()"
266
+ ]
267
+ },
268
+ {
269
+ "cell_type": "code",
270
+ "execution_count": 7,
271
+ "id": "3d5b7413",
272
+ "metadata": {
273
+ "scrolled": false
274
+ },
275
+ "outputs": [
276
+ {
277
+ "data": {
278
+ "text/html": [
279
+ "<div>\n",
280
+ "<style scoped>\n",
281
+ " .dataframe tbody tr th:only-of-type {\n",
282
+ " vertical-align: middle;\n",
283
+ " }\n",
284
+ "\n",
285
+ " .dataframe tbody tr th {\n",
286
+ " vertical-align: top;\n",
287
+ " }\n",
288
+ "\n",
289
+ " .dataframe thead th {\n",
290
+ " text-align: right;\n",
291
+ " }\n",
292
+ "</style>\n",
293
+ "<table border=\"1\" class=\"dataframe\">\n",
294
+ " <thead>\n",
295
+ " <tr style=\"text-align: right;\">\n",
296
+ " <th></th>\n",
297
+ " <th>N</th>\n",
298
+ " <th>P</th>\n",
299
+ " <th>K</th>\n",
300
+ " <th>temperature</th>\n",
301
+ " <th>humidity</th>\n",
302
+ " <th>ph</th>\n",
303
+ " <th>rainfall</th>\n",
304
+ " </tr>\n",
305
+ " </thead>\n",
306
+ " <tbody>\n",
307
+ " <tr>\n",
308
+ " <th>count</th>\n",
309
+ " <td>2200.000000</td>\n",
310
+ " <td>2200.000000</td>\n",
311
+ " <td>2200.000000</td>\n",
312
+ " <td>2200.000000</td>\n",
313
+ " <td>2200.000000</td>\n",
314
+ " <td>2200.000000</td>\n",
315
+ " <td>2200.000000</td>\n",
316
+ " </tr>\n",
317
+ " <tr>\n",
318
+ " <th>mean</th>\n",
319
+ " <td>50.551818</td>\n",
320
+ " <td>53.362727</td>\n",
321
+ " <td>48.149091</td>\n",
322
+ " <td>25.616244</td>\n",
323
+ " <td>71.481779</td>\n",
324
+ " <td>6.469480</td>\n",
325
+ " <td>103.463655</td>\n",
326
+ " </tr>\n",
327
+ " <tr>\n",
328
+ " <th>std</th>\n",
329
+ " <td>36.917334</td>\n",
330
+ " <td>32.985883</td>\n",
331
+ " <td>50.647931</td>\n",
332
+ " <td>5.063749</td>\n",
333
+ " <td>22.263812</td>\n",
334
+ " <td>0.773938</td>\n",
335
+ " <td>54.958389</td>\n",
336
+ " </tr>\n",
337
+ " <tr>\n",
338
+ " <th>min</th>\n",
339
+ " <td>0.000000</td>\n",
340
+ " <td>5.000000</td>\n",
341
+ " <td>5.000000</td>\n",
342
+ " <td>8.825675</td>\n",
343
+ " <td>14.258040</td>\n",
344
+ " <td>3.504752</td>\n",
345
+ " <td>20.211267</td>\n",
346
+ " </tr>\n",
347
+ " <tr>\n",
348
+ " <th>25%</th>\n",
349
+ " <td>21.000000</td>\n",
350
+ " <td>28.000000</td>\n",
351
+ " <td>20.000000</td>\n",
352
+ " <td>22.769375</td>\n",
353
+ " <td>60.261953</td>\n",
354
+ " <td>5.971693</td>\n",
355
+ " <td>64.551686</td>\n",
356
+ " </tr>\n",
357
+ " <tr>\n",
358
+ " <th>50%</th>\n",
359
+ " <td>37.000000</td>\n",
360
+ " <td>51.000000</td>\n",
361
+ " <td>32.000000</td>\n",
362
+ " <td>25.598693</td>\n",
363
+ " <td>80.473146</td>\n",
364
+ " <td>6.425045</td>\n",
365
+ " <td>94.867624</td>\n",
366
+ " </tr>\n",
367
+ " <tr>\n",
368
+ " <th>75%</th>\n",
369
+ " <td>84.250000</td>\n",
370
+ " <td>68.000000</td>\n",
371
+ " <td>49.000000</td>\n",
372
+ " <td>28.561654</td>\n",
373
+ " <td>89.948771</td>\n",
374
+ " <td>6.923643</td>\n",
375
+ " <td>124.267508</td>\n",
376
+ " </tr>\n",
377
+ " <tr>\n",
378
+ " <th>max</th>\n",
379
+ " <td>140.000000</td>\n",
380
+ " <td>145.000000</td>\n",
381
+ " <td>205.000000</td>\n",
382
+ " <td>43.675493</td>\n",
383
+ " <td>99.981876</td>\n",
384
+ " <td>9.935091</td>\n",
385
+ " <td>298.560117</td>\n",
386
+ " </tr>\n",
387
+ " </tbody>\n",
388
+ "</table>\n",
389
+ "</div>"
390
+ ],
391
+ "text/plain": [
392
+ " N P K temperature humidity \\\n",
393
+ "count 2200.000000 2200.000000 2200.000000 2200.000000 2200.000000 \n",
394
+ "mean 50.551818 53.362727 48.149091 25.616244 71.481779 \n",
395
+ "std 36.917334 32.985883 50.647931 5.063749 22.263812 \n",
396
+ "min 0.000000 5.000000 5.000000 8.825675 14.258040 \n",
397
+ "25% 21.000000 28.000000 20.000000 22.769375 60.261953 \n",
398
+ "50% 37.000000 51.000000 32.000000 25.598693 80.473146 \n",
399
+ "75% 84.250000 68.000000 49.000000 28.561654 89.948771 \n",
400
+ "max 140.000000 145.000000 205.000000 43.675493 99.981876 \n",
401
+ "\n",
402
+ " ph rainfall \n",
403
+ "count 2200.000000 2200.000000 \n",
404
+ "mean 6.469480 103.463655 \n",
405
+ "std 0.773938 54.958389 \n",
406
+ "min 3.504752 20.211267 \n",
407
+ "25% 5.971693 64.551686 \n",
408
+ "50% 6.425045 94.867624 \n",
409
+ "75% 6.923643 124.267508 \n",
410
+ "max 9.935091 298.560117 "
411
+ ]
412
+ },
413
+ "execution_count": 7,
414
+ "metadata": {},
415
+ "output_type": "execute_result"
416
+ }
417
+ ],
418
+ "source": [
419
+ "crop.describe()"
420
+ ]
421
+ },
422
+ {
423
+ "cell_type": "code",
424
+ "execution_count": 20,
425
+ "id": "1056bfba",
426
+ "metadata": {},
427
+ "outputs": [
428
+ {
429
+ "data": {
430
+ "text/plain": [
431
+ "label\n",
432
+ "rice 100\n",
433
+ "maize 100\n",
434
+ "jute 100\n",
435
+ "cotton 100\n",
436
+ "coconut 100\n",
437
+ "papaya 100\n",
438
+ "orange 100\n",
439
+ "apple 100\n",
440
+ "muskmelon 100\n",
441
+ "watermelon 100\n",
442
+ "grapes 100\n",
443
+ "mango 100\n",
444
+ "banana 100\n",
445
+ "pomegranate 100\n",
446
+ "lentil 100\n",
447
+ "blackgram 100\n",
448
+ "mungbean 100\n",
449
+ "mothbeans 100\n",
450
+ "pigeonpeas 100\n",
451
+ "kidneybeans 100\n",
452
+ "chickpea 100\n",
453
+ "coffee 100\n",
454
+ "Name: count, dtype: int64"
455
+ ]
456
+ },
457
+ "execution_count": 20,
458
+ "metadata": {},
459
+ "output_type": "execute_result"
460
+ }
461
+ ],
462
+ "source": [
463
+ "crop['label'].value_counts()"
464
+ ]
465
+ },
466
+ {
467
+ "cell_type": "markdown",
468
+ "id": "3e3af150",
469
+ "metadata": {},
470
+ "source": [
471
+ "# Encoding"
472
+ ]
473
+ },
474
+ {
475
+ "cell_type": "code",
476
+ "execution_count": 24,
477
+ "id": "8c35d395",
478
+ "metadata": {},
479
+ "outputs": [],
480
+ "source": [
481
+ "crop_dict = {\n",
482
+ " 'rice': 1,\n",
483
+ " 'maize': 2,\n",
484
+ " 'jute': 3,\n",
485
+ " 'cotton': 4,\n",
486
+ " 'coconut': 5,\n",
487
+ " 'papaya': 6,\n",
488
+ " 'orange': 7,\n",
489
+ " 'apple': 8,\n",
490
+ " 'muskmelon': 9,\n",
491
+ " 'watermelon': 10,\n",
492
+ " 'grapes': 11,\n",
493
+ " 'mango': 12,\n",
494
+ " 'banana': 13,\n",
495
+ " 'pomegranate': 14,\n",
496
+ " 'lentil': 15,\n",
497
+ " 'blackgram': 16,\n",
498
+ " 'mungbean': 17,\n",
499
+ " 'mothbeans': 18,\n",
500
+ " 'pigeonpeas': 19,\n",
501
+ " 'kidneybeans': 20,\n",
502
+ " 'chickpea': 21,\n",
503
+ " 'coffee': 22\n",
504
+ "}\n",
505
+ "crop['crop_num']= crop['label'].map(crop_dict)"
506
+ ]
507
+ },
508
+ {
509
+ "cell_type": "code",
510
+ "execution_count": 25,
511
+ "id": "b1a53f7f",
512
+ "metadata": {},
513
+ "outputs": [
514
+ {
515
+ "data": {
516
+ "text/html": [
517
+ "<div>\n",
518
+ "<style scoped>\n",
519
+ " .dataframe tbody tr th:only-of-type {\n",
520
+ " vertical-align: middle;\n",
521
+ " }\n",
522
+ "\n",
523
+ " .dataframe tbody tr th {\n",
524
+ " vertical-align: top;\n",
525
+ " }\n",
526
+ "\n",
527
+ " .dataframe thead th {\n",
528
+ " text-align: right;\n",
529
+ " }\n",
530
+ "</style>\n",
531
+ "<table border=\"1\" class=\"dataframe\">\n",
532
+ " <thead>\n",
533
+ " <tr style=\"text-align: right;\">\n",
534
+ " <th></th>\n",
535
+ " <th>N</th>\n",
536
+ " <th>P</th>\n",
537
+ " <th>K</th>\n",
538
+ " <th>temperature</th>\n",
539
+ " <th>humidity</th>\n",
540
+ " <th>ph</th>\n",
541
+ " <th>rainfall</th>\n",
542
+ " <th>label</th>\n",
543
+ " <th>crop_num</th>\n",
544
+ " </tr>\n",
545
+ " </thead>\n",
546
+ " <tbody>\n",
547
+ " <tr>\n",
548
+ " <th>0</th>\n",
549
+ " <td>90</td>\n",
550
+ " <td>42</td>\n",
551
+ " <td>43</td>\n",
552
+ " <td>20.879744</td>\n",
553
+ " <td>82.002744</td>\n",
554
+ " <td>6.502985</td>\n",
555
+ " <td>202.935536</td>\n",
556
+ " <td>rice</td>\n",
557
+ " <td>1</td>\n",
558
+ " </tr>\n",
559
+ " <tr>\n",
560
+ " <th>1</th>\n",
561
+ " <td>85</td>\n",
562
+ " <td>58</td>\n",
563
+ " <td>41</td>\n",
564
+ " <td>21.770462</td>\n",
565
+ " <td>80.319644</td>\n",
566
+ " <td>7.038096</td>\n",
567
+ " <td>226.655537</td>\n",
568
+ " <td>rice</td>\n",
569
+ " <td>1</td>\n",
570
+ " </tr>\n",
571
+ " <tr>\n",
572
+ " <th>2</th>\n",
573
+ " <td>60</td>\n",
574
+ " <td>55</td>\n",
575
+ " <td>44</td>\n",
576
+ " <td>23.004459</td>\n",
577
+ " <td>82.320763</td>\n",
578
+ " <td>7.840207</td>\n",
579
+ " <td>263.964248</td>\n",
580
+ " <td>rice</td>\n",
581
+ " <td>1</td>\n",
582
+ " </tr>\n",
583
+ " <tr>\n",
584
+ " <th>3</th>\n",
585
+ " <td>74</td>\n",
586
+ " <td>35</td>\n",
587
+ " <td>40</td>\n",
588
+ " <td>26.491096</td>\n",
589
+ " <td>80.158363</td>\n",
590
+ " <td>6.980401</td>\n",
591
+ " <td>242.864034</td>\n",
592
+ " <td>rice</td>\n",
593
+ " <td>1</td>\n",
594
+ " </tr>\n",
595
+ " <tr>\n",
596
+ " <th>4</th>\n",
597
+ " <td>78</td>\n",
598
+ " <td>42</td>\n",
599
+ " <td>42</td>\n",
600
+ " <td>20.130175</td>\n",
601
+ " <td>81.604873</td>\n",
602
+ " <td>7.628473</td>\n",
603
+ " <td>262.717340</td>\n",
604
+ " <td>rice</td>\n",
605
+ " <td>1</td>\n",
606
+ " </tr>\n",
607
+ " <tr>\n",
608
+ " <th>...</th>\n",
609
+ " <td>...</td>\n",
610
+ " <td>...</td>\n",
611
+ " <td>...</td>\n",
612
+ " <td>...</td>\n",
613
+ " <td>...</td>\n",
614
+ " <td>...</td>\n",
615
+ " <td>...</td>\n",
616
+ " <td>...</td>\n",
617
+ " <td>...</td>\n",
618
+ " </tr>\n",
619
+ " <tr>\n",
620
+ " <th>2195</th>\n",
621
+ " <td>107</td>\n",
622
+ " <td>34</td>\n",
623
+ " <td>32</td>\n",
624
+ " <td>26.774637</td>\n",
625
+ " <td>66.413269</td>\n",
626
+ " <td>6.780064</td>\n",
627
+ " <td>177.774507</td>\n",
628
+ " <td>coffee</td>\n",
629
+ " <td>22</td>\n",
630
+ " </tr>\n",
631
+ " <tr>\n",
632
+ " <th>2196</th>\n",
633
+ " <td>99</td>\n",
634
+ " <td>15</td>\n",
635
+ " <td>27</td>\n",
636
+ " <td>27.417112</td>\n",
637
+ " <td>56.636362</td>\n",
638
+ " <td>6.086922</td>\n",
639
+ " <td>127.924610</td>\n",
640
+ " <td>coffee</td>\n",
641
+ " <td>22</td>\n",
642
+ " </tr>\n",
643
+ " <tr>\n",
644
+ " <th>2197</th>\n",
645
+ " <td>118</td>\n",
646
+ " <td>33</td>\n",
647
+ " <td>30</td>\n",
648
+ " <td>24.131797</td>\n",
649
+ " <td>67.225123</td>\n",
650
+ " <td>6.362608</td>\n",
651
+ " <td>173.322839</td>\n",
652
+ " <td>coffee</td>\n",
653
+ " <td>22</td>\n",
654
+ " </tr>\n",
655
+ " <tr>\n",
656
+ " <th>2198</th>\n",
657
+ " <td>117</td>\n",
658
+ " <td>32</td>\n",
659
+ " <td>34</td>\n",
660
+ " <td>26.272418</td>\n",
661
+ " <td>52.127394</td>\n",
662
+ " <td>6.758793</td>\n",
663
+ " <td>127.175293</td>\n",
664
+ " <td>coffee</td>\n",
665
+ " <td>22</td>\n",
666
+ " </tr>\n",
667
+ " <tr>\n",
668
+ " <th>2199</th>\n",
669
+ " <td>104</td>\n",
670
+ " <td>18</td>\n",
671
+ " <td>30</td>\n",
672
+ " <td>23.603016</td>\n",
673
+ " <td>60.396475</td>\n",
674
+ " <td>6.779833</td>\n",
675
+ " <td>140.937041</td>\n",
676
+ " <td>coffee</td>\n",
677
+ " <td>22</td>\n",
678
+ " </tr>\n",
679
+ " </tbody>\n",
680
+ "</table>\n",
681
+ "<p>2200 rows × 9 columns</p>\n",
682
+ "</div>"
683
+ ],
684
+ "text/plain": [
685
+ " N P K temperature humidity ph rainfall label \\\n",
686
+ "0 90 42 43 20.879744 82.002744 6.502985 202.935536 rice \n",
687
+ "1 85 58 41 21.770462 80.319644 7.038096 226.655537 rice \n",
688
+ "2 60 55 44 23.004459 82.320763 7.840207 263.964248 rice \n",
689
+ "3 74 35 40 26.491096 80.158363 6.980401 242.864034 rice \n",
690
+ "4 78 42 42 20.130175 81.604873 7.628473 262.717340 rice \n",
691
+ "... ... .. .. ... ... ... ... ... \n",
692
+ "2195 107 34 32 26.774637 66.413269 6.780064 177.774507 coffee \n",
693
+ "2196 99 15 27 27.417112 56.636362 6.086922 127.924610 coffee \n",
694
+ "2197 118 33 30 24.131797 67.225123 6.362608 173.322839 coffee \n",
695
+ "2198 117 32 34 26.272418 52.127394 6.758793 127.175293 coffee \n",
696
+ "2199 104 18 30 23.603016 60.396475 6.779833 140.937041 coffee \n",
697
+ "\n",
698
+ " crop_num \n",
699
+ "0 1 \n",
700
+ "1 1 \n",
701
+ "2 1 \n",
702
+ "3 1 \n",
703
+ "4 1 \n",
704
+ "... ... \n",
705
+ "2195 22 \n",
706
+ "2196 22 \n",
707
+ "2197 22 \n",
708
+ "2198 22 \n",
709
+ "2199 22 \n",
710
+ "\n",
711
+ "[2200 rows x 9 columns]"
712
+ ]
713
+ },
714
+ "execution_count": 25,
715
+ "metadata": {},
716
+ "output_type": "execute_result"
717
+ }
718
+ ],
719
+ "source": [
720
+ "crop"
721
+ ]
722
+ },
723
+ {
724
+ "cell_type": "code",
725
+ "execution_count": 26,
726
+ "id": "dff5caca",
727
+ "metadata": {},
728
+ "outputs": [
729
+ {
730
+ "data": {
731
+ "text/html": [
732
+ "<div>\n",
733
+ "<style scoped>\n",
734
+ " .dataframe tbody tr th:only-of-type {\n",
735
+ " vertical-align: middle;\n",
736
+ " }\n",
737
+ "\n",
738
+ " .dataframe tbody tr th {\n",
739
+ " vertical-align: top;\n",
740
+ " }\n",
741
+ "\n",
742
+ " .dataframe thead th {\n",
743
+ " text-align: right;\n",
744
+ " }\n",
745
+ "</style>\n",
746
+ "<table border=\"1\" class=\"dataframe\">\n",
747
+ " <thead>\n",
748
+ " <tr style=\"text-align: right;\">\n",
749
+ " <th></th>\n",
750
+ " <th>N</th>\n",
751
+ " <th>P</th>\n",
752
+ " <th>K</th>\n",
753
+ " <th>temperature</th>\n",
754
+ " <th>humidity</th>\n",
755
+ " <th>ph</th>\n",
756
+ " <th>rainfall</th>\n",
757
+ " <th>crop_num</th>\n",
758
+ " </tr>\n",
759
+ " </thead>\n",
760
+ " <tbody>\n",
761
+ " <tr>\n",
762
+ " <th>0</th>\n",
763
+ " <td>90</td>\n",
764
+ " <td>42</td>\n",
765
+ " <td>43</td>\n",
766
+ " <td>20.879744</td>\n",
767
+ " <td>82.002744</td>\n",
768
+ " <td>6.502985</td>\n",
769
+ " <td>202.935536</td>\n",
770
+ " <td>1</td>\n",
771
+ " </tr>\n",
772
+ " <tr>\n",
773
+ " <th>1</th>\n",
774
+ " <td>85</td>\n",
775
+ " <td>58</td>\n",
776
+ " <td>41</td>\n",
777
+ " <td>21.770462</td>\n",
778
+ " <td>80.319644</td>\n",
779
+ " <td>7.038096</td>\n",
780
+ " <td>226.655537</td>\n",
781
+ " <td>1</td>\n",
782
+ " </tr>\n",
783
+ " <tr>\n",
784
+ " <th>2</th>\n",
785
+ " <td>60</td>\n",
786
+ " <td>55</td>\n",
787
+ " <td>44</td>\n",
788
+ " <td>23.004459</td>\n",
789
+ " <td>82.320763</td>\n",
790
+ " <td>7.840207</td>\n",
791
+ " <td>263.964248</td>\n",
792
+ " <td>1</td>\n",
793
+ " </tr>\n",
794
+ " <tr>\n",
795
+ " <th>3</th>\n",
796
+ " <td>74</td>\n",
797
+ " <td>35</td>\n",
798
+ " <td>40</td>\n",
799
+ " <td>26.491096</td>\n",
800
+ " <td>80.158363</td>\n",
801
+ " <td>6.980401</td>\n",
802
+ " <td>242.864034</td>\n",
803
+ " <td>1</td>\n",
804
+ " </tr>\n",
805
+ " <tr>\n",
806
+ " <th>4</th>\n",
807
+ " <td>78</td>\n",
808
+ " <td>42</td>\n",
809
+ " <td>42</td>\n",
810
+ " <td>20.130175</td>\n",
811
+ " <td>81.604873</td>\n",
812
+ " <td>7.628473</td>\n",
813
+ " <td>262.717340</td>\n",
814
+ " <td>1</td>\n",
815
+ " </tr>\n",
816
+ " </tbody>\n",
817
+ "</table>\n",
818
+ "</div>"
819
+ ],
820
+ "text/plain": [
821
+ " N P K temperature humidity ph rainfall crop_num\n",
822
+ "0 90 42 43 20.879744 82.002744 6.502985 202.935536 1\n",
823
+ "1 85 58 41 21.770462 80.319644 7.038096 226.655537 1\n",
824
+ "2 60 55 44 23.004459 82.320763 7.840207 263.964248 1\n",
825
+ "3 74 35 40 26.491096 80.158363 6.980401 242.864034 1\n",
826
+ "4 78 42 42 20.130175 81.604873 7.628473 262.717340 1"
827
+ ]
828
+ },
829
+ "execution_count": 26,
830
+ "metadata": {},
831
+ "output_type": "execute_result"
832
+ }
833
+ ],
834
+ "source": [
835
+ "crop.drop(['label'],axis=1,inplace=True)\n",
836
+ "crop.head()"
837
+ ]
838
+ },
839
+ {
840
+ "cell_type": "markdown",
841
+ "id": "a5494675",
842
+ "metadata": {},
843
+ "source": [
844
+ "# Train Test Split"
845
+ ]
846
+ },
847
+ {
848
+ "cell_type": "code",
849
+ "execution_count": 27,
850
+ "id": "5a049f55",
851
+ "metadata": {},
852
+ "outputs": [],
853
+ "source": [
854
+ "X = crop.drop(['crop_num'],axis=1)\n",
855
+ "y = crop['crop_num']"
856
+ ]
857
+ },
858
+ {
859
+ "cell_type": "code",
860
+ "execution_count": 28,
861
+ "id": "9d223a69",
862
+ "metadata": {},
863
+ "outputs": [
864
+ {
865
+ "data": {
866
+ "text/html": [
867
+ "<div>\n",
868
+ "<style scoped>\n",
869
+ " .dataframe tbody tr th:only-of-type {\n",
870
+ " vertical-align: middle;\n",
871
+ " }\n",
872
+ "\n",
873
+ " .dataframe tbody tr th {\n",
874
+ " vertical-align: top;\n",
875
+ " }\n",
876
+ "\n",
877
+ " .dataframe thead th {\n",
878
+ " text-align: right;\n",
879
+ " }\n",
880
+ "</style>\n",
881
+ "<table border=\"1\" class=\"dataframe\">\n",
882
+ " <thead>\n",
883
+ " <tr style=\"text-align: right;\">\n",
884
+ " <th></th>\n",
885
+ " <th>N</th>\n",
886
+ " <th>P</th>\n",
887
+ " <th>K</th>\n",
888
+ " <th>temperature</th>\n",
889
+ " <th>humidity</th>\n",
890
+ " <th>ph</th>\n",
891
+ " <th>rainfall</th>\n",
892
+ " </tr>\n",
893
+ " </thead>\n",
894
+ " <tbody>\n",
895
+ " <tr>\n",
896
+ " <th>0</th>\n",
897
+ " <td>90</td>\n",
898
+ " <td>42</td>\n",
899
+ " <td>43</td>\n",
900
+ " <td>20.879744</td>\n",
901
+ " <td>82.002744</td>\n",
902
+ " <td>6.502985</td>\n",
903
+ " <td>202.935536</td>\n",
904
+ " </tr>\n",
905
+ " <tr>\n",
906
+ " <th>1</th>\n",
907
+ " <td>85</td>\n",
908
+ " <td>58</td>\n",
909
+ " <td>41</td>\n",
910
+ " <td>21.770462</td>\n",
911
+ " <td>80.319644</td>\n",
912
+ " <td>7.038096</td>\n",
913
+ " <td>226.655537</td>\n",
914
+ " </tr>\n",
915
+ " <tr>\n",
916
+ " <th>2</th>\n",
917
+ " <td>60</td>\n",
918
+ " <td>55</td>\n",
919
+ " <td>44</td>\n",
920
+ " <td>23.004459</td>\n",
921
+ " <td>82.320763</td>\n",
922
+ " <td>7.840207</td>\n",
923
+ " <td>263.964248</td>\n",
924
+ " </tr>\n",
925
+ " <tr>\n",
926
+ " <th>3</th>\n",
927
+ " <td>74</td>\n",
928
+ " <td>35</td>\n",
929
+ " <td>40</td>\n",
930
+ " <td>26.491096</td>\n",
931
+ " <td>80.158363</td>\n",
932
+ " <td>6.980401</td>\n",
933
+ " <td>242.864034</td>\n",
934
+ " </tr>\n",
935
+ " <tr>\n",
936
+ " <th>4</th>\n",
937
+ " <td>78</td>\n",
938
+ " <td>42</td>\n",
939
+ " <td>42</td>\n",
940
+ " <td>20.130175</td>\n",
941
+ " <td>81.604873</td>\n",
942
+ " <td>7.628473</td>\n",
943
+ " <td>262.717340</td>\n",
944
+ " </tr>\n",
945
+ " <tr>\n",
946
+ " <th>...</th>\n",
947
+ " <td>...</td>\n",
948
+ " <td>...</td>\n",
949
+ " <td>...</td>\n",
950
+ " <td>...</td>\n",
951
+ " <td>...</td>\n",
952
+ " <td>...</td>\n",
953
+ " <td>...</td>\n",
954
+ " </tr>\n",
955
+ " <tr>\n",
956
+ " <th>2195</th>\n",
957
+ " <td>107</td>\n",
958
+ " <td>34</td>\n",
959
+ " <td>32</td>\n",
960
+ " <td>26.774637</td>\n",
961
+ " <td>66.413269</td>\n",
962
+ " <td>6.780064</td>\n",
963
+ " <td>177.774507</td>\n",
964
+ " </tr>\n",
965
+ " <tr>\n",
966
+ " <th>2196</th>\n",
967
+ " <td>99</td>\n",
968
+ " <td>15</td>\n",
969
+ " <td>27</td>\n",
970
+ " <td>27.417112</td>\n",
971
+ " <td>56.636362</td>\n",
972
+ " <td>6.086922</td>\n",
973
+ " <td>127.924610</td>\n",
974
+ " </tr>\n",
975
+ " <tr>\n",
976
+ " <th>2197</th>\n",
977
+ " <td>118</td>\n",
978
+ " <td>33</td>\n",
979
+ " <td>30</td>\n",
980
+ " <td>24.131797</td>\n",
981
+ " <td>67.225123</td>\n",
982
+ " <td>6.362608</td>\n",
983
+ " <td>173.322839</td>\n",
984
+ " </tr>\n",
985
+ " <tr>\n",
986
+ " <th>2198</th>\n",
987
+ " <td>117</td>\n",
988
+ " <td>32</td>\n",
989
+ " <td>34</td>\n",
990
+ " <td>26.272418</td>\n",
991
+ " <td>52.127394</td>\n",
992
+ " <td>6.758793</td>\n",
993
+ " <td>127.175293</td>\n",
994
+ " </tr>\n",
995
+ " <tr>\n",
996
+ " <th>2199</th>\n",
997
+ " <td>104</td>\n",
998
+ " <td>18</td>\n",
999
+ " <td>30</td>\n",
1000
+ " <td>23.603016</td>\n",
1001
+ " <td>60.396475</td>\n",
1002
+ " <td>6.779833</td>\n",
1003
+ " <td>140.937041</td>\n",
1004
+ " </tr>\n",
1005
+ " </tbody>\n",
1006
+ "</table>\n",
1007
+ "<p>2200 rows × 7 columns</p>\n",
1008
+ "</div>"
1009
+ ],
1010
+ "text/plain": [
1011
+ " N P K temperature humidity ph rainfall\n",
1012
+ "0 90 42 43 20.879744 82.002744 6.502985 202.935536\n",
1013
+ "1 85 58 41 21.770462 80.319644 7.038096 226.655537\n",
1014
+ "2 60 55 44 23.004459 82.320763 7.840207 263.964248\n",
1015
+ "3 74 35 40 26.491096 80.158363 6.980401 242.864034\n",
1016
+ "4 78 42 42 20.130175 81.604873 7.628473 262.717340\n",
1017
+ "... ... .. .. ... ... ... ...\n",
1018
+ "2195 107 34 32 26.774637 66.413269 6.780064 177.774507\n",
1019
+ "2196 99 15 27 27.417112 56.636362 6.086922 127.924610\n",
1020
+ "2197 118 33 30 24.131797 67.225123 6.362608 173.322839\n",
1021
+ "2198 117 32 34 26.272418 52.127394 6.758793 127.175293\n",
1022
+ "2199 104 18 30 23.603016 60.396475 6.779833 140.937041\n",
1023
+ "\n",
1024
+ "[2200 rows x 7 columns]"
1025
+ ]
1026
+ },
1027
+ "execution_count": 28,
1028
+ "metadata": {},
1029
+ "output_type": "execute_result"
1030
+ }
1031
+ ],
1032
+ "source": [
1033
+ "X"
1034
+ ]
1035
+ },
1036
+ {
1037
+ "cell_type": "code",
1038
+ "execution_count": 29,
1039
+ "id": "d2601fcf",
1040
+ "metadata": {},
1041
+ "outputs": [
1042
+ {
1043
+ "data": {
1044
+ "text/plain": [
1045
+ "0 1\n",
1046
+ "1 1\n",
1047
+ "2 1\n",
1048
+ "3 1\n",
1049
+ "4 1\n",
1050
+ " ..\n",
1051
+ "2195 22\n",
1052
+ "2196 22\n",
1053
+ "2197 22\n",
1054
+ "2198 22\n",
1055
+ "2199 22\n",
1056
+ "Name: crop_num, Length: 2200, dtype: int64"
1057
+ ]
1058
+ },
1059
+ "execution_count": 29,
1060
+ "metadata": {},
1061
+ "output_type": "execute_result"
1062
+ }
1063
+ ],
1064
+ "source": [
1065
+ "y"
1066
+ ]
1067
+ },
1068
+ {
1069
+ "cell_type": "code",
1070
+ "execution_count": 30,
1071
+ "id": "c561ea31",
1072
+ "metadata": {},
1073
+ "outputs": [
1074
+ {
1075
+ "data": {
1076
+ "text/plain": [
1077
+ "(2200,)"
1078
+ ]
1079
+ },
1080
+ "execution_count": 30,
1081
+ "metadata": {},
1082
+ "output_type": "execute_result"
1083
+ }
1084
+ ],
1085
+ "source": [
1086
+ "y.shape"
1087
+ ]
1088
+ },
1089
+ {
1090
+ "cell_type": "code",
1091
+ "execution_count": 31,
1092
+ "id": "caba8efb",
1093
+ "metadata": {},
1094
+ "outputs": [],
1095
+ "source": [
1096
+ "from sklearn.model_selection import train_test_split"
1097
+ ]
1098
+ },
1099
+ {
1100
+ "cell_type": "code",
1101
+ "execution_count": 32,
1102
+ "id": "6774a9dd",
1103
+ "metadata": {},
1104
+ "outputs": [],
1105
+ "source": [
1106
+ "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)"
1107
+ ]
1108
+ },
1109
+ {
1110
+ "cell_type": "code",
1111
+ "execution_count": 33,
1112
+ "id": "41b6bcbb",
1113
+ "metadata": {},
1114
+ "outputs": [
1115
+ {
1116
+ "data": {
1117
+ "text/html": [
1118
+ "<div>\n",
1119
+ "<style scoped>\n",
1120
+ " .dataframe tbody tr th:only-of-type {\n",
1121
+ " vertical-align: middle;\n",
1122
+ " }\n",
1123
+ "\n",
1124
+ " .dataframe tbody tr th {\n",
1125
+ " vertical-align: top;\n",
1126
+ " }\n",
1127
+ "\n",
1128
+ " .dataframe thead th {\n",
1129
+ " text-align: right;\n",
1130
+ " }\n",
1131
+ "</style>\n",
1132
+ "<table border=\"1\" class=\"dataframe\">\n",
1133
+ " <thead>\n",
1134
+ " <tr style=\"text-align: right;\">\n",
1135
+ " <th></th>\n",
1136
+ " <th>N</th>\n",
1137
+ " <th>P</th>\n",
1138
+ " <th>K</th>\n",
1139
+ " <th>temperature</th>\n",
1140
+ " <th>humidity</th>\n",
1141
+ " <th>ph</th>\n",
1142
+ " <th>rainfall</th>\n",
1143
+ " </tr>\n",
1144
+ " </thead>\n",
1145
+ " <tbody>\n",
1146
+ " <tr>\n",
1147
+ " <th>1656</th>\n",
1148
+ " <td>17</td>\n",
1149
+ " <td>16</td>\n",
1150
+ " <td>14</td>\n",
1151
+ " <td>16.396243</td>\n",
1152
+ " <td>92.181519</td>\n",
1153
+ " <td>6.625539</td>\n",
1154
+ " <td>102.944161</td>\n",
1155
+ " </tr>\n",
1156
+ " <tr>\n",
1157
+ " <th>752</th>\n",
1158
+ " <td>37</td>\n",
1159
+ " <td>79</td>\n",
1160
+ " <td>19</td>\n",
1161
+ " <td>27.543848</td>\n",
1162
+ " <td>69.347863</td>\n",
1163
+ " <td>7.143943</td>\n",
1164
+ " <td>69.408782</td>\n",
1165
+ " </tr>\n",
1166
+ " <tr>\n",
1167
+ " <th>892</th>\n",
1168
+ " <td>7</td>\n",
1169
+ " <td>73</td>\n",
1170
+ " <td>25</td>\n",
1171
+ " <td>27.521856</td>\n",
1172
+ " <td>63.132153</td>\n",
1173
+ " <td>7.288057</td>\n",
1174
+ " <td>45.208411</td>\n",
1175
+ " </tr>\n",
1176
+ " <tr>\n",
1177
+ " <th>1041</th>\n",
1178
+ " <td>101</td>\n",
1179
+ " <td>70</td>\n",
1180
+ " <td>48</td>\n",
1181
+ " <td>25.360592</td>\n",
1182
+ " <td>75.031933</td>\n",
1183
+ " <td>6.012697</td>\n",
1184
+ " <td>116.553145</td>\n",
1185
+ " </tr>\n",
1186
+ " <tr>\n",
1187
+ " <th>1179</th>\n",
1188
+ " <td>0</td>\n",
1189
+ " <td>17</td>\n",
1190
+ " <td>30</td>\n",
1191
+ " <td>35.474783</td>\n",
1192
+ " <td>47.972305</td>\n",
1193
+ " <td>6.279134</td>\n",
1194
+ " <td>97.790725</td>\n",
1195
+ " </tr>\n",
1196
+ " <tr>\n",
1197
+ " <th>...</th>\n",
1198
+ " <td>...</td>\n",
1199
+ " <td>...</td>\n",
1200
+ " <td>...</td>\n",
1201
+ " <td>...</td>\n",
1202
+ " <td>...</td>\n",
1203
+ " <td>...</td>\n",
1204
+ " <td>...</td>\n",
1205
+ " </tr>\n",
1206
+ " <tr>\n",
1207
+ " <th>1638</th>\n",
1208
+ " <td>10</td>\n",
1209
+ " <td>5</td>\n",
1210
+ " <td>5</td>\n",
1211
+ " <td>21.213070</td>\n",
1212
+ " <td>91.353492</td>\n",
1213
+ " <td>7.817846</td>\n",
1214
+ " <td>112.983436</td>\n",
1215
+ " </tr>\n",
1216
+ " <tr>\n",
1217
+ " <th>1095</th>\n",
1218
+ " <td>108</td>\n",
1219
+ " <td>94</td>\n",
1220
+ " <td>47</td>\n",
1221
+ " <td>27.359116</td>\n",
1222
+ " <td>84.546250</td>\n",
1223
+ " <td>6.387431</td>\n",
1224
+ " <td>90.812505</td>\n",
1225
+ " </tr>\n",
1226
+ " <tr>\n",
1227
+ " <th>1130</th>\n",
1228
+ " <td>11</td>\n",
1229
+ " <td>36</td>\n",
1230
+ " <td>31</td>\n",
1231
+ " <td>27.920633</td>\n",
1232
+ " <td>51.779659</td>\n",
1233
+ " <td>6.475449</td>\n",
1234
+ " <td>100.258567</td>\n",
1235
+ " </tr>\n",
1236
+ " <tr>\n",
1237
+ " <th>1294</th>\n",
1238
+ " <td>11</td>\n",
1239
+ " <td>124</td>\n",
1240
+ " <td>204</td>\n",
1241
+ " <td>13.429886</td>\n",
1242
+ " <td>80.066340</td>\n",
1243
+ " <td>6.361141</td>\n",
1244
+ " <td>71.400430</td>\n",
1245
+ " </tr>\n",
1246
+ " <tr>\n",
1247
+ " <th>860</th>\n",
1248
+ " <td>32</td>\n",
1249
+ " <td>78</td>\n",
1250
+ " <td>22</td>\n",
1251
+ " <td>23.970814</td>\n",
1252
+ " <td>62.355576</td>\n",
1253
+ " <td>7.007038</td>\n",
1254
+ " <td>53.409060</td>\n",
1255
+ " </tr>\n",
1256
+ " </tbody>\n",
1257
+ "</table>\n",
1258
+ "<p>1760 rows × 7 columns</p>\n",
1259
+ "</div>"
1260
+ ],
1261
+ "text/plain": [
1262
+ " N P K temperature humidity ph rainfall\n",
1263
+ "1656 17 16 14 16.396243 92.181519 6.625539 102.944161\n",
1264
+ "752 37 79 19 27.543848 69.347863 7.143943 69.408782\n",
1265
+ "892 7 73 25 27.521856 63.132153 7.288057 45.208411\n",
1266
+ "1041 101 70 48 25.360592 75.031933 6.012697 116.553145\n",
1267
+ "1179 0 17 30 35.474783 47.972305 6.279134 97.790725\n",
1268
+ "... ... ... ... ... ... ... ...\n",
1269
+ "1638 10 5 5 21.213070 91.353492 7.817846 112.983436\n",
1270
+ "1095 108 94 47 27.359116 84.546250 6.387431 90.812505\n",
1271
+ "1130 11 36 31 27.920633 51.779659 6.475449 100.258567\n",
1272
+ "1294 11 124 204 13.429886 80.066340 6.361141 71.400430\n",
1273
+ "860 32 78 22 23.970814 62.355576 7.007038 53.409060\n",
1274
+ "\n",
1275
+ "[1760 rows x 7 columns]"
1276
+ ]
1277
+ },
1278
+ "execution_count": 33,
1279
+ "metadata": {},
1280
+ "output_type": "execute_result"
1281
+ }
1282
+ ],
1283
+ "source": [
1284
+ "X_train"
1285
+ ]
1286
+ },
1287
+ {
1288
+ "cell_type": "markdown",
1289
+ "id": "ab13cdf8",
1290
+ "metadata": {},
1291
+ "source": [
1292
+ "\n",
1293
+ "# Scale the features using MinMaxScaler"
1294
+ ]
1295
+ },
1296
+ {
1297
+ "cell_type": "code",
1298
+ "execution_count": 34,
1299
+ "id": "f19981a7",
1300
+ "metadata": {},
1301
+ "outputs": [],
1302
+ "source": [
1303
+ "from sklearn.preprocessing import MinMaxScaler\n",
1304
+ "ms = MinMaxScaler()\n",
1305
+ "\n",
1306
+ "X_train = ms.fit_transform(X_train)\n",
1307
+ "X_test = ms.transform(X_test)"
1308
+ ]
1309
+ },
1310
+ {
1311
+ "cell_type": "code",
1312
+ "execution_count": 35,
1313
+ "id": "f3f50c64",
1314
+ "metadata": {},
1315
+ "outputs": [
1316
+ {
1317
+ "data": {
1318
+ "text/plain": [
1319
+ "array([[0.12142857, 0.07857143, 0.045 , ..., 0.9089898 , 0.48532225,\n",
1320
+ " 0.29685161],\n",
1321
+ " [0.26428571, 0.52857143, 0.07 , ..., 0.64257946, 0.56594073,\n",
1322
+ " 0.17630752],\n",
1323
+ " [0.05 , 0.48571429, 0.1 , ..., 0.57005802, 0.58835229,\n",
1324
+ " 0.08931844],\n",
1325
+ " ...,\n",
1326
+ " [0.07857143, 0.22142857, 0.13 , ..., 0.43760347, 0.46198144,\n",
1327
+ " 0.28719815],\n",
1328
+ " [0.07857143, 0.85 , 0.995 , ..., 0.76763665, 0.44420505,\n",
1329
+ " 0.18346657],\n",
1330
+ " [0.22857143, 0.52142857, 0.085 , ..., 0.56099735, 0.54465022,\n",
1331
+ " 0.11879596]])"
1332
+ ]
1333
+ },
1334
+ "execution_count": 35,
1335
+ "metadata": {},
1336
+ "output_type": "execute_result"
1337
+ }
1338
+ ],
1339
+ "source": [
1340
+ "X_train"
1341
+ ]
1342
+ },
1343
+ {
1344
+ "cell_type": "markdown",
1345
+ "id": "752a08ae",
1346
+ "metadata": {},
1347
+ "source": [
1348
+ "# Training Models"
1349
+ ]
1350
+ },
1351
+ {
1352
+ "cell_type": "code",
1353
+ "execution_count": 51,
1354
+ "id": "ac6ef55e",
1355
+ "metadata": {},
1356
+ "outputs": [
1357
+ {
1358
+ "name": "stdout",
1359
+ "output_type": "stream",
1360
+ "text": [
1361
+ "Logistic Regression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0 10 0 0 1 0 0 13 0 0 0 0]\n",
1380
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 0 0 18 2 0 0]\n",
1381
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20 0 0]\n",
1382
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 26 0]\n",
1383
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 17]]\n",
1384
+ "==========================================================\n",
1385
+ "Naive Bayes with accuracy : 0.9954545454545455\n",
1386
+ "Confusion matrix : [[17 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1387
+ " [ 0 21 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1388
+ " [ 0 0 23 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1389
+ " [ 0 0 0 17 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1390
+ " [ 0 0 0 0 27 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1391
+ " [ 0 0 0 0 0 23 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1392
+ " [ 0 0 0 0 0 0 14 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1393
+ " [ 0 0 0 0 0 0 0 23 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1394
+ " [ 0 0 0 0 0 0 0 0 17 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1395
+ " [ 0 0 0 0 0 0 0 0 0 19 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1396
+ " [ 0 0 0 0 0 0 0 0 0 0 14 0 0 0 0 0 0 0 0 0 0 0]\n",
1397
+ " [ 0 0 0 0 0 0 0 0 0 0 0 19 0 0 0 0 0 0 0 0 0 0]\n",
1398
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 21 0 0 0 0 0 0 0 0 0]\n",
1399
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 23 0 0 0 0 0 0 0 0]\n",
1400
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 11 0 0 0 0 0 0 0]\n",
1401
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20 0 0 0 0 0 0]\n",
1402
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 19 0 0 0 0 0]\n",
1403
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 24 0 0 0 0]\n",
1404
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 23 0 0 0]\n",
1405
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20 0 0]\n",
1406
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 26 0]\n",
1407
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 17]]\n",
1408
+ "==========================================================\n",
1409
+ "Support Vector Machine with accuracy : 0.9681818181818181\n",
1410
+ "Confusion matrix : [[14 0 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1411
+ " [ 0 20 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1412
+ " [ 0 0 22 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1]\n",
1413
+ " [ 0 0 0 17 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1414
+ " [ 0 0 0 0 27 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1415
+ " [ 0 0 0 0 0 23 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1416
+ " [ 0 0 0 0 0 0 14 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1417
+ " [ 0 0 0 0 0 0 0 23 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1418
+ " [ 0 0 0 0 0 0 0 0 17 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1419
+ " [ 0 0 0 0 0 0 0 0 0 19 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1420
+ " [ 0 0 0 0 0 0 0 0 0 0 14 0 0 0 0 0 0 0 0 0 0 0]\n",
1421
+ " [ 0 0 0 0 0 0 0 0 0 0 0 19 0 0 0 0 0 0 0 0 0 0]\n",
1422
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 21 0 0 0 0 0 0 0 0 0]\n",
1423
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 23 0 0 0 0 0 0 0 0]\n",
1424
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 11 0 0 0 0 0 0 0]\n",
1425
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 19 0 0 0 0 0 0]\n",
1426
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 19 0 0 0 0 0]\n",
1427
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 0 21 0 0 0 0]\n",
1428
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 20 2 0 0]\n",
1429
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20 0 0]\n",
1430
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 26 0]\n",
1431
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 17]]\n",
1432
+ "==========================================================\n",
1433
+ "K-Nearest Neighbors with accuracy : 0.9704545454545455\n",
1434
+ "Confusion matrix : [[14 0 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1435
+ " [ 0 21 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1436
+ " [ 1 0 22 0 0 0 0 0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 0 0 0 0 0 0 0 0 0 0 0 0 0 26 0]\n",
1455
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 17]]\n",
1456
+ "==========================================================\n",
1457
+ "Decision Tree with accuracy : 0.9818181818181818\n",
1458
+ "Confusion matrix : [[17 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1459
+ " [ 0 20 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1460
+ " [ 3 0 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1461
+ " [ 0 0 0 17 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1462
+ " [ 0 0 0 0 27 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1463
+ " [ 0 0 0 0 0 23 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1464
+ " [ 0 0 0 0 0 0 14 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1465
+ " [ 0 0 0 0 0 0 0 23 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1466
+ " [ 0 0 0 0 0 0 0 0 17 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1467
+ " [ 0 0 0 0 0 0 0 0 0 19 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1468
+ " [ 0 0 0 0 0 0 0 0 0 0 14 0 0 0 0 0 0 0 0 0 0 0]\n",
1469
+ " [ 0 0 0 0 0 0 0 0 0 0 0 19 0 0 0 0 0 0 0 0 0 0]\n",
1470
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 21 0 0 0 0 0 0 0 0 0]\n",
1471
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 23 0 0 0 0 0 0 0 0]\n",
1472
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 11 0 0 0 0 0 0 0]\n",
1473
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20 0 0 0 0 0 0]\n",
1474
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 19 0 0 0 0 0]\n",
1475
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 22 0 0 0 0]\n",
1476
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 23 0 0 0]\n",
1477
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20 0 0]\n",
1478
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 26 0]\n",
1479
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 17]]\n",
1480
+ "==========================================================\n"
1481
+ ]
1482
+ },
1483
+ {
1484
+ "name": "stdout",
1485
+ "output_type": "stream",
1486
+ "text": [
1487
+ "Random Forest with accuracy : 0.9931818181818182\n",
1488
+ "Confusion matrix : [[17 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1489
+ " [ 0 21 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1490
+ " [ 0 0 23 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1491
+ " [ 0 0 0 17 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1492
+ " [ 0 0 0 0 27 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1493
+ " [ 0 0 0 0 0 23 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1494
+ " [ 0 0 0 0 0 0 14 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1495
+ " [ 0 0 0 0 0 0 0 23 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1496
+ " [ 0 0 0 0 0 0 0 0 17 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1497
+ " [ 0 0 0 0 0 0 0 0 0 19 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1498
+ " [ 0 0 0 0 0 0 0 0 0 0 14 0 0 0 0 0 0 0 0 0 0 0]\n",
1499
+ " [ 0 0 0 0 0 0 0 0 0 0 0 19 0 0 0 0 0 0 0 0 0 0]\n",
1500
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 21 0 0 0 0 0 0 0 0 0]\n",
1501
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 23 0 0 0 0 0 0 0 0]\n",
1502
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 11 0 0 0 0 0 0 0]\n",
1503
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20 0 0 0 0 0 0]\n",
1504
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 19 0 0 0 0 0]\n",
1505
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 23 0 0 0 0]\n",
1506
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 23 0 0 0]\n",
1507
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20 0 0]\n",
1508
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 26 0]\n",
1509
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 17]]\n",
1510
+ "==========================================================\n",
1511
+ "Bagging with accuracy : 0.9886363636363636\n",
1512
+ "Confusion matrix : [[17 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1513
+ " [ 0 21 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1514
+ " [ 1 0 22 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1515
+ " [ 0 0 0 17 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1516
+ " [ 0 0 0 0 27 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1517
+ " [ 0 0 0 0 0 23 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1518
+ " [ 0 0 0 0 0 0 14 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1519
+ " [ 0 0 0 0 0 0 0 23 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1520
+ " [ 0 0 0 0 0 0 0 0 17 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1521
+ " [ 0 0 0 0 0 0 0 0 0 19 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1522
+ " [ 0 0 0 0 0 0 0 0 0 0 14 0 0 0 0 0 0 0 0 0 0 0]\n",
1523
+ " [ 0 0 0 0 0 0 0 0 0 0 0 19 0 0 0 0 0 0 0 0 0 0]\n",
1524
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 21 0 0 0 0 0 0 0 0 0]\n",
1525
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 23 0 0 0 0 0 0 0 0]\n",
1526
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 11 0 0 0 0 0 0 0]\n",
1527
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20 0 0 0 0 0 0]\n",
1528
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 19 0 0 0 0 0]\n",
1529
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 22 0 0 0 0]\n",
1530
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 23 0 0 0]\n",
1531
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20 0 0]\n",
1532
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 26 0]\n",
1533
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 17]]\n",
1534
+ "==========================================================\n"
1535
+ ]
1536
+ },
1537
+ {
1538
+ "name": "stderr",
1539
+ "output_type": "stream",
1540
+ "text": [
1541
+ "C:\\Users\\Noor Saeed\\AppData\\Roaming\\Python\\Python39\\site-packages\\sklearn\\ensemble\\_weight_boosting.py:527: FutureWarning: The SAMME.R algorithm (the default) is deprecated and will be removed in 1.6. Use the SAMME algorithm to circumvent this warning.\n",
1542
+ " warnings.warn(\n"
1543
+ ]
1544
+ },
1545
+ {
1546
+ "name": "stdout",
1547
+ "output_type": "stream",
1548
+ "text": [
1549
+ "AdaBoost with accuracy : 0.09545454545454546\n",
1550
+ "Confusion matrix : [[ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 19 0 0 0 0 0 0 0]\n",
1551
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 21 0 0 0 0 0 0 0]\n",
1552
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 23 0 0 0 0 0 0 0]\n",
1553
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 17 0 0 0 0 0 0 0]\n",
1554
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 27 0 0 0 0 0 0 0]\n",
1555
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 23 0 0 0 0 0 0 0]\n",
1556
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 14 0 0 0 0 0 0 0]\n",
1557
+ " [ 0 0 0 0 0 0 0 0 0 0 23 0 0 0 0 0 0 0 0 0 0 0]\n",
1558
+ " [ 0 0 0 0 0 0 0 0 17 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1559
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 19 0 0 0 0 0 0 0]\n",
1560
+ " [ 0 0 0 0 0 0 0 0 0 0 14 0 0 0 0 0 0 0 0 0 0 0]\n",
1561
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 19 0 0 0 0 0 0 0]\n",
1562
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 21 0 0 0 0 0 0 0]\n",
1563
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 23 0 0 0 0 0 0 0]\n",
1564
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 11 0 0 0 0 0 0 0]\n",
1565
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20 0 0 0 0 0 0 0]\n",
1566
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 19 0 0 0 0 0 0 0]\n",
1567
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 24 0 0 0 0 0 0 0]\n",
1568
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 23 0 0 0 0 0 0 0]\n",
1569
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20 0 0 0 0 0 0 0]\n",
1570
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 26 0 0 0 0 0 0 0]\n",
1571
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 17 0 0 0 0 0 0 0]]\n",
1572
+ "==========================================================\n",
1573
+ "Gradient Boosting with accuracy : 0.9818181818181818\n",
1574
+ "Confusion matrix : [[15 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1575
+ " [ 0 20 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1576
+ " [ 0 0 23 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1577
+ " [ 0 0 0 17 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1578
+ " [ 0 0 1 0 26 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1579
+ " [ 0 0 0 0 0 23 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1580
+ " [ 0 0 0 0 0 0 14 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1581
+ " [ 0 0 0 0 0 0 0 23 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1582
+ " [ 0 0 0 0 0 0 0 0 17 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1583
+ " [ 0 0 0 0 0 0 0 0 0 19 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1584
+ " [ 0 0 0 0 0 0 0 0 0 0 14 0 0 0 0 0 0 0 0 0 0 0]\n",
1585
+ " [ 0 0 0 0 0 0 0 0 0 0 0 19 0 0 0 0 0 0 0 0 0 0]\n",
1586
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 21 0 0 0 0 0 0 0 0 0]\n",
1587
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 23 0 0 0 0 0 0 0 0]\n",
1588
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 11 0 0 0 0 0 0 0]\n",
1589
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20 0 0 0 0 0 0]\n",
1590
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 19 0 0 0 0 0]\n",
1591
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 23 0 0 0 0]\n",
1592
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 22 0 0 0]\n",
1593
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20 0 0]\n",
1594
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 26 0]\n",
1595
+ " [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 17]]\n",
1596
+ "==========================================================\n",
1597
+ "Extra Trees with accuracy : 0.8863636363636364\n",
1598
+ "Confusion matrix : [[12 0 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n",
1599
+ " [ 0 18 0 2 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0]\n",
1600
+ " [ 6 0 15 0 0 0 0 0 0 0 0 0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 0 0 0 0 0 0 0 0 0 26 0]\n",
1619
+ " [ 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 16]]\n",
1620
+ "==========================================================\n"
1621
+ ]
1622
+ }
1623
+ ],
1624
+ "source": [
1625
+ "from sklearn.linear_model import LogisticRegression\n",
1626
+ "from sklearn.naive_bayes import GaussianNB\n",
1627
+ "from sklearn.svm import SVC\n",
1628
+ "from sklearn.neighbors import KNeighborsClassifier\n",
1629
+ "from sklearn.tree import DecisionTreeClassifier\n",
1630
+ "from sklearn.tree import ExtraTreeClassifier\n",
1631
+ "from sklearn.ensemble import RandomForestClassifier\n",
1632
+ "from sklearn.ensemble import BaggingClassifier\n",
1633
+ "from sklearn.ensemble import GradientBoostingClassifier\n",
1634
+ "from sklearn.ensemble import AdaBoostClassifier\n",
1635
+ "from sklearn.metrics import accuracy_score,confusion_matrix\n",
1636
+ "\n",
1637
+ "# create instances of all models\n",
1638
+ "models = {\n",
1639
+ " 'Logistic Regression': LogisticRegression(),\n",
1640
+ " 'Naive Bayes': GaussianNB(),\n",
1641
+ " 'Support Vector Machine': SVC(),\n",
1642
+ " 'K-Nearest Neighbors': KNeighborsClassifier(),\n",
1643
+ " 'Decision Tree': DecisionTreeClassifier(),\n",
1644
+ " 'Random Forest': RandomForestClassifier(),\n",
1645
+ " 'Bagging': BaggingClassifier(),\n",
1646
+ " 'AdaBoost': AdaBoostClassifier(),\n",
1647
+ " 'Gradient Boosting': GradientBoostingClassifier(),\n",
1648
+ " 'Extra Trees': ExtraTreeClassifier(),\n",
1649
+ "}\n",
1650
+ "\n",
1651
+ "\n",
1652
+ "for name, model in models.items():\n",
1653
+ " model.fit(X_train,y_train)\n",
1654
+ " ypred = model.predict(X_test)\n",
1655
+ " \n",
1656
+ " print(f\"{name} with accuracy : {accuracy_score(y_test,ypred)}\")\n",
1657
+ " print(\"Confusion matrix : \",confusion_matrix(y_test,ypred))\n",
1658
+ " print(\"==========================================================\")\n",
1659
+ " "
1660
+ ]
1661
+ },
1662
+ {
1663
+ "cell_type": "code",
1664
+ "execution_count": 38,
1665
+ "id": "4659be4d",
1666
+ "metadata": {},
1667
+ "outputs": [
1668
+ {
1669
+ "data": {
1670
+ "text/plain": [
1671
+ "0.9931818181818182"
1672
+ ]
1673
+ },
1674
+ "execution_count": 38,
1675
+ "metadata": {},
1676
+ "output_type": "execute_result"
1677
+ }
1678
+ ],
1679
+ "source": [
1680
+ "# finally selected randomforest model\n",
1681
+ "\n",
1682
+ "rfc = RandomForestClassifier()\n",
1683
+ "rfc.fit(X_train,y_train)\n",
1684
+ "ypred = rfc.predict(X_test)\n",
1685
+ "accuracy_score(y_test,ypred)"
1686
+ ]
1687
+ },
1688
+ {
1689
+ "cell_type": "code",
1690
+ "execution_count": 44,
1691
+ "id": "3e72d7f1",
1692
+ "metadata": {},
1693
+ "outputs": [
1694
+ {
1695
+ "data": {
1696
+ "text/plain": [
1697
+ "0.9954545454545455"
1698
+ ]
1699
+ },
1700
+ "execution_count": 44,
1701
+ "metadata": {},
1702
+ "output_type": "execute_result"
1703
+ }
1704
+ ],
1705
+ "source": [
1706
+ "# or gausianjb\n",
1707
+ "gnb = GaussianNB()\n",
1708
+ "gnb.fit(X_train,y_train)\n",
1709
+ "ypred = gnb.predict(X_test)\n",
1710
+ "accuracy_score(y_test,ypred)"
1711
+ ]
1712
+ },
1713
+ {
1714
+ "cell_type": "markdown",
1715
+ "id": "859d9922",
1716
+ "metadata": {},
1717
+ "source": [
1718
+ "# Predictive System"
1719
+ ]
1720
+ },
1721
+ {
1722
+ "cell_type": "code",
1723
+ "execution_count": 61,
1724
+ "id": "17f3a3fe",
1725
+ "metadata": {},
1726
+ "outputs": [],
1727
+ "source": [
1728
+ "def recommendation(N,P,k,temperature,humidity,ph,rainfal):\n",
1729
+ " features = np.array([[N,P,k,temperature,humidity,ph,rainfal]])\n",
1730
+ " transformed_features = ms.fit_transform(features)\n",
1731
+ " prediction = rfc.predict(transformed_features)\n",
1732
+ " print(prediction)\n",
1733
+ " return prediction[0] "
1734
+ ]
1735
+ },
1736
+ {
1737
+ "cell_type": "code",
1738
+ "execution_count": 62,
1739
+ "id": "64ffd9d3",
1740
+ "metadata": {},
1741
+ "outputs": [
1742
+ {
1743
+ "name": "stdout",
1744
+ "output_type": "stream",
1745
+ "text": [
1746
+ "[9]\n",
1747
+ "Muskmelon is a best crop to be cultivated \n"
1748
+ ]
1749
+ }
1750
+ ],
1751
+ "source": [
1752
+ "# new inputs\n",
1753
+ "\n",
1754
+ "\n",
1755
+ "N = 40\n",
1756
+ "P = 50\n",
1757
+ "k = 50\n",
1758
+ "temperature = 40.0\n",
1759
+ "humidity = 20\n",
1760
+ "ph = 100\n",
1761
+ "rainfall = 100\n",
1762
+ "\n",
1763
+ "predict = recommendation(N,P,k,temperature,humidity,ph,rainfall)\n",
1764
+ "\n",
1765
+ "crop_dict = {1: \"Rice\", 2: \"Maize\", 3: \"Jute\", 4: \"Cotton\", 5: \"Coconut\", 6: \"Papaya\", 7: \"Orange\",\n",
1766
+ " 8: \"Apple\", 9: \"Muskmelon\", 10: \"Watermelon\", 11: \"Grapes\", 12: \"Mango\", 13: \"Banana\",\n",
1767
+ " 14: \"Pomegranate\", 15: \"Lentil\", 16: \"Blackgram\", 17: \"Mungbean\", 18: \"Mothbeans\",\n",
1768
+ " 19: \"Pigeonpeas\", 20: \"Kidneybeans\", 21: \"Chickpea\", 22: \"Coffee\"}\n",
1769
+ "\n",
1770
+ "if predict in crop_dict:\n",
1771
+ " crop = crop_dict[predict]\n",
1772
+ " print(\"{} is a best crop to be cultivated \".format(crop))\n",
1773
+ "else:\n",
1774
+ " print(\"Sorry are not able to recommend a proper crop for this environment\")"
1775
+ ]
1776
+ },
1777
+ {
1778
+ "cell_type": "code",
1779
+ "execution_count": 63,
1780
+ "id": "2ea8ffda",
1781
+ "metadata": {},
1782
+ "outputs": [
1783
+ {
1784
+ "name": "stdout",
1785
+ "output_type": "stream",
1786
+ "text": [
1787
+ "[9]\n",
1788
+ "Muskmelon is a best crop to be cultivated \n"
1789
+ ]
1790
+ }
1791
+ ],
1792
+ "source": [
1793
+ "# new inputs 2\n",
1794
+ "\n",
1795
+ "\n",
1796
+ "N = 100\n",
1797
+ "P = 90\n",
1798
+ "k = 100\n",
1799
+ "temperature = 50.0\n",
1800
+ "humidity = 90.0\n",
1801
+ "ph = 100\n",
1802
+ "rainfall = 202.0\n",
1803
+ "\n",
1804
+ "predict = recommendation(N,P,k,temperature,humidity,ph,rainfall)\n",
1805
+ "\n",
1806
+ "crop_dict = {1: \"Rice\", 2: \"Maize\", 3: \"Jute\", 4: \"Cotton\", 5: \"Coconut\", 6: \"Papaya\", 7: \"Orange\",\n",
1807
+ " 8: \"Apple\", 9: \"Muskmelon\", 10: \"Watermelon\", 11: \"Grapes\", 12: \"Mango\", 13: \"Banana\",\n",
1808
+ " 14: \"Pomegranate\", 15: \"Lentil\", 16: \"Blackgram\", 17: \"Mungbean\", 18: \"Mothbeans\",\n",
1809
+ " 19: \"Pigeonpeas\", 20: \"Kidneybeans\", 21: \"Chickpea\", 22: \"Coffee\"}\n",
1810
+ "\n",
1811
+ "if predict in crop_dict:\n",
1812
+ " crop = crop_dict[predict]\n",
1813
+ " print(\"{} is a best crop to be cultivated \".format(crop))\n",
1814
+ "else:\n",
1815
+ " print(\"Sorry are not able to recommend a proper crop for this environment\")"
1816
+ ]
1817
+ },
1818
+ {
1819
+ "cell_type": "code",
1820
+ "execution_count": 64,
1821
+ "id": "d0dccd4e",
1822
+ "metadata": {},
1823
+ "outputs": [
1824
+ {
1825
+ "name": "stdout",
1826
+ "output_type": "stream",
1827
+ "text": [
1828
+ "[9]\n",
1829
+ "Muskmelon is a best crop to be cultivated \n"
1830
+ ]
1831
+ }
1832
+ ],
1833
+ "source": [
1834
+ "# new inputs 2\n",
1835
+ "N = 10\n",
1836
+ "P = 10\n",
1837
+ "k = 10\n",
1838
+ "temperature = 15.0\n",
1839
+ "humidity = 80.0\n",
1840
+ "ph = 4.5\n",
1841
+ "rainfall = 10.0\n",
1842
+ "\n",
1843
+ "predict = recommendation(N,P,k,temperature,humidity,ph,rainfall)\n",
1844
+ "\n",
1845
+ "crop_dict = {1: \"Rice\", 2: \"Maize\", 3: \"Jute\", 4: \"Cotton\", 5: \"Coconut\", 6: \"Papaya\", 7: \"Orange\",\n",
1846
+ " 8: \"Apple\", 9: \"Muskmelon\", 10: \"Watermelon\", 11: \"Grapes\", 12: \"Mango\", 13: \"Banana\",\n",
1847
+ " 14: \"Pomegranate\", 15: \"Lentil\", 16: \"Blackgram\", 17: \"Mungbean\", 18: \"Mothbeans\",\n",
1848
+ " 19: \"Pigeonpeas\", 20: \"Kidneybeans\", 21: \"Chickpea\", 22: \"Coffee\"}\n",
1849
+ "\n",
1850
+ "if predict in crop_dict:\n",
1851
+ " crop = crop_dict[predict]\n",
1852
+ " print(\"{} is a best crop to be cultivated \".format(crop))\n",
1853
+ "else:\n",
1854
+ " print(\"Sorry are not able to recommend a proper crop for this environment\")"
1855
+ ]
1856
+ },
1857
+ {
1858
+ "cell_type": "code",
1859
+ "execution_count": 66,
1860
+ "id": "fa3d3b8c",
1861
+ "metadata": {},
1862
+ "outputs": [],
1863
+ "source": [
1864
+ "import pickle\n",
1865
+ "pickle.dump(rfc,open('model.pkl','wb'))\n",
1866
+ "pickle.dump(ms,open('minmaxscaler.pkl','wb'))"
1867
+ ]
1868
+ },
1869
+ {
1870
+ "cell_type": "code",
1871
+ "execution_count": null,
1872
+ "id": "a55a48a3",
1873
+ "metadata": {},
1874
+ "outputs": [],
1875
+ "source": []
1876
+ },
1877
+ {
1878
+ "cell_type": "code",
1879
+ "execution_count": null,
1880
+ "id": "c97733fc",
1881
+ "metadata": {},
1882
+ "outputs": [],
1883
+ "source": []
1884
+ }
1885
+ ],
1886
+ "metadata": {
1887
+ "kernelspec": {
1888
+ "display_name": "Python 3 (ipykernel)",
1889
+ "language": "python",
1890
+ "name": "python3"
1891
+ },
1892
+ "language_info": {
1893
+ "codemirror_mode": {
1894
+ "name": "ipython",
1895
+ "version": 3
1896
+ },
1897
+ "file_extension": ".py",
1898
+ "mimetype": "text/x-python",
1899
+ "name": "python",
1900
+ "nbconvert_exporter": "python",
1901
+ "pygments_lexer": "ipython3",
1902
+ "version": "3.9.12"
1903
+ }
1904
+ },
1905
+ "nbformat": 4,
1906
+ "nbformat_minor": 5
1907
+ }
Crop_recommendation.csv ADDED
The diff for this file is too large to render. See raw diff
 
README.md ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Crop Recommendation System Using Machine Learning
2
+ # Description
3
+ The Crop Recommendation System is a machine learning-based application that provides recommendations for suitable crops based on various environmental and soil conditions. It aims to assist farmers and agricultural professionals in making informed decisions about crop selection, optimizing yields, and maximizing profitability.
4
+
5
+ The system takes into account several factors such as soil type, climate, rainfall, temperature, humidity, and pH levels to determine the most suitable crops for a given region. By analyzing historical data and using predictive models, the system provides personalized recommendations tailored to the specific conditions of a farm or agricultural area.
6
+
7
+ # Key Features
8
+ Input Data Collection: The system allows users to input relevant data such as soil parameters, climate information, and geographic location.
9
+ Data Preprocessing: The input data is preprocessed to handle missing values, normalize or scale features, and transform categorical variables.
10
+ Machine Learning Models: Various machine learning algorithms are employed, including decision trees, random forests, support vector machines (SVM), and gradient boosting techniques, to build predictive models.
11
+ Model Training and Evaluation: The models are trained on historical data and evaluated using appropriate performance metrics to ensure accuracy and reliability.
12
+ Crop Recommendation: Based on the trained models, the system recommends the most suitable crops for the given input parameters.
13
+ User-Friendly Interface: The system provides a user-friendly interface where users can easily input their data, view recommendations, and explore additional information.
14
+
15
+ # Technologies Used
16
+ Python: Programming language used for model development, data preprocessing, and web application development.
17
+ Scikit-learn: Machine learning library used for model training, evaluation, and prediction.
18
+ Pandas: Data manipulation library used for data preprocessing and analysis.
19
+ NumPy: Library for numerical computing used for handling arrays and mathematical operations.
20
+ Flask: Web framework used for building the user interface and handling HTTP requests.
21
+ HTML/CSS: Markup and styling languages used for designing the web interface.
22
+ JavaScript: Scripting language used for client-side interactions and enhancing the user interface.
23
+ # Installation and Usage
24
+ Clone the repository: git clone https://github.com/your-username/crop-recommendation-system.git
25
+ Install the required dependencies: pip install -r requirements.txt
26
+ Run the application: python app.py
27
+ Access the application through the web browser at http://localhost:5000
28
+ # Future Enhancements
29
+ Integration of real-time weather data to improve the accuracy of recommendations.
30
+ Incorporation of crop market prices and profitability analysis to assist farmers in making economically viable decisions.
31
+ Development of a mobile application for convenient access and usage on smartphones and tablets.
32
+ Integration of user feedback and data collection to continuously enhance the recommendation system's performance.
33
+ Contributing
34
+ Contributions to the project are welcome. If you have any suggestions, bug reports, or feature requests, please submit them through the issue tracker on the GitHub repository.
35
+
36
+
37
+
38
+ # Acknowledgements
39
+ We would like to express our gratitude to the agricultural research community, farmers, and organizations for providing valuable insights, data, and domain knowledge that contributed to the development of this Crop Recommendation System.
40
+
41
+ # Contact
42
+ For any inquiries or questions, please contact us at 611noorsaeed@gmail.com
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask,request,render_template
2
+ import numpy as np
3
+ import pandas
4
+ import sklearn
5
+ import pickle
6
+
7
+ # importing model
8
+ model = pickle.load(open('model.pkl','rb'))
9
+ sc = pickle.load(open('standscaler.pkl','rb'))
10
+ ms = pickle.load(open('minmaxscaler.pkl','rb'))
11
+
12
+ # creating flask app
13
+ app = Flask(__name__)
14
+
15
+ @app.route('/')
16
+ def index():
17
+ return render_template("index.html")
18
+
19
+ @app.route("/predict",methods=['POST'])
20
+ def predict():
21
+ N = request.form['Nitrogen']
22
+ P = request.form['Phosporus']
23
+ K = request.form['Potassium']
24
+ temp = request.form['Temperature']
25
+ humidity = request.form['Humidity']
26
+ ph = request.form['Ph']
27
+ rainfall = request.form['Rainfall']
28
+
29
+ feature_list = [N, P, K, temp, humidity, ph, rainfall]
30
+ single_pred = np.array(feature_list).reshape(1, -1)
31
+
32
+ scaled_features = ms.transform(single_pred)
33
+ final_features = sc.transform(scaled_features)
34
+ prediction = model.predict(final_features)
35
+
36
+ crop_dict = {1: "Rice", 2: "Maize", 3: "Jute", 4: "Cotton", 5: "Coconut", 6: "Papaya", 7: "Orange",
37
+ 8: "Apple", 9: "Muskmelon", 10: "Watermelon", 11: "Grapes", 12: "Mango", 13: "Banana",
38
+ 14: "Pomegranate", 15: "Lentil", 16: "Blackgram", 17: "Mungbean", 18: "Mothbeans",
39
+ 19: "Pigeonpeas", 20: "Kidneybeans", 21: "Chickpea", 22: "Coffee"}
40
+
41
+ if prediction[0] in crop_dict:
42
+ crop = crop_dict[prediction[0]]
43
+ result = "{} is the best crop to be cultivated right there".format(crop)
44
+ else:
45
+ result = "Sorry, we could not determine the best crop to be cultivated with the provided data."
46
+ return render_template('index.html',result = result)
47
+
48
+
49
+
50
+
51
+ # python main
52
+ if __name__ == "__main__":
53
+ app.run(debug=True)
img.jpg ADDED
index.html ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1">
6
+ <title>Bootstrap demo</title>
7
+ <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KK94CHFLLe+nY2dmCWGMq91rCGa5gtU4mk92HdvYe+M/SXH301p5ILy+dN9+nJOZ" crossorigin="anonymous">
8
+ </head>
9
+ <style>
10
+ h1 {
11
+ color: mediumseagreen;
12
+ text-align: center;
13
+ }
14
+
15
+ .warning {
16
+ color: red;
17
+ font-weight: bold;
18
+ text-align: center;
19
+ }
20
+ .card{
21
+ margin-left:410px;
22
+ margin-top: 20px;
23
+ color: white;
24
+ }
25
+ .container{
26
+ background:#edf2f7;
27
+ font-weight: bold;
28
+ padding-bottom:10px;
29
+ border-radius: 15px;
30
+ }
31
+ </style>
32
+
33
+
34
+
35
+
36
+ <body style="background:#BCBBB8">
37
+ <!--=======================navbar=====================================================-->
38
+ <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
39
+ <div class="container-fluid">
40
+ <a class="navbar-brand" href="/">Crop Recommendation</a>
41
+ <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
42
+ <span class="navbar-toggler-icon"></span>
43
+ </button>
44
+ <div class="collapse navbar-collapse" id="navbarSupportedContent">
45
+ <ul class="navbar-nav me-auto mb-2 mb-lg-0">
46
+ <li class="nav-item">
47
+ <a class="nav-link active" aria-current="page" href="#">home</a>
48
+ </li>
49
+ <li class="nav-item">
50
+ <a class="nav-link" href="#">Contact</a>
51
+ </li>
52
+ <li class="nav-item">
53
+ <a class="nav-link disabled">About</a>
54
+ </li>
55
+ </ul>
56
+ <form class="d-flex" role="search">
57
+ <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
58
+ <button class="btn btn-outline-success" type="submit">Search</button>
59
+ </form>
60
+ </div>
61
+ </div>
62
+ </nav>
63
+
64
+ <!--==========================================================================================-->
65
+ <div class="container my-3 mt-3">
66
+ <h1 class="text-success">Crop Recommendation System <span class="text-success">🌱</span></h1>
67
+
68
+ <!-- adding form-->
69
+ <form action="/predict" method="POST">
70
+ <div class="row">
71
+ <div class="col-md-4">
72
+ <label for="Nitrogen">Nitrogen</label>
73
+ <input type="number" id="Nitrogen" name="Nitrogen" placeholder="Enter Nitrogen" class="form-control" required step="0">
74
+ </div>
75
+ <div class="col-md-4">
76
+ <label for="Phosporus">Phosphorus</label>
77
+ <input type="number" id="Phosporus" name="Phosporus" placeholder="Enter Phosphorus" class="form-control" required step="00">
78
+ </div>
79
+ <div class="col-md-4">
80
+ <label for="Potassium">Potassium</label>
81
+ <input type="number" id="Potassium" name="Potassium" placeholder="Enter Potassium" class="form-control" required step="0">
82
+ </div>
83
+ </div>
84
+
85
+ <div class="row mt-4">
86
+ <div class="col-md-4">
87
+ <label for="Temperature">Temperature</label>
88
+ <input type="number" step="0.01" id="Temperature" name="Temperature" placeholder="Enter Temperature in °C" class="form-control" required step="0">
89
+ </div>
90
+ <div class="col-md-4">
91
+ <label for="Humidity">Humidity</label>
92
+ <input type="number" step="0.01" id="Humidity" name="Humidity" placeholder="Enter Humidity in %" class="form-control" required step="0">
93
+ </div>
94
+ <div class="col-md-4">
95
+ <label for="pH">pH</label>
96
+ <input type="number" step="0.01" id="Ph" name="Ph" placeholder="Enter pH value" class="form-control" required step="0">
97
+ </div>
98
+ </div>
99
+
100
+ <div class="row mt-4">
101
+ <div class="col-md-4">
102
+ <label for="Rainfall">Rainfall</label>
103
+ <input type="number" step="0.01" id="Rainfall" name="Rainfall" placeholder="Enter Rainfall in mm" class="form-control" required>
104
+ </div>
105
+ </div>
106
+
107
+
108
+
109
+ <div class="row mt-4">
110
+
111
+ <div class="col-md-12 text-center">
112
+ <button type="submit" class="btn btn-primary btn-lg">Get Recommendation</button>
113
+ </div>
114
+ </div>
115
+ </form>
116
+
117
+
118
+
119
+ {% if result %}
120
+ <div class="card bg-dark" style="width: 18rem;">
121
+ <img src="{{url_for('static', filename='img.jpg')}}" class="card-img-top" alt="...">
122
+ <div class="card-body">
123
+ <h5 class="card-title">Recommend Crop for cultivation is:</h5>
124
+ <p class="card-text">{{ result }}</p>
125
+ </div>
126
+ </div>
127
+ {% endif %}
128
+ </div>
129
+
130
+ <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ENjdO4Dr2bkBIFxQpeoTz1HIcje39Wm4jDKdf19U8gI4ddQ3GYNS7NTKfAdVQSZe" crossorigin="anonymous"></script>
131
+ </body>
132
+ </html>
minmaxscaler.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:760d074bdbb55cef82c47ab687ab6b1d1680da57b1c72515d01f759a729a9544
3
+ size 760
model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:082afffa1041148988bf3f094265b401bba6f74ddadbcb3f3a85815978412a75
3
+ size 3535013
standscaler.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:034d859860b10c9e2f3f3fd06cadb7a274488a2fb90be8262c20b5351ebccf4f
3
+ size 617