Esempio n. 1
0
from sklearn.metrics import confusion_matrix
import numpy as np


def evaluate(predictions, test_y):
    accuracy = sum(i == j for i, j in zip(predictions, test_y)) / len(test_y)

    return accuracy


n_realizations = 20
hit_rates = []
X, y = datasets.get_car_numbers_dataset()
# X = Extractor.lbp_extraction(X , 24, 8)
# X = Extractor.hu_extraction(X)
X = Extractor.glcm_extraction(X)

# create new a knn model
knn = KNeighborsClassifier()

for _ in range(n_realizations):
    # split dataset into train and test data
    X_train, X_test, y_train, y_test = train_test_split(X,
                                                        y,
                                                        test_size=0.2,
                                                        stratify=y)

    # create a dictionary of all values we want to test for n_neighbors
    param_grid = {'n_neighbors': np.arange(3, 20, 2)}

    # use gridsearch to test all values for n_neighbors