99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀38萬條數(shù)據(jù),用python分析保險(xiǎn)產(chǎn)品交叉銷售相關(guān)因素!
38萬條數(shù)據(jù),用python分析保險(xiǎn)產(chǎn)品交叉銷售相關(guān)因素!
2020-11-16
收藏

CDA數(shù)據(jù)分析師 出品  

作者:真達(dá)、Mika

數(shù)據(jù):真達(dá)  

【導(dǎo)讀】今天的內(nèi)容是一期python實(shí)戰(zhàn)訓(xùn)練,我們來手把手教你用Python分析保險(xiǎn)產(chǎn)品交叉銷售和哪些因素有關(guān)。

01、實(shí)戰(zhàn)背景

首先介紹下實(shí)戰(zhàn)的背景, 這次的數(shù)據(jù)集來自kaggle:

https://www.kaggle.com/anmolkumar/health-insurance-cross-sell-prediction

我們的客戶是一家保險(xiǎn)公司,最近新推出了一款汽車保險(xiǎn)?,F(xiàn)在他們的需要是建立一個(gè)模型,用來預(yù)測去年的投保人是否會(huì)對這款汽車保險(xiǎn)感興趣。

我們知道,保險(xiǎn)單指的是,保險(xiǎn)公司承諾為特定類型的損失、損害、疾病或死亡提供賠償保證,客戶則需要定期向保險(xiǎn)公司支付一定的保險(xiǎn)費(fèi)。這里再進(jìn)一步說明一下。

例如,你每年要為20萬的健康保險(xiǎn)支付2000元的保險(xiǎn)費(fèi)。那么你肯定會(huì)想,保險(xiǎn)公司只收取5000元的保費(fèi),這種情況下,怎么能承擔(dān)如此高的住院費(fèi)用呢? 這時(shí),“概率”的概念就出現(xiàn)了。例如,像你一樣,可能有100名客戶每年支付2000元的保費(fèi),但當(dāng)年住院的可能只有少數(shù)人,(比如2-3人),而不是所有人。通過這種方式,每個(gè)人都分擔(dān)了其他人的風(fēng)險(xiǎn)。

和醫(yī)療保險(xiǎn)一樣,買了車險(xiǎn)的話,每年都需要向保險(xiǎn)公司支付一定數(shù)額的保險(xiǎn)費(fèi),這樣在車輛發(fā)生意外事故時(shí),保險(xiǎn)公司將向客戶提供賠償(稱為“保險(xiǎn)金額”)。

我們要做的就是建立模型,來預(yù)測客戶是否對汽車保險(xiǎn)感興趣。這對保險(xiǎn)公司來說是非常有幫助的,公司可以據(jù)此制定溝通策略,接觸這些客戶,并優(yōu)化其商業(yè)模式和收入。

02、數(shù)據(jù)理解

為了預(yù)測客戶是否對車輛保險(xiǎn)感興趣,我們需要了解一些客戶信息 (性別、年齡等)、車輛(車齡、損壞情況)、保單(保費(fèi)、采購渠道)等信息。

數(shù)據(jù)劃分為訓(xùn)練集和測試集,訓(xùn)練數(shù)據(jù)包含381109筆客戶資料,每筆客戶資料包含12個(gè)字段,1個(gè)客戶ID字段、10個(gè)輸入字段及1個(gè)目標(biāo)字段-Response是否響應(yīng)(1代表感興趣,0代表不感興趣)。測試數(shù)據(jù)包含127037筆客戶資料;字段個(gè)數(shù)與訓(xùn)練數(shù)據(jù)相同,目標(biāo)字段沒有值。字段的定義可參考下文。

下面我們開始吧!

03、數(shù)據(jù)讀入和預(yù)覽

首先開始數(shù)據(jù)讀入和預(yù)覽。

# 數(shù)據(jù)整理
import numpy as np 
import pandas as pd 

# 可視化
import matplotlib.pyplot as plt 
import seaborn as sns 
import plotly as py 
import plotly.graph_objs as go 
import plotly.express as px 
pyplot = py.offline.plot 
from exploratory_data_analysis import EDAnalysis # 自定義
# 讀入訓(xùn)練集
train = pd.read_csv('../data/train.csv')
train.head() 
# 讀入測試集
test = pd.read_csv('../data/test.csv')
test.head() 
print(train.info())
print('-' * 50)
print(test.info()) 
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 381109 entries, 0 to 381108
Data columns (total 12 columns):
 #   Column                Non-Null Count   Dtype  
---  ------                --------------   -----  
 0   id                    381109 non-null  int64  
 1   Gender                381109 non-null  object 
 2   Age                   381109 non-null  int64  
 3   Driving_License       381109 non-null  int64  
 4   Region_Code           381109 non-null  float64
 5   Previously_Insured    381109 non-null  int64  
 6   Vehicle_Age           381109 non-null  object 
 7   Vehicle_Damage        381109 non-null  object 
 8   Annual_Premium        381109 non-null  float64
 9   Policy_Sales_Channel  381109 non-null  float64
 10  Vintage               381109 non-null  int64  
 11  Response              381109 non-null  int64  
