日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

(含Python源码)Python实现K阶多项式的5种回归算法(regression)

發布時間:2024/7/23 python 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (含Python源码)Python实现K阶多项式的5种回归算法(regression) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

0、文章結構

為了方便客官根據需要取閱,節約時間,文章目錄結構如下:

  • 問題描述
  • 理論部分:五種回歸算法
  • 兩種Python讀取文件的方法
  • Python實現五種回歸算法
  • 使用的工具箱
  • 總結

1、問題描述

K階多項式表達式

其中,

現有數據集

為了方便回歸運算,標記如下:

通過數據集,求出回歸參數。

2、五種回歸算法

2.1 QP solver

2.2 LP solver

3、?Python代碼獲取數據

? ? ? 下面將介紹兩種獲取數據的方法,一種是獲取txt文件,另一種是Mat文件。

3.1 獲取text文件數據

? ? ? ? ? 這種方法獲取數據后,還可以對數據進行預處理。

? ? ? ? ? 獲取text文件數據

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

對數據進行處理,形成??矩陣

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文件

返回的矩陣數據

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實現五種回歸算法

4.1 least squares(LS)

代碼實現??求解

#求least squared parameter theta1 def theta1(samplex,sampley): #output k*1return np.dot(np.matrix(np.dot(samplex,samplex.transpose())).I,samplex).dot(sampley)

代碼實現預測函數

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)

實現預測函數代碼

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()

實現預測函數代碼

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]

實現預測函數代碼

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

實現預測函數代碼

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_covariance

5、工具箱

import numpy as np from cvxopt import matrix from cvxopt import solvers

6、總結

這篇博客源于一個編程作業的小問題。在實現第一個和第三個算法時,花的時間最多。查找資料,調試代碼,前后遇到了過很多坑,感謝CSDN上的大佬,我總能從他們的博客中找到解決辦法。

?

?

總結

以上是生活随笔為你收集整理的(含Python源码)Python实现K阶多项式的5种回归算法(regression)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。