【发布时间】:2015-11-22 23:17:07
【问题描述】:
我正在我的代码中使用 sklearn 进行初步测试。
我正在测试:
1) sklearn.cross_validation.cross_val_score
2) sklearn.cross_validation.train_test_split
喜欢这个question。
代码如下:
#X is my data and Y the corresponding binary labels
#My classifier
clf = svm.SVC(class_weight='auto', kernel=kernel, gamma=gamma,
degree=degree, cache_size=cache_size,probability=probability)
#1st method: ShuffleSplit and cross validation
cv = cross_validation.ShuffleSplit(X.shape[0], n_iter=5,
test_size=0.4, random_state=0)
#Scoring
scores = cross_validation.cross_val_score(clf, X, Y,
cv=cv, n_jobs=3, scoring="roc_auc")
#2nd method: train_test_split
X_train, X_test, y_train, y_test = cross_validation.train_test_split(
X, Y, test_size=0.4, random_state=42)
clf.fit(X_train, y_train)
pred_test = clf.predict(X_test)
#Scoring
score = roc_auc_score(y_test, pred_test)
与另一个问题的不同之处在于,我的数据在 1) 和 2) 两种情况下都是随机的。
但是对于案例 1),我得到以下分数:
[ 0.9453893 0.94878745 0.95197478 0.95150763 0.94971746]
对于情况 2):
0.867637
我实际上完全不明白这种不同分数的原因,也无法得到我在这里缺少的东西。
分数不应该相似吗?
感谢您的宝贵时间。
【问题讨论】:
-
尝试在分类器中设置
random_state:clf = svm.SVC(class_weight='auto', kernel=kernel, gamma=gamma, degree=degree, cache_size=cache_size,probability=probability, random_state=0) -
谢谢。它给出了完全相同的结果:/
-
对不起,你说这解决了没有?
-
不,它并不能解决我似乎遇到的这个问题。
标签: python scikit-learn cross-validation