Exemple #1
0
    def test_fit_genetic_alg():
        """Test fit method using the genetic_alg algorithm"""

        network = LinearRegression(algorithm='genetic_alg',
                                   bias=False,
                                   learning_rate=1,
                                   clip_max=1,
                                   max_attempts=100)

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

        y = np.reshape(np.array([1, 1, 0, 0, 1, 1]), [6, 1])

        network.fit(X, y)
        fitted = network.fitted_weights

        assert (sum(fitted) < 4 and len(fitted) == 4 and min(fitted) >= -1
                and max(fitted) <= 1)
Exemple #2
0
    def test_fit_gradient_descent():
        """Test fit method using the gradient_descent algorithm"""

        network = LinearRegression(algorithm='gradient_descent',
                                   bias=False,
                                   learning_rate=0.1,
                                   clip_max=1,
                                   max_attempts=100)

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

        y = np.reshape(np.array([1, 1, 0, 0, 1, 1]), [6, 1])

        weights = np.array([1, 1, 1, 1])

        network.fit(X, y, init_weights=weights)
        fitted = network.fitted_weights

        assert (sum(fitted) <= 4 and len(fitted) == 4 and min(fitted) >= -1
                and max(fitted) <= 1)
Exemple #3
0
    def test_predict_bias():
        """Test predict method with bias term"""

        network = LinearRegression(algorithm='random_hill_climb',
                                   bias=True,
                                   learning_rate=1,
                                   clip_max=1,
                                   max_attempts=100)

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

        network.fitted_weights = np.array([1, 1, 1, 1, 1])
        network.node_list = [5, 1]
        network.output_activation = identity

        x = np.reshape(np.array([3, 1, 5, 5, 3, 2]), [6, 1])

        assert np.array_equal(network.predict(X), x)