算法实践第四天
任务
使用网格搜索法对7个模型进行调优(调参时采用五折交叉验证的方式),并进行模型评估
代码实现
导入包
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn import metrics
加载数据
file_path = 'G:\DatawhaleWeek01\Data\data_all.csv'
row_data = pd.read_csv(file_path)
划分数据集
X = row_data.drop(columns=['status']).values
y = row_data['status'].values
X_train,X_test,y_train,y_test = train_test_split(X, y, test_size=0.3,random_state=2018)
数据标准化
scaler = StandardScaler()
scaler.fit(X_train)
X_train_standard = scaler.transform(X_train)
X_test_standard = scaler.transform(X_test)
定义网格搜索交叉验证函数(5折)
from sklearn.model_selection import GridSearchCV
def gridsearch(model, parameters):
grid = GridSearchCV(model, parameters,scoring='accuracy' ,cv=5)
grid = grid.fit(X_train_standard,y_train)
if hasattr(model, 'decision_function'):
y_predict_pro = grid.decision_function(X_test_standard)
else:
y_predict_pro = grid.predict_proba(X_test_standard)[:, 1]
print('best score:', grid.best_score_)
print