Example #1
0
    print("岭回归的系数(已经缩放至原尺寸):",unStandCoff)
    print("岭回归的截距(已经缩放至原尺寸):",-1*sum(multiply(unStandCoff,xMean))+yMean)
    print('岭回归RSS:', LoadData.rssError(Y, YHat))




if __name__ == '__main__':
    lgX = []
    lgY = []
    setDataCollect(lgX, lgY)
    lgX = mat(lgX)
    lgY = mat(lgY).T
    m = lgX.shape[0]
    n = lgX.shape[1]
    lgX1 = LoadData.addAllOneColumn(lgX)
    print('属性个数:', n)
    print('训练实例个数:', m)
    set_printoptions(suppress=True)
    print('属性矩阵:', lgX)
    print('类标签矩阵矩阵:', lgY.T)

    theta = regression.standRegres(lgX1, lgY)  # theta 是n*1 训练数据直接当测试数据
    print('线性回归系数为:', theta)
    YHat = lgX1 * theta  # YHat 是 m*1
    # for i in range(m):
    #     print('真实值:', lgY[i], '预测值:', YHat[i])
    print('标准线性回归RSS:', LoadData.rssError(lgY, YHat))

    crossValidationRidgeRegression(lgX, lgY)
import LoadData
import ForwardStagewiseR
import regression
import matplotlib.pyplot as plt
from numpy import *

X,Y,attNum,trainingSampleNum=LoadData.loadDataSet('abalone.txt')
xStd=std(X,0) #得到标准化前X的标准差
yMean=mean(Y,0) #得到中心化前的Y的均值
XStand,YCentered=LoadData.standardize(X),LoadData.centered(Y)

numIt=5000
allWS=ForwardStagewiseR.forwardStagewiseR(XStand,YCentered,0.005,numIt)
YHat1=XStand*(mat(allWS[numIt-1]).T)+yMean
allWS=allWS/xStd
print("前向逐步回归系数最后一次迭代系数为:",allWS[numIt-1])
print("前向逐步回归的Error为",LoadData.rssError(Y,YHat1))

xOne=LoadData.addAllOneColumn(X)
thetaStd=regression.standRegres(xOne, Y) # theta 是n*1 训练数据直接当测试数据
print('线性回归系数为:',thetaStd.T)
YHat2=xOne*thetaStd
print("标准线性回归的Error为",LoadData.rssError(Y,YHat2))

plt.plot(range(numIt),allWS)
plt.show()