Python Scikit学习中的随机状态(伪随机数)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28064634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Random state (Pseudo-random number) in Scikit learn
提问by Elizabeth Susan Joseph
I want to implement a machine learning algorithm in scikit learn, but I don't understand what this parameter random_state
does? Why should I use it?
我想在scikit learn中实现一个机器学习算法,但是我不明白这个参数random_state
是做什么的?我为什么要使用它?
I also could not understand what is a Pseudo-random number.
我也无法理解什么是伪随机数。
采纳答案by elyase
train_test_split
splits arrays or matrices into random train and test subsets. That means that everytime you run it without specifying random_state
, you will get a different result, this is expected behavior. For example:
train_test_split
将数组或矩阵拆分为随机训练和测试子集。这意味着每次在不指定的情况下运行它时random_state
,都会得到不同的结果,这是预期的行为。例如:
Run 1:
运行 1:
>>> a, b = np.arange(10).reshape((5, 2)), range(5)
>>> train_test_split(a, b)
[array([[6, 7],
[8, 9],
[4, 5]]),
array([[2, 3],
[0, 1]]), [3, 4, 2], [1, 0]]
Run 2
运行 2
>>> train_test_split(a, b)
[array([[8, 9],
[4, 5],
[0, 1]]),
array([[6, 7],
[2, 3]]), [4, 2, 0], [3, 1]]
It changes. On the other hand if you use random_state=some_number
, then you can guarantee that the output of Run 1will be equal to the output of Run 2, i.e. your split will be always the same.
It doesn't matter what the actual random_state
number is 42, 0, 21, ... The important thing is that everytime you use 42, you will always get the same output the first time you make the split.
This is useful if you want reproducible results, for example in the documentation, so that everybody can consistently see the same numbers when they run the examples.
In practice I would say, you should set the random_state
to some fixed number while you test stuff, but then remove it in production if you really need a random (and not a fixed) split.
它改变。另一方面,如果您使用random_state=some_number
,那么您可以保证Run 1的输出将等于Run 2的输出,即您的分割将始终相同。实际random_state
数字是 42, 0, 21, ... 无关紧要,重要的是每次使用 42 时,第一次进行拆分时总是会得到相同的输出。如果您想要可重现的结果,例如在文档中,这将非常有用,这样每个人在运行示例时都可以始终如一地看到相同的数字。在实践中,我会说,您应该random_state
在测试内容时将其设置为某个固定数字,但是如果您确实需要随机(而不是固定)拆分,则在生产中将其删除。
Regarding your second question, a pseudo-random number generator is a number generator that generates almost truly random numbers. Why they are not truly random is out of the scope of this question and probably won't matter in your case, you can take a look hereform more details.
关于您的第二个问题,伪随机数生成器是生成几乎真正随机数的数字生成器。为什么它们不是真正随机的超出了这个问题的范围,并且在您的情况下可能无关紧要,您可以在此处查看更多详细信息。
回答by umar salman
If you don't specify the random_state
in your code, then every time you run(execute) your code a new random value is generated and the train and test datasets would have different values each time.
如果您没有random_state
在代码中指定,那么每次运行(执行)代码时都会生成一个新的随机值,并且每次训练和测试数据集都会有不同的值。
However, if a fixed value is assigned like random_state = 42
then no matter how many times you execute your code the result would be the same .i.e, same values in train and test datasets.
但是,如果分配了一个固定值,random_state = 42
那么无论您执行代码多少次,结果都是相同的。即,训练和测试数据集中的值相同。
回答by Bogdan Korecki
sklearn.model_selection.train_test_split(*arrays, **options)[source]
Split arrays or matrices into random train and test subsets
将数组或矩阵拆分为随机训练和测试子集
Parameters: ...
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. source: http://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html
如果是 int,random_state 是随机数生成器使用的种子;如果是 RandomState 实例,random_state 是随机数生成器;如果没有,随机数生成器是 np.random 使用的 RandomState 实例。来源:http: //scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html
'''Regarding the random state, it is used in many randomized algorithms in sklearn to determine the random seed passed to the pseudo-random number generator. Therefore, it does not govern any aspect of the algorithm's behavior. As a consequence, random state values which performed well in the validation set do not correspond to those which would perform well in a new, unseen test set. Indeed, depending on the algorithm, you might see completely different results by just changing the ordering of training samples.''' source: https://stats.stackexchange.com/questions/263999/is-random-state-a-parameter-to-tune
'''关于随机状态,它在sklearn中的许多随机算法中被用来确定传递给伪随机数生成器的随机种子。因此,它不控制算法行为的任何方面。因此,在验证集中表现良好的随机状态值与在新的、看不见的测试集中表现良好的随机状态值不对应。事实上,根据算法的不同,你可能会通过改变训练样本的顺序看到完全不同的结果。'''来源:https: //stats.stackexchange.com/questions/263999/is-random-state-a-parameter -调
回答by Rishi Bansal
If you don't mention the random_state in the code, then whenever you execute your code a new random value is generated and the train and test datasets would have different values each time.
如果您没有在代码中提及 random_state,那么每当您执行代码时,都会生成一个新的随机值,并且每次训练和测试数据集都会有不同的值。
However, if you use a particular value for random_state(random_state = 1 or any other value) everytime the result will be same,i.e, same values in train and test datasets. Refer below code:
但是,如果您每次都使用 random_state(random_state = 1 或任何其他值) 的特定值,结果将相同,即训练和测试数据集中的值相同。参考以下代码:
import pandas as pd
from sklearn.model_selection import train_test_split
test_series = pd.Series(range(100))
size30split = train_test_split(test_series,random_state = 1,test_size = .3)
size25split = train_test_split(test_series,random_state = 1,test_size = .25)
common = [element for element in size25split[0] if element in size30split[0]]
print(len(common))
Doesn't matter how many times you run the code, the output will be 70.
不管你运行多少次代码,输出都是 70。
70
Try to remove the random_state and run the code.
尝试删除 random_state 并运行代码。
import pandas as pd
from sklearn.model_selection import train_test_split
test_series = pd.Series(range(100))
size30split = train_test_split(test_series,test_size = .3)
size25split = train_test_split(test_series,test_size = .25)
common = [element for element in size25split[0] if element in size30split[0]]
print(len(common))
Now here output will be different each time you execute the code.
现在,每次执行代码时,这里的输出都会不同。
回答by Arad Haselirad
random_state number splits the test and training datasets with a random manner. In addition to what is explained here, it is important to remember that random_state value can have significant effect on the quality of your model (by quality I essentially mean accuracy to predict). For instance, If you take a certain dataset and train a regression model with it, without specifying the random_state value, there is the potential that everytime, you will get a different accuracy result for your trained model on the test data. So it is important to find the best random_state value to provide you with the most accurate model. And then, that number will be used to reproduce your model in another occasion such as another research experiment. To do so, it is possible to split and train the model in a for-loop by assigning random numbers to random_state parameter:
random_state number 以随机方式分割测试和训练数据集。除了这里解释的内容之外,重要的是要记住 random_state 值会对模型的质量产生重大影响(质量我本质上是指预测的准确性)。例如,如果您采用某个数据集并用它训练回归模型,而未指定 random_state 值,则每次都有可能在测试数据上获得训练模型的不同准确度结果。因此,找到最佳的 random_state 值以提供最准确的模型非常重要。然后,该数字将用于在另一个场合(例如另一个研究实验)中重现您的模型。为此,
for j in range(1000):
X_train, X_test, y_train, y_test = train_test_split(X, y , random_state =j, test_size=0.35)
lr = LarsCV().fit(X_train, y_train)
tr_score.append(lr.score(X_train, y_train))
ts_score.append(lr.score(X_test, y_test))
J = ts_score.index(np.max(ts_score))
X_train, X_test, y_train, y_test = train_test_split(X, y , random_state =J, test_size=0.35)
M = LarsCV().fit(X_train, y_train)
y_pred = M.predict(X_test)`
回答by MdNazmulHossain
If there is no randomstate provided the system will use a randomstate that is generated internally. So, when you run the program multiple times you might see different train/test data points and the behavior will be unpredictable. In case, you have an issue with your model you will not be able to recreate it as you do not know the random number that was generated when you ran the program.
如果没有提供随机状态,系统将使用内部生成的随机状态。因此,当您多次运行该程序时,您可能会看到不同的训练/测试数据点,并且行为将是不可预测的。如果您的模型有问题,您将无法重新创建它,因为您不知道运行程序时生成的随机数。
If you see the Tree Classifiers - either DT or RF, they try to build a try using an optimal plan. Though most of the times this plan might be the same there could be instances where the tree might be different and so the predictions. When you try to debug your model you may not be able to recreate the same instance for which a Tree was built. So, to avoid all this hassle we use a random_state while building a DecisionTreeClassifier or RandomForestClassifier.
如果您看到树分类器 - DT 或 RF,它们会尝试使用最佳计划进行尝试。尽管大多数情况下这个计划可能是相同的,但也可能存在树可能不同的情况,因此预测也是如此。当您尝试调试模型时,您可能无法重新创建为其构建树的相同实例。因此,为了避免所有这些麻烦,我们在构建 DecisionTreeClassifier 或 RandomForestClassifier 时使用 random_state。
PS: You can go a bit in depth on how the Tree is built in DecisionTree to understand this better.
PS:您可以深入了解如何在 DecisionTree 中构建树以更好地理解这一点。
randomstate is basically used for reproducing your problem the same every time it is run. If you do not use a randomstate in traintestsplit, every time you make the split you might get a different set of train and test data points and will not help you in debugging in case you get an issue.
randomstate 基本上用于在每次运行时以相同的方式重现您的问题。如果您不在 traintestsplit 中使用随机状态,则每次进行拆分时,您可能会得到一组不同的训练和测试数据点,并且在遇到问题时不会帮助您进行调试。
From Doc:
来自文档:
If int, randomstate is the seed used by the random number generator; If RandomState instance, randomstate is the random number generator; If None, the random number generator is the RandomState instance used by np.random.
如果是 int,randomstate 是随机数生成器使用的种子;如果是 RandomState 实例,randomstate 是随机数生成器;如果没有,随机数生成器是 np.random 使用的 RandomState 实例。