dtypes: float64(3), int64(6), object(3)
memory usage: 34.9+ MB
None
--------------------------------------------------
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 127037 entries, 0 to 127036
Data columns (total 11 columns):
 #   Column                Non-Null Count   Dtype  
---  ------                --------------   -----  
 0   id                    127037 non-null  int64  
 1   Gender                127037 non-null  object 
 2   Age                   127037 non-null  int64  
 3   Driving_License       127037 non-null  int64  
 4   Region_Code           127037 non-null  float64
 5   Previously_Insured    127037 non-null  int64  
 6   Vehicle_Age           127037 non-null  object 
 7   Vehicle_Damage        127037 non-null  object 
 8   Annual_Premium        127037 non-null  float64
 9   Policy_Sales_Channel  127037 non-null  float64
 10  Vintage               127037 non-null  int64  
dtypes: float64(3), int64(5), object(3)
memory usage: 10.7+ MB
None

04、探索性分析

下面,我們基于訓(xùn)練數(shù)據(jù)集進(jìn)行探索性數(shù)據(jù)分析。

1. 描述性分析

首先對數(shù)據(jù)集中數(shù)值型屬性進(jìn)行描述性統(tǒng)計(jì)分析。

desc_table = train.drop(['id', 'Vehicle_Age'], axis=1).describe().T
desc_table

 通過描述性分析后,可以得到以下結(jié)論。從以上描述性分析結(jié)果可以得出:

  • 客戶年齡:客戶的年齡范圍在20 ~ 85歲之間,平均年齡是38歲,青年群體居多;
  • 是否有駕照:99.89%客戶都持有駕照;
  • 之前是否投保:45.82%的客戶已經(jīng)購買了車輛保險(xiǎn);
  • 年度保費(fèi):客戶的保費(fèi)范圍在2630 ~ 540165之間,平均的保費(fèi)金額是30564。
  • 往來時(shí)長:此數(shù)據(jù)基于過去一年的數(shù)據(jù),客戶的往來時(shí)間范圍在10~299天之間,平均往來時(shí)長為154天。
  • 是否響應(yīng):平均來看,客戶對車輛保險(xiǎn)感興趣的概率為12.25%。

2. 目標(biāo)變量的分布

訓(xùn)練集共有381109筆客戶資料,其中感興趣的有46710人,占比12.3%,不感興趣的有334399人,占比87.7%。

train['Response'].value_counts() 
0    334399
1     46710
Name: Response, dtype: int64
values = train['Response'].value_counts().values.tolist()

# 軌跡
trace1 = go.Pie(labels=['Not interested', 'Interested'], 
                values=values,
                hole=.5,
                marker={'line': {'color': 'white', 'width': 1.3}}
               )
# 軌跡列表
data = [trace1] 
# 布局
layout = go.Layout(title=f'Distribution_ratio of Response', height=600)
# 畫布
fig = go.Figure(data=data, layout=layout)
# 生成HTML
pyplot(fig, filename='./html/目標(biāo)變量分布.html') 

3. 性別因素

條形圖可以看出,男性的客戶群體對汽車保險(xiǎn)感興趣的概率稍高,是13.84%,相較女性客戶高出3個(gè)百分點(diǎn)。

pd.crosstab(train['Gender'], train['Response'])  
# 實(shí)例類
eda = EDAnalysis(data=train, id_col='id', target='Response')

# 柱形圖
fig = eda.draw_bar_stack_cat(colname='Gender')
pyplot(fig, filename='./html/性別與是否感興趣.html') 

4. 之前是否投保

沒有購買汽車保險(xiǎn)的客戶響應(yīng)概率更高,為22.54%,有購買汽車保險(xiǎn)的客戶則沒有這一需求,感興趣的概率僅為0.09%。

pd.crosstab(train['Previously_Insured'], train['Response'])  
fig = eda.draw_bar_stack_cat(colname='Previously_Insured')
pyplot(fig, filename='./html/之前是否投保與是否感興趣.html')  

5. 車齡因素

車齡越大,響應(yīng)概率越高,大于兩年的車齡感興趣的概率最高,為29.37%,其次是1~2年車齡,概率為17.38%。小于1年的僅為4.37%。

6. 車輛損壞情況

車輛曾經(jīng)損壞過的客戶有較高的響應(yīng)概率,為23.76%,相比之下,客戶過去車輛沒有損壞的響應(yīng)概率僅為0.52%

7. 不同年齡

