0.目的:使用网格搜索法对7个模型(逻辑回归、SVM、决策树、随机森林、GBDT、XGBoost和LightGBM)进行调优(调参时采用五折交叉验证的方式),并进行模型评估,记得展示代码的运行结果。
1.导入,读取,定义
#导入包
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn import metrics
from sklearn.model_selection import GridSeqarchCV
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import GradientBoostingClassifier
from xgboost import XGBClassifier
from lightgbm import LGBMClassifier
from sklearn.model_selection import GridSearchCV
#读取
data = pd.read_csv('data_all.csv')
print(data,shape)
X = data.drop(['statua'], axis = 1)
y = data_all['status']
x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.3,random_state=2018)
from sklearn import preprocessing
x_scaled = preprocessing.scale(x_train)
x_scaled_test = preprocessing.scale(x_test)
#定义
lr = LogisticRegression()
svc = SVC()
clfclf = DecisionTreeClassifier()
rfc = RandomForestClassifier()
gbc = GradientBoostingClassifier()
xgb = XGBClassifier(probability=True)
lgb = LGBMClassifier(probability=True)
2.模型评估
#模型评估
def model_metrics(clf, y_target, y_predict):
accuracy = accuracy_score(y_target, y_predict)
print('accuracy: ', accuracy)
precision = precision_score(y_target, y_predict)
print('precision: ', precision)
recall = recall_score(y_target, y_predict)
print('recall: ', recall)
3.1逻辑回归
#逻辑回归
param_grid = {'C':[0.1,1,2,3],'penalty':['l1','l2']}
grid=GridSearchCV(estimator=lr,param_grid=param_grid,cv=5,scoring='accuracy')
grid.fit(x_scaled,y_train)
print(grid.best_score_)
print(grid.best_params_)
print(grid.best_estimator_)
运行结果
accuracy: 0.7890679747722494
precision: 0.6746987951807228
recall: 0.31197771587743733
3.2 svm
#svm
param_grid = {'C':[1,10,100],'kernel':['linear','sigmoid','rbf']}
grid=GridSearchCV(estimator=svc,param_grid=parameters,cv=5)
grid.fit(x_scaled,y_train)
print(grid.best_score_)
print(grid.best_params_)
print(grid.best_estimator_)
运行结果
accuracy: 0.7575332866152769
precision: 0.8823529411764706
recall: 0.04178272980501393
3.3 决策树
#决策树
param_grid = {'criterion': ['gini', 'entropy'], 'max_depth': [1,2,3,4,5,6], 'splitter': ['best', 'random'],
'max_features': ['log2', 'sqrt', 'auto']}
grid=GridSearchCV(estimator=clf,param_grid=param_grid,cv=5)
grid.fit(x_scaled,y_train)
print(grid.best_score_)
print(grid.best_params_)
print(grid.best_estimator_)
运行结果
accuracy: 0.7561317449194114
precision: 0.5578947368421052
recall: 0.14763231197771587
3.4 随机森林
#随机森林
param_grid = {'n_estimators': range(1,200), 'max_features': ['log2', 'sqrt', 'auto']}
grid=GridSearchCV(estimator=rfc,param_grid=param_grid,cv=5)
grid.fit(x_scaled,y_train)
print(grid.best_score_)
print(grid.best_params_)
print(grid.best_estimator_)
运行结果
accuracy: 0.7848633496846531
precision: 0.6857142857142857
recall: 0.26740947075208915
3.5 GBDT
#GBDT
param_grid = {'n_estimators': range(1,100,10),'learning_rate': np.arange(0.1, 1, 0.1)}
grid=GridSearchCV(estimator=gbc,param_grid=param_grid,cv=5)
grid.fit(x_scaled,y_train)
print(grid.best_score_)
print(grid.best_params_)
print(grid.best_estimator_)
运行结果
accuracy: 0.7561317449194114
precision: 0.5578947368421052
recall: 0.14763231197771587
3.6 XGBoost
#XGBoost
param_grid = {'eta': np.arange(0.1, 1, 0.1), 'max_depth': range(1,6,1), 'min_child_weight': range(1,6,1)}
grid=GridSearchCV(estimator=xgb,param_grid=param_grid,cv=5)
grid.fit(x_scaled,y_train)
print(grid.best_score_)
print(grid.best_params_)
print(rid.best_estimator_)
运行结果
accuracy: 0.7876664330763841
precision: 0.6521739130434783
recall: 0.3342618384401114
3.7 LightGBM
#LightGBM
param_grid = {'learning_rate': np.arange(0.1,0.5,0.1), 'max_depth': range(1,6,1), 'n_estimators':range(30,50,5)}
grid=GridSearchCV(estimator=lgb,param_grid=param_grid,cv=5)
grid.fit(x_scaled,y_train)
print(grid.best_score_)
print(grid.best_params_)
print(grid.best_estimator_)
运行结果
accuracy: 0.7932725998598459
precision: 0.6839080459770115
recall: 0.33147632311977715