<repo_name>LavanyaSrini/Machine_learning<file_sep>/Eigen_KNN.py
#run Eigen_KNN.py
#from sklearn.metrics import accuracy_score
#accuracy_score(label_p, predictions)









from sklearn.neighbors import KNeighborsClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix 
from PIL import Image
from imutils import paths
import numpy as np
import argparse
import os
from sklearn.metrics import accuracy_score
from sklearn.decomposition import PCA
import scipy.linalg as la
import cv2




ap = argparse.ArgumentParser()
ap.add_argument("-d", "--dataset", type=str, default="Gallery_GENI",
	help="path to directory containing the 'Gallery' dataset")

ap.add_argument("-d1", "--dataset1", type=str, default="Probe_GENI",
	help="path to directory containing the 'Probe' dataset1")


ap.add_argument("-m", "--model", type=str, default="knn",
	help="type of python machine learning model to use")
args = vars(ap.parse_args())


models = {
	"knn": KNeighborsClassifier(n_neighbors=1),

}



imagePaths = paths.list_images(args["dataset"])
X_data = []
labels = []


for imagePath in imagePaths:

	image = Image.open(imagePath)
	image=np.array(image)
	X_data.append(image)
	label = imagePath.split(os.path.sep)[-2]
	labels.append(label)
X_data=np.array(X_data)
n_samples = X_data.shape[0]

pca = PCA()
X_data=X_data.reshape(X_data.shape[0],X_data.shape[1]*X_data.shape[2])


X_centered = X_data - np.mean(X_data, axis=0)
cov_matrix = np.dot(X_centered.T, X_centered) / n_samples
evals, evecs = np.linalg.eig(cov_matrix)
idx = np.argsort(evals)[::-1]
evecs = evecs[:,idx]
evals = evals[idx]
variance_retained=np.cumsum(evals)/np.sum(evals)


var_per=.98
index=np.argmax(variance_retained>=var_per)

evecs = evecs[:,:index+1]

reduced_data=np.dot(evecs.T, X_data.T).T

clf=PCA(var_per)
X_train=X_data

X_train=clf.fit_transform(X_train)

le = LabelEncoder()
labels = le.fit_transform(labels)

model = models[args["model"]]

model.fit(X_train,labels)



#-------------------------

imagePaths1 = paths.list_images(args["dataset1"])
X_data1 = []
label_p = []


for imagePath1 in imagePaths1:
	image1 = Image.open(imagePath1)
	image1 = np.array(image1)
	X_data1.append(image1)
	label1 = imagePath1.split(os.path.sep)[-2]
	label_p.append(label1)
X_data1=np.array(X_data1)
n_samples1 = X_data1.shape[0]
pca1 = PCA()

X_data1 = X_data1.reshape(X_data1.shape[0],X_data1.shape[1]*X_data1.shape[2])

X_test1=clf.transform(X_data1)

le = LabelEncoder()
label_p = le.fit_transform(label_p)







predictions = model.predict(X_test1)    
print(predictions)    
    
    
  
    













<file_sep>/README.md
# Machine_learning

Simple_classifier.py 
Extract mean and standard deviation as features and classify using knn, lda, svm...

PCA_KNN.py
Extract features using standard pca and classify using knn.

Eigen_PCA.py
Extract features by calculating covmatrix,eigen values and eigen vectors calculate pca and classify using knn.

Try with data provided in Zip files.
<file_sep>/PCA_KNN.py
# large dataset is vertorized and detect using knn
#run PCA_KNN.py
#from sklearn.metrics import accuracy_score
#accuracy_score(label_p, predictions)


from sklearn.neighbors import KNeighborsClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix 
from PIL import Image
from imutils import paths
import numpy as np
import argparse
import os
from sklearn.metrics import accuracy_score
from sklearn.decomposition import PCA
import scipy.linalg as la






ap = argparse.ArgumentParser()
ap.add_argument("-d", "--dataset", type=str, default="Gallery",
	help="path to directory containing the 'Gallery' dataset")

