- 积分
- 21884
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2013-10-31
- 最后登录
- 1970-1-1
|
GrADS
系统平台: |
Python |
问题截图: |
- |
问题概况: |
定义的三个函数中,xArr和yArr应该是散点图中x和y轴的一元序列,但是testPoint和testArr指的是什么呢?报错也没有调试好,画出来的拟合曲线是断断续续的 |
我看过提问的智慧: |
看过 |
自己思考时长(天): |
2 |
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
本帖最后由 南气斑织逍遥 于 2019-10-18 11:22 编辑
from numpy import *
#====================用线性回归找到最佳拟合曲线===========
#加载数据集
def loadDataSet(filename):
numFeat = len(open(filename).readline().split("\t")) -1
dataMat = []; labelMat = []
fr = open(filename)
for line in fr.readlines():
lineArr = []
curLine = line.strip().split("\t")
for i in range(numFeat):
lineArr.append(float(curLine))
dataMat.append(lineArr)
labelMat.append(float(curLine[-1]))
return dataMat,labelMat
#计算最佳拟合曲线
def standRegress(xArr,yArr):
xMat = mat(xArr); yMat = mat(yArr).T #.T代表转置矩阵
xTx = xMat.T * xMat
if linalg.det(xTx) ==0.0: #linalg.det(xTx) 计算行列式的值
print ("This matrix is singular , cannot do inverse")
return
ws = xTx.I * (xMat.T * yMat)
return ws
#测试上边的函数
xArr,yArr = loadDataSet("ex0.txt")
ws = standRegress(xArr, yArr)
print ("ws(相关系数):",ws) #ws 存放的就是回归系数
#画图展示
def show():
import matplotlib.pyplot as plt
xMat = mat(xArr); yMat = mat(yArr)
yHat = xMat*ws
fig = plt.figure() #创建绘图对象
ax = fig.add_subplot(111) #111表示将画布划分为1行2列选择使用从上到下第一块
#scatter绘制散点图
ax.scatter(xMat[:,1].flatten().A[0],yMat.T[:,0].flatten().A[0])
#复制,排序
xCopy =xMat.copy()
xCopy.sort(0)
yHat = xCopy * ws
#plot画线
ax.plot(xCopy[:,1],yHat)
plt.show()
show()
#利用numpy库提供的corrcoef来计算预测值和真实值得相关性
yHat = mat(xArr) * ws #yHat = xMat * ws
print ("相关性:",corrcoef(yHat.T,mat(yArr)) )
#====================用线性回归找到最佳拟合曲线===========
import matplotlib.pyplot as plt
#==================局部加权线性回归================
def lwlr(testPoint,xArr,yArr,k=1.0):
xMat = mat(xArr); yMat = mat(yArr).T
m = shape(xMat)[0]
weights = mat(eye((m))) #产生对角线矩阵
for j in range(m):
diffMat = testPoint - xMat[j,:]
#更新权重值,以指数级递减
weights[j,j] = exp(diffMat * diffMat.T /(-2.0*k**2))
xTx = xMat.T * (weights * xMat)
if linalg.det(xTx) == 0.0:
print ("this matrix is singular,cannot do inverse")
return
ws = xTx.I * (xMat.T * (weights * yMat))
return testPoint * ws
def lwlrTest(testArr,xArr,yArr,k=1.0):
m = shape(testArr)[0]
yHat = zeros(m)
for i in range(m):
yHat =lwlr(testArr,xArr,yArr,k)
return yHat
xArr,yArr = loadDataSet('ex.txt')
#xArr = loadtxt('/disk5/lzb/sunying/scatter_20yr/pr-x.txt')
#yArr = loadtxt('/disk5/lzb/sunying/scatter_20yr/pr-y.txt')
print ("k=1.0:",lwlr(xArr[0],xArr,yArr,1.0))
print ("k=0.001:",lwlr(xArr[0],xArr,yArr,0.001))
print ("k=0.003:",lwlr(xArr[0],xArr,yArr,0.003))
#画图
def showlwlr():
yHat = lwlrTest(xArr, xArr, yArr, 0.003)
xMat = mat(xArr)
srtInd = xMat[:,1].argsort(0)
xSort = xMat[srtInd][:,0,:]
fig = plt.figure() #创建绘图对象
ax = fig.add_subplot(111) #111表示将画布划分为1行2列选择使用从上到下第一块
ax.plot(xSort[:,1],yHat[srtInd])
#scatter绘制散点图
ax.scatter(xMat[:,1].flatten().A[0],mat(yArr).T[:,0].flatten().A[0],s=2,c='red')
plt.show()
showlwlr()
报错信息:this matrix is singular,cannot do inverse
this matrix is singular,cannot do inverse
this matrix is singular,cannot do inverse
this matrix is singular,cannot do inverse
this matrix is singular,cannot do inverse
this matrix is singular,cannot do inverse
this matrix is singular,cannot do inverse
读入的数据'ex.txt'是
1.000000 0.2690527 -0.001646584
1.000000 0.2954022 0.1238775
1.000000 0.2909003 -0.05472248
1.000000 0.2646619 -0.07170648
1.000000 0.2431909 -0.09840683
1.000000 0.2797779 0.007707621
1.000000 0.2724597 0.05066166
1.000000 0.2758838 -0.009732046
1.000000 0.2798901 -0.1534021
1.000000 0.2807672 -0.1282553
1.000000 0.2614075 -0.07972595
1.000000 0.2743964 -0.02874548
1.000000 0.2503992 -0.1353439
1.000000 0.1714569 -0.2294806
1.000000 0.04798097 -0.3665651
1.000000 0.06015016 -0.1344432
1.000000 0.03959535 0.04808203
1.000000 0.08010011 -0.1170028
1.000000 0.08165712 -0.1655375
1.000000 0.03176637 -0.2797517
1.000000 0.07593446 -0.1432897
1.000000 0.1295404 -0.07015182
1.000000 0.1928027 -0.1948421
1.000000 0.2250586 -0.1112877
1.000000 0.260904 -0.1982028
1.000000 0.2580951 -0.1082056
1.000000 0.3004584 -0.04234913
1.000000 0.2967188 -0.02759912
1.000000 0.3631018 -0.04297901
1.000000 0.4124005 0.01313069
1.000000 0.4163599 -0.1504389
1.000000 0.4693945 -0.1199375
1.000000 0.4037305 0.01099413
1.000000 0.3453302 -0.1980881
1.000000 0.4089026 -0.1234809
1.000000 0.4242572 -0.0564435
1.000000 0.4985657 -0.1687974
1.000000 0.5441779 -0.1587868
1.000000 0.5810352 -0.0530166
1.000000 0.6546307 -0.164349
1.000000 0.7011005 -0.113253
1.000000 0.6691455 -0.1333736
1.000000 0.4406342 -0.1927065
1.000000 0.4644074 -0.1827072
1.000000 0.5041645 -0.1699146
1.000000 0.6426361 -0.008793893
1.000000 0.7339722 -0.09469118
1.000000 0.7987915 -0.04628391
1.000000 0.8668793 -0.02125628
1.000000 0.9305609 0.03831121
1.000000 0.9887042 0.05882955
1.000000 0.9985553 0.02848571
1.000000 1.044725 0.07723319
1.000000 1.115700 0.06708035
1.000000 1.157659 -0.09891472
1.000000 1.170163 0.05359491
1.000000 1.201005 0.06594299
1.000000 1.206708 0.04018773
1.000000 1.281634 0.09678216
1.000000 1.300016 -0.09687274
1.000000 1.318334 0.07383142
1.000000 1.374397 0.1372537
1.000000 1.439332 0.0788305
1.000000 1.469289 -0.007136605
1.000000 1.492446 0.02735374
1.000000 1.535245 0.09715249
1.000000 1.574530 -0.1475203
1.000000 1.587145 -0.003214587
1.000000 1.618475 0.1681613
1.000000 1.629144 0.07992561
1.000000 1.694170 0.04280506
1.000000 1.746714 0.08608485
1.000000 1.772911 0.08603626
1.000000 1.801633 0.02927345
1.000000 1.857291 0.1741208
1.000000 1.913522 0.08095916
1.000000 1.955875 0.2327352
1.000000 1.970507 0.12446
1.000000 2.035365 0.04954539
1.000000 2.096785 0.2015546
1.000000 2.109739 0.2633146
1.000000 2.132035 0.2311534
1.000000 2.155673 0.1254212
1.000000 2.268190 0.2871278
1.000000 2.300741 0.377954
1.000000 2.344852 0.4841966
1.000000 2.406571 0.2762064
1.000000 2.454569 0.3223111
1.000000 2.506483 0.4164303
1.000000 2.561117 0.4518343
1.000000 2.607687 0.3644027
1.000000 2.634192 0.285948
1.000000 2.668698 0.3624722
1.000000 2.741822 0.3612472
1.000000 2.772562 0.3880408
1.000000 2.804566 0.3555022
1.000000 2.864974 0.5396597
1.000000 2.918063 0.4457485
1.000000 2.978275 0.5346702
1.000000 3.036667 0.4766079
1.000000 3.095585 0.6484076
1.000000 3.158454 0.6802605
1.000000 3.217654 0.4886608
1.000000 3.257523 0.6309823
1.000000 3.276222 0.5082104
1.000000 3.310760 0.6470503
1.000000 3.388524 0.5208067
1.000000 3.461133 0.6910182
1.000000 3.510771 0.5201572
1.000000 3.577636 0.646807
1.000000 3.618009 0.7583407
1.000000 3.686428 0.5603041
1.000000 3.739355 0.7154951
1.000000 3.785264 0.5994745
1.000000 3.849101 0.8402539
1.000000 3.890382 0.8681579
1.000000 3.953280 0.9398329
1.000000 4.002823 0.7048933
1.000000 4.040903 0.8129911
1.000000 4.113554 0.801637
1.000000 4.179027 0.9570037
1.000000 4.222798 0.8394619
1.000000 4.285365 0.9481013
1.000000 4.361596 0.9880667
1.000000 4.395833 1.045621
1.000000 4.419237 0.9276648
1.000000 4.474612 0.9854791
1.000000 4.534513 0.8925385
1.000000 4.600213 0.9968973
1.000000 4.651669 1.015769
1.000000 4.692225 0.9166655
1.000000 4.768079 1.017643
1.000000 4.840543 1.292384
1.000000 4.902685 1.087071
1.000000 4.972134 1.129415
1.000000 5.035988 1.184613
1.000000 5.075732 1.074397
1.000000 5.129654 1.083988
1.000000 5.197710 1.056617
1.000000 5.225694 0.9570371
1.000000 5.269509 1.238351
1.000000 5.354656 1.192033
1.000000 5.413908 1.181873
1.000000 5.431566 1.150368
1.000000 5.507626 1.396955
1.000000 5.572452 1.314763
1.000000 5.619345 1.192607
1.000000 5.690846 1.457747
1.000000 5.761062 1.398177
1.000000 5.794088 1.311191
1.000000 5.839091 1.314177
出图:
|
|