(含Python源码)Python实现K阶多项式的5种回归算法(regression)
0、文章結(jié)構(gòu)
為了方便客官根據(jù)需要取閱,節(jié)約時(shí)間,文章目錄結(jié)構(gòu)如下:
- 問題描述
- 理論部分:五種回歸算法
- 兩種Python讀取文件的方法
- Python實(shí)現(xiàn)五種回歸算法
- 使用的工具箱
- 總結(jié)
1、問題描述
K階多項(xiàng)式表達(dá)式
其中,
現(xiàn)有數(shù)據(jù)集
為了方便回歸運(yùn)算,標(biāo)記如下:
通過數(shù)據(jù)集,求出回歸參數(shù)。
2、五種回歸算法
2.1 QP solver
2.2 LP solver
3、?Python代碼獲取數(shù)據(jù)
? ? ? 下面將介紹兩種獲取數(shù)據(jù)的方法,一種是獲取txt文件,另一種是Mat文件。
3.1 獲取text文件數(shù)據(jù)
? ? ? ? ? 這種方法獲取數(shù)據(jù)后,還可以對數(shù)據(jù)進(jìn)行預(yù)處理。
? ? ? ? ? 獲取text文件數(shù)據(jù)
def get_data():#1*50sampleX = np.loadtxt("D:/路徑/文件名.文件格式")#1*50sampleY = np.loadtxt("D:/路徑/文件名.文件格式")#1*100polyX = np.loadtxt("D:/路徑/文件名.文件格式")#1*100polyY = np.loadtxt("D:/路徑/文件名.文件格式")#50*1samplex = sampleX.reshape(len(sampleX), 1)# 50*1sampley = sampleY.reshape(len(sampleY), 1)#100*1polyx = polyX.reshape(len(polyX), 1)# 100*1polyy = polyY.reshape(len(polyY), 1)return samplex, sampley, polyx, polyy對數(shù)據(jù)進(jìn)行處理,形成??矩陣
def fi_vector(samplex): #(k+1)*1return np.array([samplex**j for j in range(k+1)]).reshape(k+1,1)def fi_array(samplex): #n*(K+1)return np.array([fi_vector(x) for x in samplex]).transpose()#the matrix of samplyx def fi_matrix(X): #n*1return np.matrix(fi_array(X))3.2 獲取Mat文件
返回的矩陣數(shù)據(jù)
def get_data3():data_path="D:/路徑/文件名.mat"data = scio.loadmat(data_path)samplex=data['trainx']sampley=data['trainy']polyx=data['testx']polyy=data['testy']return samplex, sampley, polyx, polyy?4、Python實(shí)現(xiàn)五種回歸算法
4.1 least squares(LS)
代碼實(shí)現(xiàn)??求解
#求least squared parameter theta1 def theta1(samplex,sampley): #output k*1return np.dot(np.matrix(np.dot(samplex,samplex.transpose())).I,samplex).dot(sampley)代碼實(shí)現(xiàn)預(yù)測函數(shù)
def prediction1(polyx,theta1): #output 100*1return np.dot(polyx.transpose(),theta1)4.2 Regularized LS(RLS)
求解??代碼
lamda=1 def theta2(samplex,sampley): #k*1return (np.matrix(np.dot(fi_matrix(samplex), fi_matrix(samplex).transpose()) + \lamda * np.identity(len(fi_matrix(samplex)))).I).dot(fi_matrix(samplex)).dot(sampley)實(shí)現(xiàn)預(yù)測函數(shù)代碼
def prediction2(polyx,theta2): # 100*1return np.dot(np.matrix(fi_matrix(polyx)).transpose(), theta2)4.3 L1-regularized LS(LASSO)
求解??代碼
lamda2=1 def theta3(samplex,sampley):fi_matrix_squared = np.dot(fi_matrix(samplex), fi_matrix(samplex).transpose())fi_matrix_sampley = np.dot(fi_matrix(samplex),sampley)fi_matrix_sampley_aug = np.concatenate((fi_matrix_sampley, -1 * fi_matrix_sampley), axis=0)Hl = np.concatenate((fi_matrix_squared, -1 * fi_matrix_squared), axis=0)Hr = np.concatenate((-1 * fi_matrix_squared, fi_matrix_squared), axis=0)H = np.concatenate((Hl, Hr), axis=1)f = lamda2 * np.ones((len(fi_matrix_sampley_aug), 1)) - fi_matrix_sampley_augG = -1 * np.identity((len(H)))value = np.zeros((len(H), 1))Theta3 = solvers.qp(matrix(H), matrix(f), matrix(G), matrix(value))['x']return np.matrix([Theta3[i] - Theta3[i + k+1] for i in range(int(len(Theta3) / 2))]).transpose()實(shí)現(xiàn)預(yù)測函數(shù)代碼
def prediction3(polyx, theta3):return np.dot(fi_matrix(polyx).transpose(), theta3)4.4 Robust regression(RR)
求解??代碼
def theta4(samplex,sampley):f1 = np.concatenate((np.zeros((9, 1)), np.ones((len(samplex), 1))), axis=0)Al = np.concatenate((-1 * fi_matrix(samplex).transpose(), fi_matrix(samplex).transpose()), axis=0)Ar = np.concatenate((-1 * np.identity(len(samplex)), -1 * np.identity(len(samplex))), axis=0)A = np.concatenate((Al, Ar), axis=1)b = np.concatenate((-1 * sampley, sampley), axis=0)return solvers.lp(matrix(f1), matrix(A), matrix(b))['x'][0:9]實(shí)現(xiàn)預(yù)測函數(shù)代碼
def prediction4(polyx, theta4):return np.dot(fi_matrix(polyx).transpose(), theta4)4.5 Bayesian regression(BR)
求解??代碼
alpha=1 variance=5 def posterior5(samplex,sampley):covariance = np.matrix((1 / alpha) * np.identity(len(fi_matrix(samplex)))+ (1 / variance) * np.dot(fi_matrix(samplex),fi_matrix(samplex).transpose())).Imean = (1 / variance) * covariance.dot(fi_matrix(samplex)).dot(sampley)return covariance, mean實(shí)現(xiàn)預(yù)測函數(shù)代碼
def prediction5(polyx, covariance, mean):pre_mean = np.dot(fi_matrix(polyx).transpose(), mean)pre_covariance = np.dot(fi_matrix(polyx).transpose(),covariance).dot(fi_matrix(polyx))return pre_mean, pre_covariance5、工具箱
import numpy as np from cvxopt import matrix from cvxopt import solvers6、總結(jié)
這篇博客源于一個(gè)編程作業(yè)的小問題。在實(shí)現(xiàn)第一個(gè)和第三個(gè)算法時(shí),花的時(shí)間最多。查找資料,調(diào)試代碼,前后遇到了過很多坑,感謝CSDN上的大佬,我總能從他們的博客中找到解決辦法。
?
?
總結(jié)
以上是生活随笔為你收集整理的(含Python源码)Python实现K阶多项式的5种回归算法(regression)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。