python– 使scikit确定性?

python– 使scikit确定性?,第1张

概述我正在使用scikit-learn训练一些分类器.我做交叉验证,然后计算AUC.但是,每次运行测试时,我都会获得不同的AUC编号,尽管我确保使用种子和RandomState.我希望我的测试是确定性的.这是我的代码:from sklearn.utils import shuffle SEED = 0 random_state = np.random.Rando

我正在使用scikit-learn训练一些分类器.我做交叉验证,然后计算AUC.但是,每次运行测试时,我都会获得不同的AUC编号,尽管我确保使用种子和RandomState.我希望我的测试是确定性的.这是我的代码:

from sklearn.utils import shuffleSEED = 0random_state = np.random.RandomState(SEED)X,y = shuffle(data,Y,random_state=random_state)X_train,X_test,y_train,y_test = \        cross_valIDation.train_test_split(X,y,test_size=test_size,random_state=random_state)clf = linear_model.LogisticRegression()kfold = cross_valIDation.KFold(len(X),n_folds=n_folds)mean_tpr = 0.0mean_fpr = np.linspace(0,1,100)for train,test in kfold:        probas_ = clf.fit(X[train],Y[train]).predict_proba(X[test])        fpr,tpr,thresholds = roc_curve(Y[test],probas_[:,1])        mean_tpr += interp(mean_fpr,fpr,tpr)        mean_tpr[0] = 0.0mean_tpr /= len(kfold)mean_tpr[-1] = 1.0mean_auc = auc(mean_fpr,mean_tpr)

我的问题:
1-我的代码中是否有错误导致每次运行时结果都不同?
2-是否存在使scikit具有确定性的全局方法?

编辑:

我刚试过这个:

test_size = 0.5X = np.random.randint(10,size=(10,2))Y = np.random.randint(2,size=(10))SEED = 0random_state = np.random.RandomState(SEED)X_train,y_test = \    cross_valIDation.train_test_split(X,random_state=random_state)print X_train # I recorded the result

然后我做了:

X_train,random_state=6) #notice the change in random_state

然后我做了:

X_train,random_state=random_state)print X_train #the result is different from the first one!!!!

如你所见,虽然我使用了相同的random_state,但我得到了不同的结果!怎么解决这个?最佳答案LogisticRegression在内部使用随机性,并且有一个(未记录的,稍后会修复)random_state参数.

没有设置随机状态的全局方法,因为遗憾的是LogisticRegression上的随机状态和SVM代码只能以Hacky方式设置.那是因为这段代码来自liblinear和libSVM,后者使用C标准库的rand函数,无法以原则方式播种.

编辑以上是正确的,但可能不是问题的原因.您正在通过调用线程化单个np.random.RandomState,而您应该传递相同的整数种子以便于重现.

总结

以上是内存溢出为你收集整理的python – 使scikit确定性?全部内容,希望文章能够帮你解决python – 使scikit确定性?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1207894.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存