コード例 #1
0
    def test_lwlr(self):
        # python -m unittest tests_regression.Tests_Regression.test_lwlr
        import locally_weighted_linear_regression as lwlr1
        from discomll.regression import locally_weighted_linear_regression as lwlr2

        x_train, y_train, x_test, y_test = datasets.regression_data()
        train_data, test_data = datasets.regression_data_discomll()

        lwlr1 = lwlr1.Locally_Weighted_Linear_Regression()
        taus = [1, 10, 25]
        sorted_indices = np.argsort([str(el) for el in x_test[:, 1].tolist()])

        for tau in taus:
            thetas1, estimation1 = lwlr1.fit(x_train, y_train, x_test, tau=tau)
            thetas1, estimation1 = np.array(thetas1)[sorted_indices], np.array(
                estimation1)[sorted_indices]

            results = lwlr2.fit_predict(train_data, test_data, tau=tau)
            thetas2, estimation2 = [], []

            for x_id, (est, thetas) in result_iterator(results):
                estimation2.append(est)
                thetas2.append(thetas)

            self.assertTrue(np.allclose(thetas1, thetas2, atol=1e-8))
            self.assertTrue(np.allclose(estimation1, estimation2, atol=1e-3))
コード例 #2
0
    def test_lwlr(self):
        # python -m unittest tests_regression.Tests_Regression.test_lwlr
        import locally_weighted_linear_regression as lwlr1
        from discomll.regression import locally_weighted_linear_regression as lwlr2

        x_train, y_train, x_test, y_test = datasets.regression_data()
        train_data, test_data = datasets.regression_data_discomll()

        lwlr1 = lwlr1.Locally_Weighted_Linear_Regression()
        taus = [1, 10, 25]
        sorted_indices = np.argsort([str(el) for el in x_test[:, 1].tolist()])

        for tau in taus:
            thetas1, estimation1 = lwlr1.fit(x_train, y_train, x_test, tau=tau)
            thetas1, estimation1 = np.array(thetas1)[sorted_indices], np.array(estimation1)[sorted_indices]

            results = lwlr2.fit_predict(train_data, test_data, tau=tau)
            thetas2, estimation2 = [], []

            for x_id, (est, thetas) in result_iterator(results):
                estimation2.append(est)
                thetas2.append(thetas)

            self.assertTrue(np.allclose(thetas1, thetas2, atol=1e-8))
            self.assertTrue(np.allclose(estimation1, estimation2, atol=1e-3))
コード例 #3
0
            W = np.diag(weights)  # diagonal matrix with weights
            x_W = np.dot(X.T, W)
            A = np.dot(x_W, X)
            b = np.dot(x_W, y)
            thetas.append(np.linalg.lstsq(A, b)[0])  # calculate thetas for given x with: A^-1 * b
            estimation.append(np.dot(x, thetas[-1]))  # calculate estimation for given x and thetas

        return thetas, estimation


if __name__ == '__main__':
    """ 
    Example was taken from: 
    http://www.dsplog.com/2012/02/05/weighted-least-squares-and-locally-weighted-linear-regression/  
    """
    import datasets
    import matplotlib.pyplot as plt

    X, y = datasets.regression_data()

    lwlr = Locally_Weighted_Linear_Regression()
    taus = [1, 10, 25]
    plt.scatter(X[:, 1], y)  # Plot train data

    color = ["r", "g", "b"]
    for i, tau in enumerate(taus):
        thetas, estimation = lwlr.fit(X, y, X, tau=tau)
        plt.plot(X[:, 1], estimation, c=color[i])  # Plot prediction

    plt.show()
コード例 #4
0
            A = np.dot(x_W, X)
            b = np.dot(x_W, y)
            thetas.append(np.linalg.lstsq(
                A, b)[0])  # calculate thetas for given x with: A^-1 * b
            estimation.append(np.dot(
                x, thetas[-1]))  # calculate estimation for given x and thetas

        return thetas, estimation


if __name__ == '__main__':
    """ 
    Example was taken from: 
    http://www.dsplog.com/2012/02/05/weighted-least-squares-and-locally-weighted-linear-regression/  
    """
    import datasets
    import matplotlib.pyplot as plt

    X, y = datasets.regression_data()

    lwlr = Locally_Weighted_Linear_Regression()
    taus = [1, 10, 25]
    plt.scatter(X[:, 1], y)  # Plot train data

    color = ["r", "g", "b"]
    for i, tau in enumerate(taus):
        thetas, estimation = lwlr.fit(X, y, X, tau=tau)
        plt.plot(X[:, 1], estimation, c=color[i])  # Plot prediction

    plt.show()