Esempio n. 1
0
    def test_score_matches_sklearn_performance(self):
        SLR = sklearn_LR()
        SLR.fit(self.X_train, self.Y_train)
        SLR_score = SLR.score(self.X_test, self.Y_test)

        self.test_LR.fit(self.X_train, self.Y_train)
        test_LR_score = self.test_LR.score(self.X_test, self.Y_test)

        self.assertAlmostEqual(SLR_score, test_LR_score, 1)
Esempio n. 2
0
 def __init__(self, penalty='l2', tol=1e-6, C=1.0, class_weight=None, max_iter=500, name="LogisticRegression"):
     super(LogisticRegression, self).__init__(name)
     self.penalty = penalty
     self.C = C
     self.class_weight = class_weight
     self.max_iter = max_iter
     self.tol = tol
     self.model = sklearn_LR(penalty=self.penalty, tol=self.tol, C=self.C, solver='sag',
                             class_weight=self.class_weight, multi_class='multinomial')
Esempio n. 3
0
def test_linear_regression_positive_vs_nonpositive(setup):
    # Test differences with LinearRegression when positive=False.
    X, y = make_sparse_uncorrelated(random_state=0)

    # reg = LinearRegression(positive=True)
    reg = sklearn_LR(positive=True)
    reg.fit(X, y)
    regn = LinearRegression(positive=False)
    regn.fit(X, y)

    assert np.mean(((reg.coef_ - regn.coef_)**2).to_numpy()) > 1e-3
Esempio n. 4
0
def test_linear_regression(setup):
    # Regular model fitting, #samples > 2, #features >= 2
    X = [[1, 1.5], [1.8, 2], [4, 5]]
    Y = [1, 2, 3]

    reg = LinearRegression()
    reg.fit(X, Y)

    model = sklearn_LR()
    model.fit(X, Y)

    assert_array_almost_equal(reg.coef_, model.coef_)
    assert_array_almost_equal(reg.intercept_, model.intercept_)
    assert_array_almost_equal(reg.predict(X), model.predict(X))

    # Regular model fitting, #samples <= 2, # features < 2
    error_msg = (re.escape("Does not support sigular matrix!"))

    X = [[1], [2]]
    Y = [1, 2]

    reg = LinearRegression()
    reg.fit(X, Y)

    model = sklearn_LR()
    model.fit(X, Y)

    assert_array_almost_equal(reg.coef_, model.coef_)
    assert_array_almost_equal(reg.intercept_, model.intercept_)
    assert_array_almost_equal(reg.predict(X), model.predict(X))

    # Extra case #1: singluar matrix, degenerate input
    error_msg = (re.escape("Does not support sigular matrix!"))

    X = [[1]]
    Y = [0]

    reg = LinearRegression()
    with pytest.raises(NotImplementedError, match=error_msg):
        reg.fit(X, Y)
Esempio n. 5
0
def test_linear_regression_positive_vs_nonpositive_when_positive(setup):
    # Test LinearRegression fitted coefficients
    # when the problem is positive.
    n_samples = 200
    n_features = 4
    X = rng.rand(n_samples, n_features)
    y = X[:, 0] + 2 * X[:, 1] + 3 * X[:, 2] + 1.5 * X[:, 3]

    # reg = LinearRegression(positive=True)
    reg = sklearn_LR(positive=True)
    reg.fit(X, y)
    regn = LinearRegression(positive=False)
    regn.fit(X, y)

    assert np.mean(((reg.coef_ - regn.coef_)**2).to_numpy()) < 1e-6
Esempio n. 6
0
results['手撕']['随机森林'] = my_score
print('\n')
del my_model, sklearn_model
# ---------------------------------------------------------------------- #

# ----------------------------   逻辑回归   ---------------------------- #
print('开始训练逻辑回归... ')
lda = LDA(dims=128)
lda.fit(x_train, y_train)
_x_train = lda.transform(x_train)
_x_test = lda.transform(x_test)
_x_train = normalize(_x_train)
_x_test = normalize(_x_test)

my_model = my_LR(lr=0.1, dropout=0.1)
sklearn_model = sklearn_LR()
print('训练sklearn模型... ', end='')
sklearn_model.fit(_x_train, y_train)
print('Done!')
y_pred = sklearn_model.predict(_x_test)
sklearn_score = accuracy_score(y_test, y_pred)
print('sklearn逻辑回归准确率:{:.4}'.format(sklearn_score))
print('训练手撕模型... ', end='')
my_model.fit(_x_train, y_train, epochs=50)
print('Done!')
y_pred = my_model.predict(_x_test)
my_score = accuracy_score(y_test, y_pred)
print('手撕逻辑回归准确率:{:.4}'.format(my_score))
results['sklearn']['逻辑回归'] = sklearn_score
results['手撕']['逻辑回归'] = my_score
print('\n')