Task01 赛题理解及baseline学习

import os
import gc
import math
import pandas as pd
import numpy as np

import lightgbm as lgb
import xgboost as xgb
#from catboost import CatBoostRegressor
from sklearn.linear_model import SGDRegressor, LinearRegression, Ridge
from sklearn.preprocessing import MinMaxScaler

from sklearn.model_selection import StratifiedKFold, KFold
from sklearn.metrics import log_loss
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder

from tqdm import tqdm
import matplotlib.pyplot as plt
import time
import warnings
warnings.filterwarnings('ignore')
#1.读取数据
train = pd.read_csv('train.csv')
test=pd.read_csv('testA.csv')
train.head()
idheartbeat_signalslabel
000.9912297987616655,0.9435330436439665,0.764677...0.0
110.9714822034884503,0.9289687459588268,0.572932...0.0
221.0,0.9591487564065292,0.7013782792997189,0.23...2.0
330.9757952826275774,0.9340884687738161,0.659636...0.0
440.0,0.055816398940721094,0.26129357194994196,0...2.0
train.shape
(100000, 3)
train.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 100000 entries, 0 to 99999
Data columns (total 3 columns):
 #   Column             Non-Null Count   Dtype  
---  ------             --------------   -----  
 0   id                 100000 non-null  int64  
 1   heartbeat_signals  100000 non-null  object 
 2   label              100000 non-null  float64
dtypes: float64(1), int64(1), object(1)
memory usage: 2.3+ MB
test.head()
idheartbeat_signals
01000000.9915713654170097,1.0,0.6318163407681274,0.13...
11000010.6075533139615096,0.5417083883163654,0.340694...
21000020.9752726292239277,0.6710965234906665,0.686758...
31000030.9956348033996116,0.9170249621481004,0.521096...
41000041.0,0.8879490481178918,0.745564725322326,0.531...
def reduce_mem_usage(df):
    start_mem = df.memory_usage().sum() / 1024**2 
    print('Memory usage of dataframe is {:.2f} MB'.format(start_mem))
    
    for col in df.columns:
        col_type = df[col].dtype
        
        if col_type != object:
            c_min = df[col].min()
            c_max = df[col].max()
            if str(col_type)[:3] == 'int':
                if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:
                    df[col] = df[col].astype(np.int8)
                elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:
                    df[col] = df[col].astype(np.int16)
                elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:
                    df[col] = df[col].astype(np.int32)
                elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:
                    df[col] = df[col].astype(np.int64)  
            else:
                if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:
                    df[col] = df[col].astype(np.float16)
                elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:
                    df[col] = df[col].astype(np.float32)
                else:
                    df[col] = df[col].astype(np.float64)
        else:
            df[col] = df[col].astype('category')

    end_mem = df.memory_usage().sum() / 1024**2 
    print('Memory usage after optimization is: {:.2f} MB'.format(end_mem))
    print('Decreased by {:.1f}%'.format(100 * (start_mem - end_mem) / start_mem))
    
    return df
# 简单预处理
train_list = []

for items in train.values:
    train_list.append([items[0]] + [float(i) for i in items[1].split(',')] + [items[2]])
train = pd.DataFrame(np.array(train_list))
train.columns = ['id'] + ['s_'+str(i) for i in range(len(train_list[0])-2)] + ['label']
train = reduce_mem_usage(train)
Memory usage of dataframe is 157.93 MB
Memory usage after optimization is: 39.67 MB
Decreased by 74.9%
train.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 100000 entries, 0 to 99999
Columns: 207 entries, id to label
dtypes: float16(206), float32(1)
memory usage: 39.7 MB
train.columns
Index(['id', 'heartbeat_signals', 'label'], dtype='object')
test_list=[]
for items in test.values:
    test_list.append([items[0]] + [float(i) for i in items[1].split(',')])

test = pd.DataFrame(np.array(test_list))
test.columns = ['id'] + ['s_'+str(i) for i in range(len(test_list[0])-1)]
test = reduce_mem_usage(test)
Memory usage of dataframe is 31.43 MB
Memory usage after optimization is: 7.90 MB
Decreased by 74.9%
train.to_csv('train_1.csv',index=False)
test.to_csv('test1.csv',index=False)
train = pd.read_csv('train_1.csv')
test=pd.read_csv('test_1.csv')
test.shape
(20000, 206)
#4.训练数据/测试数据准备
x_train = train.drop(['id','label'], axis=1)
y_train = train['label']
x_test=test.drop(['id'], axis=1)
def abs_sum(y_pre,y_tru):
    y_pre=np.array(y_pre)
    y_tru=np.array(y_tru)
    loss=sum(sum(abs(y_pre-y_tru)))
    return loss
