這是一份kaggle上的銀行的數(shù)據(jù)集,研究該數(shù)據(jù)集可以預(yù)測客戶是否認(rèn)購定期存款y。這里包含20個(gè)特征。
1. 分析框架

2. 數(shù)據(jù)讀取,數(shù)據(jù)清洗
#導(dǎo)入相關(guān)包
importnumpyasnp
importpandasaspd
#讀取數(shù)據(jù)
data=pd.read_csv('./1bank-additional-full.csv')
#查看表的行列數(shù)
data.shape
輸出:


這里只有nr.employed這列有丟失數(shù)據(jù),查看下:
data['nr.employed'].value_counts()

這里只有5191.0這個(gè)值,沒有其他的,且只有7763條數(shù)據(jù),這里直接將這列當(dāng)做異常值,直接將這列直接刪除了。
#data.drop('nr.employed',axis=1,inplace=True)
3. 探索性數(shù)據(jù)分析
3.1查看各年齡段的人數(shù)的分布
這里可以看出該銀行的主要用戶主要集中在23-60歲這個(gè)年齡層,其中29-39這個(gè)年齡段的人數(shù)相對(duì)其他年齡段多。
importmatplotlib.pyplotasplt
importseabornassns
plt.rcParams['font.sans-serif']='SimHei'
plt.figure(figsize=(20,8),dpi=256)
sns.countplot(x='age',data=data)
plt.title("各年齡段的人數(shù)")

3.2 其他特征的一些分布
plt.figure(figsize=(18,16),dpi=512)
plt.subplot(221)
sns.countplot(x='contact',data=data)
plt.title("contact分布情況")
plt.subplot(222)
sns.countplot(x='day_of_week',data=data)
plt.title("day_of_week分布情況")
plt.subplot(223)
sns.countplot(x='default',data=data)
plt.title("default分布情況")
plt.subplot(224)
sns.countplot(x='education',data=data)
plt.xticks(rotation=70)
plt.title("education分布情況")
plt.savefig('./1.png')

plt.figure(figsize=(18,16),dpi=512)
plt.subplot(221)
sns.countplot(x='housing',data=data)
plt.title("housing分布情況")
plt.subplot(222)
sns.countplot(x='job',data=data)
plt.xticks(rotation=70)
plt.title("job分布情況")
plt.subplot(223)
sns.countplot(x='loan',data=data)
plt.title("loan分布情況")
plt.subplot(224)
sns.countplot(x='marital',data=data)
plt.xticks(rotation=70)
plt.title("marital分布情況")
plt.savefig('./2.png')

plt.figure(figsize=(18,8),dpi=512)
plt.subplot(221)
sns.countplot(x='month',data=data)
plt.xticks(rotation=30)
plt.subplot(222)
sns.countplot(x='poutcome',data=data)
plt.xticks(rotation=30)
plt.savefig('./3.png')

3.3 各特征的相關(guān)性
plt.figure(figsize=(10,8),dpi=256)
plt.rcParams['axes.unicode_minus']=False
sns.heatmap(data.corr(),annot=True)
plt.savefig('./4.png')

4. 特征規(guī)范化
4.1 將自變量的特征值轉(zhuǎn)換成標(biāo)簽類型
#特征化數(shù)據(jù)
fromsklearn.preprocessingimportLabelEncoder
features=['contact','day_of_week','default','education','housing',
'job','loan','marital','month','poutcome']
le_x=LabelEncoder()
forfeatureinfeatures:
data[feature]=le_x.fit_transform(data[feature])
4.2 將結(jié)果y值轉(zhuǎn)換成0、1
defparse_y(x):
if(x=='no'):
return0
else:
return1
data['y']=data['y'].apply(parse_y)
data['y']=data['y'].astype(int)
4.3 數(shù)據(jù)規(guī)范化
#數(shù)據(jù)規(guī)范化到正態(tài)分布的數(shù)據(jù)
#測試數(shù)據(jù)和訓(xùn)練數(shù)據(jù)的分割
fromsklearn.preprocessingimportStandardScaler
fromsklearn.model_selectionimporttrain_test_split
ss=StandardScaler()
train_x,test_x,train_y,test_y=train_test_split(data.iloc[:,:-1],
data['y'],
test_size=0.3)
train_x=ss.fit_transform(train_x)
test_x=ss.transform(test_x)
5. 模型訓(xùn)練
5.1 AdaBoost分類器
fromsklearn.ensembleimportAdaBoostClassifier
fromsklearn.metricsimportaccuracy_score
ada=AdaBoostClassifier()
ada.fit(train_x,train_y)
predict_y=ada.predict(test_x)
print("準(zhǔn)確率:",accuracy_score(test_y,predict_y))

