Kaggle-Predict Calorie Expenditure-(回归+xgb+cat+lgb+模型融合)

Predict Calorie Expenditure

题意:

给出每个人的基本信息,预测运动后的卡路里消耗值。

数据处理:

1.构造出人体机能、运动相关的特征值。

2.所有特征值进行从新组合,注意唯独爆炸

3.对连续信息分箱变成离散

建立模型:

1.xgb模型,lgb模型,cat模型

2.使用stack堆叠融合,使用3折交叉验证

3.对xgb、lgb、cat进行K折交叉验证,最终和stack进行结果融合。

代码:
python 复制代码
import os
import sys
import warnings
import numpy as np
import pandas as pd
import seaborn
from catboost import CatBoostRegressor
from lightgbm import LGBMRegressor
from matplotlib import pyplot as plt
import lightgbm
from mlxtend.regressor import StackingCVRegressor
from sklearn import clone
from sklearn.ensemble import VotingRegressor, StackingClassifier, StackingRegressor
from sklearn.linear_model import Lasso, LogisticRegression, RidgeCV
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score, make_scorer, mean_squared_log_error
from sklearn.model_selection import train_test_split, GridSearchCV, cross_val_score
from sklearn.preprocessing import StandardScaler
from xgboost import XGBRegressor
from sklearn.preprocessing import RobustScaler
from sklearn.model_selection import KFold
from sklearn.linear_model import Ridge

def init():
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'  # 仅输出错误日志
    warnings.simplefilter('ignore')  # 忽略警告日志
    pd.set_option('display.width', 1000)
    pd.set_option('display.max_colwidth', 1000)
    pd.set_option("display.max_rows", 1000)
    pd.set_option("display.max_columns", 1000)


def show_dataframe(df):
    print("查看特征值和特征值类型\n" + str(df.dtypes) + "\n" + "-" * 100)
    print("查看前10行信息\n" + str(df.head()) + "\n" + "-" * 100)
    print("查看每个特征值的各种数据统计信息\n" + str(df.describe()) + "\n" + "-" * 100)
    print("输出重复行的个数\n" + str(df.duplicated().sum()) + "\n" + "-" * 100)
    print("查看每列的缺失值个数\n" + str(df.isnull().sum()) + "\n" + "-" * 100)
    print("查看缺失值的具体信息\n" + str(df.info()) + "\n" + "-" * 100)
    #print("输出X所有值出现的是什么,还有对应出现的次数\n" + str(df['X'].value_counts()) + "\n" + "-" * 100)


def show_relation(data, colx, coly):  # 输出某一特征值与目标值的关系
    if data[colx].dtype == 'object' or data[colx].dtype == 'category' or len(data[colx].unique()) < 20:
        seaborn.boxplot(x=colx, y=coly, data=data)
    else:
        plt.scatter(data[colx], data[coly])
    plt.xlabel(colx)
    plt.ylabel(coly)
    plt.show()

# 自定义RMSLE评分函数(GridSearchCV需要最大化评分,因此返回负RMSLE)
def rmsle_scorer(y_true, y_pred):
    y_pred = np.clip(y_pred, 1e-15, None)  # 防止对0取对数
    y_true = np.clip(y_true, 1e-15, None)
    log_error = np.log(y_pred + 1) - np.log(y_true + 1)
    rmsle = np.sqrt(np.mean(log_error ** 2))
    return -rmsle  # 返回负值,因为GridSearchCV默认最大化评分

