赞
踩
一:使用pipeline进行数据预处理,模型构建
from sklearn.pipeline import Pipeline
from sklearn.svm import SVC
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
# load and split the data
cancer = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(cancer.data, cancer.target, random_state=0)
pipe = Pipeline([("scaler", MinMaxScaler()), ("svm", SVC())])#有两个步骤
pipe.fit(X_train, y_train) #自动对数据进行scaler,再用scale之后的数据输入SVC构建模型
print("Test score: {:.2f}".format(pipe.score(X_test, y_test)))
二:将pipeline应用到GridSearchCV
#接着上面的代码
param_grid = {'svm__C': [0.001, 0.01, 0.1, 1, 10, 100],
'svm__gamma': [0.001, 0.01, 0.1, 1, 10, 100]}
#注意这里参数svm__C:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。