コード例 #1
0
def test_y_parameter_with_dimension_different_from_1_should_raise_exception_when_fitting(
):
    X = np.array([[1, 2], [3, 4]])
    y = np.array([[5], [6]])
    knn = KNN()

    with pytest.raises(InvalidDimension):
        knn.fit(X, y)
コード例 #2
0
def test_y_parameter_with_length_different_of_number_of_rows_in_x_should_raise_exception_when_fitting(
):
    X = np.array([[1, 2], [3, 4]])
    y = np.array([5, 6, 7])
    knn = KNN()

    with pytest.raises(IncompatibleShape):
        knn.fit(X, y)
コード例 #3
0
def test_x_parameter_with_dimension_different_from_2_should_raise_exception_when_fitting(
):
    X = np.array([1, 2, 3, 4])
    y = np.array([5, 6])
    knn = KNN()

    with pytest.raises(InvalidDimension):
        knn.fit(X, y)
コード例 #4
0
def test_prediction_with_manhattan_distance():
    X = np.array([[1, 1], [1000, 1000], [2, 2], [980, 1099], [3, 3],
                  [1100, 1010]])
    y = np.array([1, 2, 1, 2, 1, 2])
    X_test = np.array([[1, 5], [999, 999], [15, 15], [1500, 1500], [50, 50]])
    y_test = np.array([1, 2, 1, 2, 1])
    knn = KNN(metric='manhattan')
    knn.fit(X, y)

    predicted = knn.predict(X_test)

    assert np.all(predicted == y_test)
コード例 #5
0
def test_parameters_when_fitting_must_have_equal_values_but_being_different_instance(
):
    X = np.array([[1, 2], [3, 4]])
    y = np.array([5, 6])
    knn = KNN()

    knn.fit(X, y)

    assert (X == knn.X).all()
    assert id(knn.X) != id(X)
    assert (y == knn.y).all()
    assert id(knn.y) != id(y)
    assert len(knn.classes) == 2
コード例 #6
0
def test_using_iris_dataset():
    iris = datasets.load_iris()
    X = iris.data
    y = iris.target

    X_train, X_test, y_train, y_test = train_test_split(X,
                                                        y,
                                                        test_size=0.2,
                                                        random_state=42)

    model = KNN(k=3, metric="euclidean")
    model.fit(X_train, y_train)
    y_pred = model.predict(X_test)

    acc = round(accuracy_score(y_test, y_pred), 2)

    assert acc == 1.0
コード例 #7
0
def test_compare_with_sklearn_knn_when_using_euclidean_distance():
    rng = np.random.RandomState(42)
    metric = 'euclidean'
    X, y = make_classification(n_samples=700, random_state=rng)
    X_train, X_test, y_train, y_test = train_test_split(X,
                                                        y,
                                                        test_size=0.3,
                                                        random_state=42)

    sk_knn = KNeighborsClassifier(n_neighbors=3, metric=metric)
    sk_knn.fit(X_train, y_train)
    sk_result = sk_knn.predict(X_test)

    knn = KNN(k=3, metric=metric)
    knn.fit(X_train, y_train)
    y_predicted = knn.predict(X_test)

    assert np.all(y_predicted == sk_result)
コード例 #8
0
def test_knn_predict_probabilities_comparing_with_sklearn_knn():
    digits = datasets.load_digits()
    metric = 'euclidean'
    k = 11
    X = digits.data
    y = digits.target
    X_train, X_test, y_train, y_test = train_test_split(X,
                                                        y,
                                                        test_size=0.2,
                                                        random_state=42)

    sk_knn = KNeighborsClassifier(n_neighbors=k, metric=metric)
    sk_knn.fit(X_train, y_train)
    sk_result = sk_knn.predict_proba(X_test)

    model = KNN(k=k, metric=metric)
    model.fit(X_train, y_train)
    result = model.predict_proba(X_test)

    assert np.all(result == sk_result)
コード例 #9
0
def test_x_parameter_with_dimension_different_from_2_should_raise_exception_when_predicting_proba(
):
    X = np.array([[1, 1], [1000, 1000], [2, 2], [980, 1099], [3, 3],
                  [1100, 1010]])
    y = np.array([1, 2, 1, 2, 1, 2])
    X_test = np.array([1, 5, 999, 999, 15, 15, 1500, 1500, 50, 50])

    knn = KNN()
    knn.fit(X, y)

    with pytest.raises(InvalidDimension):
        knn.predict_proba(X_test)
コード例 #10
0
def test_x_parameter_with_different_number_of_columns_from_training_x_should_raise_exception_when_predicting_proba(
):
    X = np.array([[1, 1], [1000, 1000], [2, 2], [980, 1099], [3, 3],
                  [1100, 1010]])
    y = np.array([1, 2, 1, 2, 1, 2])
    X_test = np.array([[1, 5, 5], [999, 999, 999], [15, 15, 15],
                       [1500, 1500, 1500], [50, 50, 50]])

    knn = KNN()
    knn.fit(X, y)

    with pytest.raises(IncompatibleShape):
        knn.predict_proba(X_test)
コード例 #11
0
from src.knn import KNN
from sklearn import datasets
from sklearn.model_selection import train_test_split
import numpy as np 


# Iris Dataset 
iris = datasets.load_iris()
X_train, X_temp, y_train, y_temp = \
    train_test_split(iris.data, iris.target, test_size=.3)
X_validation, X_test, y_validation, y_test = \
    train_test_split(X_temp, y_temp, test_size=.3)

# Initialize KNN algorithm
neighbor = KNN()

# Feed dataset into KNN
neighbor.fit(X_train,y_train)

# Evaluate prediction of KNN 
print(neighbor.predict(X_train))
print(neighbor.score(X_train, y_train))
print(neighbor.score(X_test, y_test))

#####################################################################
# Multi-class classification Toy example 
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [8, 8]])
y = np.array([1,1,1,0,0,2])

neighbor.fit(X,y)
print(neighbor.predict(X))
コード例 #12
0
def test_invalid_metric_should_raise_exception():
    metric = 'a invalid metric'

    with pytest.raises(InvalidMetric):
        KNN(metric=metric)
コード例 #13
0
def test_set_k_parameter():
    knn = KNN(k=11)

    assert knn.k == 11