コード例 #1
0
    def test_RidgeLinearRegression_normal_fit_data1_2(self, data1):
        y = data1[:, -1:]
        X = data1[:, :-1]
        lr = RidgeLinearRegression(_lambda=1)
        lr.fit(X, y, strategy="normal_equation")

        assert_allclose([[-3.889], [1.192]], lr.theta, rtol=0, atol=0.001)
コード例 #2
0
    def test_RidgeLinearRegression_cost_data2_1(self, data2):
        y = data2[:, -1:]
        X = data2[:, :-1]
        _, n = X.shape
        theta = ones((n + 1, 1), dtype=float64)
        lr = RidgeLinearRegression(theta, _lambda=0)

        assert_allclose([[64828197300.798]], lr.cost(X, y), rtol=0, atol=0.001)
コード例 #3
0
    def test_RidgeLinearRegression_fit_MBGD3(self, data1):
        y = data1[:, -1:]
        X = data1[:, :-1]
        m, n = X.shape
        theta = ones((n + 1, 1), dtype=float64)
        lr = RidgeLinearRegression(theta, _lambda=100)
        lr.fit(X, y, strategy="MBGD", alpha=1, num_iters=1, b=m)

        assert_allclose([[-2.321], [-24.266]], lr.theta, rtol=0, atol=0.001)
コード例 #4
0
    def test_RidgeLinearRegression_cost_data1_2(self, data1):

        y = data1[:, -1:]
        X = data1[:, :-1]
        _, n = X.shape
        theta = ones((n + 1, 1), dtype=float64)
        lr = RidgeLinearRegression(theta, _lambda=100)

        assert_allclose([[10.781984]], lr.cost(X, y), rtol=0, atol=0.001)
コード例 #5
0
    def test_RidgeLinearRegression_fit_MBGD4(self, data1):
        X = array([[0, 1, 2], [-1, 5, 3], [2, 0, 1]])
        y = array([[0.3], [1.2], [0.5]])
        lr = RidgeLinearRegression(_lambda=10)
        lr.fit(X, y, strategy="MBGD", alpha=1, num_iters=1, b=1)

        assert_allclose(array([[8.3], [-0.8], [132.3], [123.8]]),
                        lr.theta,
                        rtol=0,
                        atol=0.001)
コード例 #6
0
    def test_RidgeLinearRegression_fit_BGD1(self, data2):
        y = data2[:, -1:]
        X = data2[:, :-1]
        lr = RidgeLinearRegression(_lambda=0)
        lr.fit(X, y, strategy="BGD", alpha=1, num_iters=1)

        assert_allclose([[340412.659], [764209128.191], [1120367.702]],
                        lr.theta,
                        rtol=0,
                        atol=0.001)
コード例 #7
0
    def test_RidgeLinearRegression_fit_MBGD2(self, err):
        X = array([[0, 1, 2], [-1, 5, 3], [2, 0, 1]])
        y = array([[0.3], [1.2], [0.5]])
        lr = RidgeLinearRegression(_lambda=0)
        lr.fit(X, y, strategy="MBGD", alpha=1, num_iters=1, b=1)

        assert_allclose(array([[2.3], [11.2], [-11.7], [-2.2]]),
                        lr.theta,
                        rtol=0,
                        atol=0.001,
                        equal_nan=False)
コード例 #8
0
    def test_RidgeLinearRegression_fit_unknown(self, data1):
        X = array([[0, 1, 2], [-1, 5, 3], [2, 0, 1]])
        y = array([[0.3], [1.2], [0.5]])
        lr = RidgeLinearRegression(_lambda=10)

        with pytest.raises(ValueError) as excinfo:
            lr.fit(X, y, strategy="oompa_loompa")

        msg = excinfo.value.args[0]
        assert msg == ("'oompa_loompa' (type '<class 'str'>') was passed. ",
                       'The strategy parameter for the fit function should ',
                       "be 'BGD' or 'SGD' or 'MBGD' or 'normal_equation'.")
コード例 #9
0
    def test_RidgeLinearRegression_constructor1(self, data1):
        theta = ones((3, 1), dtype=float64)
        lr = RidgeLinearRegression(theta, _lambda=13.50)

        assert lr._lambda == 13.50
        assert_allclose(array([[1.], [1.], [1.]]),
                        lr.theta,
                        rtol=0,
                        atol=0.001)
コード例 #10
0
    def test_RidgeLinearRegression_predict2(self):
        X = array([[3.5]])
        theta = array([[-3.6303], [1.1664]])
        lr = RidgeLinearRegression(theta, _lambda=10)

        assert_allclose([[0.4521]], lr.predict(X), rtol=0, atol=0.001)
コード例 #11
0
    def test_RidgeLinearRegression_constructor2(self):
        lr = RidgeLinearRegression()

        assert lr.theta is None
        assert lr._lambda == 0