예제 #1
0
def test_NNC_imbalanced():
    # Make some training data
    train = Lab1.generate_data()
    x_train = train[['height', 'width']]
    y_train = train['label']

    # Make some new data to test with
    test = Lab1.generate_data(
        random_state=43,
        proportion=0.3)  # switch to class b as the most common
    x_test = test[['height', 'width']]
    y_test = test['label']

    clf = Lab1.OneNearestNeighborClassifier()  # Initialize
    clf.fit(x_train, y_train)  # Learn from the training data

    prediction = clf.predict(x_test)  # Make a prediction about the test data
    accuracy = np.sum(y_test == prediction) / len(y_test)
    target_accuracy = 0.65
    assert accuracy >= target_accuracy, f'Accuracy {accuracy} != {target_accuracy}'
예제 #2
0
def test_NNClassifier():
    # Make some training data
    train = Lab1.generate_data()
    x_train = train[['height', 'width']]
    y_train = train['label']

    # Make some new data to test with
    test = Lab1.generate_data(random_state=43)  # we want different test data
    x_test = test[['height', 'width']]
    y_test = test['label']

    classifier = Lab1.OneNearestNeighborClassifier()  # Initialize
    classifier.fit(x_train, y_train)  # Learn from the training data

    prediction = classifier.predict(
        x_test)  # Make a prediction about the test data

    accuracy = np.sum(y_test == prediction) / len(y_test)
    target_accuracy = 0.75
    assert accuracy >= target_accuracy, f'Accuracy {accuracy} != {target_accuracy}'