python - 模型如何才能在随机数据上获得完全相同的准确性?

标签 python machine-learning keras

我正在比较遗传学分类的模型,它们将基因在 4 类之间进行分类,以确定、可能、可能或未知影响疾病。

我运行这些,它们的准确率都在 65-80% 左右,这是通过嵌套交叉验证来避免过度拟合来获得这些百分比的。但然后我给这些模型一个随机数据集,一些模型在多次运行中设法获得完全相同的准确性(没有交叉验证,只是通过分割测试数据看到准确性),这真的可能吗?模型是否已达到稳定水平?从我的初学者的角度来看,我认为这不太可能,任何帮助将不胜感激。

这是我用于测试随机数据的代码:

inner_cv = KFold(n_splits=10, shuffle=True, random_state=seed)
outer_cv = KFold(n_splits=10, shuffle=True, random_state=seed)

models = []
models.append(('LR', dcv.GridSearchCV(logreg, LR_par, cv=inner_cv, iid=False, n_jobs=-1)))
models.append(('SVM', dcv.GridSearchCV(svm, tuned_parameters, cv=inner_cv, iid=False, n_jobs=-1)))
models.append(('RFC', dcv.GridSearchCV(rfc, param_grid, cv=inner_cv,iid=False, n_jobs=-1)))
models.append(('Keras', GridSearchCV(estimator=keras, param_grid=kerasparams, cv=inner_cv,iid=False, n_jobs=-1)))

arr = np.arange(5400).reshape((600, 9))
random = np.random.permutation(arr)
ran = np.random.randint(4, size=600)
rand = np.column_stack((random, ran))
print(rand.shape)
X1 = rand[0:600,0:8]
Y1 = rand[0:600,-1]
print("Random data counts of label '0': {}".format(sum(ran==0)))
print("Random data counts of label '1': {}".format(sum(ran==1)))
print("Random data counts of label '2': {}".format(sum(ran==2)))
print("Random data counts of label '3': {}".format(sum(ran==3)))
print(X1.shape)
print(Y1.shape)
X1 = MinMaxScaler().fit_transform(X1)

X_train1, X_test1, Y_train1, Y_test1 = train_test_split(X1, Y1, test_size=0.2, random_state=0)

for name, model in models:
    model.fit(X1, Y1)
    print(name, 'Random accuracy: {:.2f}'.format(model.score(X_test1, Y_test1)*100),  '%')

输出如下:

(600, 10)
Random data counts of label '0': 136
Random data counts of label '1': 153
Random data counts of label '2': 155
Random data counts of label '3': 156
(600, 8)
(600,)


LR Random accuracy: 26.67 %
SVM Random accuracy: 26.67 %
RFC Random accuracy: 38.33 %
Keras Random accuracy: 23.33 %

这似乎是这样的,如果我运行这段代码 4 次,第一次精度不同,接下来的 3 次对于 SVM 和 LR(逻辑回归)通常保持相同。

我在 anaconda 中使用 python 3.7 并使用 Jupyterlab 0.35.4

最佳答案

据我所知,您的问题在于您使用 sklearn 中的 KFold 的方式。就 RFC(对随机森林分类器进行猜测)而言,它也有一个随机元素。 Logistic回归也有一个随机因素,而SVM我不知道。对于它的值(value),这里是一些文档。在 stackoverflow 上提问之前,我建议先查找每种算法的文档。

来自 KFold 的文档:

shuffle : boolean, optional

Whether to shuffle the data before splitting into batches.

random_state : int, RandomState instance or None, optional, default=None

If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by np.random. Used when shuffle == True.

和随机森林分类器

bootstrap : boolean, optional (default=True)

Whether bootstrap samples are used when building trees. If False, the whole datset is used to build each tree.

因此,如果您每次都想要完全相同的数据,则应该将 shuffle 设置为 false 并删除 random_state(因为默认值为 None)。

inner_cv = KFold(n_splits=10, shuffle=False)
outer_cv = KFold(n_splits=10, shuffle=False)

但这并不能确保您获得相同的结果,因为这很大程度上取决于您使用的算法。根据文档,Scikit-Learn 的随机森林和逻辑回归都有一个无法关闭的随机元素。

关于python - 模型如何才能在随机数据上获得完全相同的准确性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56057681/

相关文章:

tensorflow - 使用 Keras 构建多变量、多任务 LSTM

python - 网页抓取编码价格

c# - 最近邻分类算法的 NumPy 实现以完全相同的方式对所有内容进行分类

python - 如何在 Django 模板上实现 "back"链接?

machine-learning - 除了深度学习以外,还有什么方法可以使问答机器人更好地表现?

python - 如何让 Keras 模型在不同的拟合调用中提前停止

python - 在 TensorFlow Functional API 中保存和加载具有相同图表的多个模型

python - 使用 Keras 对 3 类进行图像分类仅返回一个值,而不是 1 X 3 数组

Python 极地 : Calculate rolling mode over multiple columns

python - 如何启动 django cms 项目