import regression from numpy import * xArr, yArr = regression.loadDataSet('abalone.txt') regression.stageWise(xArr, yArr, 0.01, 200) regression.stageWise(xArr, yArr, 0.001, 5000) xMat = mat(xArr) yMat = mat(yArr).T xMat = regression.regularize(xMat) yM = mean(yMat, 0) yMat = yMat - yM weights = regression.standRegres(xMat, yMat.T) print(weights.T)
import regression from numpy import * xArray, yArray = regression.loadDataSet("./regression/abalone.txt") # yHat01 = regression.lwlrTest(xArray[100:199], xArray[0:99], yArray[0:99], 0.1) # yHat1 = regression.lwlrTest(xArray[100:199], xArray[0:99], yArray[0:99], 1) # yHat10 = regression.lwlrTest(xArray[100:199], xArray[0:99], yArray[0:99], 10) # print regression.rssError(yArray[100:199], yHat01) # print regression.rssError(yArray[100:199], yHat1) # print regression.rssError(yArray[100:199], yHat10) # wsStand = regression.standRegres(xArray[0:99], yArray[0:99]) # yHat = mat(xArray[100:199]) * wsStand # print regression.rssError(yArray[100:199], yHat.T.A) # ridgeWeights = regression.ridgeTest(xArray, yArray) # import matplotlib.pyplot as plt # fig = plt.figure() # ax = fig.add_subplot(111) # ax.plot(ridgeWeights) # plt.show() print regression.stageWise(xArray, yArray, 0.01, 200)
yHat1 = regression.lwlrTest(abX[0:99], abX[0:99], abY[0:99], 1) yHat10 = regression.lwlrTest(abX[0:99], abX[0:99], abY[0:99], 10) Err0r01 = regression.rssError(abY[0:99],yHat01.T) Err0r1 = regression.rssError(abY[0:99],yHat1.T) Err0r10 = regression.rssError(abY[0:99],yHat10.T) print('Err0r01 = ',Err0r01) print('Err0r1 = ',Err0r1) print('Err0r10 = ',Err0r10) ''' #------------------------------------------------------ abX, abY = regression.loadDataSet('abalone.txt') ridgeWeights = regression.ridgeTest(abX, abY) #StandWeights =regression.standRegres(abX, abY) import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111) ax.plot(ridgeWeights) plt.show() #------------------------------------------------------ ''' xArr,yArr = regression.loadDataSet('abalone.txt') #print('xArr = ',xArr) #print('yArr = ',yArr) regression.stageWise(xArr,yArr) '''
# print(regression.lwlr(xArr[0], xArr, yArr, 1.0)) # print(regression.lwlr(xArr[0], xArr, yArr, 0.001)) # # yHat = regression.lwlrTest(xArr, xArr, yArr, 0.003) # xMat = mat(xArr) # srtInd = xMat[:, 1].argsort(0) # xSort = xMat[srtInd][:, 0, :] # # import matplotlib.pyplot as plt # fig = plt.figure() # ax = fig.add_subplot(111) # # ax.plot(xSort[:, 1], yHat[srtInd]) # ax.scatter(xMat[:, 1].flatten().A[0], mat(yArr).T.flatten().A[0], s=2, c='red') # plt.show() # abX, abY = regression.loadDataSet('../data/abalone.txt') # ridgeWeights = regression.ridgeTest(abX, abY) # print(ridgeWeights) # # import matplotlib.pyplot as plt # fig = plt.figure() # ax = fig.add_subplot(111) # ax.plot(ridgeWeights) # plt.show() xArr, yArr = regression.loadDataSet('../data/abalone.txt') regression.stageWise(xArr, yArr, 0.01, 200)
print(regression.lwlr(xArr[0],xArr,yArr,0.001)) ''' #权重图像 #regression.plotWei() #使用线性回归预测鲍鱼的年龄 #regression.predictAbaAge() #岭回归 #regression.testRidgeAbaLone() #逐步前向回归 xArr,yArr=regression.loadDataSet('abalone.txt') wsArry = regression.stageWise(xArr,yArr,0.005,1000) print(wsArry) fig=plt.figure(1) ax=fig.add_subplot(111) ax.plot(wsArry) plt.show() xMat=mat(xArr) yMat=mat(yArr).T #数据标准化 xMat=regression.regularize(xMat) yMat -= mean(yMat,0) weights=regression.standRegres(xMat,yMat.T)
# 岭回归 import regression dm, ls = regression.loadDataSet('abalone.txt') ridgeWeights = regression.ridgeTest(dm, ls) import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111) ax.plot(ridgeWeights) plt.show() # 前向逐步回归 import regression from numpy import * dm, ls = regression.loadDataSet('abalone.txt') wMat = regression.stageWise(dm, ls, 0.01, 200) wMat = regression.stageWise(dm, ls, 0.001, 5000) xMat = mat(dm) yMat = mat(ls).T xMat = (xMat - mean(xMat, 0)) / var(xMat, 0) yMat = yMat - mean(yMat, 0) regression.standRegres(xMat, yMat.T).T import regression regression.setDataCollect() import regression from numpy import * lgx, lgy = regression.loadLEGOData('out.txt') # ws = regression.standRegres(lgx,lgy) # lgx = mat(lgx)
# -*- coding:utf-8 -*- import regression from numpy import * xArr, yArr = regression.loadDataSet("abalone.txt") #stageWeight = regression.stageWise(xArr, yArr, 0.01, 200) #print (stageWeight) stageWeight = regression.stageWise(xArr, yArr, 0.0001, 50000) #print (stageWeight)
# -*- coding:utf-8 -*- import regression from numpy import * xArr,yArr = regression.loadDataSet("abalone.txt") #stageWeight = regression.stageWise(xArr, yArr, 0.01, 200) #print (stageWeight) stageWeight = regression.stageWise(xArr, yArr, 0.0001, 50000) #print (stageWeight)
import regression from numpy import * import matplotlib.pyplot as plt abX, abY = regression.loadDataSet('abalone.txt') regression.stageWise(abX, abY, 0.001, 5000)
ax = fig.add_subplot(111) ax.plot(ridgeWeights) plt.show() xArr, yArr = regression.loadDataSet('abalone.txt') #regression.stageWise(xArr, yArr, 0.01, 200) #regression.stageWise(xArr, yArr, 0.001, 5000) xMat, yMat = mat(xArr), mat(yArr).T xMat = regression.regularize(xMat) yM = mean(yMat, 0) yMat = yMat - yM weights = regression.standRegres(xMat, yMat.T) print weights.T weights = regression.stageWise(xArr, yArr, 0.005, 1000) fig = plt.figure() ax = fig.add_subplot(111) ax.plot(weights) plt.show() ''' lgX, lgY = [], [] regression.setDataCollect(lgX, lgY) print shape(lgX) lgX1 = mat(ones((58, 5))) lgX1[:, 1:5] = mat(lgX) print lgX1[0] ws = regression.standRegres(lgX1, lgY) print ws print lgX1[0] * ws
print(regression.rssError(abY[100:199],yHat01.T)) print(regression.rssError(abY[100:199],yHat1.T)) print(regression.rssError(abY[100:199],yHat10.T)) ws=regression.standRegres(abX[0:99],abY[0:99]) yHat=mat(abX[100:199])*ws print(regression.rssError(abY[100:199],yHat.T.A)) ''' ''' #---------------------岭回归-----------# ridgeWeights=regression.ridgeTest(abX,abY) import matplotlib.pyplot as plt fig=plt.figure() ax=fig.add_subplot(111) ax.plot(ridgeWeights) plt.show() ''' #---------------向前逐步回归----------# stage=regression.stageWise(abX,abY,0.005,1000) import matplotlib.pyplot as plt fig=plt.figure() ax=fig.add_subplot(111) ax.plot(stage) plt.show()
# abaloneStageWise.py import regression from numpy import * import matplotlib.pyplot as plt xArray, yArray = regression.loadDataSet('abalone.txt') xArray = array(xArray, dtype=float) yArray = array(yArray, dtype=float) # regression.stageWise(xArray, yArray, 0.01, 200) stageWeights = regression.stageWise(xArray, yArray, 0.001, 5000) fig = plt.figure() ax = fig.add_subplot(111) ax.plot(stageWeights) plt.show() print(' ---- lwlr output for this dataset : ---- ') xMat = mat(xArray) yMat = mat(yArray).T xMat = regression.regularize(xMat) yM = mean(yMat, 0) yMat = yMat - yM weights = regression.standRegres(xMat, yMat.T) print('weights.T : ', weights.T)
def test_stageWise(self): xArr, yArr = regression.load_dataset('abalone.txt') print regression.stageWise(xArr, yArr, 0.01, 200) regression.crossValidation(xArr, yArr, 10)
print corrcoef(yHat.T, yMat) plt.show() # 在训练集上预测鲍鱼年龄 featArr, ageArr = regression.loadDataSet('abalone.txt') # predictionArr01 = regression.lwlrTest(featArr[0:99], featArr[0:99],ageArr[0:99],0.0972) # predictionArr1 = regression.lwlrTest(featArr[0:99], featArr[0:99],ageArr[0:99],1.0) # predictionArr10 = regression.lwlrTest(featArr[0:99], featArr[0:99],ageArr[0:99],10) # # print regression.ressError(ageArr[0:99], predictionArr01.T) # print regression.ressError(ageArr[0:99], predictionArr1.T) # print regression.ressError(ageArr[0:99], predictionArr10.T) # 在测试集上预测鲍鱼年龄,在测试集上,k=2时效果最好 # predictionArr01 = regression.lwlrTest(featArr[100:199], featArr[0:99],ageArr[0:99],0.0972) # predictionArr1 = regression.lwlrTest(featArr[100:199], featArr[0:99],ageArr[0:99],1.0) # predictionArr10 = regression.lwlrTest(featArr[100:199], featArr[0:99],ageArr[0:99],2) # print regression.ressError(ageArr[100:199], predictionArr01.T) # print regression.ressError(ageArr[100:199], predictionArr1.T) # print regression.ressError(ageArr[100:199], predictionArr10.T) # 岭回归 这个有问题,下次看一下 ridgeWeights = regression.ridgeTest(featArr, ageArr) # # # 前向逐步回归 stageWise = regression.stageWise(featArr, ageArr, 0.01, 200) fig = plt.figure() ax = fig.add_subplot(111) ax.plot(ridgeWeights) # ax.plot(stageWise) # plt.show()
#srtind = b[:,1].argsort(0) #c = b[srtind] # #b = np.mat(b) #srtind = b[:,1].argsort(0) #c = b[srtind] # abX,abY = regression.loadDataSet('abalone.txt') #print(regression.stageWise(xArr,yArr,0.01,200)) ridgeweights = regression.ridgeTest(abX,abY) fig = plt.figure() ax = fig.add_subplot(111) ax.plot(ridgeweights) plt.show() returnMat = regression.stageWise(abX,abY,0.01,1000) fig = plt.figure() ax = fig.add_subplot(111) ax.plot(returnMat) plt.show()
ax = fig.add_subplot(111) ax.plot(ridgeWeights) plt.show() xArr, yArr = regression.loadDataSet('abalone.txt') #regression.stageWise(xArr, yArr, 0.01, 200) #regression.stageWise(xArr, yArr, 0.001, 5000) xMat, yMat = mat(xArr), mat(yArr).T xMat = regression.regularize(xMat) yM = mean(yMat, 0) yMat = yMat - yM weights = regression.standRegres(xMat, yMat.T) print weights.T weights = regression.stageWise(xArr, yArr, 0.005, 1000) fig = plt.figure() ax = fig.add_subplot(111) ax.plot(weights) plt.show() ''' lgX, lgY = [], [] regression.setDataCollect(lgX, lgY) print shape(lgX) lgX1 = mat(ones((58, 5))) lgX1[:, 1:5] = mat(lgX) print lgX1[0] ws = regression.standRegres(lgX1, lgY) print ws print lgX1[0] * ws print lgX1[-1] * ws