1.基础模型
1.1多层感知机(MLP)
1.2极限学习机(ELM)
1.3高阶线性回归
y = a 11 x 1 + ⋯ + a 1 n x 1 n + a 21 x 2 + ⋯ + a 2 n x 2 n + ⋯ + a m 1 x m + ⋯ + a m n x m n y=a_{11}x_1+\dots+a_{1n}x_1^n+a_{21}x_2+\dots+a_{2n}x_2^n+\dots+a_{m1}x_m+\dots+a_{mn}x_m^n y=a11x1+⋯+a1nx1n+a21x2+⋯+a2nx2n+⋯+am1xm+⋯+amnxmn
1.4径向基函数(RBF)
基本原理
相关代码:
import numpy as np
from sklearn.cluster import KMeans
from sklearn.linear_model import LinearRegression
# 生成一些随机数据作为示例
np.random.seed(42)
X = np.random.rand(100, 6) # 6维特征数据
y_true = 5 * X + np.random.normal(0, 0.5, size=(100, 6)) # 6维真实的目标值
# 使用K均值聚类算法初始化径向基函数中心
num_clusters = 5
kmeans = KMeans(n_clusters=num_clusters, random_state=42)
kmeans.fit(X)
centers = kmeans.cluster_centers_
# 计算径向基函数的宽度
sigma = np.mean([np.linalg.norm(x - centers[i]) for i in range(num_clusters)])
# 计算径向基函数的值
def radial_basis_function(x, center, sigma):
return np.exp(-np.linalg.norm(x - center) ** 2 / (2 * sigma ** 2))
X_rbf = np.array([[radial_basis_function(x, center, sigma) for center in centers] for x in X])
# 使用线性模型拟合误差
lin_model = LinearRegression()
lin_model.fit(X_rbf, y_true)
# 进行预测
def predict(x):
rbf_features = np.array([radial_basis_function(x, center, sigma) for center in centers])
return lin_model.predict(rbf_features.reshape(1, -1))
# 检验拟合结果
sample_index = 10
sample_input = X[sample_index]
sample_true_output = y_true[sample_index]
sample_predicted_output = predict(sample_input)
print("Sample Input:", sample_input)
print("True Output:", sample_true_output)
print("Predicted Output:", sample_predicted_output)
1.5高斯混合模型回归(GMM)
import numpy as np
import matplotlib.pyplot as plt
from sklearn.mixture import GaussianMixture
# 生成一些示例数据
np.random.seed(0)
X = np.sort(5 * np.random.rand(100, 1), axis=0)
y = np.sin(X).ravel() + 0.1 * (np.random.randn(100))
# 使用高斯混合回归拟合数据
gmm = GaussianMixture(n_components=3)
gmm.fit(X, y)
# 使用拟合好的模型进行预测
X_new = np.linspace(0, 5, 1000)[:, np.newaxis]
y_pred = gmm.predict(X_new)
# 绘制原始数据和拟合结果
plt.scatter(X, y, color='black', label='data')
plt.plot(X_new, np.sin(X_new).ravel(), color='red', label='ground truth')
plt.plot(X_new, y_pred, color='blue', label='GMM regression')
plt.xlabel('X')
plt.ylabel('y')
plt.title('Gaussian Mixture Regression')
plt.legend()
plt.show()
2.集成学习
使用Bagging策略,对每个基础模型的预测结果进行投票或平均。对集成模型进行性能评估,并进一步调优,例如调整投票权重或模型参数。
使用stacking方法:基本原理
相关代码:
from sklearn.model_selection import train_test_split, KFold
from sklearn.ensemble import RandomForestRegressor
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
import numpy as np
# 准备训练数据集
X = np.array([[10], [20], [15], [25], [30]]) # 绳驱动量
Y = np.array([2.5, 3.6, 2.8, 4.0, 4.5]) # 关节位姿误差
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=42)
# 初始化基础模型
models = [
RandomForestRegressor(n_estimators=50, random_state=42),
LinearRegression()
]
# 初始化元模型
meta_model = LinearRegression()
# 预测结果集合
predictions = np.zeros((len(X_test), len(models)))
# 训练基础模型并进行预测
for i, model in enumerate(models):
model.fit(X_train, y_train)
predictions[:, i] = model.predict(X_test)
# 训练元模型
meta_model.fit(predictions, y_test)
# 集成预测
stacking_predictions = meta_model.predict(predictions)
# 计算集成模型的均方误差
mse_stacking = mean_squared_error(y_test, stacking_predictions)
print("Stacking 模型的均方误差:", mse_stacking)