Exemple #1
0
def test_x_parameter_with_dimension_different_from_2_should_raise_exception_when_predicting(
):
    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(X_test)
Exemple #2
0
def test_x_parameter_with_different_number_of_columns_from_training_x_should_raise_exception_when_predicting(
):
    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(X_test)
Exemple #3
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)
Exemple #4
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
Exemple #5
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)
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))
print(neighbor.score(X,y))