
线性模型理解(二):用基函数获得多维的参数
y = x ^ 2 在不同三角多项式基函数维度下的曲线
y = x ^ 3 在不同多项式基函数维度下的曲线
y = x ^ 3 在不同三角多项式基函数维度下的曲线
发布日期:2021-05-06 22:01:57
浏览次数:20
分类:精选文章
本文共 2945 字,大约阅读时间需要 9 分钟。
import mathimport numpy as npimport matplotlib.pyplot as pltif 1: def generx2(sta, end, num): # 生成y=x^2的测试集 # 随机干扰因子 sampleNo = num mu = 0.01 sigma = 0.1 np.random.seed(0) s = np.random.normal(mu, sigma, sampleNo) X = np.linspace(sta, end, num) # 在返回(-1, 1)范围内的等差序列 Y = X * X + s x = X.reshape(-1, 1) y = Y.reshape(-1, 1) return x, y def generx3(sta, end, num): # 生成y=x^3的测试集 # 随机干扰因子 sampleNo = num mu = 0.01 sigma = 0.1 np.random.seed(0) s = np.random.normal(mu, sigma, sampleNo) X = np.linspace(sta, end, num) # 在返回(-1, 1)范围内的等差序列 Y = X * X * X + s x = X.reshape(-1, 1) y = Y.reshape(-1, 1) return x, y def polyfunc(x, dim): # 多项式作为基函数的线性模型 px = [] for i in x: cell = [] for j in range(dim): # 基函数 y = [1, x, x^2, x^3 ... , x^9] cell.append(i[0] ** j) px.append(cell) px = np.matrix(px) print('px shape:', np.shape(px)) return px def trigofunc(x, dim): # 三角函数作为基函数的线性模型 px = [] for i in x: cell = [] for j in range(dim): # 基函数 y = [1, x, x^2, x^3 ... , x^9] if j == 0: cell.append(1) else: ers = j >> 1 cou = j & 0x1 if cou == 0: res = math.sin(ers * i[0]) else: res = math.cos(ers * i[0]) cell.append(res) px.append(cell) px = np.matrix(px) print('px shape:', np.shape(px)) return px def corefunc(x): # 高斯核模型 h = 1 temp = 2 * h * h Kx = [] for i in x: cell = 0 for j in x: cell = cell + (i - j) ** 2 cell = cell ** 0.5 cell = np.exp(- (cell / temp)) Kx.append(cell) Kx = np.matrix(Kx) print('Kx shape:', np.shape(Kx)) return Kx def linear_model_test(i, dim): #线性模型 x, y = generx2(-1, 1, 400) plt.plot(x, y) px = trigofunc(x, dim) pfunc = y.T * np.linalg.pinv(px).T # 生成测试集 tx, ty = generx2(-1.5, 1.5, 2000) tempx = trigofunc(tx, dim) predict = pfunc * tempx.T plt.plot(tx, predict.T, label=str(i)) def core_model_test(): # 核模型 x, y = generx2(-1, 1, 400) print(x.shape, y.shape) plt.plot(x, y) px = corefunc(x) pfunc = y * np.linalg.pinv(px) # 生成测试集 tx, ty = generx2(-1, 1, 400) tempx = corefunc(tx) predict = pfunc * tempx plt.plot(tx, predict)if __name__ == '__main__': for i in range(10): linear_model_test(i, i) plt.legend() plt.show()
y = x ^ 2 在不同多项式基函数维度下的曲线




发表评论
最新留言
哈哈,博客排版真的漂亮呢~
[***.90.31.176]2025年03月23日 15时20分41秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
python如何对字符串进行html转义与反转义?
2019-03-06
开发小白也毫无压力的hexo静态博客建站全攻略 - 躺坑后亲诉心路历程
2019-03-06
golang基础--类型与变量
2019-03-06
.NetCore外国一些高质量博客分享
2019-03-06
解决WebRTC中不同的浏览器之间适配的问题
2019-03-06
深入理解JavaScript函数
2019-03-06
【spring源码系列】之【xml解析】
2019-03-06
(在模仿中精进数据可视化07)星球研究所大坝分布可视化
2019-03-06
(数据科学学习手札02)Python与R在循环语句与条件语句上的异同
2019-03-06
(数据科学学习手札27)sklearn数据集分割方法汇总
2019-03-06
(数据科学学习手札40)tensorflow实现LSTM时间序列预测
2019-03-06
[整理] 哪些集合类是线程安全的?(Java)
2019-03-06
8 个警示和学习的 5 个阶段
2019-03-06
c# 图片带水纹波动
2019-03-06
H5 贪吃蛇源码
2019-03-06
从零开始学安全(十六)● Linux vim命令
2019-03-06
从零开始学安全(三十四)●百度杯 ctf比赛 九月场 sqli
2019-03-06
3389连接痕迹清除
2019-03-06
发生系统错误 6118
2019-03-06