【飞桨学习赛:MarTech Challenge 点击反欺诈预测】第10名方案

该内容展示了一个模型从基线版本V1到V5的迭代过程,分数从86.746提升至89.0787。过程中进行了数据探索,处理缺失值和object类型字段,优化特征工程(如构造面积、时间差等特征),尝试LightGBM、XGBoost等模型,采用交叉验证,最终得到分数89.1093的最佳版本。

☞☞☞AI 智能聊天, 问答助手, AI 智能搜索, 免费无限量使用 DeepSeek R1 模型☜☜☜

【飞桨学习赛:martech challenge 点击反欺诈预测】第10名方案 - 创想鸟

In [ ]

#best版本-89.1093分数import pandas as pdimport warningswarnings.filterwarnings('ignore')# 数据加载和去除Unnameed字段train = pd.read_csv('./train.csv')test = pd.read_csv('./test1.csv')train = train.iloc[:, 1:]test = test.iloc[:,1:]res = pd.DataFrame(test['sid'])# 去除数据探索发现问题的字段col = train.columns.tolist()remove_list = ['lan', 'os','label', 'sid']for i in remove_list:    col.remove(i)features = train[col]test_features = test[col]# 对osv进行数据清洗def osv_trans(x):    x = str(x).replace('Android_', '').replace('Android ', '').replace('W', '')    if str(x).find('.')>0:        temp_index1 = x.find('.')        if x.find(' ')>0:            temp_index2 = x.find(' ')        else:            temp_index2 = len(x)         if x.find('-')>0:            temp_index2 = x.find('-')                    result = x[0:temp_index1] + '.' + x[temp_index1+1:temp_index2].replace('.', '')        try:            return float(result)        except:            print('有错误: '+x)            return 0    try:        return float(x)    except:        print('有错误: '+x)        return 0features['osv'].fillna('8.1.0', inplace=True)features['osv'] = features['osv'].apply(osv_trans)test_features['osv'].fillna('8.1.0', inplace=True)test_features['osv'] = test_features['osv'].apply(osv_trans)# 对timestamp进行数据清洗与特征变换,from datetime import datetimefeatures['timestamp'] = features['timestamp'].apply(lambda x: datetime.fromtimestamp(x/1000))test_features['timestamp'] = test_features['timestamp'].apply(lambda x: datetime.fromtimestamp(x/1000))temp = pd.DatetimeIndex(features['timestamp'])features['year'] = temp.yearfeatures['month'] = temp.monthfeatures['day'] = temp.dayfeatures['hour'] = temp.hourfeatures['minute'] = temp.minutefeatures['week_day'] = temp.weekday #星期几start_time = features['timestamp'].min()features['time_diff'] = features['timestamp'] - start_timefeatures['time_diff'] = features['time_diff'].dt.days + features['time_diff'].dt.seconds/3600/24temp = pd.DatetimeIndex(test_features['timestamp'])test_features['year'] = temp.yeartest_features['month'] = temp.monthtest_features['day'] = temp.daytest_features['hour'] = temp.hourtest_features['minute'] = temp.minutetest_features['week_day'] = temp.weekday #星期几 test_features['time_diff'] = test_features['timestamp'] - start_timetest_features['time_diff'] = test_features['time_diff'].dt.days + test_features['time_diff'].dt.seconds/3600/24features = features.drop(['timestamp'],axis = 1)test_features = test_features.drop(['timestamp'],axis = 1)# 对version进行数据清洗与特征变换def version_trans(x):    if x=='V3':        return 3    if x=='v1':        return 1    if x=='P_Final_6':        return 6    if x=='V6':        return 6    if x=='GA3':        return 3    if x=='GA2':        return 2    if x=='V2':        return 2    if x=='50':        return 5    return int(x)features['version'] = features['version'].apply(version_trans)test_features['version'] = test_features['version'].apply(version_trans)features['version'] = features['version'].astype('int')test_features['version'] = test_features['version'].astype('int')# 对lan进行数据清洗与特征变换 对于有缺失的lan 设置为22    lan_map = {'zh-CN': 1, 'zh_CN':2, 'Zh-CN': 3, 'zh-cn': 4, 'zh_CN_#Hans':5, 'zh': 6, 'ZH': 7, 'cn':8, 'CN':9, 'zh-HK': 10, 'tw': 11, 'TW': 12, 'zh-TW': 13,             'zh-MO':14, 'en':15, 'en-GB': 16, 'en-US': 17, 'ko': 18, 'ja': 19, 'it': 20, 'mi':21} train['lan'] = train['lan'].map(lan_map)test['lan'] = test['lan'].map(lan_map)train['lan'].fillna(22, inplace=True)test['lan'].fillna(22, inplace=True)# 构造面积特征和构造相除特征features['dev_area'] = features['dev_height'] * features['dev_width']test_features['dev_area'] = test_features['dev_height'] * test_features['dev_width']features['dev_rato'] = features['dev_height'] / features['dev_width']test_features['dev_rato'] = test_features['dev_height'] / test_features['dev_width']# APP版本与操作系统版本差features['version_osv'] = features['osv'] - features['version']test_features['version_osv'] = test_features['osv'] - test_features['version']# 对fea_hash与fea1_hash特征变换features['fea_hash_len'] = features['fea_hash'].map(lambda x: len(str(x)))features['fea1_hash_len'] = features['fea1_hash'].map(lambda x: len(str(x)))features['fea_hash'] = features['fea_hash'].map(lambda x: 0 if len(str(x))>16 else int(x))features['fea1_hash'] = features['fea1_hash'].map(lambda x: 0 if len(str(x))>16 else int(x))test_features['fea_hash_len'] = test_features['fea_hash'].map(lambda x: len(str(x)))test_features['fea1_hash_len'] = test_features['fea1_hash'].map(lambda x: len(str(x)))test_features['fea_hash'] = test_features['fea_hash'].map(lambda x: 0 if len(str(x))>16 else int(x))test_features['fea1_hash'] = test_features['fea1_hash'].map(lambda x: 0 if len(str(x))>16 else int(x))#通过特征比,寻找关键特征,构造新特征,新特征字段 = 原始特征字段 + 1def find_key_feature(train, selected):    temp = pd.DataFrame(columns = [0,1])    temp0 = train[train['label'] == 0]    temp1 = train[train['label'] == 1]    temp[0] = temp0[selected].value_counts() / len(temp0) * 100    temp[1] = temp1[selected].value_counts() / len(temp1) * 100    temp[2] = temp[1] / temp[0]    #选出大于10倍的特征    result = temp[temp[2] > 10].sort_values(2, ascending = False).index    return resultselected_cols = ['osv','apptype', 'carrier', 'dev_height', 'dev_ppi','dev_width', 'media_id',                  'package', 'version', 'fea_hash', 'location', 'fea1_hash','cus_type']key_feature = {}for selected in selected_cols:    key_feature[selected] = find_key_feature(train, selected)def f(x, selected):    if x in key_feature[selected]:        return 1    else:        return 0for selected in selected_cols:    if len(key_feature[selected]) > 0:        features[selected+'1'] = features[selected].apply(f, args = (selected,))        test_features[selected+'1'] = test_features[selected].apply(f, args = (selected,))        print(selected+'1 created')#CatBoost模型from catboost import CatBoostClassifierfrom sklearn.model_selection import StratifiedKFoldfrom sklearn.metrics import roc_auc_scoremodel=CatBoostClassifier(            loss_function="Logloss",            eval_metric="AUC",            task_type="GPU",            learning_rate=0.1,            iterations=1000,            random_seed=2021,            od_type="Iter",            depth=7)n_folds =10 #十折交叉校验answers = []mean_score = 0data_x=featuresdata_y=train['label']sk = StratifiedKFold(n_splits=n_folds, shuffle=True, random_state=2021)all_test = test_features.copy()for train, test in sk.split(data_x, data_y):      x_train = data_x.iloc[train]    y_train = data_y.iloc[train]    x_test = data_x.iloc[test]    y_test = data_y.iloc[test]    clf = model.fit(x_train,y_train, eval_set=(x_test,y_test),verbose=500) # 500条打印一条日志        yy_pred_valid=clf.predict(x_test,prediction_type='Probability')[:,-1]    print('cat验证的auc:{}'.format(roc_auc_score(y_test, yy_pred_valid)))    mean_score += roc_auc_score(y_test, yy_pred_valid) / n_folds        y_pred_valid = clf.predict(all_test,prediction_type='Probability')[:,-1]    answers.append(y_pred_valid) print('mean valAuc:{}'.format(mean_score))cat_pre=sum(answers)/n_foldscat_preres['label']=[1 if x>=0.5 else 0 for x in cat_pre]res.to_csv('./baselinev6.csv',index=False)

       

