dataset name = faces
faces.data = independent variables
faces.target = dependent variable
from sklearn.svm import SVC
from sklearn.decomposition import PCA
from sklearn.pipeline import make_pipeline
pca = PCA(n_components=150, whiten=True, random_state=42)
svc = SVC(kernel="rbf", class_weight="balanced")
model = make_pipeline(pca, svc)
# spliting data from faces dataset. data is x and target is y
from sklearn.model_selection import train_test_split
Xtrain, Xtest, ytrain, ytest = train_test_split(faces.data, faces.target, random_state=42)
我已经为PCA和SVC创建了一个管道,然后将数据分为训练集和测试集。
# explore combinations of paramters
from sklearn.model_selection import GridSearchCV
param_grid = {'svc_C':[1,5,10,50],
'svc_gamma':[0.0001, 0.0005, 0.001, 0.005]}
# instantiate grid of GridSearchCV class
# model uses pca to extract meaningful features then svc to find support vector
grid = GridSearchCV(model, param_grid)
grid.fit(Xtrain,ytrain)
当我尝试通过PCA和SVC后使用GridSearchCV训练数据时,它给我一个错误,提示"ValueError: Invalid parameter svc_C for estimator Pipeline"
任何提示?
答案 0 :(得分:1)
我敢打赌,您的参数应该包括双底线,例如:
param_grid = {'svc__C':[1,5,10,50],
'svc__gamma':[0.0001, 0.0005, 0.001, 0.005]}