x_train.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 100000 entries, 0 to 99999
Columns: 205 entries, s_0 to s_204
dtypes: float16(205)
memory usage: 39.1 MB
y_train
0        0.0
1        0.0
2        2.0
3        0.0
4        2.0
        ... 
99995    0.0
99996    2.0
99997    3.0
99998    2.0
99999    0.0
Name: label, Length: 100000, dtype: float16
def cv_model(clf, train_x, train_y, test_x, clf_name):
    folds = 2
    seed = 2021
    kf = KFold(n_splits=folds, shuffle=True, random_state=seed)
    test = np.zeros((test_x.shape[0],4))

    cv_scores = []
    onehot_encoder = OneHotEncoder(sparse=False)
    for i, (train_index, valid_index) in enumerate(kf.split(train_x, train_y)):#i:1-5;(train_index, valid_index)每次划分后的训练集索引和测试集索引
        print('************************************ {} ************************************'.format(str(i+1)))
        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]
        
        if clf_name == "lgb":
            train_matrix = clf.Dataset(trn_x, label=trn_y)#将数据转换成lightgbm需要的形式
            valid_matrix = clf.Dataset(val_x, label=val_y)

            params = {
                'boosting_type': 'gbdt',# 训练方式:传统的梯度提升决策树
                'objective': 'multiclass',#目标 多分类
                'num_class': 4,#分类个数
                'num_leaves': 2 ** 5,# number of leaves for one tree, alias: num_leaf/单棵树的最大叶子数,31(默认)
                'feature_fraction': 0.8,# feature sub-sample, will random select 80% feature to train on each iteration# alias: sub_feature
                'bagging_fraction': 0.8,# Bagging farction, will random select 80% data on bagging # alias: sub_row
                'bagging_freq': 4,# Support bagging (data sub-sample), will perform bagging every 4 iterations
                'learning_rate': 0.1,# 学习率/衰减因子,0.1(默认)
                'seed': seed,#(random_state, random_seed)
                'nthread': 28, # 最大线程个数
                'n_jobs':24,
                'verbose': -1,#训练日志显示数,1(默认)
            }

            model = clf.train(params, 
                      train_set=train_matrix, #categorical_feature=0,1,2,表示列0,1,2是categorical;ignore_column:将特定的列完全忽略
                      valid_sets=valid_matrix, #转换格式后的数据集,上同
                      num_boost_round=500, #估计器的数目,也就是boosting迭代的次数,也可以说是残差树的数目
                      verbose_eval=100, #迭代多少次打印
                      early_stopping_rounds=50)#有验证集的话,提前停止的轮数/有多少次分数没有提高则停止
            val_pred = model.predict(val_x, num_iteration=model.best_iteration)#不需要转换成lightgbm格式数据
            test_pred = model.predict(test_x, num_iteration=model.best_iteration) #不需要转换成lightgbm格式数据
            
        val_y=np.array(val_y).reshape(-1, 1)#转换成1列
        val_y = onehot_encoder.fit_transform(val_y)
        print('预测的概率矩阵为:')
        print(test_pred)
        test += test_pred
        score=abs_sum(val_y, val_pred)
        cv_scores.append(score)
        print(cv_scores)
    print("%s_scotrainre_list:" % clf_name, cv_scores)
    print("%s_score_mean:" % clf_name, np.mean(cv_scores))
    print("%s_score_std:" % clf_name, np.std(cv_scores))
    test=test/kf.n_splits

    return test
def lgb_model(x_train, y_train, x_test):
    lgb_test = cv_model(lgb, x_train, y_train, x_test, "lgb")
    return  lgb_test
lgb_test = lgb_model(x_train, y_train, x_test)
************************************ 1 ************************************
Training until validation scores don't improve for 50 rounds
[100]	valid_0's multi_logloss: 0.0704711
[200]	valid_0's multi_logloss: 0.0549909
[300]	valid_0's multi_logloss: 0.0528245
Early stopping, best iteration is:
[307]	valid_0's multi_logloss: 0.0527672
预测的概率矩阵为:
[[9.99957299e-01 3.65514675e-05 2.15328640e-06 3.99633487e-06]
 [1.69790672e-04 1.68415918e-03 9.98145910e-01 1.39703148e-07]
 [2.56058215e-06 1.92758463e-07 1.54282144e-06 9.99995704e-01]
 ...
 [4.95018598e-02 1.97696400e-04 9.50268244e-01 3.21994803e-05]
 [9.99949933e-01 4.93829878e-05 3.43103019e-07 3.41406778e-07]
 [9.55306577e-01 4.78105718e-03 3.36174390e-02 6.29492698e-03]]
