task5 网格搜索,调参

#决策树
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))

在这里插入图片描述

#SVM
svc = SVC(kernel='linear',C=0.4)
svc_1 = svc.fit(X_train,y_train)
params = [
        {'kernel': ['linear'], 'C': [1, 10, 100, 100]},
        {'kernel': ['poly'], 'C': [1], 'degree': [2, 3]},
        {'kernel': ['rbf'], 'C': [1, 10, 100, 100], 'gamma':[1, 0.1, 0.01, 0.001]}
        ]
grid = GridSearchCV(svc_1, params,refit=True,return_train_score=True,cv=5)
grid_svc = grid.fit(X_train, y_train)
y_predict_svc = grid_svc.predict(X_test)
print ('Accracy:',grid_svc.score(X_test,y_test))
print (classification_report(y_predict_svc,y_test,target_names=['0','1']))
print('precision:',precision_score(y_test, y_predict_svc)) 
print('recall:',recall_score(y_test, y_predict_svc))
print('F1-score:',f1_score(y_test, y_predict_svc)) 
print('AUC:',roc_auc_score(y_test, y_predict_svc))

#随机森林
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))

在这里插入图片描述

#XGBoost
XGB = XGBClassifier()
xgb_1 = XGB.fit(X_train,y_train)
y_predict_xgb = XGB.predict(X_test)   
print ('Accracy:',XGB.score(X_test,y_test))
print(classification_report(y_test,y_predict_xgb))
print('precision:',precision_score(y_test, y_predict_xgb)) 
print('recall:',recall_score(y_test, y_predict_xgb))
print('F1-score:',f1_score(y_test, y_predict_xgb))
print('AUC:',roc_auc_score(y_test, y_predict_xgb))
param_test1 = {'max_depth':range(3,10,2),'min_child_weight':range(1,6,2)}
grid = GridSearchCV(xgb_1, param_test1,cv=5)
grid_xgb = grid.fit(X_train, y_train)
y_predict_xgb = grid_xgb.predict(X_test)
print ('Accracy:',grid_xgb.score(X_test,y_test))
print (classification_report(y_predict_xgb,y_test,target_names=['0','1']))
print('precision:',precision_score(y_test, y_predict_xgb)) 
print('recall:',recall_score(y_test, y_predict_xgb))
print('F1-score:',f1_score(y_test, y_predict_xgb)) 
print('AUC:',roc_auc_score(y_test, y_predict_xgb))

在这里插入图片描述

猜你喜欢

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