有错误: f073b_changxiang_v01_b1b8_20180915有错误: %E6%B1%9F%E7%81%B5OS+5.0有错误: GIONEE_YNGA

       

项目思考的过程与baseline迭代版本

BaseLine V1_lgb–分数: 86.746

切换盘符:

jupyter notebook D:

       

一、数据探索

1、去除Unnameed字段

train = train.iloc[:, 1:]test = test.iloc[:,1:]

       

2、查看字段类型

写法1:

train.info()

       

写法2:

或者直接查看类型为object的列

train.select_dtypes(include='object').columns

       

发现以下字段为object类型需要进行数值变换

 7   lan         316720 non-null  object  10  os          500000 non-null  object  11  osv         493439 non-null  object  15  version     500000 non-null  object  16  fea_hash    500000 non-null  object

       

以lan为例查看里面数据情况

train['lan'].value_counts()

       

3、查看缺失值的个数

写法1:

train.isnull().sum()

       

写法2:

t = train.isnull().sum()t[t>0]

       

发现以下字段缺少比较多

lan           183280osv             6561

       

4、唯一值的个数

查看唯一值的个数

features = train.columns.tolist()for feature in features:    if train[feature].nunique() ==1:        print(feature,train[feature].nunique())

       

发现os字段的唯一值个数太少

