Пример #1
0
def test_classifier_binary():
    data = load_breast_cancer()
    X = data.data
    y = data.target

    clf_skl = KNeighborsClassifier(3) #k=3
    clf_bareml = KNNClassifier(3) #k=3

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)

    clf_skl.fit(X_train,y_train)
    clf_bareml.fit(X_train,y_train)

    preds_skl = clf_skl.predict(X_test).tolist()
    preds_bareml = clf_bareml.predict(X_test).tolist()

    # should be the same result
    assert all([a == b for a, b in zip(preds_bareml, preds_skl)])
Пример #2
0
def test_gaussian():
    data = load_iris()
    X = data.data
    y = data.target

    clf_skl = GaussianNB_skl()
    clf_bareml = GaussianNB()

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)

    clf_skl.fit(X_train, y_train)
    clf_bareml.fit(X_train, y_train)

    preds_skl = clf_skl.predict(X_test).tolist()
    preds_bareml = clf_bareml.predict(X_test).tolist()

    # should be the same result
    assert all([a == b for a, b in zip(preds_bareml, preds_skl)])
Пример #3
0
def test_regressor():
    data = load_boston()
    X = data.data
    y = data.target

    reg_skl = KNeighborsRegressor(3) #k=3
    reg_bareml = KNNRegressor(3) #k=3

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)

    reg_skl.fit(X_train,y_train)
    reg_bareml.fit(X_train,y_train)

    preds_skl = reg_skl.predict(X_test).tolist()
    preds_bareml = reg_bareml.predict(X_test).tolist()

    # should be the same result
    assert all([a == b for a, b in zip(preds_bareml, preds_skl)])
Пример #4
0
def test_sigmoid_kernel():
    data = load_boston()
    X = data.data
    y = data.target

    reg_skl = SklKernelRidge(alpha=1.0,kernel='sigmoid')
    reg_bareml = KernelRidge(alpha=1.0,kernel='sigmoid')

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)

    reg_skl.fit(X_train,y_train)
    reg_bareml.fit(X_train,y_train)

    # round in 4 decimals
    preds_skl = np.round(reg_skl.predict(X_test),4).tolist() 
    preds_bareml = np.round(reg_bareml.predict(X_test),4).tolist()

    # should be the same result
    assert np.allclose(preds_bareml, preds_skl)
Пример #5
0
def test_polynomial_kernel():
    data = load_boston()
    X = data.data
    y = data.target

    reg_skl = SklKernelRidge(alpha=2.0,kernel='polynomial',degree=2)
    reg_bareml = KernelRidge(alpha=2.0,kernel='polynomial',degree=2)

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)

    reg_skl.fit(X_train,y_train)
    reg_bareml.fit(X_train,y_train)

    # with polynomial kernel, sklearn's result is not quite stable
    # so check with only 1 decimal
    preds_skl = np.round(reg_skl.predict(X_test),1).tolist() 
    preds_bareml = np.round(reg_bareml.predict(X_test),1).tolist()

    # should be the same result
    assert np.allclose(preds_bareml, preds_skl)
Пример #6
0
def test_gaussian_onehot():
    data = load_iris()
    X = data.data
    y = data.target

    clf_skl = GaussianNB_skl()
    clf_bareml = GaussianNB()

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
    
    # one-hot encoding for bareml classifier
    y_train_onehot = pd.get_dummies(y_train).values

    clf_skl.fit(X_train, y_train)
    clf_bareml.fit(X_train, y_train_onehot)

    preds_skl = clf_skl.predict(X_test).tolist()
    preds_bareml = np.argmax(clf_bareml.predict(X_test),axis=1)

    # should be the same result
    assert all([a == b for a, b in zip(preds_bareml, preds_skl)])
Пример #7
0
def test_ridge_gradient_descent():
    data = load_boston()
    X = data.data
    y = data.target

    reg_skl = Ridge(alpha=1.0)
    reg_bareml = RidgeRegression(alpha=1.0, solver='GD')

    X_train, X_test, y_train, y_test = train_test_split(X,
                                                        y,
                                                        test_size=0.3,
                                                        random_state=0)

    reg_skl.fit(X_train, y_train)
    reg_bareml.fit(X_train, y_train)

    preds_skl = reg_skl.predict(X_test)
    preds_bareml = reg_bareml.predict(X_test)

    # close enough
    assert all(np.abs(preds_skl - preds_bareml) < 2.0)
Пример #8
0
def test_ridge_analytical():
    data = load_boston()
    X = data.data
    y = data.target

    reg_skl = Ridge(alpha=1.0)
    reg_bareml = RidgeRegression(alpha=1.0, solver='analytical')

    X_train, X_test, y_train, y_test = train_test_split(X,
                                                        y,
                                                        test_size=0.3,
                                                        random_state=0)

    reg_skl.fit(X_train, y_train)
    reg_bareml.fit(X_train, y_train)

    # round in 4 decimals
    preds_skl = np.round(reg_skl.predict(X_test), 4).tolist()
    preds_bareml = np.round(reg_bareml.predict(X_test), 4).tolist()

    # should be the same result
    assert all([a == b for a, b in zip(preds_bareml, preds_skl)])