IRis
IRis
Aim:-
Software Required:-
Procedure/Code:-
from google.colab import files
uploaded = files.upload()
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
data = pd.read_csv("iris.csv")
data
data.head()
data.tail()
data.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 149 entries, 0 to 148
Data columns (total 5 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 5.1 149 non-null float64
1 3.5 149 non-null float64
2 1.4 149 non-null float64
3 0.2 149 non-null float64
4 Iris-setosa 149 non-null object
dtypes: float64(4), object(1)
memory usage: 5.9+ KB
data.isna().sum()
X=data.drop(['Class'],axis=1)
Y=data['Class']
print(X)
Y.shape
(150,)
predicted_naive=naive.predict(X_test)
print(predicted_naive)
[0 0 2 0 0 2 0 2 2 0 0 0 0 0 1 1 0 1 2 1 2 1 2 1 1 0 0 2 0 2]
import sklearn.metrics as matrics
cm_naive=matrics.confusion_matrix(Y_test,predicted_naive)
print(cm_naive)
[[14 0 0]
[ 0 7 1]
[ 0 0 8]]
0.9666666666666667
predicted_Knn=knn.predict(X_test)
predicted_Knn = knn.predict(X_test)
cmd = ConfusionMatrixDisplay(cm_naive, display_labels=['0', '1', '2'])
cmd.plot()
from sklearn.metrics import classification_report
print(classification_report(Y_test,predicted_Knn))
accuracy_knn=matrics.accuracy_score(Y_test,predicted_Knn)
print(accuracy_knn)
1.0
knn_provbs = knn.predict_proba(X_test)
fpr = {}
tpr = {}
thresh = {}
n_class = 3
# Move plotting outside the loop:
for i in range(n_class):
fpr[i], tpr[i], thresh[i] = roc_curve(Y_test, knn_provbs[:, i], pos_label=i)
predicted_lr=lr.predict(X_test)
cm_lr=matrics.confusion_matrix(Y_test,predicted_lr)
cmd=ConfusionMatrixDisplay(cm_lr,display_labels=['0','1','2'])
cmd.plot()
0.9666666666666667
lr_probs=lr.predict_proba(X_test)
fpr={}
tpr={}
thresh={}
n_class=3
for i in range(n_class):
fpr[i],tpr[i],thresh[i]=roc_curve(Y_test,lr_probs[:,i],pos_label=i)
plt.plot(fpr[0],tpr[0],linestyle='--',color='red',label='Class 0 Vs Rest')
plt.plot(fpr[1],tpr[1],linestyle='--',color='orange',label='Class 1 Vs Rest')
plt.plot(fpr[2],tpr[2],linestyle='--',color='green',label='Class 2 Vs Rest')
plt.title('Multiclass ROC Curve')
from sklearn import svm
svm=svm.SVC(probability=True)
svm.fit(X_train,Y_train)
predicted_svm=svm.predict(X_test)
cm_svm=matrics.confusion_matrix(Y_test,predicted_svm)
cmd=ConfusionMatrixDisplay(cm_svm,display_labels=['0','1','2'])
cmd.plot()
from sklearn.metrics import classification_report
print(classification_report(Y_test,predicted_svm))
accuracy_svm=matrics.accuracy_score(Y_test,predicted_svm)
accuracy_svm
0.9666666666666667
svm_probs=svm.predict_proba(X_test)
fpr={}
tpr={}
thresh={}
n_class=3
for i in range(n_class):
fpr[i],tpr[i],thresh[i]=roc_curve(Y_test,svm_probs[:,i],pos_label=i)
plt.plot(fpr[0],tpr[0],linestyle='--',color='red',label='class 0 Vs Rest')
plt.plot(fpr[1],tpr[1],linestyle='--',color='green',label='class 1 Vs Rest')
plt.plot(fpr[2],tpr[2],linestyle='--',color='orange',label='class 2 Vs Rest')
plt.title('Multiclass ROC Curve')
predicted_dtree=dtree.predict(X_test)
predicted_dtree
array([0, 0, 2, 0, 0, 1, 0, 2, 2, 0, 0, 0, 0, 0, 1, 1, 0, 1, 2, 1, 2, 1,
2, 1, 1, 0, 0, 2, 0, 2])
cm_dtree=matrics.confusion_matrix(Y_test,predicted_dtree)
cmd=ConfusionMatrixDisplay(cm_dtree,display_labels=['0','1','2'])
cmd.plot()
dtree_probs=dtree.predict_proba(X_test)
fpr={}
tpr={}
thresh={}
n_class=3
for i in range(n_class):
fpr[i],tpr[i],thresh[i]=roc_curve(Y_test,dtree_probs[:,i],pos_label=i)
plt.plot(fpr[0],tpr[0],linestyle='--',color='red',label='class 0 Vs Rest')
plt.plot(fpr[2],tpr[2],linestyle='--',color='orange',label='class 1 Vs Rest')
plt.title('Multiclass ROC Curve')
accuracy_dtree=matrics.accuracy_score(Y_test,predicted_dtree)
accuracy_dtree
0.9333333333333333
cm_rforest=matrics.confusion_matrix(Y_test,predicted_rforest)
cmd=ConfusionMatrixDisplay(cm_rforest,display_labels=['0','1','2'])
cmd.plot()
accuracy_rforest=matrics.accuracy_score(Y_test,predicted_rforest)
accuracy_rforest
0.9666666666666667
rforest_probs=rforest.predict_proba(X_test)
fpr={}
tpr={}
thresh={}
n_class=3
for i in range(n_class):
fpr[i],tpr[i],thresh[i]=roc_curve(Y_test,rforest_probs[:,i],pos_label=i)
plt.plot(fpr[0],tpr[0],linestyle='--',color='red',label='class 0 Vs Rest')
plt.plot(fpr[1],tpr[1],linestyle='--',color='green',label='class 1 Vs Rest')
plt.plot(fpr[2],tpr[2],linestyle='--',color='orange',label='class 2 Vs Rest')
plt.title('Multiclass ROC Curve')
results=pd.DataFrame({'Model':['Naive Bayes','KNN','SVM','Logistic
Regression','Decision Tree','Random Forest'],'Score':
['.966','1.0','.966','.966','.9333','.966']})
result_df=results.sort_values(by='Score',ascending=False)
result_df=result_df.set_index('Score')
result_df
result_df