os 2

       

查看os

train['os'].value_counts()

       

发现os数据都为android

android    303175Android    196825Name: os, dtype: int64

       

5、数据探索的结论

object类型字段有:lan、osv 、osv、version、fea_hash

缺失值较多的字段有:lan、osv

唯一值个数较少且意义不大:os

没有意义的字段:sid

BaselineV1中也先去除timestamp

6、特征的相关性分析(补充)

# 对特征列进行相关性分析import matplotlib.pyplot as plt%matplotlib inlineimport seaborn as snsplt.figure(figsize=(10,10))sns.heatmap(train.corr(),cbar=True,annot=True,cmap='Blues')

       

二、数据预处理

最终去掉:【lan】【os】【osv】【version】【label】【sid】【timestamp】

remove_list = ['lan', 'os', 'osv', 'version', 'label', 'sid','timestamp']col = features #字段名for i in remove_list:    col.remove(i)features = train[col]

       

三、特征工程

1、fea_hash特征变换

#查看数据值train['fea_hash'].value_counts()#查看统计信息train['fea_hash'].describe()#查看映射的长度特征情况train['fea_hash'].map(lambda x:len(str(x))).value_counts()

       

fea_hash进行特征变换

# fea_hash的长度为新特征features['fea_hash_len'] = features['fea_hash'].map(lambda x: len(str(x)))features['fea1_hash_len'] = features['fea1_hash'].map(lambda x: len(str(x)))# 如果fea_hash很长,都归为0,否则为自己的本身features['fea_hash'] = features['fea_hash'].map(lambda x: 0 if len(str(x))>16 else int(x))features['fea1_hash'] = features['fea1_hash'].map(lambda x: 0 if len(str(x))>16 else int(x))

       

四、模型建立

test 做和train同样处理,利用lightgbm进行训练与预测,并保存,上诉过程全部合并代码如下:

#BaselineV1import pandas as pdimport warningsimport lightgbm as lgbwarnings.filterwarnings('ignore')# 数据加载train = pd.read_csv('./train.csv')test = pd.read_csv('./test1.csv')# 去除Unnameed字段train = train.iloc[:, 1:]test = test.iloc[:,1:]# 去除数据探索发现问题的字段col = train.columns.tolist()remove_list = ['lan', 'os', 'osv', 'version', 'label', 'sid','timestamp']for i in remove_list:    col.remove(i)features = train[col]test_features = test[col]# fea_hash特征变换features['fea_hash_len'] = features['fea_hash'].map(lambda x: len(str(x)))features['fea1_hash_len'] = features['fea1_hash'].map(lambda x: len(str(x)))features['fea_hash'] = features['fea_hash'].map(lambda x: 0 if len(str(x))>16 else int(x))features['fea1_hash'] = features['fea1_hash'].map(lambda x: 0 if len(str(x))>16 else int(x))test_features['fea_hash_len'] = test_features['fea_hash'].map(lambda x: len(str(x)))test_features['fea1_hash_len'] = test_features['fea1_hash'].map(lambda x: len(str(x)))test_features['fea_hash'] = test_features['fea_hash'].map(lambda x: 0 if len(str(x))>16 else int(x))test_features['fea1_hash'] = test_features['fea1_hash'].map(lambda x: 0 if len(str(x))>16 else int(x))#lightgbm进行训练与预测model = lgb.LGBMClassifier()model.fit(features,train['label'])result = model.predict(test_features)#res包括sid字段与label字段res = pd.DataFrame(test['sid'])res['label'] = result#保存在csv中res.to_csv('./baselineV1.csv',index=False)

       

BaseLine V2_lgb–分数: 88.2007

一、特征工程优化

1、利用osv特征

# 对osv进行数据清洗def osv_trans(x):    x = str(x).replace('Android_', '').replace('Android ', '').replace('W', '')    if str(x).find('.')>0:        temp_index1 = x.find('.')        if x.find(' ')>0:            temp_index2 = x.find(' ')        else:            temp_index2 = len(x)         if x.find('-')>0:            temp_index2 = x.find('-')                    result = x[0:temp_index1] + '.' + x[temp_index1+1:temp_index2].replace('.', '')        try:            return float(result)        except:            print('有错误: '+x)            return 0    try:        return float(x)    except:        print('有错误: '+x)        return 0features['osv'].fillna('8.1.0', inplace=True)features['osv'] = features['osv'].apply(osv_trans)test_features['osv'].fillna('8.1.0', inplace=True)test_features['osv'] = test_features['osv'].apply(osv_trans)

       

