国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

線性回歸

2019-11-08 18:45:31
字體:
來源:轉載
供稿:網友

1.線性回歸原理

回歸的目的就是預測數值型的目標值,回歸方程就是回歸系數和輸入線性組合的方程,求回歸系數的過程就叫回歸。線性回歸是輸入項和系數相乘在相加,非線性回歸可能認為輸出是輸入的相乘

求回歸系數就是求出誤差函數并對會回歸系數求導,并讓其為0,就可以求出回歸系數

import numpy as npimport matplotlib.pyplot as pldef loadDataSet(filename):    numFeat = len(open(filename).readline().split('/t')) - 1    dataMat = []    labelMat = []    fr = open(filename)    for line in fr.readlines():        lineArr = []        curLine = line.str

def lwlr(testPoint, xArr, yArr, k = 1.0):    xMat = np.mat(xArr)    yMat = np.mat(yArr).T    m = np.shape(xMat)[0]    weights = np.mat(np.eye((m)))    for j in range(m):        diffMat = testPoint - xMat[j,:]        weights[j,j] = np.exp(diffMat*diffMat.T/(-2.0*k**2))    xTx = xMat.T * (weights*xMat)    if np.linalg.det(xTx) == 0.0:        print 'this matrix is singular, cannot do inverse'        return    ws = xTx.I * (xMat.T*(weights*yMat))    return testPoint * wsdef lwlrTest(testArr, xArr, yArr, k = 1.0):    m = np.shape(testArr)[0]    yHat = np.zeros(m)    for i in range(m):        yHat[i] = lwlr(testArr[i], xArr, yArr, k)    return yHat

嶺回歸,當特征的數目比數據的數還多時,我們可以引入嶺回歸來解決問題.嶺回歸就是在XtX上加一個常數在乘單位矩陣,使其非奇異可求逆

def ridgeRegress(xMat, yMat, lam = 0.2):    xTx = xMat.T * xMat    denom = xTx + np.eye(np.shape(xMat)[1])*lam    if np.linalg.det(denom) == 0.0:        print 'this matrix is singular, cannot do inverse'        return    ws = denom.I * (xMat.T*yMat)    return wsdef ridgeTest(xArr, yArr):    xMat = np.mat(xArr); yMat = np.mat(yArr).T    yMean = np.mean(yMat,0)    yMat = yMat - yMean    xMean = np.mean(xMat, 0)    xVar = np.var(xMat, 0)    xMat = (xMat - xMean) / xVar    numTest = 30    wMat = np.ones((numTest, np.shape(xMat)[1]))    for i in range(30):        ws = ridgeRegress(xMat, yMat, np.exp(i-10))        wMat[i,:] = ws.T    return wMat 

前向逐步回歸

def rssError(yArr, yHatArr):    return ((yArr - yHatArr)**2).sum()def regularize(xMat):    inMat = xMat.copy()    inMatMean = np.mean(inMat,0)    inMatVar = np.var(inMat,0)    inMat = (inMat - inMatMean)/inMatVar    return inMatdef stageWise(xArr, yArr, eps = 0.01, numIt = 100):    xMat = np.mat(xArr); yMat = np.mat(yArr).T    yMean = np.mean(yMat,0)    yMat = yMat - yMean    xMat = regularize(xMat)    m,n = np.shape(xMat)    retMat = np.ones((numIt,n))    ws = np.ones((n,1))    wsTest = ws.copy()    wsMax = ws.copy()    for i in range(numIt):        print ws.T        lowestError = np.Inf        for j in range(n):            for sign in [-1,1]:                wsTest = ws.copy()                wsTest[j] += eps * sign                yTest = xMat * wsTest                rssE = rssError(yMat.A, yTest.A)                if rssE < lowestError:                    lowestError = rssE                wsMax = wsTest        ws = wsMax.copy()        retMat[i,:] = ws.T    return retMat


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 洞口县| 肃宁县| 昌邑市| 沙坪坝区| 彰化市| 弥勒县| 汶川县| 阿荣旗| 定远县| 芦山县| 浦江县| 东安县| 武山县| 张家界市| 泸西县| 祁东县| 长岛县| 淅川县| 大田县| 怀仁县| 庆安县| 驻马店市| 安吉县| 朝阳市| 渭南市| 古田县| 潼关县| 秦皇岛市| 抚顺县| 高安市| 阿拉尔市| 顺平县| 金山区| 北票市| 化隆| 金寨县| 兴隆县| 江陵县| 尼木县| 故城县| 邹平县|