我正在使用Python中的sci-kit learning在支持向量机上工作。
我已经训练了模型,使用GridSearch和交叉验证来找到最佳参数,并在15%保留集上评估了最佳模型。
最后的混淆矩阵说我有0个错误分类。
后来,当我给它一个手写数字时,该模型给了我错误的预测(我没有包括此代码,以使问题简短。)
由于SVM的错误为零,而且以后无法正确预测,因此我错误地构建了该SVM。
我的问题是这样的:
我是否可以怀疑我以某种方式不正确地将交叉验证与GridSearch一起使用?还是我给了GridSearch参数某种程度上是荒谬的,并且给了我错误的结果?
感谢您的时间和精力阅读本文。
步骤1:使用train_test_split函数将数据集分成85%/ 15%
X_train, X_test, y_train, y_test =
cross_validation.train_test_split(X, y, test_size=0.15,
random_state=0)
步骤2:将GridSearchCV函数应用于训练集以调整分类器
C_range = 10.0 ** np.arange(-2, 9)
gamma_range = 10.0 ** np.arange(-5, 4)
param_grid = dict(gamma=gamma_range, C=C_range)
cv = StratifiedKFold(y=y, n_folds=3)
grid = GridSearchCV(SVC(), param_grid=param_grid, cv=cv)
grid.fit(X, y)
print("The best classifier is: ", grid.best_estimator_)
输出在这里:
('The best classifier is: ', SVC(C=10.0, cache_size=200,
class_weight=None, coef0=0.0, degree=3,
gamma=0.0001, kernel='rbf', max_iter=-1, probability=False,
random_state=None, shrinking=True, tol=0.001, verbose=False))
步骤3:最后,在剩余的15%保留集上评估调整后的分类器。
clf = svm.SVC(C=10.0, cache_size=200, class_weight=None, coef0=0.0, degree=3,
gamma=0.001, kernel='rbf', max_iter=-1, probability=False,
random_state=None, shrinking=True, tol=0.001, verbose=False)
clf.fit(X_train, y_train)
clf.score(X_test, y_test)
y_pred = clf.predict(X_test)
输出在这里:
precision recall f1-score support
-1.0 1.00 1.00 1.00 6
1.0 1.00 1.00 1.00 30
avg / total 1.00 1.00 1.00 36
Confusion Matrix:
[[ 6 0]
[ 0 30]]
您的测试集中的数据太少(其中一个类别只有6个样本),无法对模型的预测准确性充满信心。我建议每班至少标记150个样本,并在举行的测试中保留50个样本以计算评估指标。
编辑:还查看它无法预测的新样本:特征值是否在相同范围内(例如,用于训练的数字为[0,255]而不是[0,1]或[-1,1]和测试集)?例如,当您使用matplotlib绘制新数字时,新数字是否看起来像测试集中的其他数字?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句