if __name__ == '__main__':
    init()

    df_train = pd.read_csv('/kaggle/input/playground-series-s5e5/train.csv')
    df_test = pd.read_csv('/kaggle/input/playground-series-s5e5/test.csv')


    #for col in df_train.columns:
    #   show_relation(df_train, col, 'Calories')

    #特征工程
    df_all = pd.concat([df_train.drop(['id', 'Calories'], axis=1), df_test.drop(['id'], axis=1)], axis=0)

    df_all['Sex'] = df_all['Sex'].map({'male': 0, 'female': 1})
    df_all = df_all.reset_index(drop=True)
    #构造BMI
    df_all['BMI'] = df_all['Weight'] / (df_all['Height'] / 100) ** 2

    #Harris-Benedict公式
    df_all['BMR'] = 0
    df_all.loc[df_all['Sex'] == 0, 'BMR'] = 88.362 + (13.397 * df_all['Weight']) + (4.799 * df_all['Height']) - (5.677 * df_all['Age'])
    df_all.loc[df_all['Sex'] == 1, 'BMR'] = 447.593 + (9.247 * df_all['Weight']) + (3.098 * df_all['Height']) - (4.330 * df_all['Age'])

    # 数值特征标准化
    #numeric_features = ['Age', 'Height', 'Weight', 'Duration', 'Heart_Rate', 'Body_Temp']
    #scaler = StandardScaler()
    #df_all[numeric_features] = scaler.fit_transform(df_all[numeric_features])

    #运动强度特征
    df_all['Max_HR'] = 220 - df_all['Age']  # 最大心率
    df_all['HR_Reserve_Ratio'] = df_all['Heart_Rate'] / df_all['Max_HR']

    #交互特征
    df_all['Weight_Duration'] = df_all['Weight'] * df_all['Duration']
    df_all['Sex_Weight'] = df_all['Sex'] * df_all['Weight']

    # 构造运动功率特征
    df_all['workload'] = df_all['Weight'] * df_all['Duration'] * df_all['Heart_Rate'] / 1000

    # 构造生理特征交互项
    df_all['age_heart_ratio'] = df_all['Age'] / df_all['Heart_Rate']

    # 时间维度特征(如有时间戳)
    df_all['hour_of_day'] = df_all['Duration']/60/24

    # 组合特征
    numeric_cols = df_all.columns
    for i in range(len(numeric_cols)):
        feature_1 = numeric_cols[i]
        for j in range(i + 1, len(numeric_cols)):
            feature_2 = numeric_cols[j]
            df_all[f'{feature_1}_x_{feature_2}'] = df_all[feature_1] * df_all[feature_2]

    #数值归一化
    #scaler = RobustScaler()
    #df_all = scaler.fit_transform(df_all)
    now_col = ['Age', 'Height', 'Weight', 'Duration', 'Heart_Rate', 'Body_Temp', 'BMI']
    for i in now_col:
        df_all[i + "_box"] = pd.cut(df_all[i], bins=10, labels=False, right=False)

    X_train = df_all[:df_train.shape[0]]
    Y_train = np.log1p(df_train['Calories'])
    x_test = df_all[df_train.shape[0]:]

    #xgb
    model_xgb =estimator=XGBRegressor(
            random_state=42,
            n_estimators=8000,
            objective='reg:squarederror',
            eval_metric='rmse',
            device='cuda',
            learning_rate=0.05,
            max_depth=8,
            colsample_bytree=0.75,
            subsample=0.9,
            #reg_lambda = 1,
            #reg_alpha = 0.5,
            early_stopping_rounds=500,
    )
    #lgb
    model_lgb = lightgbm.LGBMRegressor(
        n_estimators=3000,  # 增加迭代次数配合早停
        learning_rate=0.03,  # 减小学习率
        num_leaves=15,  # 限制模型复杂度
        min_child_samples=25,  # 增加最小叶子样本数
        reg_alpha=0.1,  # L1正则化
        reg_lambda=0.1,  # L2正则化
        objective='regression_l1',  # 改用MAE损失
        early_stopping_rounds=500,
    )
    #cat
    model_cat = CatBoostRegressor(
        iterations=3500,
        learning_rate=0.02,
        depth=12,
        loss_function='RMSE',
        l2_leaf_reg=3,
        random_seed=42,
        eval_metric='RMSE',
        early_stopping_rounds=200,
        verbose=1000,
        task_type='GPU',
    )
    #融合
    #创建基模型列表(需禁用早停以生成完整预测)
    base_models = [
        ('xgb', XGBRegressor(
            early_stopping_rounds=None,  # 禁用早停
            **{k: v for k, v in model_xgb.get_params().items() if k != 'early_stopping_rounds'}
        )),
        ('lgb', LGBMRegressor(
            early_stopping_rounds=None,  # 禁用早停
            **{k: v for k, v in model_lgb.get_params().items() if k != 'early_stopping_rounds'}
        )),
        ('cat', CatBoostRegressor(
            early_stopping_rounds=None,  # 禁用早停
            **{k: v for k, v in model_cat.get_params().items() if k != 'early_stopping_rounds'}
        ))
    ]
    meta_model = RidgeCV()
    model_stack = StackingRegressor(
        estimators=base_models,
        final_estimator=meta_model,
        cv=3,  # 使用3折交叉验证生成元特征
        passthrough=False,  # 不使用原始特征
        verbose=1
    )

    FOLDS = 20
    KF = KFold(n_splits=FOLDS, shuffle=True, random_state=42)
    cat_features = ['Sex']
    oof_cat = np.zeros(len(df_train))
    pred_cat = np.zeros(len(df_test))
    oof_xgb = np.zeros(len(df_train))
    pred_xgb = np.zeros(len(df_test))
    oof_lgb = np.zeros(len(df_train))
    pred_lgb = np.zeros(len(df_test))

    for i, (train_idx, valid_idx) in enumerate(KF.split(X_train, Y_train)):
        print('#' * 15, i + 1, '#' * 15)
        ## SPLIT DS
        x_train, y_train = X_train.iloc[train_idx], Y_train.iloc[train_idx]
        x_valid, y_valid = X_train.iloc[valid_idx], Y_train.iloc[valid_idx]

        ## CATBOOST fit
        model_cat.fit(x_train, y_train, eval_set=[(x_valid, y_valid)], cat_features=cat_features,
                      use_best_model=True, verbose=0)
        ## XGB FIR
        model_xgb.fit(x_train, y_train, eval_set=[(x_valid, y_valid)], verbose=0)
        ## LGB MODEL
        model_lgb.fit(x_train, y_train, eval_set=[(x_valid, y_valid)])
        ## PREDICTION CATBOOST
        oof_cat[valid_idx] = model_cat.predict(x_valid)
        pred_cat += model_cat.predict(x_test)
        ## PREDICTION XGB
        oof_xgb[valid_idx] = model_xgb.predict(x_valid)
        pred_xgb += model_xgb.predict(x_test)
        ## PREDICTION LGB
        oof_lgb[valid_idx] = model_lgb.predict(x_valid)
        pred_lgb += model_lgb.predict(x_test)

        cat_rmse = mean_squared_error(y_valid, oof_cat[valid_idx]) ** 0.5
        xgb_rmse = mean_squared_error(y_valid, oof_xgb[valid_idx]) ** 0.5
        lgb_rmse = mean_squared_error(y_valid, oof_lgb[valid_idx]) ** 0.5

        print(
            f'FOLD {i + 1} CATBOOST_RMSE = {cat_rmse:.4f} <=> XGB_RMSE = {xgb_rmse:.4f} <=> LGB_RMSE = {lgb_rmse:.4f}')

    #预测
    pred_cat /= FOLDS
    pred_xgb /= FOLDS
    pred_lgb /= FOLDS
    pred_stack = model_stack.predict(df_test)

    pred_all = np.expm1(pred_xgb) * 0.1 + np.expm1(pred_stack) * 0.80 + np.expm1(pred_cat) * 0.1

    submission = pd.DataFrame({
        'id': df_test['id'],
        'Calories': pred_all
    })
    submission['Calories'] = np.clip(submission['Calories'], a_min=1, a_max=20*df_test['Duration'])
    submission.to_csv('/kaggle/working/submission.csv', index=False)
