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

3
我有一个数据集,想要在sklearn SVM的SVC模型上运行。一些特征值的大小范围在[0,1e+7]之间。我尝试了使用未经处理的SVC,但要么计算时间太长,要么得到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)])

我想知道程序的最佳逻辑,以及在管道中featuresscalesvm的顺序。具体来说,我无法确定featuresscale是否应该交换位置,以使内容更加通俗易懂。
注意1:我希望在进行预测时使用grid_search.best_estimator_作为我的分类器模型。
注意2:我的问题是如何正确地制定管道,以便在预测步骤中从训练步骤中选择特征并进行缩放。
注意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获取完整步骤。

1
SVM 在其中,但似乎被输出中那个“...”隐藏了起来。max_iter=1, probability=FalseSVC 的参数。 - joeln
感谢您的提示,@joeln。您知道如何获取完整、未截断的打印输出吗? - ximiki
目前没有办法获取完整的未截断打印输出,我已经意识到:它是硬编码在 BaseEstimator.__repr__ 中的:https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/base.py#L148。当然,你可以单独对每个步骤进行repr... - joeln
看起来@maxymoo下面的答案揭示了完整的输出,即'grid_search.best_estimator_.steps'(未截断)与'grid_search.best_estimator_'(已截断)。 - ximiki
这只能工作是因为每个步骤的长度小于500个字符。 - joeln
对我来说“能用”就足够了。感谢指点。 - ximiki
1个回答

1
在GridsearchCV中有一个参数refit(默认为True),意味着最佳估计器将会针对完整数据集进行重新拟合;然后您可以使用best_estimator_或仅使用GridsearchCV对象上的fit方法来访问此估计器。
如果您在best_estimator_上调用predict,它将是完整的流水线,您将获得与训练阶段相同的预处理步骤。
如果您想打印出所有步骤,可以执行以下操作
print(grid_search.best_estimator_.steps)

or

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

1
感谢@maxymoo。然而,我仍然不确定我的主要困惑是:在“管道”中,“特征”,“缩放”,“svm”的顺序的最佳逻辑是什么? - ximiki
我会说你做得很对,通过尝试并比较准确度来选择。从你的结果看来,没有太大差别,0.4%的准确度并不是非常显著。不过,最好还是选择准确度更高的那个。 - maxymoo
我同意在这种情况下,精度上的微小差异比另一种选择更好,并且令人感到安慰。然而,如果存在较大的差异,则管道的顺序将很重要,因此正确的管道顺序实现和理解将是至关重要的!这就是提出这个问题的原因。 - ximiki

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接