2、利用TimeStamp特征

提取时间多尺度并计算时间diff(时间差)

# 对timestamp进行数据清洗与特征变换from datetime import datetimefeatures['timestamp'] = features['timestamp'].apply(lambda x: datetime.fromtimestamp(x/1000))test_features['timestamp'] = test_features['timestamp'].apply(lambda x: datetime.fromtimestamp(x/1000))temp = pd.DatetimeIndex(features['timestamp'])features['year'] = temp.yearfeatures['month'] = temp.monthfeatures['day'] = temp.dayfeatures['hour'] = temp.hourfeatures['minute'] = temp.minutefeatures['week_day'] = temp.weekday #星期几start_time = features['timestamp'].min()features['time_diff'] = features['timestamp'] - start_timefeatures['time_diff'] = features['time_diff'].dt.days + features['time_diff'].dt.seconds/3600/24temp = pd.DatetimeIndex(test_features['timestamp'])test_features['year'] = temp.yeartest_features['month'] = temp.monthtest_features['day'] = temp.daytest_features['hour'] = temp.hourtest_features['minute'] = temp.minutetest_features['week_day'] = temp.weekday #星期几 test_features['time_diff'] = test_features['timestamp'] - start_timetest_features['time_diff'] = test_features['time_diff'].dt.days + test_features['time_diff'].dt.seconds/3600/24col = features.columns.tolist()col.remove('timestamp')features = features[col]test_features = test_features[col]

       

3、利用Version特征

# 对version进行数据清洗与特征变换def version_trans(x):    if x=='V3':        return 3    if x=='v1':        return 1    if x=='P_Final_6':        return 6    if x=='V6':        return 6    if x=='GA3':        return 3    if x=='GA2':        return 2    if x=='V2':        return 2    if x=='50':        return 5    return int(x)features['version'] = features['version'].apply(version_trans)test_features['version'] = test_features['version'].apply(version_trans)features['version'] = features['version'].astype('int')test_features['version'] = test_features['version'].astype('int')

       

二、模型建立

上诉过程合并代码如下:

import pandas as pdimport warningsimport lightgbm as lgbwarnings.filterwarnings('ignore')# 数据加载和去除Unnameed字段train = pd.read_csv('./train.csv')test = pd.read_csv('./test1.csv')train = train.iloc[:, 1:]test = test.iloc[:,1:]# 去除数据探索发现问题的字段col = train.columns.tolist()remove_list = ['lan', 'os','label', 'sid']for i in remove_list:    col.remove(i)features = train[col]test_features = test[col]# 对osv进行数据清洗def osv_trans(x):    x = str(x).replace('Android_', '').replace('Android ', '').replace('W', '')    if str(x).find('.')>0:        temp_index1 = x.find('.')        if x.find(' ')>0:            temp_index2 = x.find(' ')        else:            temp_index2 = len(x)         if x.find('-')>0:            temp_index2 = x.find('-')                    result = x[0:temp_index1] + '.' + x[temp_index1+1:temp_index2].replace('.', '')        try:            return float(result)        except:            print('有错误: '+x)            return 0    try:        return float(x)    except:        print('有错误: '+x)        return 0features['osv'].fillna('8.1.0', inplace=True)features['osv'] = features['osv'].apply(osv_trans)test_features['osv'].fillna('8.1.0', inplace=True)test_features['osv'] = test_features['osv'].apply(osv_trans)# 对timestamp进行数据清洗与特征变换,from datetime import datetimefeatures['timestamp'] = features['timestamp'].apply(lambda x: datetime.fromtimestamp(x/1000))test_features['timestamp'] = test_features['timestamp'].apply(lambda x: datetime.fromtimestamp(x/1000))temp = pd.DatetimeIndex(features['timestamp'])features['year'] = temp.yearfeatures['month'] = temp.monthfeatures['day'] = temp.dayfeatures['hour'] = temp.hourfeatures['minute'] = temp.minutefeatures['week_day'] = temp.weekday #星期几start_time = features['timestamp'].min()features['time_diff'] = features['timestamp'] - start_timefeatures['time_diff'] = features['time_diff'].dt.days + features['time_diff'].dt.seconds/3600/24temp = pd.DatetimeIndex(test_features['timestamp'])test_features['year'] = temp.yeartest_features['month'] = temp.monthtest_features['day'] = temp.daytest_features['hour'] = temp.hourtest_features['minute'] = temp.minutetest_features['week_day'] = temp.weekday #星期几 test_features['time_diff'] = test_features['timestamp'] - start_timetest_features['time_diff'] = test_features['time_diff'].dt.days + test_features['time_diff'].dt.seconds/3600/24features = features.drop(['timestamp'],axis = 1)test_features = test_features.drop(['timestamp'],axis = 1)# 对version进行数据清洗与特征变换def version_trans(x):    if x=='V3':        return 3    if x=='v1':        return 1    if x=='P_Final_6':        return 6    if x=='V6':        return 6    if x=='GA3':        return 3    if x=='GA2':        return 2    if x=='V2':        return 2    if x=='50':        return 5    return int(x)features['version'] = features['version'].apply(version_trans)test_features['version'] = test_features['version'].apply(version_trans)features['version'] = features['version'].astype('int')test_features['version'] = test_features['version'].astype('int')# 对fea_hash与fea1_hash特征变换features['fea_hash_len'] = features['fea_hash'].map(lambda x: len(str(x)))features['fea1_hash_len'] = features['fea1_hash'].map(lambda x: len(str(x)))features['fea_hash'] = features['fea_hash'].map(lambda x: 0 if len(str(x))>16 else int(x))features['fea1_hash'] = features['fea1_hash'].map(lambda x: 0 if len(str(x))>16 else int(x))test_features['fea_hash_len'] = test_features['fea_hash'].map(lambda x: len(str(x)))test_features['fea1_hash_len'] = test_features['fea1_hash'].map(lambda x: len(str(x)))test_features['fea_hash'] = test_features['fea_hash'].map(lambda x: 0 if len(str(x))>16 else int(x))test_features['fea1_hash'] = test_features['fea1_hash'].map(lambda x: 0 if len(str(x))>16 else int(x))#lightgbm进行训练与预测model = lgb.LGBMClassifier()model.fit(features,train['label'])result = model.predict(test_features)#res包括sid字段与label字段res = pd.DataFrame(test['sid'])res['label'] = result#保存在csv中res.to_csv('./baselineV2.csv',index=False)print("已完成")

       

