我有一个数据集,想要在sklearn SVM的
到目前为止我的代码:
我想知道程序的最佳逻辑,以及在管道中
注意1:我希望在进行预测时使用
注意2:我的问题是如何正确地制定管道,以便在预测步骤中从训练步骤中选择特征并进行缩放。
注意3:我注意到
下面是一些结果,表明顺序可能很重要:
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)])
我想知道程序的最佳逻辑,以及在管道中
features
、scale
和svm
的顺序。具体来说,我无法确定features
和scale
是否应该交换位置,以使内容更加通俗易懂。注意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
获取完整步骤。
max_iter=1, probability=False
是SVC
的参数。 - joelnBaseEstimator.__repr__
中的:https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/base.py#L148。当然,你可以单独对每个步骤进行repr... - joeln