直方圖中可以看出,年齡較高的群體和較低的群體響應(yīng)的概率較低,30~60歲之前的客戶響應(yīng)概率較高。通過可視化探索,我們大致可以知道:

車齡在1年以上,之前有車輛損壞的情況出現(xiàn),且未購買過車輛保險(xiǎn)的客戶有較高的響應(yīng)概率。

05、數(shù)據(jù)預(yù)處理

此部分工作主要包含字段選擇,數(shù)據(jù)清洗和數(shù)據(jù)編碼,字段的處理如下:

  • Region_Code和Policy_Sales_Channel:分類數(shù)過多,且不易解讀,刪除;
  • Annual_Premium:異常值處理
  • Gender、Vehicle_Age、Vehicle_Damage:分類型數(shù)據(jù)轉(zhuǎn)換為數(shù)值型編碼
# 刪除字段
train = train.drop(['Region_Code', 'Policy_Sales_Channel'], axis=1) 

# 蓋帽法處理異常值
f_max = train['Annual_Premium'].mean() + 3*train['Annual_Premium'].std()
f_min = train['Annual_Premium'].mean() - 3*train['Annual_Premium'].std() 

train.loc[train['Annual_Premium'] > f_max, 'Annual_Premium'] = f_max
train.loc[train['Annual_Premium'] < f_min, 'Annual_Premium'] = f_min 

# 數(shù)據(jù)編碼
train['Gender'] = train['Gender'].map({'Male': 1, 'Female': 0}) 
train['Vehicle_Damage'] = train['Vehicle_Damage'].map({'Yes': 1, 'No': 0}) 
train['Vehicle_Age'] = train['Vehicle_Age'].map({'< 1 Year': 0, '1-2 Year': 1, '> 2 Years': 2}) 
train.head() 

測試集做相同的處理:

# 刪除字段
test = test.drop(['Region_Code', 'Policy_Sales_Channel'], axis=1)  
# 蓋帽法處理
test.loc[test['Annual_Premium'] > f_max, 'Annual_Premium'] = f_max
test.loc[test['Annual_Premium'] < f_min, 'Annual_Premium'] = f_min 

# 數(shù)據(jù)編碼
test['Gender'] = test['Gender'].map({'Male': 1, 'Female': 0}) 
test['Vehicle_Damage'] = test['Vehicle_Damage'].map({'Yes': 1, 'No': 0}) 
test['Vehicle_Age'] = test['Vehicle_Age'].map({'< 1 Year': 0, '1-2 Year': 1, '> 2 Years': 2}) 
test.head() 

06、數(shù)據(jù)建模

我們選擇使用以下幾種模型進(jìn)行建置,并比較模型的分類效能。首先在將訓(xùn)練集劃分為訓(xùn)練集和驗(yàn)證集,其中訓(xùn)練集用于訓(xùn)練模型,驗(yàn)證集用于驗(yàn)證模型效果。首先導(dǎo)入建模庫:

# 建模
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from lightgbm import LGBMClassifier

# 預(yù)處理
from sklearn.preprocessing import StandardScaler, MinMaxScaler

# 模型評(píng)估
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.metrics import confusion_matrix, classification_report, accuracy_score, f1_score, roc_auc_score
# 劃分特征和標(biāo)簽
X = train.drop(['id', 'Response'], axis=1)
y = train['Response'] 

# 劃分訓(xùn)練集和驗(yàn)證集(分層抽樣) 
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, stratify=y, random_state=0) 
print(X_train.shape, X_val.shape, y_train.shape, y_val.shape) 
(304887, 8) (76222, 8) (304887,) (76222,)
# 處理樣本不平衡,對0類樣本進(jìn)行降采樣
from imblearn.under_sampling import RandomUnderSampler

under_model = RandomUnderSampler(sampling_strategy={0:133759, 1:37368}, random_state=0)
X_train, y_train = under_model.fit_sample(X_train, y_train)  
# 保存一份極值標(biāo)準(zhǔn)化的數(shù)據(jù)
mms = MinMaxScaler()

X_train_scaled = pd.DataFrame(mms.fit_transform(X_train), columns=x_under.columns)
X_val_scaled = pd.DataFrame(mms.transform(X_val), columns=x_under.columns)

# 測試集
X_test = test.drop('id', axis=1) 
X_test_scaled = pd.DataFrame(mms.transform(X_test), columns=X_test.columns)  

1. KNN算法

# 建立knn
knn = KNeighborsClassifier(n_neighbors=3, n_jobs=-1)
knn.fit(X_train_scaled, y_train)

y_pred = knn.predict(X_val_scaled)