[1955.3947895231136]
************************************ 2 ************************************
Training until validation scores don't improve for 50 rounds
[100]	valid_0's multi_logloss: 0.0682392
[200]	valid_0's multi_logloss: 0.0526494
[300]	valid_0's multi_logloss: 0.0504195
Early stopping, best iteration is:
[291]	valid_0's multi_logloss: 0.0503616
预测的概率矩阵为:
[[9.99869813e-01 1.14722981e-04 2.99645841e-06 1.24677403e-05]
 [9.79354868e-05 2.00967286e-03 9.97892104e-01 2.87704544e-07]
 [4.55551526e-06 1.74254833e-07 3.71127623e-06 9.99991559e-01]
 ...
 [4.87378712e-02 1.90985930e-04 9.51004592e-01 6.65512467e-05]
 [9.99943945e-01 5.54682404e-05 3.49358046e-07 2.37799631e-07]
 [9.08801290e-01 1.60743827e-03 6.74482993e-02 2.21429722e-02]]
[1955.3947895231136, 1935.860233737527]
lgb_scotrainre_list: [1955.3947895231136, 1935.860233737527]
lgb_score_mean: 1945.6275116303204
lgb_score_std: 9.767277892793231
temp=pd.DataFrame(lgb_test)
result=pd.read_csv('sample_submit.csv')
result['label_0']=temp[0]
result['label_1']=temp[1]
result['label_2']=temp[2]
result['label_3']=temp[3]
result.to_csv('submit.csv',index=False)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 保险反欺诈预测是金融数据分析的重要应用之一。该赛题是基于保险数据集,旨在通过分析和挖掘数据特征,建立一个反欺诈预测模型的基准线。 首先,我们需要对保险数据集进行预处理和清洗,包括处理缺失值、异常值和重复值等。然后,我们可以进行特征工程,提取出与反欺诈相关的特征。常见的特征可以包括被保险人的年龄、职业、保险金额、历史理赔记录等信息。 接下来,我们可以选择合适的机器学习算法来构建预测模型。常用的算法包括逻辑回归、决策树、随机森林等。在构建模型之前,我们需要将数据集划分为训练集和测试集,用训练集进行模型训练,然后用测试集评估模型的性能。 评估模型的性能可以使用常见的指标,如准确率、精确率、召回率和F1值等。这些指标可以帮助我们评估模型的预测能力和误判率。 最后,我们需要对模型进行优化和改进。可以通过调整模型的参数、增加更多的特征或者尝试其他的机器学习算法来提高模型的预测性能。同时,对于不平衡样本问题,可以采用欠采样、过采样或者集成学习等方法来解决。 总结起来,保险反欺诈预测的baseline建立包括数据预处理、特征工程、模型构建和优化等步骤。通过不断地优化和改进,我们可以建立一个有效的反欺诈预测模型,提高保险公司的风险控制能力。 ### 回答2: 金融数据分析赛题2: 保险反欺诈预测baseline是指在保险领域中,利用金融数据分析的方法来预测保险反欺诈的基础模型。 保险反欺诈预测是指利用大数据和机器学习算法等技术手段,对保险投保人的风险进行分析和预测,从而提高保险公司的风险管理能力,减少保险欺诈行为。 基于金融数据分析的保险反欺诈预测baseline主要包括以下几个步骤: 1. 数据收集:收集与保险欺诈相关的数据,包括投保人的基本信息、历史保险记录、理赔记录等,以及其他与保险欺诈相关的非保险数据。 2. 数据清洗和预处理:对收集到的数据进行清洗和预处理,包括去除异常值、缺失值处理、数据标准化等。确保数据的质量和可用性。 3. 特征工程:根据业务需求和领域知识,对数据进行特征提取和构建。包括基本特征、组合特征和衍生特征等。 4. 模型选择和训练:选择适用于保险反欺诈预测的机器学习模型,例如逻辑回归、决策树、支持向量机等。通过训练数据拟合模型,并进行调参和验证,得到最佳模型。 5. 模型评估和优化:利用评价指标如准确率、召回率、F1值等对模型进行评估,并进行模型优化和调整,提高模型的预测性能。 6. 模型应用和部署:将优化后的模型应用于实际场景,进行实时预测和反欺诈行为识别。并对模型进行监测和更新,保持模型的准确性和稳定性。 基于以上步骤,金融数据分析赛题2的保险反欺诈预测baseline可以建立一个初步的保险反欺诈预测模型,并得到一组基本的预测结果。然后可以根据比赛的具体要求和模型效果进行进一步的改进和优化,提高保险反欺诈预测的准确性和稳定性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值