当前位置:   article > 正文

Datawhale AI夏令营第二期——机器学习 基于神经网络stack融合策略的多模型融合

Datawhale AI夏令营第二期——机器学习 基于神经网络stack融合策略的多模型融合

#AI夏令营 #Datawhale夏令营

基于神经网络stack融合策略的多模型融合

改进点:

1.数据清洗,异常值替换(板块2)

2.基于神经网络的stack模型融合(板块6)

根据大佬的提示对Task3所做的改进,大佬链接:http://t.csdnimg.cn/RSC3o

1.模型导入

导入所需要包:

  1. import pandas as pd
  2. import numpy as np
  3. from sklearn.model_selection import StratifiedKFold, KFold, GroupKFold
  4. import lightgbm as lgb
  5. import xgboost as xgb
  6. from catboost import CatBoostRegressor
  7. from sklearn.metrics import mean_squared_error, mean_absolute_error

 2.数据信息展示及可视化

赛题数据由训练集和测试集组成,为了保证比赛的公平性,将每日日期进行脱敏,用1-N进行标识,即1为数据集最近一天,其中1-10为测试集数据。数据集由字段id(房屋id)、 dt(日标识)、type(房屋类型)、target(实际电力消耗)组成,如下图所示。

每个房间给出其从第11-506天的电力消耗,共2877305行,5832个房间,19种房间类型,下图为train.csv的数据展示。

 房间类型与电力消耗关系如下图所示:

  1. import matplotlib.pyplot as plt
  2. import pandas as pd
  3. import matplotlib.cm as cm
  4. plt.rcParams['font.family'] = ['SimHei']
  5. plt.rcParams['axes.unicode_minus'] = False
  6. target = train.groupby('type')['target'].mean().reset_index()
  7. plt.figure(figsize=(6, 4))
  8. norm = plt.Normalize(vmin=0, vmax=len(target['type'].unique())-1)
  9. colors = cm.viridis(norm(range(len(target['type'].unique()))))
  10. plt.bar(target['type'], target['target'], color=colors)
  11. plt.xlabel('房间类型')
  12. plt.ylabel('平均消耗电力')
  13. plt.title('房间类型与电力消耗关系')
  14. plt.show()

 以第一个房间编号为例,展示其随时间的电力变化,如下图所示:

  1. id_all = train[train['id'] == '00037f39cf']
  2. plt.figure(figsize=(8, 4))
  3. plt.plot(id_all['dt'], id_all['target'], linestyle='-')
  4. plt.xlabel('日期')
  5. plt.ylabel('电力消耗')
  6. plt.title('电力消耗随日期变化')
  7. plt.show()

通过该图可知,数据有明显的异常值,需要进行数据筛除以及补全,故对全篇数据进行循环,检查数据合理性,对不合理数据进行平均值替换,得出新的train.csv文件

  1. #数据预处理,异常值转换
  2. products = train['id'].unique()
  3. # 创建一个空的 DataFrame 来存储修改后的数据
  4. modified_train = train.copy()
  5. for product in products:
  6. # 筛选出特定产品的数据
  7. id_all = modified_train[modified_train['id'] == product]
  8. ls = id_all['target'].tolist() # 转换为列表以便修改
  9. # 使用列表推导式来处理异常值
  10. new_ls = [ls[0]] # 初始值,避免索引-1错误
  11. for i in range(1, len(ls) - 1):
  12. if abs(ls[i] - ls[i - 1]) >= 10 and abs(ls[i] - ls[i + 1]) >= 10:
  13. ll=(ls[i - 1] + ls[i + 1]) / 2
  14. new_ls.append(ll)
  15. else:
  16. new_ls.append(ls[i])
  17. new_ls.append(ls[-1]) # 添加最后一个值,避免索引超出范围
  18. # 更新 DataFrame 中的 target 列
  19. modified_train.loc[modified_train['id'] == product, 'target'] = new_ls
  20. # 最后,将所有修改后的数据写入 CSV 文件
  21. modified_train[['id', 'dt', 'type', 'target']].to_csv('train1.csv', index=None)

