import numpy as np
from sklearn.ensemble import AdaBoostClassifier
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from neupy import algorithms, estimators
数据准备
假设有多个输入特征 X1, X2, …, Xn,并且类别标签 Y
假设数据已经存储在 X 和 Y 中,每个变量为二维数组,形状为 (样本数, 特征数)
参数设置
numFeatures = X.shape[1] # 输入特征的数量
numBoostingIterations = 10 # Adaboost 迭代次数
grnnSmoothingFactor = 0.1 # GRNN的平滑因子
数据预处理
inputFeatures = X
outputLabels = Y
数据划分为训练集和测试集
trainInput, testInput, trainLabels, testLabels = train_test_split(inputFeatures, outputLabels, test_size=0.2)
特征标准化
scaler = StandardScaler()
trainInput = scaler.fit_transform(trainInput)
testInput = scaler.transform(testInput)
构建 GRNN 模型
grnn = algorithms.GRNN(std=grnnSmoothingFactor)
模型训练
grnn.train(trainInput, trainLabels)
使用 Adaboost 进行分类预测
boostedModels = []
for i in range(numBoostingIterations):
# 创建一个新的 GRNN 模型
grnnModel = algorithms.GRNN(std=grnnSmoothingFactor)
# 生成带有权重的样本
weights = np.exp(-estimators.euclidean(trainLabels, grnn.predict(trainInput)) ** 2)
weights /= sum(weights)
# 使用带权重的样本训练 GRNN 模型
grnnModel.train(trainInput, trainLabels, weights=weights)
# 添加到集成模型中
boostedModels.append((grnnModel, 1.0))
集成模型的分类预测
predictions = np.zeros_like(testLabels)
for model, weight in boostedModels:
predictions += weight * model.predict(testInput)
将预测结果转换为类别标签
predictions = np.round(predictions).astype(int)
计算准确率
accuracy = accuracy_score(testLabels, predictions)
print(“测试集准确率:”, accuracy)