def __init__(self, dimensions=0, k_neighbors=5, use_kernel=False): ''' Constructor for FaceRecognizer. Args (optional): k_rank (int): How many principal components to keep. k_neighbors (int): How many neighbors to compare against in the kNN classifier. ''' self.pca_model = PCAModel(dimensions=dimensions, use_kernel=use_kernel) self.knn_classifier = KNNClassifier(neighbors=k_neighbors) self.instances = None
def main(): df_beijing = pd.read_csv(BEIJING_PATH) df_shenyang = pd.read_csv(SHENYANG_PATH) X, Y = prepare_data_and_labels(df_beijing, df_shenyang) clf = KNNClassifier(K=5) validate(clf, X, Y) print("GUANGZHOU") df_guangzhou = pd.read_csv(GUANGZHOU_PATH) X_test, Y_test = prepare_data_and_labels(df_guangzhou) test(clf, X_test, Y_test) print("SHANGHAI") df_shanghai = pd.read_csv(SHANGHAI_PATH) X_test, Y_test = prepare_data_and_labels(df_shanghai) test(clf, X_test, Y_test)
import numpy as np import KNNClassifier import matplotlib.pyplot as plt if __name__ == '__main__': image_size = 28 # width and length no_of_different_labels = 10 # i.e. 0, 1, 2, 3, ..., 9 image_pixels = image_size * image_size data_path = "/mnist/" train_data = np.loadtxt(data_path + "mnist_train.csv", delimiter=",") test_data = np.loadtxt(data_path + "mnist_test.csv", delimiter=",") fac = 0.99 / 255 train_imgs = np.asfarray(train_data[:, 1:]) * fac + 0.01 test_imgs = np.asfarray(test_data[:, 1:]) * fac + 0.01 train_labels = np.asfarray(train_data[:, :1]) test_labels = np.asfarray(test_data[:, :1]) knn = KNNClassifier(distance='Euclidean', K=5) knn.fit(train_imgs, train_labels) results = knn.predict(test_imgs) # # for i in range(10): # img = train_imgs[i].reshape((28,28)) # plt.imshow(img, cmap="Greys") # plt.show() from sklearn.metrics import confusion_matrix confusion_matrix(test_labels, results)
########################################### ## KNN test kodları from sklearn.datasets import load_iris from sklearn.utils import shuffle iris_X, iris_y = load_iris(return_X_y=True) iris_X, iris_y = shuffle(iris_X, iris_y) X_train = iris_X[:-30] X_test = iris_X[-30:] y_train = iris_y[:-30] y_test = iris_y[-30:] from KNNClassifier import * knn = KNNClassifier("eucledean", 10) knn.buildModel(X_train, y_train) knn.evaluateModel(X_test, y_test) knn.showLabel(X_test[5], load_iris()) ######################################### ## Naive bayes test kodları from sklearn.datasets import load_iris from sklearn.utils import shuffle iris_X, iris_y = load_iris(return_X_y=True) iris_X, iris_y = shuffle(iris_X, iris_y) X_train = iris_X[:-30] X_test = iris_X[-30:] y_train = iris_y[:-30]