task6 模型融合

    #决策树
dtc = tree.DecisionTreeClassifier()
dtc_1 = dtc.fit(X_train, y_train)
y_predict_dtc = dtc.predict(X_test)
parameters = {'max_depth': range(1, 6)}
grid = GridSearchCV(dtc_1, parameters, cv=2)
grid_dtc = grid.fit(X_train, y_train)
y_predict_dtc = grid_dtc.predict(X_test)
print ('Accracy:',grid_dtc.score(X_test,y_test))
print (classification_report(y_predict_dtc,y_test,target_names=['0','1']))
print('precision:',precision_score(y_test, y_predict_dtc)) 
print('recall:',recall_score(y_test, y_predict_dtc))
print('F1-score:',f1_score(y_test, y_predict_dtc)) 
print('AUC:',roc_auc_score(y_test, y_predict_dtc))

#随机森林
rfc = RandomForestClassifier()
rfc_1 = rfc.fit(X_train,y_train)
y_predict_rfc = rfc.predict(X_test)
param_test1 ={'n_estimators':range(10,71,10)}  
grid = GridSearchCV(rfc_1, param_test1,cv=5)
grid_rfc = grid.fit(X_train, y_train)
y_predict_rfc = grid_svc.predict(X_test)
print ('Accracy:',grid_rfc.score(X_test,y_test))
print (classification_report(y_predict_rfc,y_test,target_names=['0','1']))
print('precision:',precision_score(y_test, y_predict_rfc)) 
print('recall:',recall_score(y_test, y_predict_rfc))
print('F1-score:',f1_score(y_test, y_predict_rfc)) 
print('AUC:',roc_auc_score(y_test, y_predict_rfc))

sclf = StackingCVClassifier(classifiers=[dtc, rfc], meta_classifier=lr, use_probas=True, n_folds=3, verbose=3)
sclf.fit(X_train, y_train
        print("Accuracy: %0.2f (+/- %0.2f) [%s]" 
          % (scores.mean(), scores.std())))

猜你喜欢

转载自blog.csdn.net/weixin_43891494/article/details/88430013
今日推荐