print('Simple KNeighborsClassifier accuracy:%.3f' % (accuracy_score(y_val, y_pred)))
print('Simple KNeighborsClassifier f1_score: %.3f' % (f1_score(y_val, y_pred)))  
print('Simple KNeighborsClassifier roc_auc_score: %.3f' % (roc_auc_score(y_val, y_pred))) 
Simple KNeighborsClassifier accuracy:0.807
Simple KNeighborsClassifier f1_score: 0.337
Simple KNeighborsClassifier roc_auc_score: 0.632
# 對測試集評(píng)估
test_y = knn.predict(X_test_scaled)
test_y[:5] 
array([0, 0, 1, 0, 0], dtype=int64)

2. Logistic回歸

# Logistic回歸
lr = LogisticRegression()
lr.fit(X_train_scaled, y_train)

y_pred = lr.predict(X_val_scaled)

print('Simple LogisticRegression accuracy:%.3f' % (accuracy_score(y_val, y_pred)))
print('Simple LogisticRegression f1_score: %.3f' % (f1_score(y_val, y_pred)))  
print('Simple LogisticRegression roc_auc_score: %.3f' % (roc_auc_score(y_val, y_pred)))
Simple LogisticRegression accuracy:0.863
Simple LogisticRegression f1_score: 0.156
Simple LogisticRegression roc_auc_score: 0.536

3. 決策樹

決策樹
dtc = DecisionTreeClassifier(max_depth=10, random_state=0) 
dtc.fit(X_train, y_train)

y_pred = dtc.predict(X_val) 

print('Simple DecisionTreeClassifier accuracy:%.3f' % (accuracy_score(y_val, y_pred)))
print('Simple DecisionTreeClassifier f1_score: %.3f' % (f1_score(y_val, y_pred)))  
print('Simple DecisionTreeClassifier roc_auc_score: %.3f' % (roc_auc_score(y_val, y_pred))) 
Simple DecisionTreeClassifier accuracy:0.849
Simple DecisionTreeClassifier f1_score: 0.310
Simple DecisionTreeClassifier roc_auc_score: 0.603

4. 隨機(jī)森林

決策樹
rfc = RandomForestClassifier(n_estimators=100, max_depth=10, n_jobs=-1)  
rfc.fit(X_train, y_train)

y_pred = rfc.predict(X_val) 

print('Simple RandomForestClassifier accuracy:%.3f' % (accuracy_score(y_val, y_pred)))
print('Simple RandomForestClassifier f1_score: %.3f' % (f1_score(y_val, y_pred)))  
print('Simple RandomForestClassifier roc_auc_score: %.3f' % (roc_auc_score(y_val, y_pred))) 
Simple RandomForestClassifier accuracy:0.870
Simple RandomForestClassifier f1_score: 0.177
Simple RandomForestClassifier roc_auc_score: 0.545

5. LightGBM

lgbm = LGBMClassifier(n_estimators=100, random_state=0)
lgbm.fit(X_train, y_train)

y_pred = lgbm.predict(X_val)

print('Simple LGBM accuracy: %.3f' % (accuracy_score(y_val, y_pred)))
print('Simple LGBM f1_score: %.3f' % (f1_score(y_val, y_pred)))  
print('Simple LGBM roc_auc_score: %.3f' % (roc_auc_score(y_val, y_pred))) 
Simple LGBM accuracy: 0.857
Simple LGBM f1_score: 0.290
Simple LGBM roc_auc_score: 0.591

綜上,以f1-score作為評(píng)價(jià)標(biāo)準(zhǔn)的情況下,KNN算法有較好的分類效能,這可能是由于數(shù)據(jù)樣本本身不平衡導(dǎo)致,后續(xù)可以通過其他類別不平衡的方式做進(jìn)一步處理,同時(shí)可以通過參數(shù)調(diào)整的方式來優(yōu)化其他模型,通過調(diào)整預(yù)測的門檻值來增加預(yù)測效能等其他方式。


——熱門課程推薦:

想學(xué)習(xí)PYTHON數(shù)據(jù)分析與金融數(shù)字化轉(zhuǎn)型精英訓(xùn)練營,您可以點(diǎn)擊>>>“人才轉(zhuǎn)型”了解課程詳情;

想從事業(yè)務(wù)型數(shù)據(jù)分析師,您可以點(diǎn)擊>>>“數(shù)據(jù)分析師”了解課程詳情;

想從事大數(shù)據(jù)分析師,您可以點(diǎn)擊>>>“大數(shù)據(jù)就業(yè)”了解課程詳情;

想成為人工智能工程師,您可以點(diǎn)擊>>>“人工智能就業(yè)”了解課程詳情;

想了解Python數(shù)據(jù)分析,您可以點(diǎn)擊>>>“Python數(shù)據(jù)分析師”了解課程詳情;

想咨詢互聯(lián)網(wǎng)運(yùn)營,你可以點(diǎn)擊>>>“互聯(lián)網(wǎng)運(yùn)營就業(yè)班”了解課程詳情; 

想了解更多優(yōu)質(zhì)課程,請點(diǎn)擊>>>

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號(hào):CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }