-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathml_model_fertilizerrecom.py
77 lines (59 loc) · 2.36 KB
/
ml_model_fertilizerrecom.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import imblearn
from imblearn.over_sampling import SMOTE
from collections import Counter
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
import pickle
import warnings
warnings.filterwarnings("ignore")
data = pd.read_csv("Fertilizer_Prediction.csv")
#print(data.head())
soil_type_label_encoder = LabelEncoder()
data["Soil Type"] = soil_type_label_encoder.fit_transform(data["Soil Type"])
crop_type_label_encoder = LabelEncoder()
data["Crop Type"] = crop_type_label_encoder.fit_transform(data["Crop Type"])
croptype_dict = {}
for i in range(len(data["Crop Type"].unique())):
croptype_dict[i] = crop_type_label_encoder.inverse_transform([i])[0]
#print(croptype_dict)
soiltype_dict = {}
for i in range(len(data["Soil Type"].unique())):
soiltype_dict[i] = soil_type_label_encoder.inverse_transform([i])[0]
#print(soiltype_dict)
fertname_label_encoder = LabelEncoder()
data["Fertilizer Name"] = fertname_label_encoder.fit_transform(data["Fertilizer Name"])
fertname_dict = {}
for i in range(len(data["Fertilizer Name"].unique())):
fertname_dict[i] = fertname_label_encoder.inverse_transform([i])[0]
#print(fertname_dict)
#print(data.head())
X = data[data.columns[:-1]]
y = data[data.columns[-1]]
#Upscaling the data
counter = Counter(y)
#print(counter)
upsample = SMOTE()
X, y = upsample.fit_resample(X, y)
counter = Counter(y)
#print(counter)
#print(f"Total Data after Upsampling: {len(X)}")#154
X_train, X_test, y_train, y_test = train_test_split(X.values, y, test_size = 0.2, random_state = 0)
#print(f"Train Data: {X_train.shape}, {y_train.shape}")
#print(f"Train Data: {X_test.shape}, {y_test.shape}")
#=================Random-ForestClassifier=========================================
rf_pipeline = make_pipeline(StandardScaler(), RandomForestClassifier(random_state = 18))
rf_pipeline.fit(X_train, y_train)
predictions = rf_pipeline.predict(X_test)
accuracy = accuracy_score(y_test, predictions)
print(f"Accuracy on Test Data: {accuracy*100}%")
pickle.dump(rf_pipeline, open("rf_pipeline.pkl", "wb"))