BaseLine V3_xgb–分数: 88.5073

一、特征工程优化

1、构造面积特征和相除特征

features['dev_area'] = features['dev_height'] * features['dev_width']test_features['dev_area'] = test_features['dev_height'] * test_features['dev_width']features['dev_rato'] = features['dev_height'] / features['dev_width']test_features['dev_rato'] = test_features['dev_height'] / test_features['dev_width']

       

2、APP版本与操作系统版本

features['version_osv'] = features['osv'] - features['version']test_features['version_osv'] = test_features['osv'] - test_features['version']

       

二、xgboost模型

1、LightGBM 祖传参数

clf = lgb.LGBMClassifier(            num_leaves=2**5-1, reg_alpha=0.25, reg_lambda=0.25, objective='multiclass',            max_depth=-1, learning_rate=0.005, min_child_samples=3, random_state=2021,            n_estimators=2000, subsample=1, colsample_bytree=1)device = gpugpu_platform_id = 0gpu_device_id = 0

       

2、XGBoost祖传参数

model_xgb = xgb.XGBClassifier(            max_depth=9, learning_rate=0.005, n_estimators=2000,             objective='multi:softprob', tree_method='gpu_hist',             subsample=0.8, colsample_bytree=0.8,             min_child_samples=3, eval_metric='logloss', reg_lambda=0.5)

       

3、使用xgboost并使用祖传参数

%%time#lightgbm进行训练与预测import xgboost as xgbmodel_xgb = xgb.XGBClassifier(            max_depth=15, learning_rate=0.05, n_estimators=5000,             objective='binary:logistic', tree_method='gpu_hist',             subsample=0.8, colsample_bytree=0.8,             min_child_samples=3, eval_metric='auc', reg_lambda=0.5        )model_xgb.fit(features,train['label'])result_xgb = model.predict(test_features)res = pd.DataFrame(test['sid'])res['label'] = result_xgbres.to_csv('./baselineV3.csv',index=False)print("已完成")

       

使用了xgboost的祖传参数

参数 含义