结果如图所示,图像相比变得较为平滑,突出数据减少,但是这里判断合理性只考虑数据和其相邻数据,数据清洗不是很全面,可以具体详细更改。

 3.特征提取

这里主要构建了历史平移特征、差分特征、和窗口统计特征;具体说明如下:

(1)历史平移特征:通过历史平移获取上个阶段的信息;

(2)差分特征:可以帮助获取相邻阶段的增长差异,描述数据的涨减变化情况。在此基础上还可以构建相邻数据比值变化、二阶差分等;

(3)窗口统计特征:窗口统计可以构建不同的窗口大小,然后基于窗口范围进统计均值、最大值、最小值、中位数、方差的信息,可以反映最近阶段数据的变化情况。

  1. # 合并训练数据和测试数据
  2. data = pd.concat([train, test], axis=0).reset_index(drop=True)
  3. data = data.sort_values(['id','dt'], ascending=False).reset_index(drop=True)
  4. # 历史平移
  5. for i in range(10,36):
  6. data[f'target_shift{i}'] = data.groupby('id')['target'].shift(i)
  7. # 历史平移 + 差分特征
  8. for i in range(1,4):
  9. data[f'target_shift10_diff{i}'] = data.groupby('id')['target_shift10'].diff(i)
  10. # 窗口统计
  11. for win in [15,30,50,70]:
  12. data[f'target_win{win}_mean'] = data.groupby('id')['target'].rolling(window=win, min_periods=3, closed='left').mean().values
  13. data[f'target_win{win}_max'] = data.groupby('id')['target'].rolling(window=win, min_periods=3, closed='left').max().values
  14. data[f'target_win{win}_min'] = data.groupby('id')['target'].rolling(window=win, min_periods=3, closed='left').min().values
  15. data[f'target_win{win}_std'] = data.groupby('id')['target'].rolling(window=win, min_periods=3, closed='left').std().values
  16. # 历史平移 + 窗口统计
  17. for win in [7,14,28,35,50,70]:
  18. data[f'target_shift10_win{win}_mean'] = data.groupby('id')['target_shift10'].rolling(window=win, min_periods=3, closed='left').mean().values
  19. data[f'target_shift10_win{win}_max'] = data.groupby('id')['target_shift10'].rolling(window=win, min_periods=3, closed='left').max().values
  20. data[f'target_shift10_win{win}_min'] = data.groupby('id')['target_shift10'].rolling(window=win, min_periods=3, closed='left').min().values
  21. data[f'target_shift10_win{win}_sum'] = data.groupby('id')['target_shift10'].rolling(window=win, min_periods=3, closed='left').sum().values
  22. data[f'target_shift710win{win}_std'] = data.groupby('id')['target_shift10'].rolling(window=win, min_periods=3, closed='left').std().values
  23. train = data[data.target.notnull()].reset_index(drop=True)
  24. test = data[data.target.isnull()].reset_index(drop=True)
  25. train_cols = [f for f in data.columns if f not in ['id','target']]

4.三种机器学习模型对数据的预测 

XGBoost、LightGBM和CatBoost是三种强大的梯度提升树算法,各自在算法结构、分类变量处理、性能优化及实际应用中展现独特优势。XGBoost以高效性和灵活性著称,适合处理大规模数据集;LightGBM则通过直方图算法和智能分裂策略,在稀疏数据处理上表现出色;而CatBoost特别擅长处理分类特征,通过自动特征处理和排序提升技术,在分类问题上具有卓越性能。

以下代码定义了一个名为cv_model的函数,旨在通过交叉验证(Cross-Validation)的方式评估并训练给定的机器学习模型(支持LightGBM,XGBoost和CatBoost三种模型)。函数接收模型实例、训练数据及其标签、测试数据、模型名称以及随机种子作为输入。函数内部,首先设置了5折交叉验证(KFold),并初始化了用于存储训练集外预测(OOF, Out-Of-Fold)结果、测试集预测结果和交叉验证分数的数组。

