コード例 #1
0
    def test_prepare_y(self):
        lor = LinearOrdinalRegression(None, None)
        y = np.array([1, 3, 5])
        y_data = lor._prepare_y(y)

        assert_array_equal(y_data, np.array([1, 2, 3]))
        assert lor.n_classes == 3
コード例 #2
0
 def test_log_likelihood(self):
     lor = LinearOrdinalRegression(None, None)
     X = np.array([[1.0], [1.0], [1.0]])
     y = np.array([1, 1, 2])
     coefficients = np.array([1.0, 1.0])
     lor.n_attributes = 1
     lor.link = expit
     assert lor._log_likelihood(coefficients, X, y) == -3.0 * np.log(0.5)
コード例 #3
0
    def test_get_column_names_array(self):
        X = np.array(None)
        lor = LinearOrdinalRegression(None, None)
        lor.n_attributes = 2

        expected = pd.DataFrame(['column_1', 'column_2'],
                                columns=['attribute names'])
        assert_frame_equal(lor._get_column_names(X), expected)
コード例 #4
0
 def test_score(self, X_ucla, y_ucla):
     X = np.array([
         [1.0, 0.0, 0.0],
         [0.0, 1.0, 0.0],
         [0.0, 2.0, 1.0],
     ])
     y = [1, 2, 3]
     lor = LinearOrdinalRegression(None, None)
     lor.beta_ = np.array([1.0, 0.5, -1.0])
     assert lor._compute_score(X, y) == (0.0 - 3.0) / (3.0 * 2.0 / 2.0)
コード例 #5
0
    def test_indicator(self):
        lor = LinearOrdinalRegression(None, None)
        y = np.array([0, 1, 3, 1, 0])
        y_data = lor._prepare_y(y)

        expected_indicator_plus = np.array([[1, 0, 0, 0, 1], [0, 1, 0, 1, 0]])

        expected_indicator_minus = np.array([[0, 1, 0, 1, 0], [0, 0, 1, 0, 0]])

        assert_array_equal(lor._indicator_plus, expected_indicator_plus)
        assert_array_equal(lor._indicator_minus, expected_indicator_minus)
コード例 #6
0
    def test_compute_basis_change(self):
        lor = LinearOrdinalRegression(None, None)
        lor.n_attributes = 2
        lor.n_classes = 5

        expected_P = np.array([[1., 0., 0., 0., 0., 0.],
                               [0., 1., 0., 0., 0., 0.],
                               [0., 0., 1., 0., 0., 0.],
                               [0., 0., 1., 1., 0., 0.],
                               [0., 0., 1., 1., 1., 0.],
                               [0., 0., 1., 1., 1., 1.]])
        assert_array_equal(expected_P, lor._compute_basis_change())
コード例 #7
0
    def test_prepare_X(self):
        lor = LinearOrdinalRegression(None, None)
        X = np.array([[-1, 0, 1], [0, 1, -1], [1, -1, 0], [-3, 3, -3],
                      [3, -3, 3]])
        X_data, X_scale, X_mean, X_std = lor._prepare_X(X)

        assert_array_equal(X_data, X)
        assert_array_equal(X_scale, X / 2.0)
        assert_array_equal(X_mean, np.array([0, 0, 0]))
        assert_array_equal(X_std, np.array([2, 2, 2]))
        assert lor.N == 5
        assert lor.n_attributes == 3
コード例 #8
0
 def test_predict_linear_product(self, X_ucla, y_ucla):
     lor = LinearOrdinalRegression(None, None)
     lor.beta_ = np.array([1, -1, 2])
     assert lor.predict_linear_product(np.ones(3)) == 1 + -1 + 2
     assert lor.predict_linear_product(np.array(
         [1, 0, 1.5])) == 1 * 1 + -1 * 0 + 2 * 1.5
コード例 #9
0
    def test_get_column_names_df(self):
        X = pd.DataFrame(columns=['a', 'b'])

        expected = pd.DataFrame(['a', 'b'], columns=['attribute names'])
        assert_frame_equal(
            LinearOrdinalRegression(None, None)._get_column_names(X), expected)
コード例 #10
0
 def test_vanishing_variance_raises_error(self):
     lor = LinearOrdinalRegression(None, None)
     X = np.array([[1, 1], [1, 2], [1, 3]])
     with pytest.raises(ValueError):
         lor._prepare_X(X)
コード例 #11
0
 def test_prepare_X_from_single_column_dataframe(self, X_ucla, y_ucla):
     X_ucla = X_ucla.iloc[:, 0]
     lor = LinearOrdinalRegression(None, None)
     lor._prepare_X(X_ucla)
     assert lor.n_attributes == 1