# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/shamim237/ML-Handcrafted_Features/blob/main/HOG_RF_Bangla_Sign_Digit.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + id="g3Yoj5eT9Cfp"
import os 
import numpy as np
import matplotlib.pyplot as plt
import cv2
import pickle
from skimage.feature import hog
import random
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
from sklearn.metrics import f1_score
from sklearn.metrics import confusion_matrix

# + colab={"base_uri": "https://localhost:8080/"} id="ZFV3WsrJyXyS" outputId="29310aa4-dc4d-44d1-c1e4-de52d496c0b9"
from google.colab import drive
drive.mount("/content/drive", force_remount=True)

# + id="0HWo7bkN9YGx"
pick_in = open('/content/drive/MyDrive/Colab Notebooks/data_hog_sign(2).pickle', 'rb')
data = pickle.load(pick_in)
pick_in.close()

# + id="nwG59xAS9ctt"
random.shuffle(data)
features = []
labels = []

# + id="3SlsUlw2-eUP"
for feature,label in data:
    features.append(feature)
    labels.append(label)

# + id="_DCAMEr99jS-"
x_train, x_test, y_train, y_test = train_test_split(features, labels, test_size=0.11, shuffle=True)

# + id="Wg3XbpZr-lmm"
clf = RandomForestClassifier().fit(x_train, y_train)

# + id="OUxp8FWx9uK2"
rf_pred = clf.predict(x_test)

# + id="zES68ZZyoLhO"
pred_prob2 = clf.predict_proba(x_test)

# + colab={"base_uri": "https://localhost:8080/"} id="tnpGpcsW9jWp" outputId="cb875f64-a428-4724-c9e3-df203c695e2b"
rf_accuracy = accuracy_score(y_test, rf_pred)
rf_f1 = f1_score(y_test, rf_pred, average='weighted')
rf_precision = precision_score(y_test, rf_pred, average= 'weighted')
rf_recall = recall_score(y_test, rf_pred, average= 'weighted')
print('Accuracy (Random Forest): ', "%.2f" % (rf_accuracy*100))
print('F1 (Random Forest): ', "%.2f" % (rf_f1*100))
print('Precision (k-NN): ', "%.2f" % (rf_precision*100))
print('Recall(k-NN): ', "%.2f" % (rf_recall*100))

# + colab={"base_uri": "https://localhost:8080/"} id="YAjeyZS59jaC" outputId="f30854ae-5069-4101-a787-649ba48529a2"
from sklearn.metrics import classification_report, confusion_matrix
print(confusion_matrix(y_test, rf_pred))
print(classification_report(y_test, rf_pred))

# + colab={"base_uri": "https://localhost:8080/", "height": 285} id="PGJXwLfkAOW2" outputId="040cc3ad-1b1e-49e4-dedc-95368de3a4b8"
categories = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

print('Prediction is: ', categories[rf_pred[0]])

my_pred = x_test[0].reshape(128, 64)
plt.imshow(my_pred, cmap='binary_r')
plt.show()

# + id="7T_jJSLGAqaw"
n_classes= 10
from sklearn.metrics import roc_curve

fpr2, tpr2, thresh2 = roc_curve(y_test, pred_prob2[:,1], pos_label=1)

# + id="qw1ZrwCApjRj"
random_probs = [0 for i in range(len(y_test))]
p_fpr, p_tpr, _ = roc_curve(y_test, random_probs, pos_label=1)

# + colab={"base_uri": "https://localhost:8080/"} id="oM8I6qArpjev" outputId="6706002e-8c00-4ba0-e728-89e335fe06ef"
from sklearn.metrics import roc_auc_score

# auc scores
#auc_score1 = roc_auc_score(y_test, pred_prob1[:,1])
auc_score2 = roc_auc_score(y_test, pred_prob2, multi_class="ovo")

print(auc_score2)