在每次交叉验证的迭代中,函数根据当前训练集和验证集的索引分割数据,并根据模型名称(clf_name)选择相应的参数和训练方式。对于LightGBM(lgb),它构建了两个数据集对象(训练集和验证集),设置了特定的参数(如学习率、树的数量、特征采样比例等),并使用这些参数训练模型,同时应用了早停法(early stopping)来防止过拟合。对于XGBoost和CatBoost,采用类似的流程,但参数和细节有所不同。

在每次迭代中,模型都会在验证集上进行预测,并将预测结果累加到OOF数组中,同时计算并存储交叉验证的分数。对于测试集,模型在每个迭代中都会进行预测,但最终的测试集预测结果是所有迭代预测结果的平均值。

  1. def cv_model(clf, train_x, train_y, test_x, clf_name, seed = 2024):
  2. '''
  3. clf:调用模型
  4. train_x:训练数据
  5. train_y:训练数据对应标签
  6. test_x:测试数据
  7. clf_name:选择使用模型名
  8. seed:随机种子
  9. '''
  10. folds = 5
  11. kf = KFold(n_splits=folds, shuffle=True, random_state=seed)
  12. oof = np.zeros(train_x.shape[0])
  13. test_predict = np.zeros(test_x.shape[0])
  14. cv_scores = []
  15. for i, (train_index, valid_index) in enumerate(kf.split(train_x, train_y)):
  16. print('************************************ {} ************************************'.format(str(i+1)))
  17. trn_x, trn_y, val_x, val_y = train_x.iloc[train_index], train_y[train_index], train_x.iloc[valid_index], train_y[valid_index]
  18. if clf_name == "lgb":
  19. train_matrix = clf.Dataset(trn_x, label=trn_y)
  20. valid_matrix = clf.Dataset(val_x, label=val_y)
  21. params = {
  22. 'boosting_type': 'gbdt',
  23. 'objective': 'regression',
  24. 'metric': 'mae',
  25. 'min_child_weight': 6,
  26. 'num_leaves': 2 ** 6,
  27. 'lambda_l2': 10,
  28. 'feature_fraction': 0.8,
  29. 'bagging_fraction': 0.8,
  30. 'bagging_freq': 4,
  31. 'learning_rate': 0.05,
  32. 'seed': 2024,
  33. 'nthread' : 16,
  34. 'verbose' : -1,
  35. }
  36. model = clf.train(params, train_matrix, 5000, valid_sets=[train_matrix, valid_matrix],
  37. categorical_feature=[], verbose_eval=500, early_stopping_rounds=500)
  38. val_pred = model.predict(val_x, num_iteration=model.best_iteration)
  39. test_pred = model.predict(test_x, num_iteration=model.best_iteration)
  40. if clf_name == "xgb":
  41. xgb_params = {
  42. 'booster': 'gbtree',
  43. 'objective': 'reg:squarederror',
  44. 'eval_metric': 'mae',
  45. 'max_depth': 5,
  46. 'lambda': 10,
  47. 'subsample': 0.7,
  48. 'colsample_bytree': 0.7,
  49. 'colsample_bylevel': 0.7,
  50. 'eta': 0.1,
  51. 'tree_method': 'hist',
  52. 'seed': 520,
  53. 'nthread': 16
  54. }
  55. train_matrix = clf.DMatrix(trn_x , label=trn_y)
  56. valid_matrix = clf.DMatrix(val_x , label=val_y)
  57. test_matrix = clf.DMatrix(test_x)
  58. watchlist = [(train_matrix, 'train'),(valid_matrix, 'eval')]
  59. model = clf.train(xgb_params, train_matrix, num_boost_round=5000, evals=watchlist, verbose_eval=500, early_stopping_rounds=500)
  60. val_pred = model.predict(valid_matrix)
  61. test_pred = model.predict(test_matrix)
  62. if clf_name == "cat":
  63. params = {'learning_rate': 0.05, 'depth': 5, 'bootstrap_type':'Bernoulli','random_seed':1314,
  64. 'od_type': 'Iter', 'od_wait': 100, 'random_seed': 11, 'allow_writing_files': False}
  65. model = clf(iterations=5000, **params)
  66. model.fit(trn_x, trn_y, eval_set=(val_x, val_y),
  67. metric_period=100,
  68. use_best_model=True,
  69. cat_features=[],
  70. verbose=1)
  71. val_pred = model.predict(val_x)
  72. test_pred = model.predict(test_x)
  73. oof[valid_index] = val_pred
  74. test_predict += test_pred / kf.n_splits
  75. score = mean_absolute_error(val_y, val_pred)
  76. cv_scores.append(score)
  77. print(cv_scores)
  78. return oof, test_predict
  79. # 选择lightgbm模型
  80. lgb_oof, lgb_test = cv_model(lgb, train[train_cols], train['target'], test[train_cols], 'lgb')
  81. # 选择xgboost模型
  82. xgb_oof, xgb_test = cv_model(xgb, train[train_cols], train['target'], test[train_cols], 'xgb')
  83. # 选择catboost模型
  84. cat_oof, cat_test = cv_model(CatBoostRegressor, train[train_cols], train['target'], test[train_cols], 'cat')

