Python,sklearn:使用MinMaxScaler和SVC的管道操作顺序



我有一个数据集,我想在上面运行sklearn-SVM的SVC模型。一些特征值的大小在[0,1e+7]的范围内。我尝试过使用SVC w/o预处理,但我要么得到了无法接受的长计算时间,要么得到了0个真正的正预测。周四,我尝试实现一个预处理步骤,特别是MinMaxScaler

到目前为止我的代码:

selection_KBest = SelectKBest()
selection_PCA = PCA()
combined_features = FeatureUnion([("pca", selection_PCA), 
                                  ("univ_select", selection_KBest)])
param_grid = dict(features__pca__n_components = range(feature_min,feature_max),
                  features__univ_select__k = range(feature_min,feature_max))
svm = SVC()            
pipeline = Pipeline([("features", combined_features), 
                     ("scale", MinMaxScaler(feature_range=(0, 1))),
                     ("svm", svm)])
param_grid["svm__C"] = [0.1, 1, 10]
cv = StratifiedShuffleSplit(y = labels_train, 
                            n_iter = 10, 
                            test_size = 0.1, 
                            random_state = 42)
grid_search = GridSearchCV(pipeline,
                           param_grid = param_grid, 
                           verbose = 1,
                           cv = cv)
grid_search.fit(features_train, labels_train)
"(grid_search.best_estimator_): ", (grid_search.best_estimator_)

我的问题具体针对以下行:

pipeline = Pipeline([("features", combined_features), 
                     ("scale", MinMaxScaler(feature_range=(0, 1))),
                     ("svm", svm)])

我想知道我的程序的最佳逻辑是什么,以及featuresscalesvmpipeline中的顺序。具体来说,我无法决定featuresscale是否应该从现在的状态切换。

注意1:我想使用grid_search.best_estimator_作为我的分类器模型来进行预测。

注意2:我关心的是制定pipeline的正确方法,以便在预测步骤时,从训练步骤中的方式中选择特征并进行缩放。

注意3:我注意到svm没有出现在我的grid_search.best_estimator_结果中。这是否意味着它没有被正确调用?

以下是一些结果,表明订单可能很重要:

pipeline = Pipeline([("scale", MinMaxScaler(feature_range=(0, 1))),
                     ("features", combined_features), 
                     ("svm", svm)]):
Pipeline(steps=[('scale', MinMaxScaler(copy=True, feature_range=(0, 1)))
('features', FeatureUnion(n_jobs=1, transformer_list=[('pca', PCA(copy=True, 
n_components=11, whiten=False)), ('univ_select', SelectKBest(k=2, 
score_func=<function f_classif at 0x000000001ED61208>))], 
transformer_weights=...f', max_iter=-1, probability=False, 
random_state=None, shrinking=True, tol=0.001, verbose=False))])
Accuracy: 0.86247   Precision: 0.38947  Recall: 0.05550 
F1: 0.09716 F2: 0.06699 Total predictions: 15000    
True positives:  111    False positives:  174   
False negatives: 1889   True negatives: 12826

pipeline = Pipeline([("features", combined_features),
                     ("scale", MinMaxScaler(feature_range=(0, 1))), 
                     ("svm", svm)]):
Pipeline(steps=[('features', FeatureUnion(n_jobs=1,
transformer_list=[('pca', PCA(copy=True, n_components=1, whiten=False)), 
('univ_select', SelectKBest(k=1, score_func=<function f_classif at   
0x000000001ED61208>))],
transformer_weights=None)), ('scale', MinMaxScaler(copy=True, feature_range=
(0,...f', max_iter=-1, probability=False, random_state=None,
shrinking=True, tol=0.001, verbose=False))])
Accuracy: 0.86680   Precision: 0.50463  Recall: 0.05450 
F1: 0.09838 F2: 0.06633 Total predictions: 15000    
True positives:  109    False positives:  107   
False negatives: 1891   True negatives: 12893

编辑1 16041310:注释3已解决。使用grid_search.best_estimator_.steps获得完整步骤。


GridsearchCV中有一个参数refit(默认为True),这意味着将根据整个数据集重新调整最佳估计器;然后,您将使用best_estimator_或仅使用GridsearchCV对象上的fit方法来访问此估计器。

best_estimator_将是完整的管道,如果您在其上调用predict,您将获得与培训阶段相同的预处理步骤。

如果你想打印出所有的步骤,你可以做

print(grid_search.best_estimator_.steps)

for step in grid_search.best_estimator_.steps:
    print(type(step))
    print(step.get_params())

最新更新