Пример #1
0
 def test_linear_regression_qr(self):
     X = numpy.array([[1, 0.5, 0], [0, 0.4, 2]], dtype=float).T
     y = numpy.array([1, 1.3, 3.9])
     b1 = linear_regression(X, y)
     b3 = linear_regression(X, y, algo="gram")
     b2 = linear_regression(X, y, algo="qr")
     self.assertEqualArray(b1, b3)
     self.assertEqualArray(b1, b2)
Пример #2
0
 def test_dim_lin_reg(self):
     X = rnd.randn(100, 7)
     eps = rnd.randn(100, 1) / 3
     y = X.sum(axis=1).reshape((X.shape[0], 1)) + eps
     y = y.ravel()
     b1 = linear_regression(X, y)
     b3 = linear_regression(X, y, algo="gram")
     b2 = linear_regression(X, y, algo="qr")
     self.assertEqualArray(b1.ravel(), b3.ravel())
     self.assertEqualArray(b1.ravel(), b2.ravel())
Пример #3
0
    def test_streaming_linear_regression(self):
        X0 = numpy.array([[1, 0.5, 10., 5., -2.], [0, 0.4, 20, 4., 2.],
                          [0, 0.4, 19, 4.2, 2.], [0, 0.7, 18, 4.1, 1.4]],
                         dtype=float).T
        y0 = numpy.array([1., 2.1, 3.1, 3.9, 5.])

        for dim in (2, 3, 4):
            X = X0[:, :dim]
            y = y0
            algo1 = []
            for i in range(dim, X.shape[0] + 1):
                bk = linear_regression(X[:i], y[:i])
                algo1.append(bk)
            algo2 = []
            self.assertRaise(
                lambda: list(streaming_linear_regression(X.T, y)),  # pylint: disable=W0640
                RuntimeError)
            for i, bk in enumerate(streaming_linear_regression(X, y)):
                algo2.append(bk.copy())
                self.assertNotEmpty(bk)
                self.assertEqualArray(algo1[i], algo2[i])
            self.assertEqual(len(algo1), len(algo2))
Пример #4
0
 def test_lineear_regression(self):
     X = numpy.array([[1, 0.5, 0], [0, 0.4, 2]], dtype=float).T
     y = numpy.array([1, 1.3, 3.9])
     b1 = linear_regression(X, y)
     b2 = linear_regression(X, y, algo="gram")
     self.assertEqualArray(b1, b2)