5. 基于Ridge回归器的模型堆叠

Ridge:Ridge回归是一种线性回归的扩展,它通过向损失函数中添加一个L2正则化项(即权重的平方和)来减少模型的复杂度,从而避免过拟合。这种方法通过约束模型系数的大小,使得模型在拟合数据的同时保持了一定的泛化能力。

以下代码实现了一个堆叠模型(Stacking Model)的过程,其中结合了三个基础模型(如LightGBM、XGBoost和CatBoost)的预测结果来构建一个新的集成模型。首先,它将三个基础模型在训练集上的OOF(Out-Of-Fold)预测和测试集上的预测结果分别拼接成新的DataFrame。然后,使用这些拼接后的数据作为新特征,通过多次重复K折交叉验证(RepeatedKFold)来训练一个线性回归模型(这里使用的是Ridge回归器),每次验证都计算并记录了模型在验证集上的平均绝对误差(MAE)。最终,通过平均所有折叠中测试集上的预测结果来得到最终的堆叠模型预测。此过程旨在通过组合不同基础模型的预测来减少过拟合风险,并提高模型的泛化能力。返回的stack_oofstack_pred分别是堆叠模型在训练集上的OOF预测和测试集上的预测结果。

  1. def stack_model(oof_1, oof_2, oof_3, predictions_1, predictions_2, predictions_3, y):
  2. '''
  3. 输入的oof_1, oof_2, oof_3可以对应lgb_oof,xgb_oof,cat_oof
  4. predictions_1, predictions_2, predictions_3对应lgb_test,xgb_test,cat_test
  5. '''
  6. # 将Series转换为DataFrame
  7. oof_1, oof_2, oof_3 = pd.DataFrame(oof_1), pd.DataFrame(oof_2), pd.DataFrame(oof_3)
  8. predictions_1, predictions_2, predictions_3 = pd.DataFrame(predictions_1), pd.DataFrame(predictions_2), pd.DataFrame(predictions_3)
  9. # 拼接OOF和测试预测
  10. train_stack = pd.concat([oof_1, oof_2, oof_3], axis=1)
  11. test_stack = pd.concat([predictions_1, predictions_2, predictions_3], axis=1)
  12. oof = np.zeros(train_stack.shape[0])
  13. predictions = np.zeros(test_stack.shape[0])
  14. scores = []
  15. folds = RepeatedKFold(n_splits=5, n_repeats=2, random_state=2021)
  16. for fold_, (trn_idx, val_idx) in enumerate(folds.split(train_stack, y)):
  17. print(f"fold n°{fold_+1}")
  18. trn_data, trn_y = train_stack.iloc[trn_idx], y.iloc[trn_idx]
  19. val_data, val_y = train_stack.iloc[val_idx], y.iloc[val_idx]
  20. clf = Ridge(random_state=2021)
  21. clf.fit(trn_data, trn_y)
  22. oof[val_idx] = clf.predict(val_data)
  23. predictions += clf.predict(test_stack) / len(folds.split(train_stack, y))
  24. score_single = mean_absolute_error(val_y, oof[val_idx])
  25. scores.append(score_single)
  26. print(f'{fold_+1}/{5}', score_single)
  27. print('mean: ', np.mean(scores))
  28. return oof, predictions
  29. stack_oof, stack_pred = stack_model(lgb_oof, xgb_oof, cat_oof, lgb_test, xgb_test, cat_test, train['target'])

 6.基于神经网络的模型堆叠

