import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
from PIL import Image
from numpy import asarray
import cv2
import matplotlib.pyplot as plt
import os

data=[]
label=[]

dir=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','del','nothing','space']
for i in dir:
    j=0
    print(i)
    for file in os.listdir('/kaggle/input/asl-alphabet/asl_alphabet_train/asl_alphabet_train/{}'.format(i)):
        img = Image.open(os.path.join('/kaggle/input/asl-alphabet/asl_alphabet_train/asl_alphabet_train/',i,str(file)))
        ndata = asarray(img)
        ndata=cv2.resize(ndata,(100,100)).flatten()
        data.append(ndata)
        label.append(i)
        j+=1
        if j>1000:
            break       
print(len(data))
test_data=[]
test_lable=[]
for file in os.listdir('/kaggle/input/asl-alphabet/asl_alphabet_test/asl_alphabet_test/'):
    img = Image.open(os.path.join('/kaggle/input/asl-alphabet/asl_alphabet_test/asl_alphabet_test/',str(file)))
    ndata = asarray(img)
    ndata=cv2.resize(ndata,(100,100)).flatten()
    test_data.append(ndata)
  

print(len(test_data))
"""
Decision Tree Classification
"""
#label encoder
from sklearn import preprocessing
 
# label_encoder object knows how to understand word labels.
label_encoder = preprocessing.LabelEncoder()
 
# Encode labels in column 'species'.
label= label_encoder.fit_transform(label)
print(np.unique(label))
test_label = []
for i in np.unique(label):
    if i!= 26:
        test_label.append(i)
from sklearn.tree import DecisionTreeRegressor 

treemodel = DecisionTreeRegressor(random_state = 0) 

treemodel.fit(data,label)
pred=treemodel.predict(test_data)
pred
from sklearn.metrics import confusion_matrix,accuracy_score

score = accuracy_score(test_label,pred)
print(score)
matrix= confusion_matrix(test_label,pred)  
matrix
"""
**logistic regression**
"""
from sklearn.linear_model import LogisticRegression
logisticRegr = LogisticRegression(solver="liblinear")
logisticRegr.fit(data,label)

sco=logisticRegr.score(test_data,test_label)
print(sco)
"""
**KNN classifier**

"""
from sklearn.neighbors import KNeighborsClassifier  
classifier= KNeighborsClassifier(n_neighbors=6, metric='minkowski', p=2 ) 
classifier.fit(data,label)

pred=classifier.predict(test_data)

from sklearn.metrics import confusion_matrix,accuracy_score
matrixx= confusion_matrix(test_label, pred)  
print("confusion matrix:")
print(matrixx)
print("accuracy:")
print(accuracy_score(testY,pred))