为什么scikit-learn的SVM分类器交叉验证如此缓慢?

我正在尝试在一个数据集上比较多个分类器。为了获得分类器的准确准确性分数,我现在对每个分类器进行10折交叉验证。除了SVM(线性和rbf核)之外,其他分类器的交叉验证都进行得很顺利。数据的加载方式如下:

dataset = pd.read_csv("data/distance_annotated_indels.txt", delimiter="\t", header=None)X = dataset.iloc[:, [5,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]].valuesy = dataset.iloc[:, 4].valuesfrom sklearn.model_selection import train_test_splitX_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)from sklearn.preprocessing import StandardScalersc = StandardScaler()X_train = sc.fit_transform(X_train)X_test = sc.transform(X_test)

例如,随机森林的交叉验证运行得很好:

start = time.time()classifier = RandomForestClassifier(n_estimators = 100, criterion = 'entropy')classifier.fit(X_train, y_train)y_pred = classifier.predict(X_test)cv = ShuffleSplit(n_splits=10, test_size=0.2)scores = cross_val_score(classifier, X, y, cv=10)print(classification_report(y_test, y_pred))print("Random Forest accuracy after 10 fold CV: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2) + ", " + str(round(time.time() - start, 3)) + "s")

输出:

             precision    recall  f1-score   support          0       0.97      0.95      0.96      3427          1       0.95      0.97      0.96      3417avg / total       0.96      0.96      0.96      6844Random Forest accuracy after 10 fold CV: 0.92 (+/- 0.06), 90.842s

然而,对于SVM,这个过程需要很长时间(等待了2个小时,仍然没有结果)。scikit-learn的网站并没有让我更明白。我应该对SVM分类器做些什么不同的事情吗?SVM的代码如下:

start = time.time()classifier = SVC(kernel = 'linear')classifier.fit(X_train, y_train)y_pred = classifier.predict(X_test)scores = cross_val_score(classifier, X, y, cv=10)print(classification_report(y_test, y_pred))print("Linear SVM accuracy after 10 fold CV: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2) + ", " + str(round(time.time() - start, 3)) + "s")

回答:

如果你的样本很多,问题的计算复杂度就会成为障碍,请参见线性SVM的训练复杂度

考虑使用cross_val_scoreverbose标志来查看更多关于进度的日志。另外,将n_jobs设置为大于1的值(或者如果内存允许,将n_jobs设置为-1以使用所有CPU),你可以通过并行化来加速计算。可以参考http://scikit-learn.org/stable/modules/generated/sklearn.model_selection.cross_val_score.html来评估这些选项。

如果性能不佳,我会考虑降低cv的值(关于这一点的讨论请参见https://stats.stackexchange.com/questions/27730/choice-of-k-in-k-fold-cross-validation

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注