鄭捷《機器學習演算法原理與編程實踐》學習筆記(第七章 預測技術與哲學)7.3 嶺迴歸

來源:互聯網
上載者:User

標籤:理論   pen   函數   維數   研究   class   nal   data   第七章   

7.3 嶺迴歸

7.3.1 驗證多重共線性

7.3.2 嶺迴歸理論

7.3.3 嶺際分析

7.3.4 k值的判斷

7.3.5 輔助函數

 (1)匯入Cube:載入資料集

def loadDataSet(filename):    numFeat    = len(open(filename).readline().split(‘\t‘))-1#get number of fields    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[i]))        dataMat.append(lineArr)        labelMat.append(float(curLine[-1]))    return dataMat,labelMat

(2)標準化矩陣資料集

 

#標準化資料集def normData(xArr,yArr):    xMat = mat(xArr)    yMat = mat(yArr).T    yMean = mean(yMat,0)    xMean = mean(xMat,0)    ynorm = yMat - yMean    xVar  = var(xMat,0)    xnorm = (xMat-xMean)/xVar    return xnorm,ynorm

 (3)繪製圖形

def scatterplot(wMat,k):#繪製圖形    fig = plt.figure()    ax  = fig.add_subplot(111)    wMatT = wMat.T    m,n   = shape(wMatT)    for i in xrange(m):        ax.plot(k,wMatT[i,:])        ax.annotate("feature["+str(i)+"]",xy = (0,wMatT[i,0]),color = ‘black‘)    plt.show()

 7.3.6 嶺迴歸實現與K值確定

#前8列為Arr,後1列為yArrxArr,yArr = loadDataSet(‘abalone.txt‘)xMat,yMat = normData(xArr,yArr) #標準化資料集Knum      = 30 #確定k的迭代次數wMat      = zeros((Knum,shape(xMat)[1]))klist     = zeros((Knum,1))for i in xrange(Knum):    k = float(i)/500  #演算法的目的是確定k的值    klist[i] = k      #k值列表    xTx      = xMat.T*xMat    denom    = xTx + eye(shape(xMat)[1])*k    if linalg.det(denom) == 0.0:        print "This matrix is singular,connot do inverse"        sys.exit(0)    ws = linalg.inv(denom) * (xMat.T*yMat)    wMat[i,:] = ws.Tprint klistscatterplot(klist,klist)scatterplot(wMat,klist)

 

 

 

 

參考資料:鄭捷《機器學習演算法原理與編程實踐》 僅供學習研究

鄭捷《機器學習演算法原理與編程實踐》學習筆記(第七章 預測技術與哲學)7.3 嶺迴歸

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.