30 lines
806 B
Plaintext
30 lines
806 B
Plaintext
== How to fix it in Scikit-Learn
|
|
|
|
To fix this issue, provide a predictable seed to the estimator or the utility function.
|
|
|
|
=== Code examples
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,python,diff-id=2,diff-type=noncompliant]
|
|
----
|
|
from sklearn.model_selection import train_test_split
|
|
from sklearn.datasets import load_iris
|
|
|
|
X, y = load_iris(return_X_y=True)
|
|
X_train, _, y_train, _ = train_test_split(X, y) # Noncompliant: no seed parameter is provided
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,python,diff-id=2,diff-type=compliant]
|
|
----
|
|
from sklearn.model_selection import train_test_split
|
|
from sklearn.datasets import load_iris
|
|
import numpy as np
|
|
|
|
rng = np.random.default_rng(42)
|
|
X, y = load_iris(return_X_y=True)
|
|
X_train, _, y_train, _ = train_test_split(X, y, random_state=rng.integers(1)) # Compliant
|
|
----
|