for j in range(n):
            for sign in [-1, 1]:
                w_test = w.copy()
                w_test[j, 0] += eps * sign
                test_error = rss(X, y, w_test)
                if test_error < min_error:
                    min_error = test_error
                    w = w_test
        all_ws[i, :] = w.T

    return all_ws


if '__main__' == __name__:
    X, y = load_data('abalone.txt')
    X, y = standarize(X), standarize(y)

    epsilon = 0.005
    niter = 1000
    all_ws = stagewise_regression(X, y, eps=epsilon, niter=niter)

    w = all_ws[-1, :]
    y_prime = X * w.T

    # 计算相关系数
    corrcoef = get_corrcoef(np.array(y.reshape(1, -1)),
                            np.array(y_prime.reshape(1, -1)))
    print('Correlation coefficient: {}'.format(corrcoef))

    # 绘制逐步线性回归回归系数变化轨迹
Exemplo n.º 2
0
    return w

def lasso_traj(X, y, ntest=30):
    ''' 获取回归系数轨迹矩阵
    '''
    _, n = X.shape
    ws = np.zeros((ntest, n))
    for i in range(ntest):
        w = lasso_regression(X, y, lambd=exp(i-10))
        ws[i, :] = w.T
        print('lambda = e^({}), w = {}'.format(i-10, w.T[0, :]))
    return ws

if '__main__' == __name__:
    X, y = load_data('abalone.txt')
    X, y = standarize(X), standarize(y)
#    w = lasso_regression(X, y, lambd=10)
#
#    y_prime = X*w
#    # 计算相关系数
#    corrcoef = get_corrcoef(np.array(y.reshape(1, -1)),
#                            np.array(y_prime.reshape(1, -1)))
#    print('Correlation coefficient: {}'.format(corrcoef))


    ntest = 30

    # 绘制轨迹
    ws = lasso_traj(X, y, ntest)
    fig = plt.figure()
    ax = fig.add_subplot(111)