6 回归集成:xgb、lgb、cat

这个代码是从kaggle上拷贝过来的:

  1. 如何使用三个树模型模块化训练;

  2. 文本特征如何做,如何挖掘;

  3. 时间特征的处理;

  4. 模型权重集成;

    import pandas as pd
    import math
    import numpy as np
    import joblib
    import optuna

    from lightgbm import LGBMRegressor
    from catboost import CatBoostRegressor
    from xgboost import XGBRegressor
    from sklearn.preprocessing import *
    from sklearn.metrics import *
    from sklearn.model_selection import *

    from sklearn.decomposition import TruncatedSVD
    from sklearn.feature_extraction.text import TfidfVectorizer

    import datetime
    import gc
    from sklearn.base import clone

    pd.set_option('display.max_columns', None)

    import warnings
    warnings.filterwarnings("ignore")

    d_s = pd.read_csv('/kaggle/input/rohlik-orders-forecasting-challenge/solution_example.csv')

    te_d = pd.read_csv('/kaggle/input/rohlik-orders-forecasting-challenge/test.csv')

    tr_d = pd.read_csv('/kaggle/input/rohlik-orders-forecasting-challenge/train.csv')

    tr_d.drop('id',axis=1,inplace=True)
    te_d.drop('id',axis=1,inplace=True)

    tr_d['holiday_name'].fillna('None', inplace=True)
    te_d['holiday_name'].fillna('None', inplace=True)

    def Process_Date(Df):

     Df['date'] = pd.to_datetime(Df['date'])
    
     Df['year'] = Df['date'].dt.year
    
     Df['day'] = Df['date'].dt.day
    
     Df['month'] = Df['date'].dt.month
    
     Df['month_name'] = Df['date'].dt.month_name()
    
     Df['day_of_week'] = Df['date'].dt.day_name()
    
     Df['week'] = Df['date'].dt.isocalendar().week
     
     Df['year_sin'] = np.sin(2 * np.pi * Df['year'])
     Df['year_cos'] = np.cos(2 * np.pi * Df['year'])
     Df['month_sin'] = np.sin(2 * np.pi * Df['month'] / 12) 
     Df['month_cos'] = np.cos(2 * np.pi * Df['month'] / 12)
     Df['day_sin'] = np.sin(2 * np.pi * Df['day'] / 31)  
     Df['day_cos'] = np.cos(2 * np.pi * Df['day'] / 31)
     Df['group']=(Df['year']-2020)*48+Df['month']*4+Df['day']//7
     
     Df['total_holidays_month'] = Df.groupby(['year', 'month'])['holiday'].transform('sum')
     Df['total_shops_closed_week'] = Df.groupby(['year', 'week'])['shops_closed'].transform('sum')
    
     Df['group_sin'] = np.sin(2 * np.pi * Df['group'] / Df['group'].max())
     Df['group_cos'] = np.cos(2 * np.pi * Df['group'] / Df['group'].max())
    
    
     return Df
    

    tr_d = Process_Date(tr_d)
    te_d = Process_Date(te_d)

    tr_d = tr_d[['warehouse', 'date', 'holiday_name', 'holiday', 'shops_closed',
    'winter_school_holidays', 'school_holidays', 'year', 'day', 'month',
    'month_name', 'day_of_week', 'week', 'year_sin', 'year_cos',
    'month_sin', 'month_cos', 'day_sin', 'day_cos', 'group',
    'total_holidays_month', 'total_shops_closed_week',
    'group_sin', 'group_cos',
    'orders']]

    le_month = LabelEncoder()
    le_week = LabelEncoder()
    le_war = LabelEncoder()

    tr_d['month_name'] = le_month.fit_transform(tr_d['month_name'])
    tr_d['day_of_week'] = le_week.fit_transform(tr_d['day_of_week'])
    tr_d['warehouse'] = le_war.fit_transform(tr_d['warehouse'])

    te_d['month_name'] = le_month.transform(te_d['month_name'])
    te_d['day_of_week'] = le_week.transform(te_d['day_of_week'])
    te_d['warehouse'] = le_war.transform(te_d['warehouse'])

    def apply_tfidf_svd(df, text_column, max_features=1000, n_components=10):

     vectorizer = TfidfVectorizer(max_features=max_features, stop_words='english')
    
     vectors = vectorizer.fit_transform(df[text_column])
    
     svd = TruncatedSVD(n_components)
    
     x_sv = svd.fit_transform(vectors)
    
     tfidf_df = pd.DataFrame(x_sv)
    
     cols = [(text_column + "_tfidf_" + str(f)) for f in tfidf_df.columns.to_list()]
    
     tfidf_df.columns = cols
    
     df = df.reset_index(drop=True)
    
     df = pd.concat([df, tfidf_df], axis="columns")
    
     return df
    

    tr_d = apply_tfidf_svd(tr_d,'holiday_name')
    te_d = apply_tfidf_svd(te_d,'holiday_name')

    tr_d.drop(['date','holiday_name'],axis=1,inplace=True)
    te_d.drop(['date','holiday_name'],axis=1,inplace=True)

    print(f"Shape Of Train Data is {tr_d.shape}")
    print(f"Shape Of Test Data is {te_d.shape}")

    %%time

    X = tr_d.drop('orders',axis=1)
    y =tr_d['orders']

    def cross_validate(model, n_splits=15):

     scores = []
     test_preds = np.zeros(len(te_d))
     
     groups = X['group']
     
     kfold = GroupKFold(n_splits=n_splits)
     
     for fold, (train_index, valid_index) in enumerate(kfold.split(X, y, groups=groups)):
         
         X_train = X.iloc[train_index]
         y_train = y.iloc[train_index]
         X_val = X.iloc[valid_index]
         y_val = y.iloc[valid_index]
                     
         m = clone(model)
         m.fit(X_train, y_train, eval_set=[(X_val, y_val)])
         
         y_pred = m.predict(X_val)
         score = mean_absolute_percentage_error(y_val, y_pred)
         
         scores.append(score)
         
         test_preds += m.predict(te_d) / n_splits
    
         gc.collect()
    
     print(f" MAPE mean: {np.array(scores).mean():.7f} (+- {np.array(scores).std():.7f})")
    
     return test_preds
    

    %%time

    SEED = 2375

    cat = CatBoostRegressor(verbose=0,learning_rate=0.01,iterations=2000,
    random_state = SEED)
    cat_test_preds = cross_validate(cat)

    SEED = 1023
    xgb = XGBRegressor(n_estimators=1000,learning_rate=0.05,verbosity=0,
    random_state=SEED)
    xgb_test_preds = cross_validate(xgb)

    %%time

    lgb = LGBMRegressor(verbose=-1,
    random_state = SEED
    )
    lgb_test_preds = cross_validate(lgb)

    %%time

    weights = {
    'cat_test_preds': 0.45,
    'lgb_test_preds': 0.45,
    'xgb_test_preds': 0.1,

    }

    cat_test_preds_weighted = cat_test_preds * weights['cat_test_preds']
    lgb_test_preds_weighted = lgb_test_preds * weights['lgb_test_preds']
    xgb_test_preds_weighted = xgb_test_preds * weights['xgb_test_preds']

    ensemble_preds = cat_test_preds_weighted + lgb_test_preds_weighted + xgb_test_preds_weighted

    d_s['orders'] = ensemble_preds
    d_s['id'] = d_s['id']

    d_s.to_csv('Submission.csv', index=False)

    print(d_s.head())

