rajan9089 commited on
Commit
2e90c43
·
verified ·
1 Parent(s): 5645562

Uploading_code_part_to_show

Browse files
Files changed (1) hide show
  1. Code_part_to_show.txt +272 -0
Code_part_to_show.txt ADDED
@@ -0,0 +1,272 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import numpy as np
3
+
4
+ crop = pd.read_csv("Crop_recommendation.csv")
5
+ crop.head()
6
+
7
+ crop.info()
8
+
9
+ # to check null value is present or not
10
+ crop.isnull().sum()
11
+
12
+ # to check duplicate value is present or not
13
+ crop.duplicated().sum()
14
+
15
+ # describe all the mathematical info of only numerical data
16
+ # 25 % === percentile
17
+ crop.describe()
18
+
19
+
20
+
21
+ #Exploring Data correlation
22
+ # corr = crop.corr()
23
+ # corr
24
+ # Select only numeric columns for correlation computation
25
+ numeric_columns = crop.select_dtypes(include=['number'])
26
+ # Compute the correlation matrix
27
+ corr = numeric_columns.corr()
28
+ corr
29
+
30
+
31
+
32
+
33
+ import seaborn as sns
34
+ sns.heatmap(corr , annot = True , cmap = 'coolwarm')
35
+
36
+
37
+
38
+ crop['label'].value_counts()
39
+
40
+
41
+
42
+
43
+ import matplotlib.pyplot as plt
44
+ sns.distplot(crop['N'])
45
+ plt.show()
46
+
47
+
48
+
49
+
50
+
51
+ #Encoding
52
+ crop_dict = {
53
+ 'rice': 1,
54
+ 'maize': 2,
55
+ 'jute': 3,
56
+ 'cotton': 4,
57
+ 'coconut': 5,
58
+ 'papaya': 6,
59
+ 'orange': 7,
60
+ 'apple': 8,
61
+ 'muskmelon': 9,
62
+ 'watermelon': 10,
63
+ 'grapes': 11,
64
+ 'mango': 12,
65
+ 'banana': 13,
66
+ 'pomegranate': 14,
67
+ 'lentil': 15,
68
+ 'blackgram': 16,
69
+ 'mungbean': 17,
70
+ 'mothbeans': 18,
71
+ 'pigeonpeas': 19,
72
+ 'kidneybeans': 20,
73
+ 'chickpea': 21,
74
+ 'coffee': 22
75
+ }
76
+ crop['crop_num'] = crop['label'].map(crop_dict) # 'crop_num' kuch v name de sakte
77
+
78
+
79
+
80
+
81
+ crop['crop_num'].value_counts()
82
+
83
+
84
+
85
+
86
+
87
+ crop.drop('label' , axis = 1 , inplace = True) # no need to do this
88
+ crop.head(500)
89
+
90
+
91
+
92
+
93
+
94
+ x = crop.drop('crop_num' , axis = 1)
95
+ y = crop['crop_num']
96
+
97
+
98
+
99
+ # Train Test Split
100
+ from sklearn.model_selection import train_test_split
101
+ x_train , x_test , y_train , y_test = train_test_split(x , y , test_size = 0.2 , random_state = 42)
102
+
103
+
104
+
105
+
106
+ x_train.shape
107
+
108
+
109
+ x_test.shape
110
+
111
+
112
+
113
+ # Scale the features using MinMaxScaler
114
+ from sklearn.preprocessing import MinMaxScaler
115
+ ms = MinMaxScaler()
116
+
117
+ # ms.fit(x_train)
118
+ x_train = ms.fit_transform(x_train)
119
+ x_test = ms.transform(x_test)
120
+
121
+
122
+
123
+
124
+
125
+
126
+
127
+ # Standardization
128
+
129
+ from sklearn.preprocessing import StandardScaler
130
+ sc = StandardScaler()
131
+
132
+ # sc.fit(x_train)
133
+
134
+ x_train = sc.fit_transform(x_train)
135
+ x_test = sc.transform(x_test)
136
+
137
+
138
+
139
+
140
+
141
+
142
+
143
+ # Training Models
144
+
145
+
146
+ from sklearn.linear_model import LogisticRegression
147
+ from sklearn.naive_bayes import GaussianNB
148
+ from sklearn.svm import SVC
149
+ from sklearn.neighbors import KNeighborsClassifier
150
+ from sklearn.tree import DecisionTreeClassifier
151
+ from sklearn.tree import ExtraTreeClassifier
152
+ from sklearn.ensemble import RandomForestClassifier
153
+ from sklearn.ensemble import BaggingClassifier
154
+ from sklearn.ensemble import GradientBoostingClassifier
155
+ from sklearn.ensemble import AdaBoostClassifier
156
+ from sklearn.metrics import accuracy_score
157
+
158
+ # create instances of all models
159
+ models = {
160
+ 'Logistic Regression': LogisticRegression(),
161
+ 'Support Vector Machine': SVC(),
162
+ 'K-Nearest Neighbors': KNeighborsClassifier(),
163
+ 'Decision Tree': DecisionTreeClassifier(),
164
+ 'Bagging': BaggingClassifier(),
165
+ 'AdaBoost': AdaBoostClassifier(),
166
+ 'Gradient Boosting': GradientBoostingClassifier(),
167
+ 'Extra Trees': ExtraTreeClassifier(),
168
+ 'Naive Bayes': GaussianNB(),
169
+ 'Random Forest': RandomForestClassifier()
170
+ }
171
+
172
+ # md = model
173
+ for name, md in models.items():
174
+ md.fit(x_train,y_train)
175
+ ypred = md.predict(x_test)
176
+
177
+ print(f"{name} with accuracy : {accuracy_score(y_test,ypred)}")
178
+
179
+
180
+
181
+
182
+
183
+
184
+
185
+
186
+
187
+
188
+ rfc = RandomForestClassifier()
189
+ rfc.fit(x_train,y_train)
190
+ ypred = rfc.predict(x_test)
191
+ accuracy_score(y_test,ypred)
192
+
193
+
194
+
195
+
196
+
197
+
198
+ # Predictive System
199
+
200
+
201
+ def recommendation(N,P,k,temperature,humidity,ph,rainfal):
202
+ features = np.array([[N,P,k,temperature,humidity,ph,rainfal]])
203
+ transformed_features = ms.transform(features)
204
+ transformed_features = sc.transform(transformed_features)
205
+ prediction = rfc.predict(transformed_features).reshape(1,-1) # .reshape(1,-1) karne se single row ka o/p dega
206
+
207
+ return prediction[0] # returns {1,2,3,....,22}
208
+
209
+
210
+ #The .reshape(1, -1) part reshapes the prediction array into a 2-dimensional array with 1 row and as many columns as necessary to fit the data
211
+
212
+
213
+
214
+
215
+
216
+
217
+
218
+
219
+
220
+ # N = 40
221
+ # P = 50
222
+ # k = 50
223
+ # temperature = 40.0
224
+ # humidity = 20.0
225
+ # ph = 100.0
226
+ # rainfall = 100.0
227
+
228
+
229
+ # N = 30
230
+ # P = 10
231
+ # k = 100
232
+ # temperature = 100.0
233
+ # humidity = 210.0
234
+ # ph = 100.0
235
+ # rainfall = 23.0
236
+
237
+ N = 30
238
+ P = 20
239
+ k = 150
240
+ temperature = 23 # Best for apple
241
+ humidity = 60
242
+ ph = 5.5
243
+ rainfall = 900
244
+
245
+ predict = recommendation(N,P,k,temperature,humidity,ph,rainfall)
246
+
247
+
248
+ crop_dict = {1: "Rice", 2: "Maize", 3: "Jute", 4: "Cotton", 5: "Coconut", 6: "Papaya", 7: "Orange",
249
+ 8: "Apple", 9: "Muskmelon", 10: "Watermelon", 11: "Grapes", 12: "Mango", 13: "Banana",
250
+ 14: "Pomegranate", 15: "Lentil", 16: "Blackgram", 17: "Mungbean", 18: "Mothbeans",
251
+ 19: "Pigeonpeas", 20: "Kidneybeans", 21: "Chickpea", 22: "Coffee"}
252
+
253
+ if predict[0] in crop_dict:
254
+ crop = crop_dict[predict[0]]
255
+ print("{} is a best crop to be cultivated ".format(crop))
256
+ else:
257
+ print("Sorry are not able to recommend a proper crop for this environment")
258
+
259
+
260
+
261
+
262
+
263
+
264
+
265
+ import pickle
266
+ pickle.dump(rfc , open('model.pkl' , 'wb')) # wb = write binary
267
+ # now 'model.pkl' is our model which can be used anywhere
268
+
269
+
270
+
271
+
272
+