max_depth含义:树的最大深度,用来避免过拟合的。max_depth越大,模型会学到更具体更局部的样本,需要使用CV函数来进行调优。 
默认值:6,典型值:3-10。
调参:值越大,越容易过拟合;值越小,越容易欠拟合。learning_rate含义:学习率,控制每次迭代更新权重时的步长
默认值:0.3,典型值:0.01-0.2。 
调参:值越小,训练越慢。n_estimators总共迭代的次数,即决策树的个数,相当于训练的轮数objective回归任务:reg:linear (默认) reg: logistic 
二分类 binary:logistic (概率) binary:logitraw (类别) 
多分类 multi:softmax num_class=n (返回类别) multi:softprob num_class=n(返回概率)tree_method可调用gpu:gpu_hist。使用功能的树的构建方法,hist代表使用直方图优化的近似贪婪的算法subsample含义:训练样本采样率(行采样),训练每棵树时,使用的数据占全部训练集的比例。这个参数控制对于每棵树,随机采样的比例。 减小这个参数的值,算法会更加保守,避免过拟合。但是,如果这个值设置得过小,它可能会导致欠拟合。
默认值:1,典型值:0.5-1。
调参:防止过拟合。colsample_bytree含义:训练每棵树时,使用的数据占全部训练集的比例。默认值为1,典型值为0.5-1。和GBM中的subsample参数一模一样。这个参数控制对于每棵树,随机采样的比例。 减小这个参数的值,算法会更加保守,避免过拟合。但是,如果这个值设置得过小,它可能会导致欠拟合。 
典型值:0.5-1 
调参:防止过拟合。min_child_samples
eval_metric用户可以添加多种评价指标,对于Python用户要以list传递参数对给程序
可供的选择如下: 
回归任务(默认rmse) :rmse–均方根误差 mae–平均绝对误差 
分类任务(默认error) : auc–roc曲线下面积 error–错误率(二分类) merror–错误率(多分类) logloss–负对数似然函数(二分类) mlogloss–负对数似然函数(多分类)reg_lambdaL2正则化系数

4、可视化的方式查看特征的重要程度

from xgboost import plot_importanceimport matplotlib.pyplot as pltplot_importance(model_xgb)

       

BaseLine V4_xgb–分数: 88.946

一、使用十折交叉验证优化

%%time# 定义10折子模型from sklearn.model_selection import StratifiedKFoldfrom sklearn.metrics import accuracy_scoredef xgb_model(clf,train_x,train_y,test):    sk=StratifiedKFold(n_splits=10,random_state=2021,shuffle = True)    prob=[]    mean_acc=0    for k,(train_index,val_index) in enumerate(sk.split(train_x,train_y)):        train_x_real=train_x.iloc[train_index]        train_y_real=train_y.iloc[train_index]        val_x=train_x.iloc[val_index]        val_y=train_y.iloc[val_index]        #模型训练及验证集测试        clf=clf.fit(train_x_real,train_y_real)        val_y_pred=clf.predict(val_x)        acc_val=accuracy_score(val_y,val_y_pred)        print('第{}个子模型 accuracy{}'.format(k+1,acc_val))        mean_acc+=mean_acc/10        #预测测试集        test_y_pred=clf.predict_proba(test)        prob.append(test_y_pred)    print(mean_acc)    mean_prob=sum(prob)/10    return mean_prob  import xgboost as xgbmodel_xgb2 = xgb.XGBClassifier(            max_depth=15, learning_rate=0.005, n_estimators=5300,             objective='binary:logistic', tree_method='gpu_hist',             subsample=0.7, colsample_bytree=0.7,             min_child_samples=3, eval_metric='auc', reg_lambda=0.5        )result_xgb=xgb_model(model_xgb2,features,train['label'],test_features) result_xgb2=[x[1] for x in result_xgb]result_xgb2=[1 if x>=0.5 else 0 for x in result_xgb2] res = pd.DataFrame(test['sid'])res['label'] = result_xgb2res.to_csv('./baselineV4.csv', index=False)print('已完成')

       

BaseLine V5_xgb–分数: 89.0787

一、特征工程优化

通过特征比,寻找关键特征,构造新特征,新特征字段 = 原始特征字段 + 1

#通过特征比,寻找关键特征,构造新特征,新特征字段 = 原始特征字段 + 1def find_key_feature(train, selected):    temp = pd.DataFrame(columns = [0,1])    temp0 = train[train['label'] == 0]    temp1 = train[train['label'] == 1]    temp[0] = temp0[selected].value_counts() / len(temp0) * 100    temp[1] = temp1[selected].value_counts() / len(temp1) * 100    temp[2] = temp[1] / temp[0]    #选出大于10倍的特征    result = temp[temp[2] > 10].sort_values(2, ascending = False).index    return resultselected_cols = ['osv','apptype', 'carrier', 'dev_height', 'dev_ppi','dev_width', 'media_id',                  'package', 'version', 'fea_hash', 'location', 'fea1_hash','cus_type']key_feature = {}for selected in selected_cols:    key_feature[selected] = find_key_feature(train, selected)key_featuredef f(x, selected):    if x in key_feature[selected]:        return 1    else:        return 0for selected in selected_cols:    if len(key_feature[selected]) > 0:        features[selected+'1'] = features[selected].apply(f, args = (selected,))        test_features[selected+'1'] = test_features[selected].apply(f, args = (selected,))        print(selected+'1 created')

   

以上就是【飞桨学习赛:MarTech Challenge 点击反欺诈预测】第10名方案的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/39572.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
回坑原作? 《空洞骑士》在《丝之歌》发布后在线人数创新高
上一篇 2025年11月6日 03:06:16
windows10的tpm是什么怎么查看版本_windows10TPM功能介绍及版本查看方法
下一篇 2025年11月6日 03:08:18

