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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

机器学习中的高斯过程简介-好文

發布時間:2025/3/19 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 机器学习中的高斯过程简介-好文 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

?

?

?

import matplotlib.pyplot as plt import numpy as np from itertools import cyclecolor_cycle = cycle('kbryg') n_variable = 20 n_sample = 5plt.figure(figsize=(500, 300)) sigma_s = np.eye(n_variable) # 協方差陣為對角矩陣for _ in range(n_sample):point = np.random.multivariate_normal(np.zeros(n_variable), sigma_s)plt.plot(np.arange(n_variable), point, color=next(color_cycle))plt.scatter(np.arange(n_variable), point)plt.rcParams['font.sans-serif'] = ['SimHei'] # 正確顯示中文 plt.rcParams['axes.unicode_minus'] = False # 正確顯示負號 plt.title('不使用核函數的情況', fontsize=24) plt.xticks(np.arange(n_variable)) plt.xlabel('y', fontsize=20) plt.show()

?

核函數來估計協方差,能很好反映各個變量y yy間的關系。因為核函數充分考慮了樣本間的相似性(大多用d dd度量)。
例如采用squared exponential核函數后,曲線變得平滑了:
?

xs = np.linspace(0, 1, n_variable) # 構建y對應的特征 sigma_s = np.exp(-(np.expand_dims(xs, axis=0) - np.expand_dims(xs, axis=1)) ** 2 / 2) # 依據特征xs構建協方差陣

?

import numpy as np from scipy.optimize import minimize from scipy.spatial.distance import pdist, cdist, squareformclass Kernel:# RBF核函數def __init__(self, theta):self.theta = thetadef __call__(self, X, Y=None):if Y is None:dists = pdist(X / self.theta, metric='sqeuclidean')K = np.exp(-0.5 * dists)K = squareform(K)np.fill_diagonal(K, 1)else:dists = cdist(X / self.theta, Y / self.theta, metric='sqeuclidean')K = np.exp(-0.5 * dists)return Kclass GPR:def __init__(self):self.K = Noneself.X = Noneself.y = Nonedef log_marginal_likelihood(self, theta):# 計算對數邊緣似然函數K = Kernel(theta)sigma = K(self.X)log_likelihood = np.log(np.linalg.det(sigma)) + \self.y @ np.linalg.inv(sigma) @ self.y + \sigma.shape[0] * np.log(2 * np.pi)return - 0.5 * log_likelihooddef fit(self, X, y):# 訓練,獲取核函數最優參數self.X = Xself.y = ydef obj_func(theta):return - self.log_marginal_likelihood(theta)theta_opt = minimize(obj_func, np.array([1.0]), method='BFGS')self.K = Kernel(theta_opt.x[0])def predict(self, X_pred):# 預測,獲取y*的條件概率分布K_pred = self.K(X_pred)K_train = self.K(self.X)K_pred_train = self.K(self.X, X_pred)K_inv = np.linalg.inv(K_train)mu = K_pred_train.T @ K_inv @ self.ysigma = K_pred - K_pred_train.T @ K_inv @ K_pred_trainreturn mu, np.diagonal(sigma)if __name__ == '__main__':import matplotlib.pyplot as pltgpr = GPR()# 生成樣本數據coefs = [6, -2.5, -2.4, -0.1, 0.2, 0.03]def f(x):total = 0for exp, coef in enumerate(coefs):total += coef * (x ** exp)return totalxs = np.linspace(-5.0, 3.5, 100)ys = f(xs)X_train = np.array([-4, -1.5, 0, 1.5, 2.5, 2.7])y_train = f(X_train)X_train = X_train.reshape(-1, 1)X_pred = np.linspace(-8, 7, 80).reshape((-1, 1))gpr.fit(X_train, y_train)y_pred, y_std = gpr.predict(X_pred)plt.plot(xs, ys, color='k', linewidth=2, label='True')plt.scatter(X_train, y_train, color='b', marker='*', linewidths=3, label='Train_data')plt.plot(X_pred, y_pred, color='r', label='Pred')plt.fill_between(X_pred.reshape(1, -1)[0], y_pred - y_std, y_pred + y_std, color='darkorange',alpha=0.2)plt.legend()plt.show()

?

參考資料
http://bridg.land/posts/gaussian-processes-1
http://www.datalearner.com/blog/1051459170229238
https://en.wikipedia.org/wiki/Gaussian_process
---------------------?
作者:slx_share?
來源:CSDN?
原文:https://blog.csdn.net/slx_share/article/details/83573339?
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!?

總結

以上是生活随笔為你收集整理的机器学习中的高斯过程简介-好文的全部內容,希望文章能夠幫你解決所遇到的問題。

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