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))

task5 网格搜索,调参_第1张图片

#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))

task5 网格搜索,调参_第2张图片

#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))

task5 网格搜索,调参_第3张图片

你可能感兴趣的:(task5 网格搜索,调参)