首页 文章

Scikit中的随机状态(伪随机数)学习

提问于
浏览
78

我想在scikit中实现一个机器学习算法学习,但是我不明白这个参数 random_state 的作用是什么?我为什么要用它?

我也无法理解什么是伪随机数 .

4 回答

  • 116

    train_test_split 将数组或矩阵拆分为随机序列和测试子集 . 这意味着每次运行它而不指定 random_state 时,您将得到不同的结果,这是预期的行为 . 例如:

    Run 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

    >>> train_test_split(a, b)
    [array([[8, 9],
            [4, 5],
            [0, 1]]),
     array([[6, 7],
            [2, 3]]), [4, 2, 0], [3, 1]]
    

    它改变 . 另一方面,如果使用 random_state=some_number ,则可以保证 Run 1 的输出等于 Run 2 的输出,即您的分割将始终相同 . 实际的 random_state 数字是42,0,21 ......并不重要 . 重要的是每次使用42时,第一次进行拆分时总会得到相同的输出 . 如果您想要可重复的结果(例如在文档中),这非常有用,这样每个人在运行示例时都可以始终看到相同的数字 . 在实践中我会说,你应该在测试内容时将 random_state 设置为某个固定数字,但是如果你真的需要一个随机(而不是固定的)分割,那么在 生产环境 中删除它 .

    关于第二个问题,伪随机数生成器是一个生成几乎真正随机数的数字生成器 . 为什么它们不是真正随机的超出了这个问题的范围,在你的情况下可能无关紧要,你可以看一下here形式的更多细节 .

  • -1

    如果未在代码中指定 random_state ,则每次运行(执行)代码时都会生成一个新的随机值,并且每次训练和测试数据集都会有不同的值 .

    但是,如果为 random_state = 42 指定了固定值,则无论执行代码多少次,结果都是相同的 . 即,列车和测试数据集中的值相同 .

  • 0

    如果您没有在代码中提及random_state,那么无论何时执行代码,都会生成一个新的随机值,并且train和test数据集每次都会有不同的值 .

    但是,如果每次结果都相同时使用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))
    

    无论你运行代码多少次,输出都是70 .

    70
    

    尝试删除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))
    

    现在,每次执行代码时输出都会有所不同 .

  • 6
    sklearn.model_selection.train_test_split(*arrays, **options)[source]
    

    将数组或矩阵拆分为随机序列和测试子集

    Parameters: ... 
        random_state : int, RandomState instance or None, optional (default=None)
    

    如果是int,则random_state是随机数生成器使用的种子;如果是RandomState实例,则random_state是随机数生成器;如果为None,则随机数生成器是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'的行为 . 因此,在验证集中表现良好的随机状态值与在新的,未见的测试集中表现良好的随机状态值不对应 . 实际上,根据算法,您可能会通过更改训练样本的顺序看到完全不同的结果 . '''来源:https://stats.stackexchange.com/questions/263999/is-random-state-a-parameter-to-tune

相关问题