简单的神经网络模型展示(并非本代码使用的网络层,仅起框架展示作用)

以下代码定义了一个名为stack_model_nn的函数,它实现了一个使用神经网络作为元学习器的堆叠模型(Stacking Model)过程。函数接收来自三个基础模型(如LightGBM、XGBoost和CatBoost)的OOF(Out-Of-Fold)预测和测试集预测结果,以及训练集的真实标签。首先,它将输入的数据从Series转换为DataFrame,并将OOF预测和测试预测结果分别拼接成新的DataFrame。

接下来,函数通过多次重复K折交叉验证(RepeatedKFold)来训练一个神经网络模型。在每个折叠中,它使用当前折叠的训练数据(即基础模型的OOF预测结果)来训练神经网络,并在验证集上进行预测以评估模型性能。神经网络模型包含三个密集层(Dense layers),使用ReLU激活函数和Adam优化器,以平均绝对误差(MAE)作为损失函数。

在每次验证后,函数计算并记录模型在验证集上的MAE分数,并将神经网络在测试集上的预测结果进行累加平均,以便最终得到整个测试集的预测。最后,函数返回堆叠模型在训练集上的OOF预测和测试集上的预测结果,以及验证过程中所有折叠的平均分数。

通过这种方式,stack_model_nn函数利用神经网络作为元学习器来整合不同基础模型的预测,旨在提高模型的预测准确性和泛化能力。

  1. def stack_model_nn(oof_1, oof_2, oof_3, predictions_1, predictions_2, predictions_3, y):
  2. # 将Series转换为DataFrame
  3. oof_1, oof_2, oof_3 = pd.DataFrame(oof_1), pd.DataFrame(oof_2), pd.DataFrame(oof_3)
  4. predictions_1, predictions_2, predictions_3 = pd.DataFrame(predictions_1), pd.DataFrame(predictions_2), pd.DataFrame(predictions_3)
  5. # 拼接OOF和测试预测
  6. train_stack = pd.concat([oof_1, oof_2, oof_3], axis=1)
  7. test_stack = pd.concat([predictions_1, predictions_2, predictions_3], axis=1)
  8. oof = np.zeros(train_stack.shape[0])
  9. predictions = np.zeros(test_stack.shape[0])
  10. scores = []
  11. folds = RepeatedKFold(n_splits=5, n_repeats=2, random_state=2021)
  12. for fold_, (trn_idx, val_idx) in enumerate(folds.split(train_stack, y)):
  13. print(f"fold n°{fold_+1}")
  14. trn_data, trn_y = train_stack.iloc[trn_idx], y.iloc[trn_idx]
  15. val_data, val_y = train_stack.iloc[val_idx], y.iloc[val_idx]
  16. # 定义神经网络模型
  17. model = Sequential([
  18. Dense(128, activation='relu', input_shape=(trn_data.shape[1],)),
  19. Dense(64, activation='relu'),
  20. Dense(1)
  21. ])
  22. model.compile(optimizer=Adam(), loss='mean_absolute_error')
  23. # 预测
  24. oof[val_idx] = model.predict(val_data).flatten()
  25. predictions += model.predict(test_stack).flatten() / len(folds.split(train_stack, y))
  26. # 计算并打印分数
  27. score_single = mean_absolute_error(val_y, oof[val_idx])
  28. scores.append(score_single)
  29. print(f'{fold_+1}/{5}', score_single)
  30. print('mean: ', np.mean(scores))
  31. return oof, predictions
  32. stack_oof, stack_pred1 = stack_model_nn(lgb_oof, xgb_oof, cat_oof, lgb_test, xgb_test, cat_test, train['target'])

本代码仅起抛砖引玉的作用,最后的结果就不贴了,欢迎各位大佬进行进一步的改进。 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/875240
推荐阅读
相关标签
  

闽ICP备14008679号