# + colab={"base_uri": "https://localhost:8080/", "height": 376} id="KFXA1IZmrxtD" outputId="0a07d684-9eac-4cfd-f674-ef30941bc184"
fpr = {}
tpr = {}
thresh ={}

n_class = 10

for i in range(n_class):    
    fpr[i], tpr[i], thresh[i] = roc_curve(y_test, pred_prob2[:,i], pos_label=i)
    
# plotting    
plt.plot(fpr[0], tpr[0], linestyle='--',color='orange', 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='red', label='Class 2 vs Rest')
plt.plot(fpr[3], tpr[3], linestyle='--',color='yellow', label='Class 3 vs Rest')
plt.plot(fpr[4], tpr[4], linestyle='--',color='purple', label='Class 4 vs Rest')
plt.plot(fpr[5], tpr[5], linestyle='--',color='lime', label='Class 5 vs Rest')
plt.plot(fpr[6], tpr[6], linestyle='--',color='brown', label='Class 6 vs Rest')
plt.plot(fpr[7], tpr[7], linestyle='--',color='cyan', label='Class 7 vs Rest')
plt.plot(fpr[8], tpr[8], linestyle='--',color='maroon', label='Class 8 vs Rest')
plt.plot(fpr[9], tpr[9], linestyle='--',color='blue', label='Class 9 vs Rest')
plt.title('Multiclass ROC curve')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive rate')
plt.legend(loc='best')
plt.savefig('Multiclass ROC',dpi=400)

# + colab={"base_uri": "https://localhost:8080/", "height": 376} id="Ar6Ix3XIpjrX" outputId="cebab8d5-0cc7-493f-d9fe-b2484a83ca2c"
plt.figure(2)
plt.xlim(0, .8)
plt.ylim(0.92, 1.01)
plt.plot(fpr[0], tpr[0], linestyle='--',color='orange', 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='red', label='Class 2 vs Rest')
plt.plot(fpr[3], tpr[3], linestyle='--',color='yellow', label='Class 3 vs Rest')
plt.plot(fpr[4], tpr[4], linestyle='--',color='purple', label='Class 4 vs Rest')
plt.plot(fpr[5], tpr[5], linestyle='--',color='lime', label='Class 5 vs Rest')
plt.plot(fpr[6], tpr[6], linestyle='--',color='brown', label='Class 6 vs Rest')
plt.plot(fpr[7], tpr[7], linestyle='--',color='cyan', label='Class 7 vs Rest')
plt.plot(fpr[8], tpr[8], linestyle='--',color='maroon', label='Class 8 vs Rest')
plt.plot(fpr[9], tpr[9], linestyle='--',color='blue', label='Class 9 vs Rest')
plt.xlabel('False positive rate')
plt.ylabel('True positive rate')
plt.title('ROC curve (zoomed in at top left)')
plt.legend(loc='best')
plt.show()

# + colab={"base_uri": "https://localhost:8080/", "height": 607} id="ANDniHZfpj1e" outputId="281bd2ad-7236-4cdf-a146-e09532bfc7fd"
from sklearn.metrics import confusion_matrix
import pandas as pd
import seaborn as sn
import matplotlib.pyplot as plt
# %matplotlib inline
import numpy as np


data = confusion_matrix(y_test, rf_pred)
df_cm = pd.DataFrame(data, columns=np.unique(y_test), index = np.unique(y_test))
df_cm.index.name = 'Actual'
df_cm.columns.name = 'Predicted'
plt.figure(figsize = (10,9))
plt.title('Confusion Matrix (HOG-SVM(Polynomial))')
sn.set(font_scale=1.4)#for label size
sn.heatmap(df_cm, cmap="Blues", annot=True, fmt= 'g' ,annot_kws={"size": 12})# font size

# + id="fuyXLkORpj7d"


# + id="R6-KTtiUpj_1"


# + id="oy6p88AXpkEB"


# + id="jk9g9bM3pkJo"


# + id="ligZ6L2jpkNV"


StarcoderdataJupyterScriptsDedupFiltered