sklearn中的random_state

很多人都把random_state解释为随机数种子。是不是很懵逼?什么是随机数种子?

我也不知道什么是随机数种子。但是,随机数种子是为了保证每次随机的结果都是一样的

Example:sklarn可以随机分割训练集和测试集(交叉验证),只需要在代码中引入model_selection.train_test_split就可以了

代码:

from sklearn import model_selection
x_train, x_test, y_train, y_test = model_selection.train_test_split(x, y, test_size=0.2, random_state=0)


这里的random_state就是为了保证程序每次运行都分割一样的训练集合测试集。否则,同样的算法模型在不同的训练集和测试集上的效果不一样。

当你用sklearn分割完测试集和训练集,确定模型和促初始参数以后,你会发现程序每运行一次,都会得到不同的准确率,无法调参。这个时候就是因为没有加random_state。加上以后就可以调参了。
参考https://blog.csdn.net/Tony_Stark_Wang/article/details/80407923

原文地址:https://www.cnblogs.com/webRobot/p/13150876.html