相关推荐

  • 开源免费PHP工具 PHP开发效率提升利器

    推荐开源免费PHP开发工具以提升效率:VS Code、Sublime Text轻量高效,PhpStorm专业强大;调试用Xdebug、Kint、Ray;依赖管理选Composer;代码质量工具包括PHPStan、Psalm、PHP_CodeSniffer;数据库管理可用%ignore_a_1%MyA…

    2026年5月10日
    000
  • Matplotlib 地图中多类型图例的创建与优化

    Matplotlib 地图中多类型图例的创建与优化Matplotlib 地图中多类型图例的创建与优化Matplotlib 地图中多类型图例的创建与优化Matplotlib 地图中多类型图例的创建与优化

    本教程旨在解决matplotlib地图可视化中,如何在一个图例中同时展示颜色块(如区域分类)和自定义标记(如特定兴趣点)的问题。文章详细介绍了当传统`patch`对象无法正确显示标记时,如何利用`matplotlib.lines.line2d`创建标记图例句柄,并将其与颜色块图例句柄合并,从而生成一…

    2026年5月10日 用户投稿
    100
  • Golang JSON序列化:控制敏感字段暴露的最佳实践

    本教程探讨golang中如何高效控制结构体字段在json序列化时的可见性。当需要将包含敏感信息的结构体数组转换为json响应时,通过利用`encoding/json`包提供的结构体标签,特别是`json:”-“`,可以轻松实现对特定字段的忽略,从而避免敏感数据泄露,确保api…

    2026年5月10日
    000
  • 利用海象运算符简化条件赋值:Python教程与最佳实践

    本文旨在探讨Python中海象运算符(:=)在条件赋值场景下的应用。通过对比传统if/else语句与海象运算符,以及条件表达式,分析海象运算符在简化代码、提高可读性方面的优势与局限性。并通过具体示例,展示如何在列表推导式等场景下合理使用海象运算符,同时强调其潜在的复杂性及替代方案,帮助开发者更好地掌…

    2026年5月10日
    100
  • 获取日期中的周数:CodeIgniter 教程

    本教程旨在帮助开发者在 CodeIgniter 框架中,从日期字符串中准确提取周数。我们将使用 PHP 内置的 DateTime 类,并提供详细的代码示例和注意事项,确保您能够轻松地在项目中实现此功能。 使用 DateTime 类获取周数 PHP 的 DateTime 类提供了一种便捷的方式来处理日…

    2026年5月10日
    000
  • 比特币新手教程 比特币交易平台有哪些

    比特币是一种去中心化的数字货币,基于区块链技术实现点对点交易,具有匿名性、有限发行和不可篡改等特点;新手可通过交易所购买,P2P交易获得比特币,常用平台包括Binance、OKX和Huobi;交易流程包括注册账户、实名认证、绑定支付方式、充值法币并下单购买,可选择市价单或限价单;比特币存储方式有交易…

    2026年5月10日
    000
  • c++中的SFINAE技术是什么_c++模板编程中的SFINAE原理与应用

    SFINAE 是“替换失败不是错误”的原则,指模板实例化时若参数替换导致错误,只要存在其他合法候选,编译器不报错而是继续重载决议。它用于条件启用模板、类型检测等场景,如通过 decltype 或 enable_if 控制函数重载,实现类型特征判断。尽管 C++20 引入 Concepts 简化了部分…

    2026年5月10日
    000
  • Go语言mgo查询构建:深入理解bson.M与日期范围查询的正确实践

    本文旨在解决go语言mgo库中构建复杂查询时,特别是涉及嵌套`bson.m`和日期范围筛选的常见错误。我们将深入剖析`bson.m`的类型特性,解释为何直接索引`interface{}`会导致“invalid operation”错误,并提供一种推荐的、结构清晰的代码重构方案,以确保查询条件能够正确…

    2026年5月10日
    100
  • RichHandler与Rich Progress集成:解决显示冲突的教程

    在使用rich库的`richhandler`进行日志输出并同时使用`progress`组件时,可能会遇到显示错乱或溢出问题。这通常是由于为`richhandler`和`progress`分别创建了独立的`console`实例导致的。解决方案是确保日志处理器和进度条组件共享同一个`console`实例…

    2026年5月10日
    000
  • 修复点击时按钮抖动:CSS垂直对齐实践

    本文探讨了在Web开发中,交互式按钮(如播放/暂停按钮)在点击时发生意外垂直位移的问题。通过分析CSS样式变化对元素布局的影响,我们发现这是由于按钮不同状态下的边框样式和内边距改变,以及默认的垂直对齐行为共同作用所致。核心解决方案是利用CSS的vertical-align属性,将其设置为middle…

    2026年5月10日
    000
  • Golang goroutine与channel调试技巧

    使用go run -race检测数据竞争,结合runtime.NumGoroutine监控协程数量,通过pprof分析阻塞调用栈,利用select超时避免永久阻塞,有效排查goroutine泄漏、死锁和数据竞争问题。 Go语言的goroutine和channel是并发编程的核心,但它们也带来了调试上…

    2026年5月10日
    000
  • 《魔兽世界》将于6月11日开启国服回归技术测试

    《魔兽世界》将于6月11日开启国服回归技术测试《魔兽世界》将于6月11日开启国服回归技术测试《魔兽世界》将于6月11日开启国服回归技术测试《魔兽世界》将于6月11日开启国服回归技术测试

    《%ign%ignore_a_1%re_a_1%》官方宣布,将于6月11日开启国服回归技术测试,时间为7天,并称可以在6月内正式开服,玩家们可以访问官网下载战网客户端并预下载“巫妖王之怒”客户端,技术测试详情见下图。 WordAi WordAI是一个AI驱动的内容重写平台 53 查看详情 以上就是《…

    2026年5月10日 用户投稿
    200
  • 使用 Jupyter Notebook 进行探索性数据分析

    Jupyter Notebook通过单元格实现代码与Markdown结合,支持数据导入(pandas)、清洗(fillna)、探索(matplotlib/seaborn可视化)、统计分析(describe/corr)和特征工程,便于记录与分享分析过程。 Jupyter Notebook 是进行探索性…

    2026年5月10日
    000
  • 如何在HTML中插入表单元素_HTML表单控件与输入类型使用指南

    HTML表单通过标签构建,包含action和method属性定义数据提交目标与方式,常用input类型如text、password、email等适配不同输入需求,配合label、required、placeholder提升可用性,结合textarea、select、button等控件实现完整交互,是…

    2026年5月10日
    000
  • 创建指定大小并填充特定数据的Golang文件教程

    本文将介绍如何使用Golang创建一个指定大小的文件,并用特定数据填充它。我们将使用 `os` 包提供的函数来创建和截断文件,从而实现快速生成大文件的目的。示例代码展示了如何创建一个10MB的文件,并将其填充为全零数据。掌握这些方法,可以方便地在例如日志系统或磁盘队列等场景中,预先创建测试文件或初始…

    2026年5月10日
    000
  • Python命令怎样使用profile分析脚本性能 Python命令性能分析的基础教程

    使用Python的cProfile模块分析脚本性能最直接的方式是通过命令行执行python -m cProfile your_script.py,它会输出每个函数的调用次数、总耗时、累积耗时等关键指标,帮助定位性能瓶颈;为进一步分析,可将结果保存为文件python -m cProfile -o ou…

    2026年5月10日
    000
  • 如何插入查询结果数据_SQL插入Select查询结果方法

    如何插入查询结果数据_SQL插入Select查询结果方法如何插入查询结果数据_SQL插入Select查询结果方法如何插入查询结果数据_SQL插入Select查询结果方法如何插入查询结果数据_SQL插入Select查询结果方法

    使用INSERT INTO…SELECT语句可高效插入数据,通过NOT EXISTS、LEFT JOIN、MERGE语句或唯一约束避免重复;表结构不一致时可通过别名、类型转换、默认值或计算字段处理;结合存储过程可提升可维护性,支持参数化与动态SQL。 将查询结果数据插入到另一个表中,可以…

    2026年5月10日 用户投稿
    000
  • 使用 WebCodecs VideoDecoder 实现精确逐帧回退

    本文档旨在解决在使用 WebCodecs VideoDecoder 进行视频解码时,实现精确逐帧回退的问题。通过比较帧的时间戳与目标帧的时间戳,可以避免渲染中间帧,从而提高用户体验。本文将提供详细的解决方案和示例代码,帮助开发者实现精确的视频帧控制。 在使用 WebCodecs VideoDecod…

    2026年5月10日
    000
  • Discord.py 交互按钮超时与持久化解决方案

    本教程旨在解决Discord.py中交互按钮在一段时间后出现“This Interaction Failed”错误的问题。我们将深入探讨视图(View)的超时机制,并提供通过正确设置timeout参数以及利用bot.add_view()方法实现按钮持久化的具体方案,确保您的机器人交互功能稳定可靠,即…

    2026年5月10日
    000
  • Debian Copilot的社区活跃度如何

    debian copilot是codeberg社区维护的ai助手,旨在为debian用户提供服务。尽管搜索结果中没有直接提供关于debian copilot社区支持活跃度的具体数据,但我们可以通过debian社区的整体活跃度和特点来推断其活跃性。 Debian社区的一般情况: Debian拥有详尽的…

    2026年5月10日
    000

发表回复

登录后才能评论
关注微信