函数内部的 random_state=7 是否比 np.random.seed(7) 更好?

Is random_state=7 better inside a function than np.random.seed(7)?

以下两个函数是否可以完全重现?

def test_SVC_A():
    svc = LinearSVC(random_state=7)
    svc.fit(X_train, y_train)
    svc.score(X_test, y_test)


def test_SVC_B():
    seed=7
    np.random.seed(seed)
    svc = LinearSVC()
    svc.fit(X_train, y_train)
    svc.score(X_test, y_test)

或者我应该更喜欢 test_SVC_A 而不是函数 test_SVC_B

我觉得这很混乱,因为我在两个过程中总是得到相同的结果?

Scikit-learn does not use its own global random state; whenever a RandomState instance or an integer random seed is not provided as an argument, it relies on the numpy global random state, which can be set using numpy.random.seed


也就是说,添加 np.random.seed() 之前 导入 LinearSVC 应该会导致相同的结果。