5.2 SVC分類器
fromsklearn.svmimportSVC
svc=SVC()
svc.fit(train_x,train_y)
predict_y=svc.predict(test_x)
print("準(zhǔn)確率:",accuracy_score(test_y,predict_y))

5.3 K鄰近值分類器
fromsklearn.neighborsimportKNeighborsClassifier
knn=KNeighborsClassifier()
knn.fit(train_x,train_y)
predict_y=knn.predict(test_x)
print("準(zhǔn)確率:",accuracy_score(test_y,predict_y))

5.4 決策樹分類器
fromsklearn.treeimportDecisionTreeClassifier
dtc=DecisionTreeClassifier()
dtc.fit(train_x,train_y)
predict_y=dtc.predict(test_x)
print("準(zhǔn)確率:",accuracy_score(test_y,predict_y))

6 模型評(píng)價(jià)
6.1 AdaBoost分類器
fromsklearn.metricsimportroc_curve
fromsklearn.metricsimportauc
plt.figure(figsize=(8,6))
fpr1,tpr1,threshoulds1=roc_curve(test_y,ada.predict(test_x))
plt.stackplot(fpr1,tpr1,color='steelblue',alpha=0.5,edgecolor='black')
plt.plot(fpr1,tpr1,linewidth=2,color='black')
plt.plot([0,1],[0,1],ls='-',color='red')
plt.text(0.5,0.4,auc(fpr1,tpr1))
plt.title('AdaBoost分類器的ROC曲線')

6.2 SVC分類器
plt.figure(figsize=(8,6))
fpr2,tpr2,threshoulds2=roc_curve(test_y,svc.predict(test_x))
plt.stackplot(fpr2,tpr2,alpha=0.5)
plt.plot(fpr2,tpr2,linewidth=2,color='black')
plt.plot([0,1],[0,1],ls='-',color='red')
plt.text(0.5,0.4,auc(fpr2,tpr2))
plt.title('SVD的ROC曲線')

6.3 K鄰近值分類器
plt.figure(figsize=(8,6))
fpr3,tpr3,threshoulds3=roc_curve(test_y,knn.predict(test_x))
plt.stackplot(fpr3,tpr3,alpha=0.5)
plt.plot(fpr3,tpr3,linewidth=2,color='black')
plt.plot([0,1],[0,1],ls='-',color='red')
plt.text(0.5,0.4,auc(fpr3,tpr3))
plt.title('K鄰近值的ROC曲線')

6.4 決策樹分類器
plt.figure(figsize=(8,6))
fpr4,tpr4,threshoulds4=roc_curve(test_y,dtc.predict(test_x))
plt.stackplot(fpr4,tpr4,alpha=0.5)
plt.plot(fpr4,tpr4,linewidth=2,color='black')
plt.plot([0,1],[0,1],ls='-',color='red')
plt.text(0.5,0.4,auc(fpr4,tpr4))
plt.title('決策樹的ROC曲線')

審核編輯 :李倩
-
算法
+關(guān)注
關(guān)注
23文章
4740瀏覽量
96728 -
數(shù)據(jù)分析
+關(guān)注
關(guān)注
2文章
1494瀏覽量
35728 -
python
+關(guān)注
關(guān)注
56文章
4848瀏覽量
88965
原文標(biāo)題:用 Python 算法預(yù)測客戶行為案例!
文章出處:【微信號(hào):DBDevs,微信公眾號(hào):數(shù)據(jù)分析與開發(fā)】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
工地AI行為識(shí)別系統(tǒng)作用
景區(qū)AI行為識(shí)別系統(tǒng)作用

工廠園區(qū)AI行為識(shí)別系統(tǒng)作用
采用可更新且具區(qū)分度錨點(diǎn)的多模態(tài)運(yùn)動(dòng)預(yù)測研究

DLP6500能否用Python編程進(jìn)行開發(fā),是否有API接口?
設(shè)備管理系統(tǒng):如何實(shí)現(xiàn)預(yù)測性維護(hù)與故障預(yù)防?

使用Python實(shí)現(xiàn)xgboost教程
基于梯度下降算法的三元鋰電池循環(huán)壽命預(yù)測

臺(tái)灣COMCHIP高端MOS:車用客戶導(dǎo)入案例型號(hào)推薦 下

臺(tái)灣COMCHIP高端MOS:車用客戶導(dǎo)入案例型號(hào)推薦 中

AI行為識(shí)別攝像機(jī)

評(píng)論