相关推荐
这个男人是小帅1 小时前
【GAT】 代码详解 (1) 运行方法【pytorch】可运行版本
人工智能·pytorch·python·深度学习·分类
__基本操作__1 小时前
边缘提取函数 [OPENCV--2]
人工智能·opencv·计算机视觉
Doctor老王1 小时前
TR3:Pytorch复现Transformer
人工智能·pytorch·transformer
热爱生活的五柒1 小时前
pytorch中数据和模型都要部署在cuda上面
人工智能·pytorch·深度学习
HyperAI超神经3 小时前
【TVM 教程】使用 Tensorize 来利用硬件内联函数
人工智能·深度学习·自然语言处理·tvm·计算机技术·编程开发·编译框架
扫地的小何尚4 小时前
NVIDIA RTX 系统上使用 llama.cpp 加速 LLM
人工智能·aigc·llama·gpu·nvidia·cuda·英伟达
埃菲尔铁塔_CV算法7 小时前
深度学习神经网络创新点方向
人工智能·深度学习·神经网络
艾思科蓝-何老师【H8053】7 小时前
【ACM出版】第四届信号处理与通信技术国际学术会议(SPCT 2024)
人工智能·信号处理·论文发表·香港中文大学
weixin_452600698 小时前
《青牛科技 GC6125:驱动芯片中的璀璨之星,点亮 IPcamera 和云台控制(替代 BU24025/ROHM)》
人工智能·科技·单片机·嵌入式硬件·新能源充电桩·智能充电枪
学术搬运工8 小时前
【珠海科技学院主办,暨南大学协办 | IEEE出版 | EI检索稳定 】2024年健康大数据与智能医疗国际会议(ICHIH 2024)
大数据·图像处理·人工智能·科技·机器学习·自然语言处理