相关推荐
生信大表哥几秒前
GPT-5-Codex VS Gemini 3 VS Claude Sonnet 4.5 新手小白入门学习教程
人工智能·gpt·学习·rstudio·数信院生信服务器
子午8 分钟前
【植物识别系统】Python+TensorFlow+Django+人工智能+深度学习+卷积神经网络算法
人工智能·python·深度学习
ONLYOFFICE8 分钟前
ONLYOFFICE 文档与桌面编辑器 9.2 版本更新说明
人工智能·编辑器·onlyoffice
sensen_kiss9 分钟前
INT301 Bio-computation 生物计算(神经网络)Pt.10 联想存储器与HOPFIELD网络
人工智能·深度学习·神经网络
九千七52610 分钟前
sklearn学习(6)决策树
人工智能·python·学习·决策树·机器学习·sklearn
All The Way North-12 分钟前
PyTorch 二分类损失函数详解:BCELoss vs BCEWithLogitsLoss 最佳实践指南
人工智能·pytorch·深度学习·机器学习·二分类损失函数
草莓熊Lotso12 分钟前
Git 多人协作全流程实战:分支协同 + 冲突解决 + 跨分支协助
linux·运维·服务器·人工智能·经验分享·git·python
丝斯201114 分钟前
AI学习笔记整理(28)—— 计算机视觉之姿态估计与动作识别
人工智能·笔记·学习
严文文-Chris15 分钟前
神经网络的前向传播、反向传播、优化器分别是什么?有什么关系?
人工智能·深度学习·神经网络
老蒋新思维15 分钟前
创客匠人峰会深度解析:创始人 IP 打造的 “情绪 + 技术” 双引擎
大数据·网络·人工智能·网络协议·tcp/ip·重构·创客匠人