ap.add_argument("-d1", "--dataset1", type=str, default="Probe",
	help="path to directory containing the 'Probe' dataset1")


ap.add_argument("-m", "--model", type=str, default="knn",
	help="type of python machine learning model to use")
args = vars(ap.parse_args())


models = {
	"knn": KNeighborsClassifier(n_neighbors=1),

}



imagePaths = paths.list_images(args["dataset"])
X_data = []
labels = []


for imagePath in imagePaths:

	image = Image.open(imagePath)
	image=np.array(image)
	X_data.append(image)
	label = imagePath.split(os.path.sep)[-2]
	labels.append(label)
X_data=np.array(X_data)
X_data=X_data.reshape(X_data.shape[0],X_data.shape[1]*X_data.shape[2])
X_mean=np.mean(X_data)
X_data=X_data-X_mean
X_data= np.array(X_data)
pca = PCA(.80)
principalComponents1 = pca.fit_transform(X_data)
le = LabelEncoder()
labels = le.fit_transform(labels)
print(labels)










print("[INFO] using '{}' model".format(args["model"]))
model = models[args["model"]]

model.fit(X_data,labels)





imagePaths1 = paths.list_images(args["dataset1"])
X_data1 = []
label_p = []

for imagePath1 in imagePaths1:

	image1 = Image.open(imagePath1)
	image1 = np.array(image1)

	X_data1.append(image1)
	label1 = imagePath1.split(os.path.sep)[-2]
	label_p.append(label1)
X_data1=np.array(X_data1)
X_data1=X_data1.reshape(X_data1.shape[0],X_data1.shape[1]*X_data1.shape[2])
X_mean1=np.mean(X_data1)
X_data1=X_data1-X_mean1
X_data1= np.array(X_data1)
pca = PCA(.80)
principalComponents2 = pca.fit_transform(X_data1)
print(principalComponents2.shape)
le = LabelEncoder()
label_p = le.fit_transform(label_p)
   
predictions = model.predict(X_data1)    
print(predictions)    
    
    




<file_sep>/Simple_classifier.py
# Split train and test dataset and run all classifier


from sklearn.neighbors import KNeighborsClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix 
from PIL import Image
from imutils import paths
import numpy as np
import argparse
import os

def extract_color_stats(image):

	features = [np.mean(image), np.std(image),
		]


	return features


ap = argparse.ArgumentParser()
ap.add_argument("-d", "--dataset", type=str, default="3scenes",
	help="path to directory containing the '3scenes' dataset")
ap.add_argument("-m", "--model", type=str, default="knn",
	help="type of python machine learning model to use")
args = vars(ap.parse_args())


models = {
	"knn": KNeighborsClassifier(n_neighbors=1),
	"naive_bayes": GaussianNB(),
	"logit": LogisticRegression(solver="lbfgs", multi_class="auto"),
	"svm": SVC(kernel="linear"),
	"decision_tree": DecisionTreeClassifier(),
	"random_forest": RandomForestClassifier(n_estimators=100),
	"mlp": MLPClassifier(),
	"lda": LinearDiscriminantAnalysis()   
}


#print("[INFO] extracting image features...")
imagePaths = paths.list_images(args["dataset"])
data = []
labels = []


for imagePath in imagePaths:

	image = Image.open(imagePath)
	features = extract_color_stats(image)
	data.append(features)


	label = imagePath.split(os.path.sep)[-2]
	labels.append(label)


le = LabelEncoder()
labels = le.fit_transform(labels)


(trainX, testX, trainY, testY) = train_test_split(data, labels,
	test_size=0.50)



print("[INFO] using '{}' model".format(args["model"]))
model = models[args["model"]]
model.fit(trainX, trainY)


print("[INFO] evaluating...")
predictions = model.predict(testX)
print(classification_report(testY, predictions,
	target_names=le.classes_))
results = confusion_matrix(testY, predictions) 
print('confusion_matrix',results)
