print('{}: w = {}'.format(i, w.T[0, :])) 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)) # 绘制逐步线性回归回归系数变化轨迹
def lasso_traj(X, y, ntest=30): ''' 获取回归系数轨迹矩阵 ''' _, n = X.shape ws = np.zeros((ntest, n)) for i in range(ntest): print(u'lasso_traj【30次】ntest:', i) 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('NYHXB.txt') #对于多因变量 X, y = standarize(X), standarize(y) ntest = 30 #绘制轨迹 ws = lasso_traj(X, y, ntest) #ws = lasso_traj(XX, yy, ntest) print(u'回归系数轨迹ws:', ws, np.shape(ws)) fig = plt.figure() ax = fig.add_subplot(111) lambdas = [i - 0 for i in range(ntest)] plt.title(u'NYHXB') plt.xlabel('Iteration') plt.ylabel('w') ax.plot(lambdas, ws) plt.show()
# 获取此点相应的回归系数 xWx = X.T * W * X if np.linalg.det(xWx) == 0: print('xWx is a singular matrix') return w = xWx.I * X.T * W * Y return w if '__main__' == __name__: k = 0.03 X, Y = load_data('ex0.txt') y_prime = [] for x in X.tolist(): w = lwlr(x, X, Y, k).reshape(1, -1).tolist()[0] y_prime.append(np.dot(x, w)) corrcoef = get_corrcoef(np.array(Y.reshape(1, -1)), np.array(y_prime)) print('Correlation coefficient: {}'.format(corrcoef)) fig = plt.figure() ax = fig.add_subplot(111) # 绘制数据点 x = X[:, 1].reshape(1, -1).tolist()[0] y = Y.reshape(1, -1).tolist()[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()
W[i, i] = exp((np.linalg.norm(x - xi))/(-2*k**2)) # 获取此点相应的回归系数 xWx = X.T*W*X if np.linalg.det(xWx) == 0: print('xWx is a singular matrix') return w = xWx.I*X.T*W*Y return w if '__main__' == __name__: k = 0.03 X, Y = load_data('ex0.txt') y_prime = [] for x in X.tolist(): w = lwlr(x, X, Y, k).reshape(1, -1).tolist()[0] y_prime.append(np.dot(x, w)) corrcoef = get_corrcoef(np.array(Y.reshape(1, -1)), np.array(y_prime)) print('Correlation coefficient: {}'.format(corrcoef)) fig = plt.figure() ax = fig.add_subplot(111) # 绘制数据点 x = X[:, 1].reshape(1, -1).tolist()[0] y = Y.reshape(1, -1).tolist()[0]
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('bingji_28.txt') #对于多因变量 #XX, yy = load_data00('MXPC.txt') #print'xx:',xx X, y = standarize(X), standarize(y) #XX, yy = standarize(XX), standarize(yy) #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 #绘制轨迹