8000 ENH Replaced RandomState.rand with equivalent uniform (#22327) · scikit-learn/scikit-learn@9f96d42 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9f96d42

Browse files
authored
ENH Replaced RandomState.rand with equivalent uniform (#22327)
1 parent 7861a4c commit 9f96d42

File tree

6 files changed

+34
-32
lines changed

6 files changed

+34
-32
lines changed< 8000 /span>

sklearn/datasets/_samples_generator.py

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@ def make_classification(
226226
centroids *= 2 * class_sep
227227
centroids -= class_sep
228228
if not hypercube:
229-
centroids *= generator.rand(n_clusters, 1)
230-
centroids *= generator.rand(1, n_informative)
229+
centroids *= generator.uniform(size=(n_clusters, 1))
230+
centroids *= generator.uniform(size=(1, n_informative))
231231

232232
# Initially draw informative features from the standard normal
233233
X[:, :n_informative] = generator.standard_normal(size=(n_samples, n_informative))
@@ -239,22 +239,22 @@ def make_classification(
239239
y[start:stop] = k % n_classes # assign labels
240240
X_k = X[start:stop, :n_informative] # slice a view of the cluster
241241

242-
A = 2 * generator. 8000 rand(n_informative, n_informative) - 1
242+
A = 2 * generator.uniform(size=(n_informative, n_informative)) - 1
243243
X_k[...] = np.dot(X_k, A) # introduce random covariance
244244

245245
X_k += centroid # shift the cluster to a vertex
246246

247247
# Create redundant features
248248
if n_redundant > 0:
249-
B = 2 * generator.rand(n_informative, n_redundant) - 1
249+
B = 2 * generator.uniform(size=(n_informative, n_redundant)) - 1
250250
X[:, n_informative : n_informative + n_redundant] = np.dot(
251251
X[:, :n_informative], B
252252
)
253253

254254
# Repeat some features
255255
if n_repeated > 0:
256256
n = n_informative + n_redundant
257-
indices = ((n - 1) * generator.rand(n_repeated) + 0.5).astype(np.intp)
257+
indices = ((n - 1) * generator.uniform(size=n_repeated) + 0.5).astype(np.intp)
258258
X[:, n : n + n_repeated] = X[:, indices]
259259

260260
# Fill useless features
@@ -263,16 +263,16 @@ def make_classification(
263263

264264
# Randomly replace labels
265265
if flip_y >= 0.0:
266-
flip_mask = generator.rand(n_samples) < flip_y
266+
flip_mask = generator.uniform(size=n_samples) < flip_y
267267
y[flip_mask] = generator.randint(n_classes, size=flip_mask.sum())
268268

269269
# Randomly shift and scale
270270
if shift is None:
271-
shift = (2 * generator.rand(n_features) - 1) * class_sep
271+
shift = (2 * generator.uniform(size=n_features) - 1) * class_sep
272272
X += shift
273273

274274
if scale is None:
275-
scale = 1 + 100 * generator.rand(n_features)
275+
scale = 1 + 100 * generator.uniform(size=n_features)
276276
X *= scale
277277

278278
if shuffle:
@@ -391,10 +391,10 @@ def make_multilabel_classification(
391391
)
392392

393393
generator = check_random_state(random_state)
394-
p_c = generator.rand(n_classes)
394+
p_c = generator.uniform(size=n_classes)
395395
p_c /= p_c.sum()
396396
cumulative_p_c = np.cumsum(p_c)
397-
p_w_c = generator.rand(n_features, n_classes)
397+
p_w_c = generator.uniform(size=(n_features, n_classes))
398398
p_w_c /= np.sum(p_w_c, axis=0)
399399

400400
def sample_example():
@@ -409,7 +409,7 @@ def sample_example():
409409
y = set()
410410
while len(y) != y_size:
411411
# pick a class with probability P(c)
412-
c = np.searchsorted(cumulative_p_c, generator.rand(y_size - len(y)))
412+
c = np.searchsorted(cumulative_p_c, generator.uniform(size=y_size - len(y)))
413413
y.update(c)
414414
y = list(y)
415415

@@ -427,7 +427,7 @@ def sample_example():
427427
# sample words with replacement from selected classes
428428
cumulative_p_w_sample = p_w_c.take(y, axis=1).sum(axis=1).cumsum()
429429
cumulative_p_w_sample /= cumulative_p_w_sample[-1]
430-
words = np.searchsorted(cumulative_p_w_sample, generator.rand(n_words))
430+
words = np.searchsorted(cumulative_p_w_sample, generator.uniform(size=n_words))
431431
return words, y
432432

433433
X_indices = array.array("i")
@@ -610,7 +610,9 @@ def make_regression(
610610
# zeros (the other features are not correlated to y and should be ignored
611611
# by a sparsifying regularizers such as L1 or elastic net)
612612
ground_truth = np.zeros((n_features, n_targets))
613-
ground_truth[:n_informative, :] = 100 * generator.rand(n_informative, n_targets)
613+
ground_truth[:n_informative, :] = 100 * generator.uniform(
614+
size=(n_informative, n_targets)
615+
)
614616

615617
y = np.dot(X, ground_truth) + bias
616618

@@ -1015,7 +1017,7 @@ def make_friedman1(n_samples=100, n_features=10, *, noise=0.0, random_state=None
10151017

10161018
generator = check_random_state(random_state)
10171019

1018-
X = generator.rand(n_samples, n_features)
1020+
X = generator.uniform(size=(n_samples, n_features))
10191021
y = (
10201022
10 * np.sin(np.pi * X[:, 0] * X[:, 1])
10211023
+ 20 * (X[:, 2] - 0.5) ** 2
@@ -1078,7 +1080,7 @@ def make_friedman2(n_samples=100, *, noise=0.0, random_state=None):
10781080
"""
10791081
generator = check_random_state(random_state)
10801082

1081-
X = generator.rand(n_samples, 4)
1083+
X = generator.uniform(size=(n_samples, 4))
10821084
X[:, 0] *= 100
10831085
X[:, 1] *= 520 * np.pi
10841086
X[:, 1] += 40 * np.pi
@@ -1143,7 +1145,7 @@ def make_friedman3(n_samples=100, *, noise=0.0, random_state=None):
11431145
"""
11441146
generator = check_random_state(random_state)
11451147

1146-
X = generator.rand(n_samples, 4)
1148+
X = generator.uniform(size=(n_samples, 4))
11471149
X[:, 0] *= 100
11481150
X[:, 1] *= 520 * np.pi
11491151
X[:, 1] += 40 * np.pi
@@ -1379,9 +1381,9 @@ def make_spd_matrix(n_dim, *, random_state=None):
13791381
"""
13801382
generator = check_random_state(random_state)
13811383

1382-
A = generator.rand(n_dim, n_dim)
1384+
A = generator.uniform(size=(n_dim, n_dim))
13831385
U, _, Vt = linalg.svd(np.dot(A.T, A), check_finite=False)
1384-
X = np.dot(np.dot(U, 1.0 + np.diag(generator.rand(n_dim))), Vt)
1386+
X = np.dot(np.dot(U, 1.0 + np.diag(generator.uniform(size=n_dim))), Vt)
13851387

13861388
return X
13871389

@@ -1441,11 +1443,11 @@ def make_sparse_spd_matrix(
14411443
random_state = check_random_state(random_state)
14421444

14431445
chol = -np.eye(dim)
1444-
aux = random_state.rand(dim, dim)
1446+
aux = random_state.uniform(size=(dim, dim))
14451447
aux[aux < alpha] = 0
14461448
aux[aux > alpha] = smallest_coef + (
14471449
largest_coef - smallest_coef
1448-
) * random_state.rand(np.sum(aux > alpha))
1450+
) * random_state.uniform(size=np.sum(aux > alpha))
14491451
aux = np.tril(aux, k=-1)
14501452

14511453
# Permute the lines: we don't want to have asymmetries in the final
@@ -1509,15 +1511,15 @@ def make_swiss_roll(n_samples=100, *, noise=0.0, random_state=None, hole=False):
15091511
generator = check_random_state(random_state)
15101512

15111513
if not hole:
1512-
t = 1.5 * np.pi * (1 + 2 * generator.rand(n_samples))
1513-
y = 21 * generator.rand(n_samples)
1514+
t = 1.5 * np.pi * (1 + 2 * generator.uniform(size=n_samples))
1515+
y = 21 * generator.uniform(size=n_samples)
15141516
else:
15151517
corners = np.array(
15161518
[[np.pi * (1.5 + i), j * 7] for i in range(3) for j in range(3)]
15171519
)
15181520
corners = np.delete(corners, 4, axis=0)
15191521
corner_index = generator.choice(8, n_samples)
1520-
parameters = generator.rand(2, n_samples) * np.array([[np.pi], [7]])
1522+
parameters = generator.uniform(size=(2, n_samples)) * np.array([[np.pi], [7]])
15211523
t, y = corners[corner_index].T + parameters
15221524

15231525
x = t * np.cos(t)
@@ -1560,9 +1562,9 @@ def make_s_curve(n_samples=100, *, noise=0.0, random_state=None):
15601562
"""
15611563
generator = check_random_state(random_state)
15621564

1563-
t = 3 * np.pi * (generator.rand(1, n_samples) - 0.5)
1565+
t = 3 * np.pi * (generator.uniform(size=(1, n_samples)) - 0.5)
15641566
x = np.sin(t)
1565-
y = 2.0 * generator.rand(1, n_samples)
1567+
y = 2.0 * generator.uniform(size=(1, n_samples))
15661568
z = np.sign(t) * (np.cos(t) - 1)
15671569

15681570
X = np.concatenate((x, y, z))

sklearn/ensemble/_gradient_boosting.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ def _random_sample_mask(np.npy_intp n_total_samples,
256256
the others are ``False``.
257257
"""
258258
cdef np.ndarray[float64, ndim=1, mode="c"] rand = \
259-
random_state.rand(n_total_samples)
259+
random_state.uniform(size=n_total_samples)
260260
cdef np.ndarray[uint8, ndim=1, mode="c", cast=True] sample_mask = \
261261
np_zeros((n_total_samples,), dtype=bool)
262262

sklearn/manifold/_mds.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def _smacof_single(
8484
sim_flat_w = sim_flat[sim_flat != 0]
8585
if init is None:
8686
# Randomly choose initial configuration
87-
X = random_state.rand(n_samples * n_components)
87+
X = random_state.uniform(size=n_samples * n_components)
8888
X = X.reshape((n_samples, n_components))
8989
else:
9090
# overrides the parameter p

sklearn/mixture/_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def _initialize_parameters(self, X, random_state):
148148
)
149149
resp[np.arange(n_samples), label] = 1
150150
elif self.init_params == "random":
151-
resp = random_state.rand(n_samples, self.n_components)
151+
resp = random_state.uniform(size=(n_samples, self.n_components))
152152
resp /= resp.sum(axis=1)[:, np.newaxis]
153153
else:
154154
raise ValueError(

sklearn/utils/estimator_checks.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -798,11 +798,11 @@ def _generate_sparse_matrix(X_csr):
798798

799799
def check_estimator_sparse_data(name, estimator_orig):
800800
rng = np.random.RandomState(0)
801-
X = rng.rand(40, 3)
801+
X = rng.uniform(size=(40, 3))
802802
X[X < 0.8] = 0
803803
X = _pairwise_estimator_convert_X(X, estimator_orig)
804804
X_csr = sparse.csr_matrix(X)
805-
y = (4 * rng.rand(40)).astype(int)
805+
y = (4 * rng.uniform(size=40)).astype(int)
806806
# catch deprecation warnings
807807
with ignore_warnings(category=FutureWarning):
808808
estimator = clone(estimator_orig)
@@ -1088,7 +1088,7 @@ def check_sample_weights_not_overwritten(name, estimator_orig):
10881088
def check_dtype_object(name, estimator_orig):
10891089
# check that estimators treat dtype object as numeric if possible
10901090
rng = np.random.RandomState(0)
1091-
X = _pairwise_estimator_convert_X(rng.rand(40, 10), estimator_orig)
1091+
X = _pairwise_estimator_convert_X(rng.uniform(size=(40, 10)), estimator_orig)
10921092
X = X.astype(object)
10931093
tags = _safe_tags(estimator_orig)
10941094
y = (X[:, 0] * 4).astype(int)

sklearn/utils/random.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def _random_choice_csc(n_samples, classes, class_probability=None, random_state=
8989
class_probability_nz
9090
)
9191
classes_ind = np.searchsorted(
92-
class_probability_nz_norm.cumsum(), rng.rand(nnz)
92+
class_probability_nz_norm.cumsum(), rng.uniform(size=nnz)
9393
)
9494
data.extend(classes[j][classes_j_nonzero][classes_ind])
9595
indptr.append(len(indices))

0 commit comments

Comments
 (0)
0