A comparison of a 3 classifiers in scikit-learn
on iris dataset.
The iris dataset is a classic and very easy multi-class classification dataset.
Checking that the notebook is running on Google Colab or not.
import sys
try:
import google.colab
!{sys.executable} -m pip -q -q install pycm
except:
pass
import os
!{sys.executable} -m pip -q -q install scikit-learn
if "Example1_files" not in os.listdir():
os.mkdir("Example1_files")
from sklearn import datasets
from sklearn.model_selection import train_test_split
from pycm import ConfusionMatrix
iris = datasets.load_iris()
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
from sklearn import svm
classifier_1 = svm.SVC(kernel='linear', C=0.01)
y_pred_1 = classifier_1.fit(X_train, y_train).predict(X_test)
cm1=ConfusionMatrix(y_test, y_pred_1)
cm1.print_matrix()
cm1.print_normalized_matrix()
cm1.Kappa
cm1.Overall_ACC
cm1.SOA1 # Landis and Koch benchmark
cm1.SOA2 # Fleiss’ benchmark
cm1.SOA3 # Altman’s benchmark
cm1.SOA4 # Cicchetti’s benchmark
cm1.save_html(os.path.join("Example1_files", "cm1"))
from sklearn.tree import DecisionTreeClassifier
classifier_2 = DecisionTreeClassifier(max_depth=5)
y_pred_2 = classifier_2.fit(X_train, y_train).predict(X_test)
cm2=ConfusionMatrix(y_test, y_pred_2)
cm2.print_matrix()
cm2.print_normalized_matrix()
cm2.Kappa
cm2.Overall_ACC
cm2.SOA1 # Landis and Koch benchmark
cm2.SOA2 # Fleiss’ benchmark
cm2.SOA3 # Altman’s benchmark
cm2.SOA4 # Cicchetti’s benchmark
cm2.save_html(os.path.join("Example1_files","cm2"))
from sklearn.ensemble import AdaBoostClassifier
classifier_3 = AdaBoostClassifier()
y_pred_3 = classifier_3.fit(X_train, y_train).predict(X_test)
cm3=ConfusionMatrix(y_test, y_pred_3)
cm3.print_matrix()
cm3.print_normalized_matrix()
cm3.Kappa
cm3.Overall_ACC
cm3.SOA1 # Landis and Koch benchmark
cm3.SOA2 # Fleiss’ benchmark
cm3.SOA3 # Altman’s benchmark
cm3.SOA4 # Cicchetti’s benchmark
cm3.save_html(os.path.join("Example1_files", "cm3"))
from pycm import Compare
cp = Compare({"C-Support vector": cm1, "Decision tree": cm2, "AdaBoost": cm3})
print(cp)
cp.save_report(os.path.join("Example1_files", "cp"))