【发布时间】:2025-12-15 06:30:01
【问题描述】:
我有 26 篇文章分为二元类别,我执行了 NB 和 5 倍 CV 分析。我使用load_files() 命令导入了我的数据,我得到了这个数组:
target': array([0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1]),
'target_names': ['negative', 'positive']
即使我阅读了 scikit 学习网站上的 LOO 文档和用户指南,我也无法掌握如何使用我的数据。以下是网站上的示例:
from sklearn import cross_validation
X = np.array([[1, 2], [3, 4]])
y = np.array([1, 2])
loo = cross_validation.LeaveOneOut(2)
len(loo)
2
print(loo)
sklearn.cross_validation.LeaveOneOut(n=2)
for train_index, test_index in loo:
print("TRAIN:", train_index, "TEST:", test_index)
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
print(X_train, X_test, y_train, y_test)
例如,在上面来自 scikit 学习网站的示例中,y 代表什么?是分类吗?什么是 train_index / test_index?那是从哪里来的?
我们将不胜感激。
问候,
古兹德
更新:
感谢您的回复,布拉德。如果我理解您的答案,我需要将我的数据分成训练/测试集吗?我用我的 NB 分析 (75:25) 做到了这一点,但在我的 5 倍 CV 实现中,这个分割 (80:20) 是由算法执行的。这是我的代码(使用 4 个不同的分类器):
def evaluate_cross_validation(clf, X, y, K):
# create a k-fold cross validation iterator of k=5 folds
cv = KFold(len(y), K, shuffle=True, random_state=0)
# by default the score used is the one returned by score >>> method of the estimator (accuracy)
scores = cross_val_score(clf, X, y, cv=cv)
print scores
print ("Mean score: {0:.3f} (+/-{1:.3f})").format(np.mean(scores), sem(scores))
clfs = [clf_1, clf_2, clf_3, clf_4]
for clf in clfs:
evaluate_cross_validation(clf, bunch.data, bunch.target, 5)
Bunch.data 是我的文章加载了load_files(),bunch.target 是我的两个类。所以,如果我的想法是正确的,留一法是 N 倍 CV,那么我可以用上述代码进行分析,其中 K = 文章数?
但是,我仍然不明白上面示例中的y = np.array([1, 2]) 是什么以及为什么需要训练集和测试集,因为除了一篇是训练集和一篇是测试集之外的所有文章。
【问题讨论】:
-
好吧,让我们先澄清一下,你有一个
target数组和一个target_names数组。你有什么特点?那个数组在哪里?如果我正确地解决了您的问题,您正在尝试将文章分类为负面或正面。如果没有关于传递给算法的文章的信息,就没有可以学习的特征。换句话说,您只有y值(目标),没有X值(特征)。 -
导入文章后得到如下数组: target': array([0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1]) 和 'target_names': ['negative', 'positive']。这在我的代码中使用,数组是 bundle.data,目标名称是 bundle.target。
标签: scikit-learn classification cross-validation supervised-learning