欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

手机传感器数据分析baseline

程序员文章站 2022-04-19 10:21:58
...

“2020创青春·交子杯” 新网银行金融科技挑战赛 AI算法赛道baseline

前言

6月份看到这个比赛,忘记报名了,回过头来已是7月底了,8月7日初赛就截止了,所以赶紧全身心投入到比赛中,希望能够取得一个比较好的名次!此baseline也是参考大佬所作,嘻嘻,不喜勿喷!

大体过程

环境是Google的colab,因为我的mac终端被我玩坏了,anaconda无法安装包了,jupyter notebook也用不了了。

from google.colab import drive
drive.mount('/content/drive')
data_path = '比赛/新网银行金融科技挑战赛/正式赛/dataset/'

导入包

import numpy as np
import pandas as pd
import lightgbm as lgb
from scipy.stats import skew
from scipy.stats import kurtosis
from scipy.stats import mode
from tqdm import tqdm
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import f1_score, precision_score, recall_score, accuracy_score
from sklearn.model_selection import StratifiedKFold
import warnings 
warnings.filterwarnings("ignore")

导入数据

df_train = pd.read_csv(data_path + 'sensor_train.csv',)
df_test = pd.read_csv(data_path + 'sensor_test.csv',)
df_submit = pd.read_csv(data_path + '提交结果示例.csv',)

合并数据

把训练集与线上测试集合并起来

df_train['status'] = 'train'
df_test['status'] = 'test'
df_train_test = pd.concat([df_train, df_test])

构建两个新特征

把三个方向的分加速度合成一个方向的加速度。

df_train_test['acc'] = (df_train_test['acc_x'] ** 2 + df_train_test['acc_y'] ** 2 + df_train_test['acc_z'] ** 2) ** 0.5
df_train_test['accg'] = (df_train_test['acc_xg'] ** 2 + df_train_test['acc_yg'] ** 2 + df_train_test['acc_zg'] ** 2) ** 0.5

抽取特征

模型参数

这个纯属乱写的,baseline嘛

params = {
          'application': 'multiclass',
          'num_class': 19,
          'boosting': 'gbdt',
          'num_leaves': 63,
          'learning_rate': 0.1,
          'bagging_fraction': 0.8,
          'feature_fraction': 0.7,
          'min_split_gain': 0.01,
          'min_child_samples': 120,
          'min_child_weight': 0.01,
          'lambda_l2': 0.05,
          'verbosity': -1,
          'data_random_seed': 2020
         }  

训练模型

for train_index, val_index in tqdm(kfold.split(X, y)):
    X_train, X_val = X[train_index], X[val_index]
    y_train, y_val = y[train_index], y[val_index]
    
    lgb_train = lgb.Dataset(X_train, y_train)
    lgb_val = lgb.Dataset(X_val, y_val)
    
    watchlist = [lgb_train, lgb_val]
    model = lgb.train(params,
                      train_set = lgb_train, 
                      num_boost_round = 5000,
                      valid_sets = watchlist,
                      verbose_eval = 30,
                      early_stopping_rounds = 80)
    
    
    X_val_predict = model.predict(X_val)
    X_test_predict = model.predict(X_test)
    
    df_train_stacking.loc[val_index,:] = X_val_predict
    df_test_stacking[:] += X_test_predict / folds

模型得分

def acc_combo(y, y_pred):
    # 数值ID与行为编码的对应关系
    mapping = {0: 'A_0', 1: 'A_1', 2: 'A_2', 3: 'A_3', 
        4: 'D_4', 5: 'A_5', 6: 'B_1',7: 'B_5', 
        8: 'B_2', 9: 'B_3', 10: 'B_0', 11: 'A_6', 
        12: 'C_1', 13: 'C_3', 14: 'C_0', 15: 'B_6', 
        16: 'C_2', 17: 'C_5', 18: 'C_6'}
    # 将行为ID转为编码
    code_y, code_y_pred = mapping[y], mapping[y_pred]
    if code_y == code_y_pred: #编码完全相同得分1.0
        return 1.0
    elif code_y.split("_")[0] == code_y_pred.split("_")[0]: #编码仅字母部分相同得分1.0/7
        return 1.0/7
    elif code_y.split("_")[1] == code_y_pred.split("_")[1]: #编码仅数字部分相同得分1.0/3
        return 1.0/3
    else:
        return 0.0

测试集结果

手机传感器数据分析baseline
最终线上评分0.691,top1现在已经突破0.8了,后续继续努力上分,每天学习新知识!