'How to get second best predicted value by Python code using Random Forest Technique?
I have tried to learn a machine learning with Random Forest classifier. Now I can predict best crop using following python code. Screenshot is displayed below. screenshot of best crop
Now I want to know how to get the second best recommended crop using this code. What should I do? My existing Code is like this.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import classification_report
from sklearn import metrics
from sklearn import tree
import warnings
warnings.filterwarnings('ignore')
df = pd.read_csv('crops.csv')
df.head()
df.tail()
#print(df.head())
df.describe()
#print(df.describe())
df['Crop_Label'].unique()
#list_crop= df['Crop_Label'].unique()
#print(list_crop)
#b = np.array('Maniocs')
#c = np.setdiff1d(list_crop,b)
#print(c)
s = df.corr()
#print(s)
sns.heatmap(s,annot = True)
features = df[['Avg_Temp','Avg_Rainfall','Avg_Humidity','Extent','Production']]
target = df['Crop_Label']
print(target)
# Initialzing empty lists to append all model's name and corresponding name
acc = []
model = []
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(features,target,test_size= 0.2,random_state = 2)
from sklearn.model_selection import cross_val_score
from sklearn.ensemble import RandomForestClassifier
RF = RandomForestClassifier(n_estimators=29, criterion = 'entropy',random_state=0)
RF.fit(X_train,y_train)
predicted = RF.predict(X_test)
x = metrics.accuracy_score(y_test,predicted)
acc.append(x)
model.append('Random Forest')
#print("Random Forest Accuracy is ",x * 100)
#print(classification_report(y_test,predicted))
score = cross_val_score(RF,features,target,cv = 2)
score
data = np.array([[29,150, 80, 24006, 100]])
prediction = RF.predict(data)
print(prediction)
My sample CSV file is here. Link is :- https://drive.google.com/file/d/1IcIwZQI08sQxxTNOV0MPP30llkjvqtD2/view?usp=sharing
Please give me any idea to get the second best crop using prediction using above code. Thank You.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
