binary classification model building

Solutions on MaxInterview for binary classification model building by the best coders in the world

showing results for - "binary classification model building"
Leonardo
04 Oct 2017
1knn=KNeighborsClassifier()
2svc=SVC()
3lr=LogisticRegression()
4dt=DecisionTreeClassifier()
5gnb=GaussianNB()
6rfc=RandomForestClassifier()
7xgb=XGBClassifier()
8gbc=GradientBoostingClassifier()
9ada=AdaBoostClassifier()
10-------------------------------------------------------------------
11models=[]
12models.append(('KNeighborsClassifier',knn))
13models.append(('SVC',svc))
14models.append(('LogisticRegression',lr))
15models.append(('DecisionTreeClassifier',dt))
16models.append(('GaussianNB',gnb))
17models.append(('RandomForestClassifier',rfc))
18models.append(('XGBClassifier',xgb))
19--------------------------------------------------------------------
20
21from sklearn.metrics import classification_report,confusion_matrix,accuracy_score,roc_curve,auc,recall_score
22--------------------------------------------------------------------
23Model=[]
24score=[]
25cv=[]
26rocscore=[]
27recall=[]
28for name,model in models:
29    print('*****************',name,'*******************')
30    print('\n')
31    Model.append(name)
32    model.fit(x_train,y_train)
33    print(model)
34    pre=model.predict(x_test)
35    print('\n')
36    AS=accuracy_score(y_test,pre)
37    print('Accuracy_score  -',AS)
38    score.append(AS*100)
39    print('\n')
40    sc=cross_val_score(model,x,y,cv=10,scoring='accuracy').mean()
41    print('cross_val_score  -',sc)
42    cv.append(sc*100)
43    print('\n')
44    fpr,tpr,threshold=roc_curve(y_test,pre)
45    roc_auc=auc(fpr,tpr)
46    print('roc_auc_score  -',roc_auc)
47    rocscore.append(roc_auc*100)
48    print('\n')
49    
50    re=recall_score(y_test,pre)
51    print('Recall_score  -',re)
52    recall.append(re*100)
53    print('\n')
54    
55    print('classification report\n',classification_report(y_test,pre))
56    print('\n')
57    cm=confusion_matrix(y_test,pre)
58    print(cm)
59    print('\n')
60    plt.figure(figsize=(10,40))
61    plt.subplot(911)
62    plt.title(name)
63    print(sns.heatmap(cm,annot=True))
64    plt.subplot(912)
65    plt.title(name)
66    plt.plot(fpr,tpr,label='Auc = ' +str(roc_auc))
67    plt.plot([0,1],[0,1],'r--')
68    plt.legend(loc='lower right')
69    plt.ylabel('True Positive Rate')
70    plt.xlabel('False Positive Rate')
71    print('\n\n')
72 ---------------------------------------------------------------------------
73result=pd.DataFrame({'Model':Model,'Accuracy_score':score,'Recall_score':recall,'Cross_val_score':cv,'Roc_auc_curve':rocscore})
74result