美文网首页
二分类银行精准营销的单一分类算法尝试

二分类银行精准营销的单一分类算法尝试

作者: christinazou | 来源:发表于2019-01-26 16:58 被阅读0次
# 加载教程中会用到的包
import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt
import seaborn as sns

# 加载 warnings
import warnings

# 忽略 warnings
warnings.filterwarnings("ignore")
# 从csv文件中写入数据
train = pd.read_csv('bank_train_set.csv')
test = pd.read_csv('bank_test_set.csv')
print(plt.style.available)   # 列出所有可用的绘图样式
plt.style.use('ggplot')      # 使用“ggplot”样式   
data=pd.concat([train,test])

处理类型变量

for s in ['campaign','contact','default','education','housing','job','loan','marital','month','poutcome']:
    data=pd.concat([data,pd.get_dummies(data[s],prefix=s+'_')],axis=1)
    data.drop(s,axis=1,inplace=True)
data.drop(columns = ['ID'], inplace = True)

取出有目标值的集合

df_train=data[data['y'].notnull()]
df_test=data[data['y'].isnull()]

knn 不同参数

neig = np.arange(1, 100)
train_accuracy = []
test_accuracy = []
# 循环K值从1到25
for i, k in enumerate(neig):
    # k从1到25(不包括1、25)
    knn = KNeighborsClassifier(n_neighbors=k)
    # 使用KNN拟合
    knn.fit(x_train,y_train)
    # 训练集的准确度
    train_accuracy.append(knn.score(x_train, y_train))
    # 测试集的准确度
    test_accuracy.append(knn.score(x_test, y_test))

# 可视化
plt.figure(figsize=[13,8])
plt.plot(neig, test_accuracy, label = 'Testing Accuracy')
plt.plot(neig, train_accuracy, label = 'Training Accuracy')
plt.legend()
plt.title('-value VS Accuracy')
plt.xlabel('Number of Neighbors')
plt.ylabel('Accuracy')
plt.xticks(neig)
plt.savefig('graph.png')
plt.show()
print("Best accuracy is {} with K = {}".format(np.max(test_accuracy),1+test_accuracy.index(np.max(test_accuracy))))

knn

from sklearn.model_selection import cross_val_score
neig = np.arange(1, 100)
train_accuracy = []
test_accuracy = []
# 循环K值从1到100
for i, k in enumerate(neig):
    # k从1到100(不包括1、100)
    knn = KNeighborsClassifier(n_neighbors=k)
    cv_result=cross_val_score(knn,x,y,cv=5,scoring='accuracy')#把数据自动分成五组,然后得到每组的准确度
    # 测试集的准确度
    test_accuracy.append(np.sum(cv_result)/5)

# 可视化
plt.figure(figsize=[13,8])
plt.plot(neig, test_accuracy, label = 'Testing Accuracy')
plt.legend()
plt.title('-value VS Accuracy')
plt.xlabel('Number of Neighbors')
plt.ylabel('Accuracy')
plt.xticks(neig)
plt.savefig('graph.png')
plt.show()

print("Best accuracy is {} with K = {}".format(np.max(test_accuracy),1+test_accuracy.index(np.max(test_accuracy))))
from sklearn.model_selection import cross_val_score
from sklearn.ensemble import RandomForestClassifier
neig = np.arange(1, 100)
train_accuracy = []
test_accuracy = []
# 循环K值从1到25
for i, k in enumerate(neig):
    # k从1到25(不包括1、25)
    clf = RandomForestClassifier(n_estimators=k, random_state=0)
    cv_result=cross_val_score(clf,x,y,cv=5,scoring='accuracy')#把数据自动分成五组,然后得到每组的准确度
    # 测试集的准确度
    test_accuracy.append(np.sum(cv_result)/5)

# 可视化
plt.figure(figsize=[13,8])
plt.plot(neig, test_accuracy, label = 'Testing Accuracy')
plt.legend()
plt.title('-value VS Accuracy')
plt.xlabel('Number of Trees')
plt.ylabel('Accuracy')
plt.xticks(neig)
plt.savefig('graph.png')
plt.show()
print("Best accuracy is {} with K = {}".format(np.max(test_accuracy),1+test_accuracy.index(np.max(test_accuracy))))
from sklearn.model_selection import cross_val_score
from sklearn.ensemble import RandomForestClassifier
neig = np.arange(1, 50)
train_accuracy = []
test_accuracy = []
# 循环K值从1到25
for i, k in enumerate(neig):
    # k从1到25(不包括1、25)
    clf = RandomForestClassifier(n_estimators=4,max_depth=k,
                                 random_state=0)

    cv_result=cross_val_score(clf,x,y,cv=5,scoring='accuracy')#把数据自动分成五组,然后得到每组的准确度
    # 测试集的准确度
    test_accuracy.append(np.sum(cv_result)/5)

# 可视化
plt.figure(figsize=[13,8])
plt.plot(neig, test_accuracy, label = 'Testing Accuracy')
plt.legend()
plt.title('-value VS Accuracy')
plt.xlabel('max_depth')
plt.ylabel('Accuracy')
plt.xticks(neig)
plt.savefig('graph.png')
plt.show()
print("Best accuracy is {} with K = {}".format(np.max(test_accuracy),1+test_accuracy.index(np.max(test_accuracy))))
Unknown-7.png

相关文章

  • 二分类银行精准营销的单一分类算法尝试

    处理类型变量 取出有目标值的集合 knn 不同参数 knn

  • 神经网络和深度学习-第二周-吴恩达 Deep Learning

    二元分类(Binary Classification) 二元分类是经典的分类问题,在程序实现中希望算法可以决定某件...

  • 分类算法与数据挖掘

    ################分类算法与数据挖掘---也就是回归于分类算法--对应于Y的0/1算法 ####分类...

  • 2.机器学习常用算法

    常见算法 监督学习:分类算法:决策树算法 Decision Tree: 银行信用评估系统临近取样 Nearest ...

  • 决策树基础介绍

    一。介绍 决策树是一种分类算法。它的算法思想是:根据单一特征对类别的重要性,将特征进行“排序”;然后分类的时候,用...

  • 逻辑回归

    Logistic regression 目的:分类还是回归?经典的二分类算法! 机器学习算法选择:先逻辑回归再用复...

  • 用SVM对手写字母进行分类

    SVM 可用于离散因变量的分类和连续因变量的预测,相对于单一的分类算法(Logistic、决策树、KNN。朴素贝叶...

  • Python 决策树

    1、决策树算法 决策树(decision tree)又叫判定树,是基于树结构对样本属性进行分类的分类算法。以二分类...

  • 数据挖掘

    预测指标 分类常见的评估指标:对于二类分类器/分类算法,评价指标主要有accuracy, [Precision,R...

  • 朴素贝叶斯分类算法

    朴素贝叶斯分类算法 算法简介 朴素贝叶斯是使用贝叶斯的条件概率来做分类判断的一种算法,具体的依据就是,对于二分类来...

网友评论

      本文标题:二分类银行精准营销的单一分类算法尝试

      本文链接:https://www.haomeiwen.com/subject/jkvijqtx.html